stm32 io操作 头文件规范
0赞在stm32众多项目开发中,有太多的对io进行操作,若置1或清0,使用官方库提供的函数,固然方便,规范,但是需要包含标准的库,尺寸较大,还得处理不同版本兼容问题,包括io初始化也太繁琐,于是操作原子等例程进行精简,
初始化如下,变得如此简单:适用于stm32f和stm32L
void Init_Io(void)
{
JTAG_Set(SWD_ENABLE); //开启SWD
RCC->APB2ENR|=1<<6;//先使能外设PORTE时钟
RCC->APB2ENR|=1<<5;//先使能外设PORTD时钟
RCC->APB2ENR|=1<<4;//先使能外设PORTC时钟
RCC->APB2ENR|=1<<3;//先使能外设PORTB时钟
RCC->APB2ENR|=1<<2;//先使能外设PORTA时钟
RCC->APB2ENR|=1<<0; //开启辅助时钟
JTAG_Set(SWD_ENABLE); //开启SWD
//AFIO->MAPR&=0XF8FFFFFF; //清除MAPR的[26:24]
//AFIO->MAPR|=0X02000000; //设置jtag模式
//PORTC6~10复用推挽输出
GPIOA->CRL&=0XFFFF0000;
GPIOA->CRL|=0X00003333;
GPIOA->ODR|=0Xffff;
//PORTC 推挽输出
头文件
#ifndef __STM32_IO_H__
#define __STM32_IO_H__
// taobao:ccjt.taobao.com
//#include "stm3"
///////////////////////////////////////////////////////////////
//位带操作,实现51类似的GPIO控制功能
//具体实现思想,参考<
//IO口操作宏定义
#define BITBAND(addr, bitnum) ((addr & 0xF0000000)+0x2000000+((addr &0xFFFFF)<<5)+(bitnum<<2))
#define MEM_ADDR(addr) *((volatile unsigned long *)(addr))
#define BIT_ADDR(addr, bitnum) MEM_ADDR(BITBAND(addr, bitnum))
#if defined(STM32L1XX_MD) || defined(STM32L1XX_HD)|| defined(STM32L1XX_MD_PLUS)
#define OFFSET_O 0x14
#define OFFSET_I 0x10
#elif defined (STM32F10X_MD) || defined (STM32F10X_HD) || defined (STM32F10X_XL) || defined (STM32F10X_CL)
#define OFFSET_O 0x0c
#define OFFSET_I 0x08
#else
#error "11222"
#endif
//IO口地址映射
#define GPIOA_ODR_Addr (GPIOA_BASE+OFFSET_O) //0x4001080C
#define GPIOB_ODR_Addr (GPIOB_BASE+OFFSET_O) //0x40010C0C
#define GPIOC_ODR_Addr (GPIOC_BASE+OFFSET_O) //0x4001100C
#define GPIOD_ODR_Addr (GPIOD_BASE+OFFSET_O) //0x4001140C
#define GPIOE_ODR_Addr (GPIOE_BASE+OFFSET_O) //0x4001180C
#define GPIOF_ODR_Addr (GPIOF_BASE+OFFSET_O) //0x40011A0C
#define GPIOG_ODR_Addr (GPIOG_BASE+OFFSET_O) //0x40011E0C
#define GPIOA_IDR_Addr (GPIOA_BASE+OFFSET_I) //0x40010808
#define GPIOB_IDR_Addr (GPIOB_BASE+OFFSET_I) //0x40010C08
#define GPIOC_IDR_Addr (GPIOC_BASE+OFFSET_I) //0x40011008
#define GPIOD_IDR_Addr (GPIOD_BASE+OFFSET_I) //0x40011408
#define GPIOE_IDR_Addr (GPIOE_BASE+OFFSET_I) //0x40011808
#define GPIOF_IDR_Addr (GPIOF_BASE+OFFSET_I) //0x40011A08
#define GPIOG_IDR_Addr (GPIOG_BASE+OFFSET_I) //0x40011E08
//IO口操作,只对单一的IO口!
//确保n的值小于16!
#define PAout(n) BIT_ADDR(GPIOA_ODR_Addr,n) //输出
#define PAin(n) BIT_ADDR(GPIOA_IDR_Addr,n) //输入
#define PBout(n) BIT_ADDR(GPIOB_ODR_Addr,n) //输出
#define PBin(n) BIT_ADDR(GPIOB_IDR_Addr,n) //输入
#define PCout(n) BIT_ADDR(GPIOC_ODR_Addr,n) //输出
#define PCin(n) BIT_ADDR(GPIOC_IDR_Addr,n) //输入
#define PDout(n) BIT_ADDR(GPIOD_ODR_Addr,n) //输出
#define PDin(n) BIT_ADDR(GPIOD_IDR_Addr,n) //输入
#define PEout(n) BIT_ADDR(GPIOE_ODR_Addr,n) //输出
#define PEin(n) BIT_ADDR(GPIOE_IDR_Addr,n) //输入
#define PFout(n) BIT_ADDR(GPIOF_ODR_Addr,n) //输出
#define PFin(n) BIT_ADDR(GPIOF_IDR_Addr,n) //输入
#define PGout(n) BIT_ADDR(GPIOG_ODR_Addr,n) //输出
#define PGin(n) BIT_ADDR(GPIOG_IDR_Addr,n) //输入
/////////////////////////////////////////////////////////////////
//Ex_NVIC_Config专用定义
#define GPIO_A 0
#define GPIO_B 1
#define GPIO_C 2
#define GPIO_D 3
#define GPIO_E 4
#define GPIO_F 5
#define GPIO_G 6
#define FTIR 1 //下降沿触发
#define RTIR 2 //上升沿触发
/////////////////////////////////////////////////////////////////
//JTAG模式设置定义
#define JTAG_SWD_DISABLE 0X02
#define SWD_ENABLE 0X01
#define JTAG_SWD_ENABLE 0X00
#endif
每个项目的头文件如下
#ifndef __HW_CFG_H__
#define __HW_CFG_H__
#define SMART
#ifdef SMART
//LED端口定义
#define LED0 PAout(15)// PA15
#define LED1 PAout(8) // PA8
#define LED2 PDout(13)
#define LED3 PDout(14)
#define LED4 PDout(15)
#define LCD_CS_SET GPIOA->BSRR=1<<0 //片选端口
#define LCD_RS_SET GPIOA->BSRR=1<<1 //数据/命令
#define LCD_WR_SET GPIOA->BSRR=1<<3 //写数据
#define LCD_RD_SET GPIOA->BSRR=1<<2 //读数据
#define LCD_CS_CLR GPIOA->BRR=1<<0 //片选端口
#define LCD_RS_CLR GPIOA->BRR=1<<1 //数据/命令
#define LCD_WR_CLR GPIOA->BRR=1<<3 //写数据
#define LCD_RD_CLR GPIOA->BRR=1<<2 //读数据
#define LCD_PORT GPIOB
// 20150128
// cckj 2011-12-22 0 1 2 3
//PB0~15,作为数据线
#define DATAOUT(x) LCD_PORT->ODR=x; //数据输出
#define DATAIN LCD_PORT->IDR; //数据输入
#define PEN PAin(9) //PC1 INT
#define DOUT PAin(6) //PC2 MISO
#define TDIN PAout(7) //PC3 MOSI
#define TCLK PAout(5) //PC0 SCLK
#define TCS PAout(4) //PC13 CS