C51 COMPILER V9.59.0.0 STC8A_SOFT_I2C 06/10/2021 10:06:09 PAGE 1 C51 COMPILER V9.59.0.0, COMPILATION OF MODULE STC8A_SOFT_I2C OBJECT MODULE PLACED IN .\list\STC8A_Soft_I2C.obj COMPILER INVOKED BY: C:\Keil_v5\C51\BIN\C51.EXE STC8A_Soft_I2C.c OMF2 OPTIMIZE(8,SPEED) BROWSE DEBUG PRINT(.\list\STC8A_ -Soft_I2C.lst) TABS(2) OBJECT(.\list\STC8A_Soft_I2C.obj) line level source 1 /*---------------------------------------------------------------------*/ 2 /* --- STC MCU Limited ------------------------------------------------*/ 3 /* --- STC 1T Series MCU Demo Programme -------------------------------*/ 4 /* --- Mobile: (86)13922805190 ----------------------------------------*/ 5 /* --- Fax: 86-0513-55012956,55012947,55012969 ------------------------*/ 6 /* --- Tel: 86-0513-55012928,55012929,55012966 ------------------------*/ 7 /* --- Web: www.STCMCU.com --------------------------------------------*/ 8 /* --- Web: www.STCMCUDATA.com ---------------------------------------*/ 9 /* --- QQ: 800003751 -------------------------------------------------*/ 10 /* 如果要在程序中使用此代码,请在程序中注明使用了STC的资料及程序 */ 11 /*---------------------------------------------------------------------*/ 12 13 #include "STC8A_Soft_I2C.h" 14 15 #define SLAW 0x5A 16 #define SLAR 0x5B 17 18 sbit SDA = P0^1; //定义SDA 19 sbit SCL = P0^0; //定义SCL 20 21 //======================================================================== 22 // 函数: void I2C_Delay(void) 23 // 描述: I2C延时函数. 24 // 参数: none. 25 // 返回: none. 26 // 版本: V1.0, 2020-09-15 27 //======================================================================== 28 void I2C_Delay(void) //for normal MCS51, delay (2 * dly + 4) T, for STC12Cxxxx delay (4 * dly + 10) T 29 { 30 1 u8 dly; 31 1 dly = MAIN_Fosc / 2000000UL; //按2us计算 32 1 while(--dly); 33 1 } 34 35 //======================================================================== 36 // I2C总线函数 37 //======================================================================== 38 void I2C_Start(void) //start the I2C, SDA High-to-low when SCL is high 39 { 40 1 SDA = 1; 41 1 I2C_Delay(); 42 1 SCL = 1; 43 1 I2C_Delay(); 44 1 SDA = 0; 45 1 I2C_Delay(); 46 1 SCL = 0; 47 1 I2C_Delay(); 48 1 } 49 50 51 void I2C_Stop(void) //STOP the I2C, SDA Low-to-high when SCL is high 52 { 53 1 SDA = 0; 54 1 I2C_Delay(); C51 COMPILER V9.59.0.0 STC8A_SOFT_I2C 06/10/2021 10:06:09 PAGE 2 55 1 SCL = 1; 56 1 I2C_Delay(); 57 1 SDA = 1; 58 1 I2C_Delay(); 59 1 } 60 61 void S_ACK(void) //Send ACK (LOW) 62 { 63 1 SDA = 0; 64 1 I2C_Delay(); 65 1 SCL = 1; 66 1 I2C_Delay(); 67 1 SCL = 0; 68 1 I2C_Delay(); 69 1 } 70 71 void S_NoACK(void) //Send No ACK (High) 72 { 73 1 SDA = 1; 74 1 I2C_Delay(); 75 1 SCL = 1; 76 1 I2C_Delay(); 77 1 SCL = 0; 78 1 I2C_Delay(); 79 1 } 80 81 void I2C_Check_ACK(void) //Check ACK, If F0=0, then right, if F0=1, then error 82 { 83 1 SDA = 1; 84 1 I2C_Delay(); 85 1 SCL = 1; 86 1 I2C_Delay(); 87 1 F0 = SDA; 88 1 SCL = 0; 89 1 I2C_Delay(); 90 1 } 91 92 //======================================================================== 93 // 函数: void I2C_WriteAbyte(u8 dat) 94 // 描述: I2C发送一个字节数据函数. 95 // 参数: 发送的数据. 96 // 返回: none. 97 // 版本: V1.0, 2020-09-15 98 //======================================================================== 99 void I2C_WriteAbyte(u8 dat) //write a byte to I2C 100 { 101 1 u8 i; 102 1 i = 8; 103 1 do 104 1 { 105 2 if(dat & 0x80) SDA = 1; 106 2 else SDA = 0; 107 2 dat <<= 1; 108 2 I2C_Delay(); 109 2 SCL = 1; 110 2 I2C_Delay(); 111 2 SCL = 0; 112 2 I2C_Delay(); 113 2 } 114 1 while(--i); 115 1 } 116 C51 COMPILER V9.59.0.0 STC8A_SOFT_I2C 06/10/2021 10:06:09 PAGE 3 117 //======================================================================== 118 // 函数: u8 I2C_ReadAbyte(void) 119 // 描述: I2C读取一个字节数据函数. 120 // 参数: none. 121 // 返回: 读取数据. 122 // 版本: V1.0, 2020-09-15 123 //======================================================================== 124 u8 I2C_ReadAbyte(void) //read A byte from I2C 125 { 126 1 u8 i,dat; 127 1 i = 8; 128 1 SDA = 1; 129 1 do 130 1 { 131 2 SCL = 1; 132 2 I2C_Delay(); 133 2 dat <<= 1; 134 2 if(SDA) dat++; 135 2 SCL = 0; 136 2 I2C_Delay(); 137 2 } 138 1 while(--i); 139 1 return(dat); 140 1 } 141 142 //======================================================================== 143 // 函数: void SI2C_WriteNbyte(u8 addr, u8 *p, u8 number) 144 // 描述: I2C写入数据函数. 145 // 参数: addr: 指定地址, *p写入数据存储位置, number写入数据个数. 146 // 返回: none. 147 // 版本: V1.0, 2020-09-15 148 //======================================================================== 149 void SI2C_WriteNbyte(u8 addr, u8 *p, u8 number) /* WordAddress,First Data Address,Byte lenth * -/ 150 { 151 1 I2C_Start(); 152 1 I2C_WriteAbyte(SLAW); 153 1 I2C_Check_ACK(); 154 1 if(!F0) //F0=0,right, F0=1,error 155 1 { 156 2 I2C_WriteAbyte(addr); 157 2 I2C_Check_ACK(); 158 2 if(!F0) 159 2 { 160 3 do 161 3 { 162 4 I2C_WriteAbyte(*p); p++; 163 4 I2C_Check_ACK(); 164 4 if(F0) break; 165 4 } 166 3 while(--number); 167 3 } 168 2 } 169 1 I2C_Stop(); 170 1 } 171 172 //======================================================================== 173 // 函数: void SI2C_ReadNbyte(u8 addr, u8 *p, u8 number) 174 // 描述: I2C读取数据函数. 175 // 参数: addr: 指定地址, *p读取数据存储位置, number读取数据个数. 176 // 返回: none. 177 // 版本: V1.0, 2020-09-15 C51 COMPILER V9.59.0.0 STC8A_SOFT_I2C 06/10/2021 10:06:09 PAGE 4 178 //======================================================================== 179 void SI2C_ReadNbyte(u8 addr, u8 *p, u8 number) /* WordAddress,First Data Address,Byte lenth */ 180 { 181 1 I2C_Start(); 182 1 I2C_WriteAbyte(SLAW); 183 1 I2C_Check_ACK(); 184 1 if(!F0) 185 1 { 186 2 I2C_WriteAbyte(addr); 187 2 I2C_Check_ACK(); 188 2 if(!F0) 189 2 { 190 3 I2C_Start(); 191 3 I2C_WriteAbyte(SLAR); 192 3 I2C_Check_ACK(); 193 3 if(!F0) 194 3 { 195 4 do 196 4 { 197 5 *p = I2C_ReadAbyte(); p++; 198 5 if(number != 1) S_ACK(); //send ACK 199 5 } 200 4 while(--number); 201 4 S_NoACK(); //send no ACK 202 4 } 203 3 } 204 2 } 205 1 I2C_Stop(); 206 1 } MODULE INFORMATION: STATIC OVERLAYABLE CODE SIZE = 237 ---- CONSTANT SIZE = ---- ---- XDATA SIZE = ---- ---- PDATA SIZE = ---- ---- DATA SIZE = ---- 10 IDATA SIZE = ---- ---- BIT SIZE = ---- ---- EDATA SIZE = ---- ---- HDATA SIZE = ---- ---- XDATA CONST SIZE = ---- ---- FAR CONST SIZE = ---- ---- END OF MODULE INFORMATION. C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)