搜索
bottom↓
回复: 22

在IAR中IO快速配置STM8,STM32F0,STM32F1,STM32F4

  [复制链接]

出0入0汤圆

发表于 2017-3-26 11:17:19 | 显示全部楼层 |阅读模式
本帖最后由 way2888 于 2017-3-26 21:18 编辑

在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);
        }
    }
}




阿莫论坛20周年了!感谢大家的支持与爱护!!

月入3000的是反美的。收入3万是亲美的。收入30万是移民美国的。收入300万是取得绿卡后回国,教唆那些3000来反美的!

出0入0汤圆

发表于 2017-3-26 12:02:14 | 显示全部楼层
good,很方便,STM32可以用吗?

出0入0汤圆

 楼主| 发表于 2017-3-26 12:52:45 | 显示全部楼层
ifus 发表于 2017-3-26 12:02
good,很方便,STM32可以用吗?

可以的,只是要对应芯片定义不同的头文件

出0入0汤圆

 楼主| 发表于 2017-3-26 13:01:23 | 显示全部楼层
这个是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();

出0入0汤圆

 楼主| 发表于 2017-3-26 13:07:29 | 显示全部楼层
本帖最后由 way2888 于 2017-3-26 13:19 编辑

这个是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();

出0入0汤圆

 楼主| 发表于 2017-3-26 13:11:41 | 显示全部楼层
本帖最后由 way2888 于 2017-3-26 13:34 编辑

这个是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();

出0入0汤圆

发表于 2017-3-26 13:22:35 | 显示全部楼层
果然很方便,感觉就象带参数的宏定义

出0入0汤圆

发表于 2017-3-26 15:21:34 | 显示全部楼层
确实很牛

出0入0汤圆

发表于 2017-3-26 20:58:33 | 显示全部楼层
不错!想知道这个宏定义在定义时是如何调试的

出0入0汤圆

发表于 2017-3-26 21:00:25 | 显示全部楼层

出0入0汤圆

发表于 2017-3-26 23:00:08 | 显示全部楼层
牛人,学习了!

出0入0汤圆

发表于 2017-3-26 23:16:49 | 显示全部楼层
漂亮,很牛

出0入12汤圆

发表于 2017-3-27 10:45:48 | 显示全部楼层
赞!

出0入0汤圆

发表于 2017-3-27 13:54:06 | 显示全部楼层
很好,谢谢。

出0入0汤圆

发表于 2017-3-28 10:01:27 | 显示全部楼层
牛人啊,使用起来方便多了

出0入0汤圆

发表于 2017-3-29 15:11:40 | 显示全部楼层
IAR特有?

出0入0汤圆

 楼主| 发表于 2017-3-29 18:12:10 | 显示全部楼层

复杂化了,如果修改端口和引脚号的话,修改的地方多了

出0入0汤圆

 楼主| 发表于 2017-3-29 18:13:36 | 显示全部楼层

其他的编译器没有试过

出0入0汤圆

发表于 2017-3-30 01:57:34 | 显示全部楼层
很牛~~~有时间试试~~~

出0入0汤圆

发表于 2017-5-3 10:00:56 | 显示全部楼层
大牛           

出0入0汤圆

发表于 2017-5-3 17:16:02 | 显示全部楼层
不错的方式,收藏了                  
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-4-26 05:47

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表