高级会员

- 积分
- 946
- 金钱
- 946
- 注册时间
- 2014-11-2
- 在线时间
- 367 小时
|
发表于 2018-9-8 19:50:15
|
显示全部楼层
这是STM8的
在IAR的c/c++ compiler选项卡language中设置c++,不然会报错
#pragma diag_suppress=pe1665 //屏蔽警告
/**************************************************/
#define PP 1 //CR1 上拉
#define OD 0 //CR1 开漏
#define FAST 1 //CR2 输出时快速模式
#define SLOW 0 //CR2 输出时慢速模式
#define EINT 1 //CR2 输入时开中断
#define DINT 0 //CR2 输入时关中断
/**************************************************/
#define PIN(port,bit,cr1,cr2,out,name) \
inline void name##_H() {P##port##_ODR|=(u8)(1<<bit);}\
inline void name##_L() {P##port##_ODR&=(u8)~(1<<bit);}\
inline void name##_T() {P##port##_ODR^=(u8)(1<<bit);}\
inline void name##(u8 x) {x ? name##_H():name##_L();}\
inline void name##_U() {P##port##_CR1|=(u8)(1<<bit);}\
inline void name##_D() {P##port##_CR1&=(u8)~(1<<bit);}\
inline void name##_CR1(u8 x) {x ? name##_U():name##_D();}\
inline void name##_S() {P##port##_CR2|=(u8)(1<<bit);}\
inline void name##_C() {P##port##_CR2&=(u8)~(1<<bit);}\
inline void name##_CR2(u8 x) {x ? name##_S():name##_C();}\
inline u8 name##() {return P##port##_IDR&(u8)(1<<bit) ? 1:0;}\
inline void name##_O() {P##port##_DDR|=(u8)(1<<bit);name##_CR1(cr1);name##_CR2(cr2);name##(out);}\
inline void name##_I() {P##port##_DDR&=(u8)~(1<<bit);name##_CR1(cr1);name##_CR2(cr2);}
/**************************************************/
把上面代码保存成一个头文件,如 def.h
使用前调用该文件就可以了
譬如:
#include "def.h"
PIN(C,3,PP,FAST,1,LED);
PIN(C,2,PP,DINT,1,KEY);
//PIN(端口号A~I,引脚号0~7,CR1,CR2,输出模式时IO初始值,引脚名称)
void main (void)
{ LED_O(); //设置IO输出
KEY_I(); //设置IO输入
while(1)
{ if(!KEY())
{ LED_H();//LED(1);
}
else
{ LED_L();//LED(0);
}
}
}
这个是M0的头文件
#pragma diag_suppress=pe1665 //屏蔽警告
/**************************************************/
#define PIN(port,bit,rcc,mode,speed,ppod,pupd,name) \
inline void name##_H() { GPIO##port##->BSRR = GPIO_Pin_##bit; } \
inline void name##_L() { GPIO##port##->BRR = GPIO_Pin_##bit; } \
inline void name##_T() { GPIO##port##->ODR&GPIO_Pin_##bit ? name##_L():name##_H(); } \
inline u8 name##() { return (GPIO##port##->IDR&GPIO_Pin_##bit) ? 1:0; } \
inline void name##(u32 x) { x ? name##_H():name##_L(); } \
inline void name##_O(u32 x) { GPIO_InitTypeDef GPIO_InitStructure; \
RCC_##rcc##PeriphClockCmd(RCC_##rcc##Periph_GPIO##port, ENABLE); \
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_##mode; \
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_##bit; \
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_##speed; \
GPIO_InitStructure.GPIO_OType= GPIO_OType_##ppod; \
GPIO_InitStructure.GPIO_PuPd= GPIO_PuPd_##pupd; \
GPIO_Init(GPIO##port, &GPIO_InitStructure); \
x ? name##_H():name##_L(); } \
inline void name##_I() { GPIO_InitTypeDef GPIO_InitStructure; \
RCC_##rcc##PeriphClockCmd(RCC_##rcc##Periph_GPIO##port, ENABLE); \
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_##mode; \
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_##bit; \
GPIO_InitStructure.GPIO_PuPd= GPIO_PuPd_##pupd; \
GPIO_Init(GPIO##port, &GPIO_InitStructure); }
/**************************************************/
/**************************************************/
// Rcc: APB1,APB2,AHB
// Mode: AN,IN,OUT,AF
// Speed: 2MHZ,10MHz,50MHz
// Ppod: PP,OD
// Pupd: NOPULL,UP,DOWN
/**************************************************/
// PIN(Port,Bit,Rcc,Mode,Speed,Ppod,Pupd,Name)
PIN(F, 0, AHB, OUT, 10MHz, PP, NOPULL, LED);
PIN(A, 1, AHB, IN, 2MHz, OD, UP, KEY);
LED_O(1);
KEY_I();
这个是M3的头文件
#pragma diag_suppress=pe1665 //屏蔽警告
/**************************************************/
#define PIN(port,bit,rcc,mode,speed,name) \
inline void name##_H(void) { GPIO##port##->BSRR = GPIO_Pin_##bit; } \
inline void name##_L(void) { GPIO##port##->BRR = GPIO_Pin_##bit; } \
inline void name##_T(void) { GPIO##port##->ODR&GPIO_Pin_##bit ? name##_L():name##_H(); } \
inline u8 name##(void) { return (GPIO##port##->IDR&GPIO_Pin_##bit) ? 1:0; } \
inline void name##(u32 x) { x ? name##_H():name##_L(); } \
inline void name##_O(u32 x) { GPIO_InitTypeDef GPIO_InitStructure; \
RCC_##rcc##PeriphClockCmd(RCC_##rcc##Periph_GPIO##port, ENABLE); \
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_##mode; \
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_##bit; \
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_##speed; \
GPIO_Init(GPIO##port, &GPIO_InitStructure); \
x ? name##_H():name##_L(); } \
inline void name##_I(void) { GPIO_InitTypeDef GPIO_InitStructure; \
RCC_##rcc##PeriphClockCmd(RCC_##rcc##Periph_GPIO##port, ENABLE); \
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_##mode; \
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_##bit; \
GPIO_Init(GPIO##port, &GPIO_InitStructure); }
/**************************************************/
// Mode: AIN,IN_FLOATING,IPD,IPU,Out_OD,Out_PP,AF_OD,AF_PP
// Speed: 2MHZ,10MHz,50MHz
// Rcc: APB1,APB2,AHB
/**************************************************/
// PIN(Port,Bit,Rcc,Mode,Speed,Name)
/**************************************************/
PIN(B,0,APB2,Out_PP,50MHz,LED);
PIN(B,0,APB2,IPU,50MHz,KEY);
LED_O(1);
KEY_I();
这个是M4的头文件
#pragma diag_suppress=pe1665 //屏蔽警告
/***********************************************************************/
inline void GPIO_MODE_PUPD(GPIO_TypeDef* GPIOx,uint32_t mode,uint32_t pupd,uint16_t bit)
{ GPIOx->MODER &= ~(GPIO_MODER_MODER0 << (bit * 2));
GPIOx->MODER |= (mode << (bit * 2));
GPIOx->PUPDR &= ~(GPIO_PUPDR_PUPDR0 << ((uint16_t)bit * 2));
GPIOx->PUPDR |= (pupd << (bit * 2));
}
/***********************************************************************/
inline void GPIO_SPEED_OT(GPIO_TypeDef* GPIOx,uint32_t speed,uint16_t ot,uint16_t bit)
{ GPIOx->OSPEEDR &= ~(GPIO_OSPEEDER_OSPEEDR0 << (bit * 2));
GPIOx->OSPEEDR |= (speed << (bit * 2));
GPIOx->OTYPER &= ~((GPIO_OTYPER_OT_0) << ((uint16_t)bit));
GPIOx->OTYPER |= (uint16_t)(ot << ((uint16_t)bit));
}
/***********************************************************************/
inline void GPIO_IN(GPIO_TypeDef* GPIOx,uint32_t pupd,uint16_t bit)
{ GPIO_MODE_PUPD(GPIOx,GPIO_Mode_IN,pupd,bit);
}
/***********************************************************************/
inline void GPIO_AN(GPIO_TypeDef* GPIOx,uint32_t pupd,uint16_t bit)
{ GPIO_MODE_PUPD(GPIOx,GPIO_Mode_AN,pupd,bit);
}
/***********************************************************************/
inline void GPIO_OUT(GPIO_TypeDef* GPIOx,uint32_t speed,uint32_t pupd,uint16_t ot,uint16_t bit)
{ GPIO_SPEED_OT(GPIOx,speed,ot,bit);
GPIO_MODE_PUPD(GPIOx,GPIO_Mode_OUT,pupd,bit);
}
/***********************************************************************/
inline void GPIO_AF(GPIO_TypeDef* GPIOx,uint32_t speed,uint32_t pupd,uint16_t ot,uint16_t bit)
{ GPIO_SPEED_OT(GPIOx,speed,ot,bit);
GPIO_MODE_PUPD(GPIOx,GPIO_Mode_AF,pupd,bit);
}
/***********************************************************************/
inline void GPIOS_IN(GPIO_TypeDef* GPIOx,uint32_t pupd,uint16_t bits)
{ uint32_t pinpos = 0x00, pos = 0x00 , currentpin = 0x00;
for(pinpos = 0x00; pinpos < 0x10; pinpos++)
{ pos = ((uint32_t)0x01) << pinpos;
currentpin = bits & pos;
if(currentpin == pos)
{ GPIO_MODE_PUPD(GPIOx,GPIO_Mode_IN,pupd,pinpos);
}
}
}
/***********************************************************************/
inline void GPIOS_AN(GPIO_TypeDef* GPIOx,uint32_t pupd,uint16_t bits)
{ uint32_t pinpos = 0x00, pos = 0x00 , currentpin = 0x00;
for(pinpos = 0x00; pinpos < 0x10; pinpos++)
{ pos = ((uint32_t)0x01) << pinpos;
currentpin = bits & pos;
if(currentpin == pos)
{ GPIO_MODE_PUPD(GPIOx,GPIO_Mode_AN,pupd,pinpos);
}
}
}
/***********************************************************************/
inline void GPIOS_OUT(GPIO_TypeDef* GPIOx,uint32_t speed,uint32_t pupd,uint16_t ot,uint16_t bits)
{ uint32_t pinpos = 0x00, pos = 0x00 , currentpin = 0x00;
for(pinpos = 0x00; pinpos < 0x10; pinpos++)
{ pos = ((uint32_t)0x01) << pinpos;
currentpin = bits & pos;
if(currentpin == pos)
{ GPIO_SPEED_OT(GPIOx,speed,ot,pinpos);
GPIO_MODE_PUPD(GPIOx,GPIO_Mode_OUT,pupd,pinpos);
}
}
}
/***********************************************************************/
inline void GPIOS_AF(GPIO_TypeDef* GPIOx,uint32_t speed,uint32_t pupd,uint16_t ot,uint8_t af,uint16_t bits)
{ uint32_t pinpos = 0x00, pos = 0x00 , currentpin = 0x00;
for(pinpos = 0x00; pinpos < 0x10; pinpos++)
{ pos = ((uint32_t)0x01) << pinpos;
currentpin = bits & pos;
if(currentpin == pos)
{ GPIO_PinAFConfig(GPIOx,pinpos,af);
GPIO_SPEED_OT(GPIOx,speed,ot,pinpos);
GPIO_MODE_PUPD(GPIOx,GPIO_Mode_AF,pupd,pinpos);
}
}
}
/***********************************************************************/
#define PIN(rcc,port,bit,ot,pupd,speed,af,name)\
inline void name##_H() {GPIO##port##->ODR|=(1<<bit);}\
inline void name##_L() {GPIO##port##->ODR&=~(1<<bit);}\
inline void name##_T() {GPIO##port##->ODR^=(1<<bit);}\
inline void name##(u8 x) {x ? name##_H():name##_L();}\
inline u8 name##() {return GPIO##port##->IDR&(1<<bit) ? 1:0;}\
inline void name##_OUT() {RCC_##rcc##PeriphClockCmd(RCC_##rcc##Periph_GPIO##port, ENABLE);\
GPIO_OUT(GPIO##port,GPIO_Speed_##speed,GPIO_PuPd_##pupd,GPIO_OType_##ot,bit);}\
inline void name##_AF() {RCC_##rcc##PeriphClockCmd(RCC_##rcc##Periph_GPIO##port, ENABLE);\
GPIO_PinAFConfig(GPIO##port, GPIO_PinSource##bit, GPIO_AF_##af);\
GPIO_AF(GPIO##port,GPIO_Speed_##speed,GPIO_PuPd_##pupd,GPIO_OType_##ot,bit);}\
inline void name##_IN() {RCC_##rcc##PeriphClockCmd(RCC_##rcc##Periph_GPIO##port, ENABLE);\
GPIO_IN(GPIO##port,GPIO_PuPd_##pupd,bit);}\
inline void name##_AN() {RCC_##rcc##PeriphClockCmd(RCC_##rcc##Periph_GPIO##port, ENABLE);\
GPIO_AN(GPIO##port,GPIO_PuPd_##pupd,bit);}
/***********************************************************************/
#define PINS(rcc,port,bits,ot,pupd,speed,af,name)\
inline void name##_OUT() {RCC_##rcc##PeriphClockCmd(RCC_##rcc##Periph_GPIO##port, ENABLE);\
GPIOS_OUT(GPIO##port,GPIO_Speed_##speed,GPIO_PuPd_##pupd,GPIO_OType_##ot,bits);}\
inline void name##_AF() {RCC_##rcc##PeriphClockCmd(RCC_##rcc##Periph_GPIO##port, ENABLE);\
GPIOS_AF(GPIO##port,GPIO_Speed_##speed,GPIO_PuPd_##pupd,GPIO_OType_##ot,GPIO_AF_##af,bits);}\
inline void name##_IN() {RCC_##rcc##PeriphClockCmd(RCC_##rcc##Periph_GPIO##port, ENABLE);\
GPIOS_IN(GPIO##port,GPIO_PuPd_##pupd,bits);}\
inline void name##_AN() {RCC_##rcc##PeriphClockCmd(RCC_##rcc##Periph_GPIO##port, ENABLE);\
GPIOS_AN(GPIO##port,GPIO_PuPd_##pupd,bits);}
/***********************************************************************/
#define GPIO_AF_ 0xFF
/***********************************************************************/
PIN(AHB1,A,0,OD,NOPULL,2MHz,,KEY);
PIN(AHB1,B,0,PP,NOPULL,100MHz,,LED);
PIN(AHB1,A,9,PP,UP,50MHz,USART1,TXD1);
PIN(AHB1,A,10,OD,UP,50MHz,USART1,RXD1);
PIN(AHB1,G,12,PP,NOPULL,50MHz,FSMC,CS);
PIN(AHB1,F,0,PP,NOPULL,50MHz,FSMC,RS);
PIN(AHB1,D,4,PP,NOPULL,50MHz,FSMC,NOE);
PIN(AHB1,D,5,PP,NOPULL,50MHz,FSMC,NWE);
PINS(AHB1,F,0xf03f,PP,NOPULL,50MHz,FSMC,ADR00_09);
PINS(AHB1,G,0x003f,PP,NOPULL,50MHz,FSMC,ADR10_15);
PINS(AHB1,D,0x3800,PP,NOPULL,50MHz,FSMC,ADR16_18);
PINS(AHB1,D,0xc003,PP,NOPULL,50MHz,FSMC,DB00_03);
PINS(AHB1,E,0xff80,PP,NOPULL,50MHz,FSMC,DB04_12);
PINS(AHB1,D,0x0700,PP,NOPULL,50MHz,FSMC,DB13_15);
PINS(AHB1,E,0x0003,PP,NOPULL,50MHz,FSMC,NBL);
KEY_IN();
LED_OUT();
TXD1_AF();
RXD1_AF();
NOE_AF(); NWE_AF();//SRAM FSMC Contorl
NBL_AF(); NCE_AF();
DB00_03_AF();DB04_12_AF();DB13_15_AF();//SRAM FSMC Data & Addr
ADR00_09_AF(); ADR10_15_AF(); ADR16_18_AF();
|
|