GPIO模拟IIC,可读可写,已验证
作者:互联网
1 #include "iic.h" 2 3 void IIC0_Delay(u32 count) 4 { 5 for(uint32_t i= count*3;i>0;i--){} 6 //Sys_HardDelayUs(count); 7 //rt_thread_mdelay(1); 8 } 9 void SCL0_OUT(void) 10 { 11 gpio_init(GPIOB, GPIO_MODE_OUT_OD, GPIO_OSPEED_50MHZ, GPIO_PIN_8); 12 } 13 void SDA0_OUT(void) 14 { 15 gpio_init(GPIOB, GPIO_MODE_OUT_OD, GPIO_OSPEED_50MHZ, GPIO_PIN_9); 16 } 17 void SDA0_IN(void) 18 { 19 gpio_init(GPIOB, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_9); 20 } 21 void IIC0_Start(void) 22 { 23 SDA0_OUT(); 24 SCL0(1); 25 SDA0(1); 26 IIC0_Delay(5); 27 SDA0(0); 28 IIC0_Delay(5); 29 SCL0(0); 30 } 31 void IIC0_Stop(void) 32 { 33 SDA0_OUT(); 34 SCL0(0); 35 SDA0(0); 36 //IIC0_Delay(2); 37 SCL0(1); 38 IIC0_Delay(5); 39 SDA0(1); 40 IIC0_Delay(5); 41 //SCL0(0); 42 } 43 44 45 u8 IIC0_Wait_Ack(void) 46 { 47 u16 cnt=0; 48 49 SDA0(1); 50 IIC0_Delay(5); 51 SCL0(1); 52 IIC0_Delay(5); 53 SDA0_IN(); 54 while(READ_SDA0()) 55 { 56 cnt++; 57 IIC0_Delay(1); 58 if(cnt>1000) 59 { 60 IIC0_Stop(); 61 return 1; 62 } 63 } 64 65 SCL0(0); 66 SDA0_OUT(); 67 SDA0(0); 68 return 0; 69 } 70 71 72 void IIC0_write_Byte(u8 data) 73 { 74 u8 i; 75 SCL0(0); 76 IIC0_Delay(2); 77 for(i = 0; i < 8; i++) 78 { 79 if((data & 0x80)) 80 { 81 SDA0(1); 82 } 83 else 84 { 85 SDA0(0); 86 } 87 88 data <<= 1; 89 //SCL0(0); 90 IIC0_Delay(2); 91 SCL0(1); 92 IIC0_Delay(2); 93 SCL0(0); 94 IIC0_Delay(2); 95 } 96 } 97 void IIC0_Write(u8* data,u32 len) 98 { 99 u8 ret=0; 100 IIC0_Start(); 101 while(len) 102 { 103 len--; 104 IIC0_write_Byte(*data); 105 ret = IIC0_Wait_Ack(); 106 if(ret) 107 { 108 //break; 109 } 110 data++; 111 } 112 IIC0_Stop(); 113 } 114 void IIC0_Ack(void) 115 { 116 SCL0(0); 117 SDA0_OUT(); 118 SDA0(0); 119 IIC0_Delay(2); 120 SCL0(1); 121 IIC0_Delay(2); 122 SCL0(0); 123 } 124 void IIC0_NAck(void) 125 { 126 SCL0(0); 127 SDA0_OUT(); 128 SDA0(1); 129 IIC0_Delay(2); 130 SCL0(1); 131 IIC0_Delay(2); 132 SCL0(0); 133 134 } 135 u8 IIC0_Read_Byte(u8 ack) 136 { 137 u8 i=0; 138 u8 bit=0; 139 u8 data=0; 140 SDA0_IN(); 141 for(i=0;i<8;i++) 142 { 143 SCL0(0); 144 IIC0_Delay(2); 145 bit = READ_SDA0(); 146 SCL0(1); 147 IIC0_Delay(2); 148 data |= (bit<<(7-i)); 149 } 150 if(ack) 151 { 152 IIC0_Ack(); 153 } 154 else 155 { 156 IIC0_NAck(); 157 } 158 return data; 159 } 160 161 void IIC0_Read(u8* data,u32 len,u8*out) 162 { 163 u8 i=0; 164 u8 ret=0; 165 IIC0_Start(); 166 while(i<2)//slave address bit0=Write,register address write 167 { 168 169 IIC0_write_Byte(*data); 170 ret = IIC0_Wait_Ack(); 171 if(ret) 172 { 173 break; 174 } 175 i++; 176 data++; 177 } 178 IIC0_Start(); 179 do//slave address bit0=Read 180 { 181 IIC0_write_Byte(*data); 182 ret = IIC0_Wait_Ack(); 183 if(ret) 184 { 185 break; 186 } 187 }while(0); 188 while(len) 189 { 190 len--; 191 *out = IIC0_Read_Byte(len);// if(len==0),NACK;if (len!=0),ACK; 192 if(!len)break; 193 out++; 194 } 195 IIC0_Stop(); 196 return; 197 } 198 199 void i2c0_soft_init(void) 200 { 201 SCL0_OUT(); 202 SDA0_OUT(); 203 } 204 205
标签:IIC,void,IIC0,Delay,SCL0,可读,SDA0,GPIO 来源: https://www.cnblogs.com/ycpkbql/p/16385321.html