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
+7
View File
@@ -0,0 +1,7 @@
#ifndef _Delay_H_
#define _Delay_H_
/*********************************************************************************************************************/
void Delay_50us(unsigned int n);
void Delay_ms(unsigned char n);
/*********************************************************************************************************************/
#endif
+7
View File
@@ -0,0 +1,7 @@
#ifndef _Delay_H_
#define _Delay_H_
/*********************************************************************************************************************/
void Delay_50us(unsigned int n);
void Delay_ms(unsigned int delay_nms);
/*********************************************************************************************************************/
#endif
+26
View File
@@ -0,0 +1,26 @@
//EPxCON寄存器定义
#define EPIE(N) (N<<7) //N=0-1
#define EPPL(N) (N<<6) //N=0-1
//EPIF定义
#define EPIF2 (1<<2)
#define EPIF1 (1<<1)
#define EPIF0 (1<<0)
enum
{
P00_INDEX = 0,
P01_INDEX = 1,
P02_INDEX = 2,
P03_INDEX = 3,
P04_INDEX = 4,
P05_INDEX = 5,
P06_INDEX = 6,
P07_INDEX = 7,
P30_INDEX = 8,
P31_INDEX = 9,
P32_INDEX = 10,
P33_INDEX = 11,
P34_INDEX = 12,
P35_INDEX = 13,
};
+32
View File
@@ -0,0 +1,32 @@
#ifndef FLASH_H
#define FLASH_H
enum
{
CMD_DATA_AREA_READ = 1,
CMD_DATA_AREA_WIRTE = 2,
CMD_DATA_AREA_ERASE_SECTOR = 3,
CMD_FLASH_ERASE_ALL = 4,
CMD_CODE_AREA_READ = 5,
CMD_CODE_AREA_WRITE = 6,
CMD_CODE_AREA_ERASE_SECTOR = 7,
};
enum
{
CMD_FLASH_UNLOCK = 0x28,
CMD_CODE_AREA_UNLOCK = 0x29,
CMD_DATA_AREA_UNLOCK = 0x2A,
CMD_FLASH_LOCK = 0xAA,
};
//źÄ´ćĆ÷FSCMDÎťś¨Ňĺ
#define IFEN (1<<7)
void Data_Area_Sector_Erase(unsigned char SectorNumber);
void Data_Area_Write_Byte(unsigned int Address,unsigned char Data);
void Data_Area_Mass_Write(unsigned int Address,unsigned char *pData,unsigned int Length);
unsigned char Data_Area_Read_Byte(unsigned int Address);
void Data_Area_Mass_Read(unsigned int Address,unsigned char *pData,unsigned int Length);
void Code_Area_Sector_Erase(unsigned char SectorNumber);
unsigned long int GetChipID(void);
#endif
+40
View File
@@ -0,0 +1,40 @@
//I2CCONś¨Ňĺ
#define I2CE(N) (N<<7)
#define I2CIE(N) (N<<6)
#define STA(N) (N<<5)
#define STP(N) (N<<4)
#define CKHD(N) (N<<3)
#define AAK(N) (N<<2)
#define CBSE(N) (N<<1)
#define STFE(N) (N<<0)
//I2CADRś¨Ňĺ
#define GCE(N) (N<<7) //N = 0~1
//I2CFLGś¨Ňĺ
#define I2CF (1<<0)
typedef enum {
I2CE_disable = 0,
I2CE_enable = 1,
} I2CE_TypeDef;
typedef enum {
I2CIE_disable = 0,
I2CIE_enable = 1,
} I2CIE_TypeDef;
typedef enum {
STFE_disable = 0,
STFE_enable = 1,
} STFE_TypeDef;
typedef enum {
I2C_SEL_P30_P31 = 0,
I2C_SEL_P03_P02 = 1,
} I2CIOS_TypeDef;
void I2C_init(I2CE_TypeDef i2ce,I2CIE_TypeDef i2cie,STFE_TypeDef stfe,unsigned char i2cadr,unsigned char i2ccr);
void I2C_SelectComPin(I2CIOS_TypeDef i2cios);
+110
View File
@@ -0,0 +1,110 @@
#ifndef PWM_H
#define PWM_H
//PWMxCON
#define PWMIE(N) (N<<7) //N=0-1
#define PWMTOG(N) (N<<6) //N=0-1
#define PWMMOD(N) (N<<3) //N=0-7
#define PWMPOL(N) (N<<2) //N=0-1
#define PWMCKS_SYS 0
#define PWMCKS_IL 1
#define PWMCKS_IH 2
//PWMIF
#define PWM5IF (1<<5)
#define PWM4IF (1<<4)
#define PWM3IF (1<<3)
#define PWM2IF (1<<2)
#define PWM1IF (1<<1)
#define PWM0IF (1<<0)
//LEFLG
#define LEF1 (1<<7)
#define LEBSY1 (1<<6)
#define LEF0 (1<<3)
#define LEBSY0 (1<<2)
//PWMCON
#define TIE(N) (N<<7)
#define ZIE(N) (N<<6)
#define PIE(N) (N<<5)
#define NIE(N) (N<<4)
#define MS(N) (N<<3) //ÖÐÐÄ¶ÔÆëģʽ
#define MOD(N) (N<<0) //»¥²¹Ä£Ê½
#define CKS(N) (N) //N=0~6
//PWMCFG
#define TOG(N) (N<<7)
typedef enum {
PWM_CH0 = 0,
PWM_CH1 = 1,
PWM_CH2 = 2,
PWM_CH3 = 3,
PWM_CH4 = 4,
PWM_CH5 = 5,
} PWM_Channel_TypeDef;
typedef enum {
PWMIE_off = 0,
PWMIE_on = 1,
} PWMIE_TypeDef;
typedef enum {
CKS_SYS = 0,
CKS_IL = 1,
CKS_IH = 2,
} PWM_CKS_TypeDef;
typedef enum {
Toggle_Off = 0,
Toggle_On = 1,
} PWM_Toggle_TypeDef;
void PWM0_init( PWMIE_TypeDef ie,
PWM_CKS_TypeDef cks,
PWM_Toggle_TypeDef toggle,
unsigned char Prescal);
void PWM1_init( PWMIE_TypeDef ie,
PWM_CKS_TypeDef cks,
PWM_Toggle_TypeDef toggle,
unsigned char Prescal,
unsigned char pwmmod);
void PWM2_init( PWMIE_TypeDef ie,
PWM_CKS_TypeDef cks,
PWM_Toggle_TypeDef toggle,
unsigned char Prescal,
unsigned char pwmmod);
void PWM3_init( PWMIE_TypeDef ie,
PWM_CKS_TypeDef cks,
PWM_Toggle_TypeDef toggle,
unsigned char Prescal);
void PWM4_init( PWMIE_TypeDef ie,
PWM_CKS_TypeDef cks,
PWM_Toggle_TypeDef toggle,
unsigned char Prescal);
void PWM5_init( PWMIE_TypeDef ie,
PWM_CKS_TypeDef cks,
PWM_Toggle_TypeDef toggle,
unsigned char Prescal);
void PWM0_CfgDivDuty(unsigned int DIV,unsigned int DUT);
void PWM1_CfgDivDuty(unsigned int DIV,unsigned int DUT);
void PWM2_CfgDivDuty(unsigned int DIV,unsigned int DUT);
void PWM3_CfgDivDuty(unsigned int DIV,unsigned int DUT);
void PWM4_CfgDivDuty(unsigned int DIV,unsigned int DUT);
void PWM5_CfgDivDuty(unsigned int DIV,unsigned int DUT);
void PWM_CfgLedut(unsigned int Ledut);
void PWM_CfgLedwtm(unsigned int Ledwtm);
void PWM1_WriteLedat(unsigned char Ledat);
void PWM2_WriteLedat(unsigned char Ledat);
void PWM_Enable(PWM_Channel_TypeDef Pwmch);
void PWM_Disable(PWM_Channel_TypeDef Pwmch);
#endif
+9
View File
@@ -0,0 +1,9 @@
//PCON¶¨Òå
#define STOP (1<<1)
#define IDLE (1<<0)
void StopEnter(void);
unsigned char CheckStopCondition(void);
void IdleEnter(void);
unsigned char CheckIdleCondition(void);
+16
View File
@@ -0,0 +1,16 @@
#ifndef SYS_CLOCK_H
#define SYS_CLOCK_H
//CKCON寄存器定义
#define IHCKE (1<<7)
#define ILCKE (1<<6)
#define SCKS (1<<0) //0-IH,1-IL
#define CKSEL_IRCH 0
#define CKSEL_IRCL 1
void Sys_Clk_Set_IRCH(void);
void Sys_Clk_Set_IRCL(void);
#endif
+5
View File
@@ -0,0 +1,5 @@
//TMCON¶¨Òå
#define TME(N) (N<<7) //N=0-1
#define TMF (1<<0)
+42
View File
@@ -0,0 +1,42 @@
#define UART0_TX_BUF_SIZE 20
#define UART0_RX_BUF_SIZE 20
typedef struct
{
unsigned char head;
unsigned char tail;
}T_Buf_Info;
#ifdef _UART_C_
#ifdef UART0_EN
T_Buf_Info xdata uart0_send;
T_Buf_Info xdata uart0_rev;
unsigned char uart0_tx_buf[UART0_TX_BUF_SIZE];
unsigned char xdata uart0_rx_buf[UART0_RX_BUF_SIZE];
bit uart0_tx_flag;
#endif
#else
#ifdef UART0_EN
extern T_Buf_Info xdata uart0_send;
extern T_Buf_Info xdata uart0_rev;
extern unsigned char uart0_tx_buf[UART0_TX_BUF_SIZE];
extern unsigned char xdata uart0_rx_buf[UART0_RX_BUF_SIZE];
extern bit uart0_tx_flag;
#endif
#endif
#ifndef UART0_EN
#define Uart0_PutChar(n)
#endif
#ifdef UART0_EN
void Uart0_PutChar(unsigned char bdat);
void Uart0_Initial(unsigned long int baudrate);
void UART0_RX_ISR(void);
void UART0_TX_ISR(void);
#endif
#ifdef PRINT_EN
void uart_printf(char *fmt,...);
#endif
+28
View File
@@ -0,0 +1,28 @@
#ifndef WDT_H
#define WDT_H
/*************************************************************************************************************/
//WDCON¶¨Òå
#define WDTS(N) (N<<6) //N=0~2
#define WDRE(N) (N<<0) //N=0~1
//WDFLG¶¨Òå
#define WDIF (1<<1)
#define WDRF (1<<0)
typedef enum {
WDTS_DISABLE = 0,
WDTS_IRCH = 1,
WDTS_IRCL = 2,
} WDTS_TypeDef;
typedef enum {
WDRE_int = 0,
WDRE_reset = 1,
} WDRE_TypeDef;
void WDT_init(WDTS_TypeDef wdts,WDRE_TypeDef wdre,unsigned int wdtvh);
void WDT_FeedDog(void);
/*************************************************************************************************************/
#endif
+198
View File
@@ -0,0 +1,198 @@
$NOMOD51
;------------------------------------------------------------------------------
; This file is part of the C51 Compiler package
; Copyright (c) 1988-2005 Keil Elektronik GmbH and Keil Software, Inc.
; Version 8.01
;
; *** <<< Use Configuration Wizard in Context Menu >>> ***
;------------------------------------------------------------------------------
; STARTUP.A51: This code is executed after processor reset.
;
; To translate this file use A51 with the following invocation:
;
; A51 STARTUP.A51
;
; To link the modified STARTUP.OBJ file to your application use the following
; Lx51 invocation:
;
; Lx51 your object file list, STARTUP.OBJ controls
;
;------------------------------------------------------------------------------
;
; User-defined <h> Power-On Initialization of Memory
;
; With the following EQU statements the initialization of memory
; at processor reset can be defined:
;
; <o> IDATALEN: IDATA memory size <0x0-0x100>
; <i> Note: The absolute start-address of IDATA memory is always 0
; <i> The IDATA space overlaps physically the DATA and BIT areas.
IDATALEN EQU 80H
;
; <o> XDATASTART: XDATA memory start address <0x0-0xFFFF>
; <i> The absolute start address of XDATA memory
XDATASTART EQU 0
;
; <o> XDATALEN: XDATA memory size <0x0-0xFFFF>
; <i> The length of XDATA memory in bytes.
XDATALEN EQU 0
;
; <o> PDATASTART: PDATA memory start address <0x0-0xFFFF>
; <i> The absolute start address of PDATA memory
PDATASTART EQU 0H
;
; <o> PDATALEN: PDATA memory size <0x0-0xFF>
; <i> The length of PDATA memory in bytes.
PDATALEN EQU 0H
;
;</h>
;------------------------------------------------------------------------------
;
;<h> Reentrant Stack Initialization
;
; The following EQU statements define the stack pointer for reentrant
; functions and initialized it:
;
; <h> Stack Space for reentrant functions in the SMALL model.
; <q> IBPSTACK: Enable SMALL model reentrant stack
; <i> Stack space for reentrant functions in the SMALL model.
IBPSTACK EQU 0 ; set to 1 if small reentrant is used.
; <o> IBPSTACKTOP: End address of SMALL model stack <0x0-0xFF>
; <i> Set the top of the stack to the highest location.
IBPSTACKTOP EQU 0xFF +1 ; default 0FFH+1
; </h>
;
; <h> Stack Space for reentrant functions in the LARGE model.
; <q> XBPSTACK: Enable LARGE model reentrant stack
; <i> Stack space for reentrant functions in the LARGE model.
XBPSTACK EQU 0 ; set to 1 if large reentrant is used.
; <o> XBPSTACKTOP: End address of LARGE model stack <0x0-0xFFFF>
; <i> Set the top of the stack to the highest location.
XBPSTACKTOP EQU 0xFFFF +1 ; default 0FFFFH+1
; </h>
;
; <h> Stack Space for reentrant functions in the COMPACT model.
; <q> PBPSTACK: Enable COMPACT model reentrant stack
; <i> Stack space for reentrant functions in the COMPACT model.
PBPSTACK EQU 0 ; set to 1 if compact reentrant is used.
;
; <o> PBPSTACKTOP: End address of COMPACT model stack <0x0-0xFFFF>
; <i> Set the top of the stack to the highest location.
PBPSTACKTOP EQU 0xFF +1 ; default 0FFH+1
; </h>
;</h>
;------------------------------------------------------------------------------
;
; Memory Page for Using the Compact Model with 64 KByte xdata RAM
; <e>Compact Model Page Definition
;
; <i>Define the XDATA page used for PDATA variables.
; <i>PPAGE must conform with the PPAGE set in the linker invocation.
;
; Enable pdata memory page initalization
PPAGEENABLE EQU 0 ; set to 1 if pdata object are used.
;
; <o> PPAGE number <0x0-0xFF>
; <i> uppermost 256-byte address of the page used for PDATA variables.
PPAGE EQU 0
;
; <o> SFR address which supplies uppermost address byte <0x0-0xFF>
; <i> most 8051 variants use P2 as uppermost address byte
PPAGE_SFR DATA 0A0H
;
; </e>
;------------------------------------------------------------------------------
; Standard SFR Symbols
ACC DATA 0E0H
B DATA 0F0H
SP DATA 81H
DPL DATA 82H
DPH DATA 83H
NAME ?C_STARTUP
?C_C51STARTUP SEGMENT CODE
?STACK SEGMENT IDATA
RSEG ?STACK
DS 1
EXTRN CODE (?C_START)
PUBLIC ?C_STARTUP
CSEG AT 0
?C_STARTUP: LJMP STARTUP1
RSEG ?C_C51STARTUP
STARTUP1:
IF IDATALEN <> 0
MOV R0,#IDATALEN - 1
CLR A
IDATALOOP: MOV @R0,A
DJNZ R0,IDATALOOP
ENDIF
IF XDATALEN <> 0
MOV DPTR,#XDATASTART
MOV R7,#LOW (XDATALEN)
IF (LOW (XDATALEN)) <> 0
MOV R6,#(HIGH (XDATALEN)) +1
ELSE
MOV R6,#HIGH (XDATALEN)
ENDIF
CLR A
XDATALOOP: MOVX @DPTR,A
INC DPTR
DJNZ R7,XDATALOOP
DJNZ R6,XDATALOOP
ENDIF
IF PPAGEENABLE <> 0
MOV PPAGE_SFR,#PPAGE
ENDIF
IF PDATALEN <> 0
MOV R0,#LOW (PDATASTART)
MOV R7,#LOW (PDATALEN)
CLR A
PDATALOOP: MOVX @R0,A
INC R0
DJNZ R7,PDATALOOP
ENDIF
IF IBPSTACK <> 0
EXTRN DATA (?C_IBP)
MOV ?C_IBP,#LOW IBPSTACKTOP
ENDIF
IF XBPSTACK <> 0
EXTRN DATA (?C_XBP)
MOV ?C_XBP,#HIGH XBPSTACKTOP
MOV ?C_XBP+1,#LOW XBPSTACKTOP
ENDIF
IF PBPSTACK <> 0
EXTRN DATA (?C_PBP)
MOV ?C_PBP,#LOW PBPSTACKTOP
ENDIF
MOV SP,#?STACK-1
; This code is required if you use L51_BANK.A51 with Banking Mode 4
;<h> Code Banking
; <q> Select Bank 0 for L51_BANK.A51 Mode 4
#if 0
; <i> Initialize bank mechanism to code bank 0 when using L51_BANK.A51 with Banking Mode 4.
EXTRN CODE (?B_SWITCH0)
CALL ?B_SWITCH0 ; init bank mechanism to code bank 0
#endif
;</h>
LJMP ?C_START
END
+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