特殊功能寄存器¶
特殊功能寄存器即外设对应的寄存器,他们有固定的地址。
特殊功能寄存器应当被定义为static
类型,且使用volatile
进行修饰;可以使用__alias_at
指定地址,使用__bitpos
指定比特位。
定义一个特殊功能寄存器的格式如下:
//定义一个sfr
volatile [const] static BASE_TYPE SFR_NAME __alias_at(SFR_ADDRESS);
//定义一个sbit
volatile [const] static _Bool SBIT_NAME __alias_at(SBIT_ADDRESS) __bitpos(SBIT_BIT_POSITION);
建议将特殊功能寄存器定义到头文件中。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | #ifndef __SFR_H #define __SFR_H #define bit _Bool #define int8_t char #define int16_t short #define int32_t long #define uint8_t unsigned int8_t #define uint16_t unsigned int16_t #define uint32_t unsigned int32_t #define __IO volatile #define __I volatile const #define __O volatile #define __sfr_at __alias_at #define __sbit_at(r, p) __alias_at(r) __bitpos(p) //-------------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------- #define BASE_IDAT 0x00 #define BASE_IADR 0x02 #define BASE_IF0 0x06 #define BASE_IEN0 0x07 //... //-------------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------- __IO static uint8_t IDAT __sfr_at(BASE_IDAT); __IO static uint8_t IADR __sfr_at(BASE_IADR); //-------------------------------------------------------------------------------------------------- #define IF0_POS_URXF 6 #define IF0_POS_UTXF 5 #define IF0_POS_LVDF 4 #define IF0_POS_INT1F 3 #define IF0_POS_T1F 2 #define IF0_POS_T0F 1 #define IF0_POS_RFF 0 #define IF0_MSK_URXF (1 << IF0_POS_URXF) #define IF0_MSK_UTXF (1 << IF0_POS_UTXF) #define IF0_MSK_LVDF (1 << IF0_POS_LVDF) #define IF0_MSK_INT1F (1 << IF0_POS_INT1F) #define IF0_MSK_T1F (1 << IF0_POS_T1F) #define IF0_MSK_T0F (1 << IF0_POS_T0F) #define IF0_MSK_RFF (1 << IF0_POS_RFF) __IO static bit IF0_BIT_URXF __sbit_at(BASE_IF0, IF0_POS_URXF); __IO static bit IF0_BIT_UTXF __sbit_at(BASE_IF0, IF0_POS_UTXF); __IO static bit IF0_BIT_LVDF __sbit_at(BASE_IF0, IF0_POS_LVDF); __IO static bit IF0_BIT_INT1F __sbit_at(BASE_IF0, IF0_POS_INT1F); __IO static bit IF0_BIT_T1F __sbit_at(BASE_IF0, IF0_POS_T1F); __IO static bit IF0_BIT_T0F __sbit_at(BASE_IF0, IF0_POS_T0F); __IO static bit IF0_BIT_RFF __sbit_at(BASE_IF0, IF0_POS_RFF); __IO static uint8_t IF0 __sfr_at(BASE_IF0); //-------------------------------------------------------------------------------------------------- #define IEN0_POS_EA 7 #define IEN0_POS_EURX 6 #define IEN0_POS_EUTX 5 #define IEN0_POS_ELVD 4 #define IEN0_POS_EINT1 3 #define IEN0_POS_ET1 2 #define IEN0_POS_ET0 1 #define IEN0_POS_ERF 0 #define IEN0_MSK_EA (1 << IEN0_POS_EA) #define IEN0_MSK_EURX (1 << IEN0_POS_EURX) #define IEN0_MSK_EUTX (1 << IEN0_POS_EUTX) #define IEN0_MSK_ELVD (1 << IEN0_POS_ELVD) #define IEN0_MSK_EINT1 (1 << IEN0_POS_EINT1) #define IEN0_MSK_ET1 (1 << IEN0_POS_ET1) #define IEN0_MSK_ET0 (1 << IEN0_POS_ET0) #define IEN0_MSK_ERF (1 << IEN0_POS_ERF) __IO static bit IEN0_BIT_EA __sbit_at(BASE_IEN0, IEN0_POS_EA); __IO static bit IEN0_BIT_EURX __sbit_at(BASE_IEN0, IEN0_POS_EURX); __IO static bit IEN0_BIT_EUTX __sbit_at(BASE_IEN0, IEN0_POS_EUTX); __IO static bit IEN0_BIT_ELVD __sbit_at(BASE_IEN0, IEN0_POS_ELVD); __IO static bit IEN0_BIT_EINT1 __sbit_at(BASE_IEN0, IEN0_POS_EINT1); __IO static bit IEN0_BIT_ET1 __sbit_at(BASE_IEN0, IEN0_POS_ET1); __IO static bit IEN0_BIT_ET0 __sbit_at(BASE_IEN0, IEN0_POS_ET0); __IO static bit IEN0_BIT_ERF __sbit_at(BASE_IEN0, IEN0_POS_ERF); __IO static uint8_t IEN0 __sfr_at(BASE_IEN0); //-------------------------------------------------------------------------------------------------- //... #endif //__SFR_H |