STM32 IO 模拟IIC I2C
来源:互联网 发布:远程控制开关机软件 编辑:程序博客网 时间:2024/05/14 14:04
#define I2C_Speed 100000#define I2C_EE I2C1#define uStatus0x80#definedTime 5#define I2C_EE_GPIO GPIOB#define I2C_EE_SCL GPIO_Pin_5#define I2C_EE_SDA GPIO_Pin_4#define I2C_EE_CLK RCC_APB1Periph_I2C1#define SCL_H GPIOB->BSRR = GPIO_Pin_5#define SCL_L GPIOB->BRR = GPIO_Pin_5#define SDA_H GPIOB->BSRR = GPIO_Pin_4#define SDA_L GPIOB->BRR = GPIO_Pin_4#define SCL_read GPIOB->IDR & GPIO_Pin_5#define SDA_read GPIOB->IDR & GPIO_Pin_4 static unsigned int cntForInitial = 0;static unsigned char fSigStatus = 0;//static bool LedStatus = true;void I2C_Init(){ GPIO_InitTypeDef GPIO_InitStructure; //* Configure I2C_EE pins: SCL and SDA GPIO_InitStructure.GPIO_Pin = I2C_EE_SCL | I2C_EE_SDA; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD; GPIO_Init(I2C_EE_GPIO, &GPIO_InitStructure);}static void i2c_start(){ SDA_H; SCL_H; DelayUs(dTime); SDA_L; DelayUs(dTime); SCL_L; DelayUs(dTime); //LedStatus = !LedStatus; //f_LCT1(LedStatus);}/*******************************************************************Fuction:Stop i2c*******************************************************************/static void i2c_stop(){ SDA_L; SCL_H; DelayUs(dTime); SDA_H; DelayUs(dTime); SCL_H; DelayUs(dTime);}/*******************************************************************Fuction:i2c Master wait for ackOnly ack*******************************************************************/static unsigned char i2c_rd_ack(){ unsigned char flag = 0; SDA_H; SCL_H; DelayUs(dTime/2); flag = SDA_read; DelayUs(dTime/2); SCL_L; DelayUs(dTime/2); if(flag == 1) return 0; return 1;}/*******************************************************************Fuction:i2c Byte transmissionOnly Send,no ack,no stop*******************************************************************/static unsigned char i2c_sb(unsigned char Byte){unsigned char cnt;SCL_L;for(cnt=0;cnt<8;cnt++) { if(Byte&0x80) SDA_H; else SDA_L; DelayUs(dTime); SCL_H; DelayUs(dTime); SCL_L; Byte <<= 1; DelayUs(dTime); } return i2c_rd_ack();}/*******************************************************************Fuction:i2c Byte receiveReturn Byte*******************************************************************/static unsigned char i2c_rb(){unsigned char cnt;unsigned char Byte=0;SDA_H; for(cnt=0;cnt<8;++cnt) { Byte <<= 1; DelayUs(dTime); SCL_H; DelayUs(dTime); if(SDA_read) Byte |= 0x01; SCL_L; DelayUs(dTime); } return Byte;}/*******************************************************************Fuction:i2c ACK Master send*******************************************************************/static void i2c_wr_ack(unsigned char ACK){ if(ACK)SDA_H;elseSDA_L; SCL_H; DelayUs(dTime); SCL_L; DelayUs(dTime);}/*******************************************************************Fuction:i2c Byte receiveReturn Byte*******************************************************************/uint8_t ReadReg(unsigned int addr){ uint8_t temp;i2c_start();if(!i2c_sb(BRG_DEV_ADDR)){i2c_stop();return 0;}if(!i2c_sb((uint8_t)(addr >> 8))){i2c_stop();return 0;}if(!i2c_sb((uint8_t)(addr & 0xFF))){i2c_stop();return 0;}i2c_start();if(!i2c_sb(BRG_DEV_ADDR |0x01)){i2c_stop();return 0;}temp = i2c_rb();i2c_wr_ack(1);i2c_stop();return temp;}unsigned char WriteReg8(unsigned int addr,unsigned char wData){i2c_start();if(!i2c_sb(BRG_DEV_ADDR)){i2c_stop();return 0;}if(!i2c_sb((unsigned char)(addr >> 8))){i2c_stop();return 0;}if(!i2c_sb((unsigned char)(addr & 0xFF))){i2c_stop();return 0;}if(!i2c_sb((unsigned char)(wData))){i2c_stop();return 0;}i2c_stop();//DelayUs(1);return 1;}unsigned char WriteReg16(unsigned int addr,unsigned char wData1,unsigned char wData2){i2c_start();if(!i2c_sb(BRG_DEV_ADDR)){i2c_stop();return 0;}if(!i2c_sb((unsigned char)(addr >> 8))){i2c_stop();return 0;}if(!i2c_sb((unsigned char)(addr & 0xFF))){i2c_stop();return 0;}if(!i2c_sb((unsigned char)(wData2))){i2c_stop();return 0;}if(!i2c_sb((unsigned char)(wData1))){i2c_stop();return 0;}i2c_stop();DelayUs(1);return 1;}unsigned char WriteReg32(unsigned int addr,unsigned char wData1,unsigned char wData2,unsigned char wData3,unsigned char wData4){i2c_start();if(!i2c_sb(BRG_DEV_ADDR)){i2c_stop();return 0;}if(!i2c_sb((unsigned char)(addr >> 8))){i2c_stop();return 0;}if(!i2c_sb((unsigned char)(addr & 0xFF))){i2c_stop();return 0;}if(!i2c_sb((unsigned char)(wData4))){i2c_stop();return 0;}if(!i2c_sb((unsigned char)(wData3))){i2c_stop();return 0;}if(!i2c_sb((unsigned char)(wData2))){i2c_stop();return 0;}if(!i2c_sb((unsigned char)(wData1))){i2c_stop();return 0;}i2c_stop();DelayUs(1);return 1;}
0 0
- STM32 IO 模拟IIC I2C
- STM32 IO口模拟I2C+驱动MPU6050
- STM32模拟 IIC
- STM32软件模拟IIC
- STM32模拟IIC
- STM32模拟IIC
- STM32模拟I2C程序
- stm32软件模拟I2C
- STM32 模拟 I2C
- STM32模拟IIC读写AT24CXX
- IO模拟I2C代码
- io模拟i2c程序
- IO模拟I2C
- 430模拟IIC(I2C)驱动程序
- STM32 模拟I2C (STM32F051)
- STM32 模拟IIC主设备 非IIC静默模式
- 关于STM32 的 硬件IIC和 模拟IIC理解学习
- STM32硬件IIC与51模拟IIC通信
- 既然选择了,就要坚持下去
- 点击menu键弹出的选项菜单
- POJ2697(BFS)
- shell 入门
- 结构体包含二级指针和二级指针内存的申请与释放问题
- STM32 IO 模拟IIC I2C
- Qt调用jrtplib实现单播、多播和广播
- 如何给二维数组排序
- Android系列之UI组件----Menu菜单
- 【java学习】Json框架
- 5882 Balanced Game
- 【Android】actionbar显示选项菜单
- Linux的SOCKET编程详解
- POJ2531