This commit is contained in:
moyuhai
2026-06-12 09:12:35 +08:00
commit 02f1638ef2
91 changed files with 38366 additions and 0 deletions
+20
View File
@@ -0,0 +1,20 @@
#ifndef _Delay_C_
#define _Delay_C_
/*********************************************************************************************************************/
void Delay_50us(unsigned int n)
{
unsigned char i;
while(n--)
{
for(i=0;i<71;i++);
}
}
void Delay_ms(unsigned int delay_nms)
{
while(delay_nms--)
{
Delay_50us(20);
}
}
/*********************************************************************************************************************/
#endif
+245
View File
@@ -0,0 +1,245 @@
#ifndef _FLASH_C_
#define _FLASH_C_
/*********************************************************************************************************************/
#include "ca51f5_config.h"
#include "../../includes/ca51f5sfr.h"
#include "../../includes/ca51f5xsfr.h"
#include "../../includes/gpiodef_f5.h"
#include "../../includes/system.h"
#include "../../Library/includes/flash.h"
#include <intrins.h>
/*********************************************************************************************************************/
/***********************************************************************************
函 数 名:Data_Area_Erase_Sector
功能描述:擦除数据区空间的一个扇区
函数说明:
输 入:unsigned char SectorNumber 扇区号,范围 0-255
返 回:无
***********************************************************************************/
void Data_Area_Sector_Erase(unsigned char SectorNumber)
{
unsigned int SectorAddress;
FSCMD = 0;
SectorAddress = 0x80*SectorNumber;
LOCK = CMD_DATA_AREA_UNLOCK; //数据区解锁
PTSH = (unsigned char)(SectorAddress>>8); //填写扇区地址
PTSL = (unsigned char)(SectorAddress); //填写扇区地址
FSCMD = CMD_DATA_AREA_ERASE_SECTOR; //执行擦除扇区操作
FSCMD = 0;
LOCK = CMD_FLASH_LOCK; //对FLASH加锁
}
/***********************************************************************************
函 数 名:Data_Area_Write_Byte
功能描述:向FLASH数据区写入一个字节数据
输 入:unsigned int Address 数据区空间写入地址
unsigned char Data 写入数据
返 回:无
***********************************************************************************/
void Data_Area_Write_Byte(unsigned int Address,unsigned char Data)
{
FSCMD = 0;
LOCK = CMD_DATA_AREA_UNLOCK; //数据区解锁
PTSH = (unsigned char)(Address>>8); //填写高位地址
PTSL = (unsigned char)Address; //填写低位地址
FSCMD = CMD_DATA_AREA_WIRTE; //执行写操作
FSDAT = Data; //装载数据
FSCMD = 0;
LOCK = CMD_FLASH_LOCK; //对FLASH加锁
}
/***********************************************************************************
函 数 名:Data_Area_Mass_Write
功能描述:向FLASH数据区批量写入数据
输 入:unsigned int wAddress 数据区空间写入起始地址
unsigned char *pData 数据指针,指向写入数据缓存数组
unsigned char Length 写入数据长度
返 回:无
***********************************************************************************/
void Data_Area_Mass_Write(unsigned int Address,unsigned char *pData,unsigned int Length)
{
unsigned int i;
FSCMD = 0;
LOCK = CMD_DATA_AREA_UNLOCK; //数据区解锁
PTSH = (unsigned char)(Address>>8); //填写高位地址
PTSL = (unsigned char)Address; //填写低位地址
FSCMD = CMD_DATA_AREA_WIRTE; //执行写操作
for(i = 0; i < Length; i++)
{
FSDAT = *pData++; //装载数据
}
FSCMD = 0;
LOCK = CMD_FLASH_LOCK; //对FLASH加锁
}
/***********************************************************************************
函 数 名:Data_Area_Read_Byte
功能描述:从FLASH数据区读出一字节数据
输 入:unsigned int Address 数据区空间读地址
返 回:读取的一字节数据
***********************************************************************************/
unsigned char Data_Area_Read_Byte(unsigned int Address)
{
unsigned char Data_Temp;
FSCMD = 0;
LOCK = CMD_DATA_AREA_UNLOCK; //数据区解锁
PTSH = (unsigned char)(Address>>8); //填写高位地址
PTSL = (unsigned char)Address; //填写低位地址
FSCMD = CMD_DATA_AREA_READ; //执行读操作
Data_Temp = FSDAT;
FSCMD = 0;
LOCK = CMD_FLASH_LOCK; //对FLASH加锁
return Data_Temp;
}
/***********************************************************************************
函 数 名:Data_Area_Mass_Read
功能描述:从FLASH数据区批量读出数据
输 入:unsigned int Address 数据区空间读起始地址
unsigned char *pData 数据指针,指向读出数据缓存数组
unsigned char Length 读数据长度
返 回:无
***********************************************************************************/
void Data_Area_Mass_Read(unsigned int Address,unsigned char *pData,unsigned int Length)
{
unsigned int i;
FSCMD = 0;
LOCK = CMD_DATA_AREA_UNLOCK; //数据区解锁
PTSH = (unsigned char)(Address>>8); //填写高位地址
PTSL = (unsigned char)Address; //填写低位地址
FSCMD = CMD_DATA_AREA_READ; //执行读操作
for(i = 0; i < Length; i++)
{
*pData++ = FSDAT;
}
FSCMD = 0;
LOCK = CMD_FLASH_LOCK; //对FLASH加锁
}
/***********************************************************************************
函 数 名:Code_Area_Erase_Sector
功能描述:擦除程序区空间的一个扇区
函数说明:
输 入:unsigned char SectorNumber 扇区号,范围 0-255
返 回:无
***********************************************************************************/
void Code_Area_Sector_Erase(unsigned char SectorNumber)
{
unsigned int SectorAddress;
FSCMD = 0;
SectorAddress = 0x80*SectorNumber;
LOCK = CMD_CODE_AREA_UNLOCK; //程序区解锁
PTSH = (unsigned char)(SectorAddress>>8); //填写扇区地址
PTSL = (unsigned char)(SectorAddress); //填写扇区地址
FSCMD = CMD_CODE_AREA_ERASE_SECTOR; //执行擦除扇区操作
FSCMD = 0;
LOCK = CMD_FLASH_LOCK; //对FLASH加锁
}
/***********************************************************************************
函 数 名:Code_Area_Write_Byte
功能描述:向FLASH程序区写入一个字节数据
输 入:unsigned int Address 程序区空间写入地址
unsigned char Data 写入数据
返 回:无
***********************************************************************************/
void Code_Area_Write_Byte(unsigned int Address,unsigned char Data)
{
FSCMD = 0;
LOCK = CMD_CODE_AREA_UNLOCK; //数据区解锁
PTSH = (unsigned char)(Address>>8); //填写高位地址
PTSL = (unsigned char)Address; //填写低位地址
FSCMD = CMD_CODE_AREA_WRITE; //执行写操作
FSDAT = Data; //装载数据
FSCMD = 0;
LOCK = CMD_FLASH_LOCK; //对FLASH加锁
}
/***********************************************************************************
函 数 名:Code_Area_Mass_Write
功能描述:向FLASH程序区批量写入数据
输 入:unsigned int Address 程序区空间写入起始地址
unsigned char *pData 数据指针,指向写入数据缓存数组
unsigned char Length 写入数据长度
返 回:无
***********************************************************************************/
void Code_Area_Mass_Write(unsigned int Address,unsigned char *pData,unsigned int Length)
{
unsigned int i;
FSCMD = 0;
LOCK = CMD_CODE_AREA_UNLOCK; //数据区解锁
PTSH = (unsigned char)(Address>>8); //填写高位地址
PTSL = (unsigned char)Address; //填写低位地址
FSCMD = CMD_CODE_AREA_WRITE; //执行写操作
for(i = 0; i < Length; i++)
{
FSDAT = *pData++; //装载数据
}
FSCMD = 0;
LOCK = CMD_FLASH_LOCK; //对FLASH加锁
}
/***********************************************************************************
函 数 名:Code_Area_Read_Byte
功能描述:从FLASH程序区读出一字节数据
输 入:unsigned int Address 程序区空间读地址
返 回:读取的一字节数据
***********************************************************************************/
unsigned char Code_Area_Read_Byte(unsigned int Address)
{
unsigned char Data_Temp;
FSCMD = 0;
LOCK = CMD_CODE_AREA_UNLOCK; //数据区解锁
PTSH = (unsigned char)(Address>>8); //填写高位地址
PTSL = (unsigned char)Address; //填写低位地址
FSCMD = CMD_CODE_AREA_READ; //执行读操作
Data_Temp = FSDAT;
FSCMD = 0;
LOCK = CMD_FLASH_LOCK; //对FLASH加锁
return Data_Temp;
}
/***********************************************************************************
函 数 名: Code_Area_Mass_Read
功能描述: 从FLASH程序区批量读出数据
输 入: unsigned int Address 程序区空间读起始地址
unsigned char *pData 数据指针,指向读出数据缓存数组
unsigned char Length 读数据长度
返 回: 无
***********************************************************************************/
void Code_Area_Mass_Read(unsigned int Address,unsigned char *pData,unsigned int Length)
{
unsigned int i;
FSCMD = 0;
LOCK = CMD_CODE_AREA_UNLOCK; //数据区解锁
PTSH = (unsigned char)(Address>>8); //填写高位地址
PTSL = (unsigned char)Address; //填写低位地址
FSCMD = CMD_CODE_AREA_READ; //执行读操作
for(i = 0; i < Length; i++)
{
*pData++ = FSDAT;
}
FSCMD = 0;
LOCK = CMD_FLASH_LOCK; //对FLASH加锁
}
/***********************************************************************************
函 数 名: GetChipID
功能描述: 从芯片读出芯片识别码(每个芯片都有唯一的识别码)
输 入: 无
返 回: 32位滚码
***********************************************************************************/
unsigned long int GetChipID(void)
{
unsigned long int ID;
LOCK = 0x2B;
FSCMD = 0x80;
PTSH = 0x00;
PTSL = 0x00;
FSCMD = 0x81;
ID = FSDAT;
ID *= 256;
ID |= FSDAT;
ID *= 256;
ID |= FSDAT;
ID *= 256;
ID |= FSDAT;
FSCMD = 0;
LOCK = 0xAA;
return ID;
}
#endif
+35
View File
@@ -0,0 +1,35 @@
#ifndef _I2C_C_
#define _I2C_C_
/*********************************************************************************************************************/
#include "ca51f5_config.h"
#include "../../../includes/ca51f5sfr.h"
#include "../../../includes/ca51f5xsfr.h"
#include "../../../includes/gpiodef_f5.h"
#include "../../../includes/system.h"
#include "../../../Library/includes/i2c.h"
#include <intrins.h>
/*********************************************************************************************************************/
void I2C_init(I2CE_TypeDef i2ce,I2CIE_TypeDef i2cie,STFE_TypeDef stfe,unsigned char i2cadr,unsigned char i2ccr)
{
I2CCON = I2CE(i2ce) | I2CIE(i2cie) | STA(0) | STP(0)| CKHD(1) | AAK(1)| CBSE(0) | STFE(stfe);
I2CADR = GCE(0)|(i2cadr>>1);
I2CCR = (i2ccr>>1);
}
void I2C_SelectComPin(I2CIOS_TypeDef i2cios)
{
I2CIOS = i2cios;
if(i2cios == I2C_SEL_P30_P31)
{
GPIO_Init(P30F,P30_I2C_SDA_SETTING | PU_EN);
GPIO_Init(P31F,P31_I2C_SCL_SETTING | PU_EN);
}
else if(i2cios == I2C_SEL_P03_P02)
{
GPIO_Init(P03F,P03_I2C_SDA_SETTING | PU_EN);
GPIO_Init(P02F,P02_I2C_SCL_SETTING | PU_EN);
}
}
#endif
+269
View File
@@ -0,0 +1,269 @@
#ifndef _PWM_C_
#define _PWM_C_
/*********************************************************************************************************************/
#include "../../includes/ca51f5sfr.h"
#include "../../includes/ca51f5xsfr.h"
#include "../../Library/includes/pwm.h"
#include <intrins.h>
/*********************************************************************************************************************/
/******************************************************************************
函数名:PWM0_init
功能描述:PWM0通道功能初始化
输入参数: ie PWM0中断开关设置
cks PWM0时钟源设置
toggle PWM0是否反相输出设置
Prescal PWM0时钟分频设置
返回值: 无
******************************************************************************/
void PWM0_init( PWMIE_TypeDef ie,
PWM_CKS_TypeDef cks,
PWM_Toggle_TypeDef toggle,
unsigned char Prescal)
{
PWM0CON = PWMIE(ie) | PWMTOG(toggle) | cks;
PWM0CKD = Prescal;
}
/******************************************************************************
函数名:PWM1_init
功能描述:PWM1通道功能初始化
输入参数: ie PWM1中断开关设置
cks PWM1时钟源设置
toggle PWM1是否反相输出设置
Prescal PWM1时钟分频设置
pwmmod pwmmod=0:PWM模式,pwmmod>0:级联LED驱动模式,同时pwmmod表示发送pwmmod个字节插入延时时间
返回值: 无
******************************************************************************/
void PWM1_init( PWMIE_TypeDef ie,
PWM_CKS_TypeDef cks,
PWM_Toggle_TypeDef toggle,
unsigned char Prescal,
unsigned char pwmmod)
{
PWM1CON = PWMIE(ie) | PWMTOG(toggle) | PWMMOD(pwmmod) | PWMPOL(0) | cks;
PWM1CKD = Prescal;
}
/******************************************************************************
函数名:PWM2_init
功能描述:PWM2通道功能初始化
输入参数: ie PWM2中断开关设置
cks PWM2时钟源设置
toggle PWM2是否反相输出设置
Prescal PWM2时钟分频设置
pwmmod pwmmod=0:PWM模式,pwmmod>0:级联LED驱动模式,同时pwmmod表示发送pwmmod个字节插入延时时间
返回值: 无
******************************************************************************/
void PWM2_init( PWMIE_TypeDef ie,
PWM_CKS_TypeDef cks,
PWM_Toggle_TypeDef toggle,
unsigned char Prescal,
unsigned char pwmmod)
{
PWM2CON = PWMIE(ie) | PWMTOG(toggle) | PWMMOD(pwmmod) | PWMPOL(0) | cks;
PWM2CKD = Prescal;
}
/******************************************************************************
函数名:PWM3_init
功能描述:PWM3通道功能初始化
输入参数: ie PWM3中断开关设置
cks PWM3时钟源设置
toggle PWM3是否反相输出设置
Prescal PWM3时钟分频设置
返回值: 无
******************************************************************************/
void PWM3_init( PWMIE_TypeDef ie,
PWM_CKS_TypeDef cks,
PWM_Toggle_TypeDef toggle,
unsigned char Prescal)
{
PWM3CON = PWMIE(ie) | PWMTOG(toggle) | cks;
PWM3CKD = Prescal;
}
/******************************************************************************
函数名:PWM4_init
功能描述:PWM4通道功能初始化
输入参数: ie PWM4中断开关设置
cks PWM4时钟源设置
toggle PWM4是否反相输出设置
Prescal PWM4时钟分频设置
返回值: 无
******************************************************************************/
void PWM4_init( PWMIE_TypeDef ie,
PWM_CKS_TypeDef cks,
PWM_Toggle_TypeDef toggle,
unsigned char Prescal)
{
PWM4CON = PWMIE(ie) | PWMTOG(toggle) | cks;
PWM4CKD = Prescal;
}
/******************************************************************************
函数名:PWM5_init
功能描述:PWM5通道功能初始化
输入参数: ie PWM5中断开关设置
cks PWM5时钟源设置
toggle PWM5是否反相输出设置
Prescal PWM5时钟分频设置
返回值: 无
******************************************************************************/
void PWM5_init( PWMIE_TypeDef ie,
PWM_CKS_TypeDef cks,
PWM_Toggle_TypeDef toggle,
unsigned char Prescal)
{
PWM5CON = PWMIE(ie) | PWMTOG(toggle) | cks;
PWM5CKD = Prescal;
}
/******************************************************************************
函数名: PWM0_CfgDivDuty
功能描述:设置PWM0周期和占空比
输入参数: DIV PWM周期设置
DUT PWM占空比设置
返回值: 无
******************************************************************************/
void PWM0_CfgDivDuty(unsigned int DIV,unsigned int DUT)
{
PWM0DIVH = (unsigned char)(DIV>>8);
PWM0DIVL = (unsigned char)(DIV);
PWM0DUTH = (unsigned char)(DUT>>8);
PWM0DUTL = (unsigned char)(DUT);
}
/******************************************************************************
函数名: PWM1_CfgDivDuty
功能描述:设置PWM1周期和占空比
输入参数: DIV PWM周期设置
DUT PWM占空比设置
返回值: 无
******************************************************************************/
void PWM1_CfgDivDuty(unsigned int DIV,unsigned int DUT)
{
PWM1DIVH = (unsigned char)(DIV>>8);
PWM1DIVL = (unsigned char)(DIV);
PWM1DUTH = (unsigned char)(DUT>>8);
PWM1DUTL = (unsigned char)(DUT);
}
/******************************************************************************
函数名: PWM2_CfgDivDuty
功能描述:设置PWM2周期和占空比
输入参数: DIV PWM周期设置
DUT PWM占空比设置
返回值: 无
******************************************************************************/
void PWM2_CfgDivDuty(unsigned int DIV,unsigned int DUT)
{
PWM2DIVH = (unsigned char)(DIV>>8);
PWM2DIVL = (unsigned char)(DIV);
PWM2DUTH = (unsigned char)(DUT>>8);
PWM2DUTL = (unsigned char)(DUT);
}
/******************************************************************************
函数名: PWM3_CfgDivDuty
功能描述:设置PWM0周期和占空比
输入参数: DIV PWM周期设置
DUT PWM占空比设置
返回值: 无
******************************************************************************/
void PWM3_CfgDivDuty(unsigned int DIV,unsigned int DUT)
{
PWM3DIVH = (unsigned char)(DIV>>8);
PWM3DIVL = (unsigned char)(DIV);
PWM3DUTH = (unsigned char)(DUT>>8);
PWM3DUTL = (unsigned char)(DUT);
}
/******************************************************************************
函数名: PWM4_CfgDivDuty
功能描述:设置PWM4周期和占空比
输入参数: DIV PWM周期设置
DUT PWM占空比设置
返回值: 无
******************************************************************************/
void PWM4_CfgDivDuty(unsigned int DIV,unsigned int DUT)
{
PWM4DIVH = (unsigned char)(DIV>>8);
PWM4DIVL = (unsigned char)(DIV);
PWM4DUTH = (unsigned char)(DUT>>8);
PWM4DUTL = (unsigned char)(DUT);
}
/******************************************************************************
函数名: PWM5_CfgDivDuty
功能描述:设置PWM5周期和占空比
输入参数: DIV PWM周期设置
DUT PWM占空比设置
返回值: 无
******************************************************************************/
void PWM5_CfgDivDuty(unsigned int DIV,unsigned int DUT)
{
PWM5DIVH = (unsigned char)(DIV>>8);
PWM5DIVL = (unsigned char)(DIV);
PWM5DUTH = (unsigned char)(DUT>>8);
PWM5DUTL = (unsigned char)(DUT);
}
/******************************************************************************
函数名: PWM_CfgLedut
功能描述:PWM1/2为级联LED驱动模式时,设置发送位为1时PWM占空比
输入参数: Ledut 发送位为1时PWM占空比
返回值: 无
******************************************************************************/
void PWM_CfgLedut(unsigned int Ledut)
{
LEDUTH = (unsigned char)(Ledut>>8);
LEDUTL = (unsigned char)(Ledut);
}
/******************************************************************************
函数名: PWM_CfgLedwtm
功能描述:PWM1/2为级联LED驱动模式时,设置延时时间
输入参数: Ledwtm 延时的PWM时钟周期数
返回值: 无
******************************************************************************/
void PWM_CfgLedwtm(unsigned int Ledwtm)
{
LEDWTMH = (unsigned char)(Ledwtm>>8);
LEDWTML = (unsigned char)(Ledwtm);
}
/******************************************************************************
函数名: PWM1_WriteLedat
功能描述:PWM1为级联LED驱动模式时,发送1字节数据
输入参数: Ledat 待发送的数据
返回值: 无
******************************************************************************/
void PWM1_WriteLedat(unsigned char Ledat)
{
while(LEFLG & LEF0);
LEDAT0 = Ledat;
}
/******************************************************************************
函数名: PWM2_WriteLedat
功能描述:PWM2为级联LED驱动模式时,发送1字节数据
输入参数: Ledat 待发送的数据
返回值: 无
******************************************************************************/
void PWM2_WriteLedat(unsigned char Ledat)
{
while(LEFLG & LEF1);
LEDAT1 = Ledat;
}
/******************************************************************************
函数名: PWM_Enable
功能描述:PWM使能
输入参数: Pwmch ,使能的PWM通道
返回值: 无
******************************************************************************/
void PWM_Enable(PWM_Channel_TypeDef Pwmch)
{
PWMEN |= 1<<Pwmch;
}
/******************************************************************************
函数名: PWM_Disable
功能描述:PWM禁能
输入参数: Pwmch ,禁能的PWM通道
返回值: 无
******************************************************************************/
void PWM_Disable(PWM_Channel_TypeDef Pwmch)
{
PWMEN &= ~(1<<Pwmch);
}
#endif
+63
View File
@@ -0,0 +1,63 @@
#ifndef _STOP_IDLE_C_
#define _STOP_IDLE_C_
/*********************************************************************************************************************/
#include "ca51f5_config.h"
#include "../../includes/ca51f5sfr.h"
#include "../../includes/ca51f5xsfr.h"
#include "../../includes/gpiodef_f5.h"
#include "../../includes/system.h"
#include "../../Library/includes/stop_idle.h"
#include <intrins.h>
/*********************************************************************************************************************/
/******************************************************************************************
函数名:StopEnter
功能说明:进入STOP模式
输入参数:无
返回值: 无
******************************************************************************************/
void StopEnter(void)
{
PCON |= STOP;
_nop_();
_nop_();
}
/******************************************************************************************
函数名:CheckStopCondition
功能说明:检查进入STOP模式的条件(注:如果STPSTH、STPSTL不为0,即使执行STOP指令,芯片也不会进入STOP)
输入参数:无
返回值: 0:不能进入STOP; 1: 可以进入STOP
******************************************************************************************/
unsigned char CheckStopCondition(void)
{
if(STPST) return 0;
else return 1;
}
/******************************************************************************************
函数名: IdleEnter
功能说明:进入IDLE模式
输入参数:无
返回值: 无
******************************************************************************************/
void IdleEnter(void)
{
PCON |= IDLE;
_nop_();
_nop_();
}
/******************************************************************************************
函数名:CheckIdleCondition
功能说明:检查进入IDLE模式的条件(注:如果IDLSTH、IDLSTL不为0,即使执行IDLE指令,芯片也不会进入IDLE)
输入参数:无
返回值: 0:不能进入IDLE; 1: 可以进入IDLE
******************************************************************************************/
unsigned char CheckIdleCondition(void)
{
if(IDLST) return 0;
else return 1;
}
/*********************************************************************************************************************/
#endif
+24
View File
@@ -0,0 +1,24 @@
#ifndef _SYSTEM_CLOCK_C_
#define _SYSTEM_CLOCK_C_
/*********************************************************************************************************************/
#include "ca51f5_config.h"
#include "../../includes/ca51f5sfr.h"
#include "../../includes/ca51f5xsfr.h"
#include "../../includes/gpiodef_f5.h"
#include "../../includes/system.h"
#include "../../Library/includes/system_clock.h"
#include <intrins.h>
/*********************************************************************************************************************/
void Sys_Clk_Set_IRCH(void)
{
CKCON |= IHCKE;
CKCON = (CKCON&0xFE) | CKSEL_IRCH;
}
void Sys_Clk_Set_IRCL(void)
{
CKCON |= ILCKE;
CKCON = (CKCON&0xFE) | CKSEL_IRCL;
}
/*********************************************************************************************************************/
#endif
+139
View File
@@ -0,0 +1,139 @@
#ifndef _UART_C_
#define _UART_C_
#include "ca51f5_config.h"
#include "../../includes/ca51f5sfr.h"
#include "../../includes/ca51f5xsfr.h"
#include "../../includes/gpiodef_f5.h"
#include "../../includes/system.h"
#include "../../Library/includes/uart.h"
#include <intrins.h>
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <absacc.h>
/*********************************************************************************************************************/
/*********************************************************************************************************************/
#ifdef UART0_EN
/*********************************************************************************************************************
注意:以下波特率配置参数前提条件是系统时钟为16MHz,如果修改系统时钟频率,波特率配置参数须另行计算。
***********************************************************************************************************************/
void Uart0_Initial(unsigned long int baudrate)
{
code unsigned long int BR_TAB[]=
{
1200,
2400,
4800,
9600,
19200,
38400,
57600,
115200,
};
code unsigned int BR_SET_TAB[][2]=
{
{416,31},
{208,31},
{104,31},
{52,31},
{26,31},
{13,31},
{10,27},
{5,27},
};
unsigned int value_temp;
unsigned char i;
uart0_send.head = 0;
uart0_send.tail = 0;
uart0_rev.head = 0;
uart0_rev.tail = 0;
uart0_tx_flag = 0;
GPIO_Init(P31F,P31_UART0_RX_SETTING);
GPIO_Init(P30F,P30_UART0_TX_SETTING);
for(i=0;i<sizeof(BR_TAB)/4;i++)
{
if(baudrate == BR_TAB[i])
{
value_temp = 0x10000 - BR_SET_TAB[i][0];
UDCKS = 0x80 | BR_SET_TAB[i][1];
break;
}
}
if(i == sizeof(BR_TAB)/4) return; //如果所设置的波特率不在表格中,可自行添加。
T2CON = 0x24;
T2CH = (unsigned char)(value_temp>>8);
T2CL = (unsigned char)(value_temp);
TH2 = (unsigned char)(value_temp>>8);
TL2 = (unsigned char)(value_temp);;
TR2 = 1;
S0CON = 0x50;
ES0 = 1;
}
void Uart0_PutChar(unsigned char bdat)
{
unsigned char free_space;
unsigned char tail_tmp;
while(1)
{
tail_tmp = uart0_send.tail;
if(uart0_send.head < tail_tmp)
{
free_space = tail_tmp - uart0_send.head;
}
else
{
free_space = UART0_TX_BUF_SIZE + tail_tmp - uart0_send.head;
}
if(free_space > 1)
{
ES0 = 0;
uart0_send.head++;
uart0_send.head %= UART0_TX_BUF_SIZE;
uart0_tx_buf[uart0_send.head] = bdat;
if(!uart0_tx_flag)
{
ES0 = 1;
uart0_send.tail++;
uart0_send.tail %= UART0_TX_BUF_SIZE;
S0BUF=uart0_tx_buf[uart0_send.tail];
uart0_tx_flag = 1;
}
else
{
ES0 = 1;
}
break;
}
}
}
void UART0_RX_ISR (void)
{
RI0 = 0;
uart0_rev.head++;
uart0_rev.head %= UART0_RX_BUF_SIZE;
uart0_rx_buf[uart0_rev.head]=S0BUF;
}
void UART0_TX_ISR (void)
{
TI0 = 0;
if(uart0_send.head!=uart0_send.tail)
{
uart0_send.tail++;
uart0_send.tail %= UART0_TX_BUF_SIZE;
S0BUF=uart0_tx_buf[uart0_send.tail];
}
else
{
uart0_tx_flag=0;
}
}
#endif
/*********************************************************************************************************************/
#endif
+36
View File
@@ -0,0 +1,36 @@
#ifndef _WDT_C_
#define _WDT_C_
/*********************************************************************************************************************/
#include "../../includes/ca51f5sfr.h"
#include "../../includes/ca51f5xsfr.h"
#include "../../includes/system.h"
#include "../../Library/includes/wdt.h"
#include <intrins.h>
/*********************************************************************************************************************/
/*****************************************************************************
函数名:WDT_init
功能描述:初始化看门狗
输入参数:wdts 看门狗时钟源设置
wdre 看门狗模式设置
wdtch 看门狗时间设置
返回值: 无
******************************************************************************/
void WDT_init(WDTS_TypeDef wdts,WDRE_TypeDef wdre,unsigned int wdtvh)
{
WDCON = WDTS(wdts) | WDRE(wdre);
WDVTHH = (unsigned char)(wdtvh>>8);
WDVTHL = (unsigned char)(wdtvh);
}
/*****************************************************************************
函数名:WDT_FeedDog
功能描述:刷新看门狗
输入参数:无
返回值: 无
******************************************************************************/
void WDT_FeedDog(void)
{
WDFLG = 0xA5;
}
/*********************************************************************************************************************/
#endif