软件模拟I2C时输入与输出切换
一 为达到类似C51的操作需要添加以下位带操作:#include "stm32f10x_gpio.h"
#include "stm32f10x_conf.h"
/* IO地址映射*/
#define GPIOA_ODR_Addr (GPIOA_BASE+12) //0x4001080C
#define GPIOB_ODR_Addr (GPIOB_BASE+12) //0x40010C0C
#define GPIOC_ODR_Addr (GPIOC_BASE+12) //0x4001100C
#define GPIOD_ODR_Addr (GPIOD_BASE+12) //0x4001140C
#define GPIOE_ODR_Addr (GPIOE_BASE+12) //0x4001180C
#define GPIOF_ODR_Addr (GPIOF_BASE+12) //0x40011A0C
#define GPIOG_ODR_Addr (GPIOG_BASE+12) //0x40011E0C
#define GPIOA_IDR_Addr (GPIOA_BASE+8) //0x40010808
#define GPIOB_IDR_Addr (GPIOB_BASE+8) //0x40010C08
#define GPIOC_IDR_Addr (GPIOC_BASE+8) //0x40011008
#define GPIOD_IDR_Addr (GPIOD_BASE+8) //0x40011408
#define GPIOE_IDR_Addr (GPIOE_BASE+8) //0x40011808
#define GPIOF_IDR_Addr (GPIOF_BASE+8) //0x40011A08
#define GPIOG_IDR_Addr (GPIOG_BASE+8) //0x40011E08
/* 输入 */
#define PAin(n) *((volatile unsigned long *)(0x42000000+((GPIOA_IDR_Addr-0x40000000)<<5)+(n<<2)))
#define PBin(n) *((volatile unsigned long *)(0x42000000+((GPIOB_IDR_Addr-0x40000000)<<5)+(n<<2)))
#define PCin(n) *((volatile unsigned long *)(0x42000000+((GPIOC_IDR_Addr-0x40000000)<<5)+(n<<2)))
#define PDin(n) *((volatile unsigned long *)(0x42000000+((GPIOD_IDR_Addr-0x40000000)<<5)+(n<<2)))
#define PEin(n) *((volatile unsigned long *)(0x42000000+((GPIOE_IDR_Addr-0x40000000)<<5)+(n<<2)))
#define PFin(n) *((volatile unsigned long *)(0x42000000+((GPIOF_IDR_Addr-0x40000000)<<5)+(n<<2)))
/* 输出 */
#define PAout(n) *((volatile unsigned long *)(0x42000000+((GPIOA_ODR_Addr-0x40000000)<<5)+(n<<2)))
#define PBout(n) *((volatile unsigned long *)(0x42000000+((GPIOB_ODR_Addr-0x40000000)<<5)+(n<<2)))
#define PCout(n) *((volatile unsigned long *)(0x42000000+((GPIOC_ODR_Addr-0x40000000)<<5)+(n<<2)))
#define PDout(n) *((volatile unsigned long *)(0x42000000+((GPIOD_ODR_Addr-0x40000000)<<5)+(n<<2)))
#define PEout(n) *((volatile unsigned long *)(0x42000000+((GPIOE_ODR_Addr-0x40000000)<<5)+(n<<2)))
二 假设SDA=PB0,SCL=PB1:
#define SDA_IN() {GPIOB->CRL&=0XFFFFFFF0;GPIOB->CRL|=8<<0;} /*也就是对应的GPIO输入输出配置寄存器进行操作:PB0的输入上拉*/
#define SDA_OUT() {GPIOB->CRL&=0XFFFFFFF0;GPIOB->CRL|=3<<0;}/*PB0的上拉,输出速度50MHZ*/
#define IIC_SCL PBout(1) /*相当于操作函数GPIO_WriteBit*/
#define IIC_SDA PBout(0)
#define READ_SDA PBin(0) /*相当于操作函数GPIO_ReadInputDataBit*/