IIC bus program (chip AT24C02 EEPROM)

share picture

 1 #include
2 #define uint unsigned int < br /> 3 #define uchar unsigned char
4
5 sbit SCL =P2^1 ;
6 sbit SDA =P2^0;
7
8 sbit K1 =P3^0;
9 sbit K2 =P3^1;
10 sbit K3 =P3^0;
11 sbit K4 =P3^1;
12
< span style="color: #008080"> 13
sbit LSA =P2^2;
14 sbit LSB =P2^3;
15 sbit LS C =P2^4;
16 uchar num;
17 uchar code smgduan[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f< /span>,0x6f};
18
19 /********************************************** *********************************
20 * Function name: Delay10us()
21 * Function function: Delay Hour 10us
22 * Input: None
< span style="color: #008080"> 23
* Output: None
24 *********************************** ********************************************/
25 void Delay10us()//The delay is determined by calculation to be 10uM
26 {
27 unsigned char a,b;
28 for(b=1;b>0;b--)
29 for(a=2;a>0 ;a--);
30
31 }
32 /************************** ************************************************** ***
33 * Function name: I2cStart()
< /span> 34 * Function: Start signal: SDA signal is generated during the high level of SCL clock signal A falling edge
35 * Input: None
36 * Output : None
37 * Note: SDA and SCL are both 0 after the start< br /> 38 **************** ************************************************** *************/
39< /span>
40 void I2cStart()
41 {
42 SDA=1;
43 Delay10us();
44 SCL=1;
45 Delay10us();//The setup time is the SDA hold time>4.7us< br /> 46 SDA=0;< br /> 47 Delay10us();//The hold time is >4us
48 SCL=0;
49 Delay10us ();
50 }
51 /********** ******************************************** *************************
52 < span style="color: #008000">* Function name: I2cStop()
53 * Function: Termination signal: SDA signal generates a rising edge during the high level of SCL clock signal
54 * Input: None
55 * Output: None
56 * Note: Keep both SDA and SCL at 1 after the end; means Bus is idle
57 ************* ************************************************** ****************/
58
59 void I2cStop()
60 {
61 SDA=0;
62 Delay10us();
63 SCL= 1;
64 Delay10us();//The establishment time is greater than 4.7us
65 SDA=1;
66 Delay10us();
67 }
68 //Write bytes for IIC< br /> 69 uchar IICsendByte(uchar dat)
70 {
71 uchar a=< span style="color: #800080">0
,b;
72< /span> SDA =dat;
73 for(a=0;a<8;a++)
74 {
75 SDA =dat>>7; //Take out the highest bit, and then move all other bits to the right
76 dat =dat<<1; // Because the value of dat has not changed, so, leave it to the left Shift one place, the highest shift will drop, and the second highest position will become the highest position
77 SDA =1;
78 delay(1);
79 SCL=< span style="color: #800080">0
;
80 delay(1);
81} //At this time, all 8-bit data has been transmitted, and then we will respond. span>
82 SDA=1; //SDA and SCL are set to 1 to pave the way for the following answer
83 delay(1);
84 SCL=1;
85 // delay(1);
86 while (SDA) //When the answer is successful, SDA=0; jump out of the loop
87 {
88 b++;
89 if(b>200)
90 SCL=< span style="color: #800080">0
;
91 dealy(1);
92 return 0; //Indicates that the sending (communication) failed
93 }
94 SCL =0;
95 delay( 1);
96 return 1; //When the sending is successful, return 1
97 }
98 //Read bytes for IIC
99 uchar IICReadByte()
100 {
101 uchar a,dat =0;
102 SD1= 1; //First let it pull High, let it be idle, so as not to make the data of the following SDA low
103 delay(1);
104 for(a=0;a<8
;a++)
105< /span> {
106 SCL =1; //In order to read data, first make SCL high, just to make Read again when the data is stable
107 dealy(1);
108 dat<<=1 ; // 0 00 10 110 must be placed in front of dat|=SDA, otherwise After putting all the 8-bit data in dat, if you move it by one bit, the highest displacement will be moved. If the data is wrong, it will be shifted by one bit to the left. When the second cycle comes over, this will be saved in the dat. The data will continue to move to the left once
109 dat|=SDA; //< /span> 01 11 111 That is to say, it only needs to move 7 times, and the highest displacement is not allowed to da t Perform a "bitwise OR" with SDA to assign the value of SDA to dat
110 delay(1);
111 SCL=0; //Let SCL low is to make Data change
112 delay(1);
113 }
114 return dat;
115 }
116 //Chip AT24C02 communication read and write
117 void AT24C02writeByte(uchar add,uchar dat)
118 < span style="color: #000000">{
119 IICstart();
120 IICsendByte(0xa0);//This is the device address. The upper four digits of the 8-bit are fixed and the last digit represents 0 for writing, 1 for reading, and the remaining three digits. Bits A1, A2, A3, we got the ground
121 IICsendByte(add) ;
122 IICsendByte(dat);//We thank you in the byte function of IIC
123 IICStop();
124 }
125 uchar AT24C02rea d(uchar add)//Because it is to read the signal, it is necessary to know the address of the read< /span>
126 {
127 uchar value;
128 IICstart();
129 IICsendByte(0xa0 );//This is the device address, the upper four digits of 8 bits are the last fixed digit It means 0 for writing, 1 for reading, and the remaining three digits A1, A2, and A3, and we are connected to the ground
130 IICsendByte(add);
131 IICstart(); //Because the address must be written before and the data inside will be read later, the direction of reading and writing will change, so it must be rewritten Start signal and device address
132 IICsendByte(0 xa1);
133 value =IICReadByte();//Read the data in the address directly, and save the value read back to value< br />134 IICstop();
135 return value;
136 }
137 //Independent key function
138< /span> void keypros()
139 {
140 if (K1==0)
141 {
142 delay(10);
143 if (K1==0)
144 {
145 AT24C02writeByte(1,num); //Key 1 realizes the value of num, Write it into the address of 1, because the AT24C02 chip has 255 memory
146 }
147 while( !K1);// Determine whether the button is released. If you forget to write this statement, the function of this function will not stop To proceed
148 }
149 if (K2==0)
150 {
151 delay(10) ;
152 if(K2==0)
153 {
154 num =AT24C02read(1< /span>); //Read out the data stored in the memory of 1, and assign it to the value of the variable num span>
155 }
156 while(!K2);
157 }
158
159 if (K3==0)
160 {
161 delay(10);
162 if( K3==0)
163 {
164 num++; / /Add 1 to the value of num
165 if(num>255) //Because the variable of num is uchar, it can only store 255 characters
166 num =0;
167 }
168 while(!K3);
169 }
170 if (K4==0)
171 {
172 span> delay(10);
173 if(K4==0)
174 {
< span style="color: #008080">175
num =0; //Meticulously clear num to zero
176 }
177 while(!K4);
178 }
< /span>179 }
180 /****************** ************************************************** ***********
181 * Function name: DigDisplay()
182 * Function function: digital tube display function
183 * Input: None
184 * Output: None
185 ************************************* ******************************************/
186 void DigDisplay()
187 {
188 u8 i;
189 for(i=0;i<4;i++)
190 {
191 switch (i) //bit selection, select the digital tube to light up,< br />192 {
193 case(0):
194 LSA=0;LSB=0;LSC=0; break;//Display 0th place
195 case
195 case span>(1):
196 LSA=1;LSB=0;LSC=0; break;//Display 1st place
197 case(2):
198 LSA =0;LSB=1;LSC= 0; break;//Display 2nd place
199 case(3):
200 LSA =1;LSB=1;LSC= 0; break;//Display 3rd place
201 }
202 P0=disp[3-i];//Send data
203 delay(100); //Scan at intervals
204 P0=0x00;//Blanking
205 }
206 }
207 < span style="color: #008000">/*
*********************** ************************************************** *****
208 * Function name: datapros()
209 * Function function: data processing function
210 * 输入: 无
211 * 输出: 无
212 ********** ************************************************** *******************/
213 void datapros()
214 {
215 disp [0]=smgduan[num/1000]; //千位
216 disp[1]=smgduan[num%1000/100];//百位
217 disp[2]=smgduan[num%1000%100/10];//个位
218 disp[3]=smgduan[num%1000%100%10];
219 }
220 void main()
221 {
222 while(1)
223 {
224 Keypros(); //按键处理函数
225 datapros(); //数据处理函数
226 DigDisplay();//数码管显示函数
227 }
228 }

View Code

分享图片

  1 #include
2 #define uint unsigned int
3 #define uchar unsigned char
4
5 sbit SCL =P2^1;
6 sbit SDA =P2^0;
7
8 sbit K1 =P3^0;
9 sbit K2 =P3^1;
10 sbit K3 =P3^0;
11 sbit K4 =P3^1;
12
13 sbit LSA =P2^2;
14 sbit LSB =P2^3;
15 sbit LSC =P2^4;
16 uchar num;
17 uchar code smgduan[] ={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
18
19 /************************************************************************ *******
20 * 函数名 : Delay10us()
21 * 函数功能 : 延时10us
22 * 输入 : 无
23 * 输出 : 无
24 *******************************************************************************/
25 void Delay10us()//通过计算来确定延时为10uM
26 {
27 unsigned char< span style="color: #000000"> a,b;
28 for(b=1;b>0;b--)
29 for(a=2;a>0;a--);
30
31 }
32 /*******************************************************************************
33 * 函数名 : I2cStart()
34 * 函数功能 : 起始信号:在SC L时钟信号在高电平期间SDA信号产生一个下降沿
35 * 输入 : 无
36 * 输出 : 无
37 * 备注 : 起始之后SDA和SCL都为0
38 *******************************************************************************/
39
40 void I2cStart()
41 {
42 SDA=1;
43 Delay10us();
44 SCL=1;
45 Delay10us();//建立时间是SDA保持时间>4.7us
46 SDA=0;
47 Delay10us();//保持时间是>4us
48 SCL=0;
49 Delay10us();
50 }
51 /* ******************************************************************************
52 * 函数名 : I2cStop()
53 * 函数功能 : 终止信号:在SCL时钟信号高电平期间SDA信号产生一个上升沿
54 * 输入 : 无
55 * 输出 : 无
56 * 备注 : 结束之后保持SDA和SCL都为1;表示总线空闲
57 *******************************************************************************/
58
59 void I2cStop()
60 {
61 SDA=0;
62 Delay10us();
63 SCL=1;
64 Delay10us();//建立时间大于4.7us
65 SDA=1;
66 Delay10us();
67 }
68 //
针对IIC的写字节
69 uchar IICsendByte(uchar dat)
70 {
71 uchar a=0,b;
72 SDA =dat;
73 for(a=0;a<8;a++)
74 {
75 SDA =dat>>7; //将最高位取出,然后其他的为都通过右移掉
76 dat =dat<<1; // 因为dat的值一直没有变化,故而,将其左移一位,最高位移掉,次高位变成最高位
77 SDA =1;
78 delay(1);
79 SCL=0;
80 delay(1);
81 } //此时8位数据已经全部传送完成 接下来进行应答
82 SDA=1; //SDA和SCL为1是为了下面应答做铺垫
83 delay(1);
84 SCL=1;
85 // delay(1);
86 while(SDA) //当应答成功的话,SDA=0;跳出循环
87 {
88 b++;
89 if(b>200)
90 SCL=0;
91 dealy(1);
92 return 0; //表示发送(通讯)失败
93 }
94 SCL =0;
95 delay(1);
96 return 1; < span style="color: #008000">//发送成功的时候,返回1
97 }
98 //针对IIC的读字节
99 uchar IICReadByte()
100 {
101 uchar a,dat =0;
102 SD1=1; //首先先让其拉高,让其处于空闲状态,不至于让下面的SDA的数据为低
103 delay(1);
104 for(a=0;a<8;a++)
105 {
106 SCL =1; //为了读数据,故而首先让SCL为高,就是为了让数据稳定后再读
107 dealy(1);
108 dat<<=1; // 0 00 10 110 要将其放在dat|=SDA前面,否则当把8位数据全部放到dat中后, 再移一位的话,就会使得最高位移走,数据出错 将其左移一位,等二次循环过来时,这是就会原来保存在dat中的数据就会继续左移一次
109 dat|=SDA; // 01 11 111 也就是只需要让其移动7次,不将最高位移走 让dat与SDA进行“按位或”将SDA的值赋给dat
110 delay(1);
111 SCL=0; //让SCL为低,就是为了让数据改变
112 delay(1);
113 }
114 return dat;
115 }
116 //芯片AT24C02通讯的读写
117 void AT24C02writeByte(uchar add,uchar dat)
118 {
119 IICstart();
120 IICsendByte(0xa0);//这是器件地址,8位 高四位是固定的 最后一位表示0为写、1为读、其余的三位A1、A2、A3,我们接了地
121 IICsendByte(add);
122 IICsendByte(dat);//应答与否我们都在IIC的字节函数中谢了
123 IICStop();
124 }
125 uchar AT24C02read(uchar add)//由于是要读取信号,故而需要知道读取的地址即可
126 {
127 uchar value;
128 IICstart();
129 IICsendByte(0xa0);//这是器件地址,8位 高四位是固定的 最后一位表示0为写、1为读、其余的三位A1、A2、A3,我们接了地
130 IICsendByte(add);
131 IICstart(); //由于前面要写入它的地址,之后要读里面的数据,所以读写的方向会发生变化,所以必须要重新写入起始信号和器件地址
132
IICsendByte(0xa1);
133 value =IICReadByte();//直接读取地址中的数据,并且将读回来的值保存到value中去
134 IICstop();
135 return value;
136 }
137 //独立按键的函数
138 void keypros()
139 {
140< /span> if (K1==0)
141 {
142 delay(10);
143 if(K1==0)
144 {
145 AT24C02writeByte(1,num); //按键1实现将num的值,写入到 1 的地址中去,因为AT24C02这个芯片有255个内存
146 }
147 while(!K1);//判断按键是否释放 如果忘记写这个语句,这个函数的功能就会不停地进行
148 }
149 if (K2==0)
150 {
151 delay(10);
152 if(K2==0)
153 {
154
num =AT24C02read(1); //将存储到 1这个内存中的数据读取出来,再赋给变量num这个值
155 }
156 while(!K2);
157 }
158
159 if (K3==0)
160 {
161 delay(10);
162 if(K3==0)
163 {
164 num++; //对num的值进行加1运行
165 if(num>255) //因为num的变量为uchar,故而只能存储255个字符
166 num =0;
167 }
168 whi le(!K3);
169 }
170 if (K4==0)
171 {
172 delay(10);
173 if(K4==0)
174 {
175 num =0; //对num精心清零运算
176 }
177 while(!K4);
178 }
179 }
180 /*******************************************************************************
181 * 函数名 :DigDisplay()
182 * 函数功能 :数码管显示函数
183 * 输入 : 无
184 * 输出 : 无
185 *******************************************************************************/
186 void DigDisplay()
187 {
188 u8 i;
189 for(i=0;i<4;i++)
190 {
191 switch(i) //位选,选择点亮的数码管,
192 {
193 case(0):
194 LSA=0;LSB=0;LSC=0; break;//显示第0位
195 case(1):
196 LSA=1;LSB=0;LSC=0; break;//显示第1位
197 case(2):
198 LSA=0;LSB=1;LSC=0; break;//显示第2位
199 case(3):
200 LSA=1;LSB=1;LSC=0; break;//显示第3位
201 }
202 P0=disp[3-i];//发送数据
203 delay(100); //间隔一段时间扫描
204 P0=0x00;//消隐
205 }
206 }
207 /********* **********************************************************************
208 * 函数名 :datapros()
209 * 函数功能 :数据处理函数
210 * 输入 : 无
211 * 输出 : 无
212 *******************************************************************************/
213 void datapros()
214 {
215 disp[0]=s mgduan[num/1000];//千位
216 disp[1]=smgduan[num%1000/100];//百位
217 disp[2]=smgduan[num%1000%100/10];//个位
218 disp[3]=smgduan[num%1000%100%10];
219 }
220 void main()
221 {
222 while(1)
223 {
224 Keypros(); //按键处理函数
225 datapros(); //数据处理函数
226 DigDisplay();//数码管显示函数
227 }
228 }

View Code

  1 #include
2 #define uint unsigned int
3 #define uchar unsigned char
4
5 sbit SCL =P2^1;
6 sbit SDA =P2^0;
7
8 sbit K1 =P3^0;
9 sbi t K2 =P3^1;
10 sbit K3 =P3^0;
11 sbit K4 =P3^1;
12
13 sbit LSA =P2^2;
14 sbit LSB =P2^3;
15 sbit LSC =P2^4;
16 uchar num;
17 uchar code smgduan[] ={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
18
19 /*******************************************************************************
20 * 函数名 : Delay10us()
21 * 函数功能 : 延时10us
22 * 输入 : 无
23 * 输出 : 无
24 *******************************************************************************/
25 void Delay10us()//通过计算来确定延时为10uM
26 {
27 unsigned char a,b;
28 for(b=1;b>0;b--)
29 for(a=2;a>0;a--);
30
31 }
32 /*******************************************************************************
33 * 函数名 : I2cStart()
34 * 函数功能 : 起始信号:在SCL时钟信号在高电平期间SDA信号产生一个下降沿
35 * 输入 : 无
36 * 输出 : 无
37 * 备注 : 起始之后SDA和SCL都为0
38 *******************************************************************************/
39
40 void I2cStart()
41 {
42 SDA=1;
43 Delay10us();
44 SCL=1;
45 Delay10us();//建立时间是SDA保持时间>4.7us
46 SDA=0
;
47 Delay10us();//保持时间是>4us
48 SCL=0;
49 Delay10us();
50 }
51 /*******************************************************************************
52 * 函数名 : I2cStop()
53 * 函数功能 : 终止信号:在SCL时钟信号高电平期间SDA信号产生一个上升沿
54 * 输入 : 无
55 * 输出 : 无
56 * 备注 : 结束之后保持SDA和SCL都为1;表示总线空闲
57 *******************************************************************************/
58
59 void I2cStop()
60 {
61 SDA=0;
62 Delay10us();
63 SCL=1;
64 Delay10us();//建立时间大于4.7us
65 SDA=1;
66 Delay10us();
67 }
68 //针对IIC的写字节
69 uchar IICsendByte(uchar dat)
70 {
71 uchar a=0< span style="color: #000000">,b;
72 SDA =dat;
73 for(a=0;a<8;a++)
74 {
75 SDA =dat>>7; //将最高位取出,然后其他的为都通过右移掉
76 dat =dat<<1; // 因为dat的值一直没有变化,故而,将其左移一位,最高位移掉,次高位变成最高位
77 SDA =1;
78 delay(1);
79 SCL=0;
80 delay(1);
81 } //此时8位数据已经全部传送完成 接下来进行应答
82 SDA=1; //SDA和SCL为1是为了下面应答做铺垫
83 delay(1);
84 SCL=1;
85 // delay(1);
86 while(SDA) //当应答成功的话,SDA=0;跳出循环
87 {
88 b++;
89 if(b>200)
90 SCL=0;
91 dealy(1);
92 return 0; //表示发送(通讯)失败
93 }
94 SCL =0;
95 delay(1);
96 return 1; //发送成功的时候,返回1
97 }
98 //针对IIC的读字节
99 uchar IICReadByte()
100 {
101 uchar a,dat =0;
102 SD1=1; //首先先让其拉高,让其处于空闲状态,不至于让下面的SDA的数据为低
103 delay(1);
104 for(a=0;a<8;a++)
105 {
106 SCL =1; //为了读数据,故而首先让SCL为高,就是为了让数据稳定后再读
107 dealy(1);
108 dat<<=1; // 0 00 10 110 要将其放在dat|=SDA前面,否则当把8位数据全部放到dat中后, 再移一位的话,就会使得最高位移走,数据出错 将其左移一位,等二次循环过来时,这是就会原来保存在dat中的数据就会继续左移一次
109 dat|=SDA; // 01 11 111 也就是只需要让其移动7次,不将最高位移走 让dat与SDA进行“按位或”将SDA的值赋给dat
110 delay(1);
111 SCL=0; //让SCL为低,就是为了让数据改变
112 delay(1);
113 }
114 return dat;
115 }
116 //芯片AT24C02通讯的读写
117 void AT24C02writeByte(uchar add,uchar dat)
118 {
119 IICstart();
120 IICsendByte(0xa0);//这是器件地址,8位 高四位是固定的 最后一位表示0为写、1为读、其余的三位A1、A2、A3,我们接了地
121 IICsendByte(add);
122 IICsendByte(dat);//应答与否我们都在IIC的字节函数中谢了
123 IICStop();
124 }
125 uchar AT24C02read(uchar add)//由于是要读取信号,故而需要知道读取的地址即可
126 {
127 uchar value;
128 IICstart();
129 IICsendByte(0xa0);//这是器件地址,8位 高四位是固定的 最后一位表示0为写、1为读、其余的三位A1、A2、A3,我们接了地
130 IICsendByte(add);
131 IICstart(); //由于前面要写入它的地址,之后要读里面的数据,所以读写的方向会发生变化,所以必须要重新写入起始信号和器件地址
132 IICsendByte(0xa1);
133 value =IICReadByte();//直接读取地址中的数据,并且将读回来的值保存到value中去
134 IICstop();
135 return value;
136 }
137 //独立按键的函数
138 void keypros()
139 {
140 if (K1==0)
141 {
142 delay(10);
143 if(K1==0)
144 {
145 AT24C02writeByte(1,num); //按键1实现将num的值,写入到 1 的地址中去,因为AT24C02这个芯片有255个内存
146 }
147 while(!K1);//判断按键是否释放 如果忘记写这个语句,这个函数的功能就会不停地进行
148 }
149 if
(K2==0)
150 {
151 delay(10);
152 if(K2==0)
153 {
154 num =AT24C02read(1); //将存储到 1这个内存中的数据读取出来,再赋给变量num这个值
155 }
156 while(!K2);
157 }
158
159 if (K3==0)
160 {
161 delay(10);
162 if(K3==0)
163 {
164 num++; //对num的值进行加1运行
165 if(num>255) //因为num的变量为uchar,故而只能存储255个字符
166 num =0;
167 }
168 while(!K3);
169 }
170 if (K4==0)
171 {
172 delay(10);
173 if(K4==0)
174 {
175 num =0; //对num精心清零运算
176 }
177 while(!K4);
178 }
179 }
180 /*******************************************************************************
181 * 函数名 :DigDisplay()
182 * 函数功能 :数码管显示函数
183 * 输入 : 无
184 * 输出 : 无
185 *******************************************************************************/
186 void DigDisplay()187 {
188 u8 i;
189 for(i=0;i<4;i++)
190 {
191 switch(i) //位选,选择点亮的数码管,
192 {
193 case(0):
194 LSA=0;LSB=0;LSC=0; break;//显示第0位
195 case(1):
196 LSA=1;LSB=0;LSC=0; break;//显示第1位
197 case(2):
198 LSA=0;LSB=1;LSC=0; break;//显示第2位
199 case(3):
200 LSA=1;LSB=1;LSC=0; break;//显示第3位
201 }
202 P0=disp[3-i];//发送数据
203 delay(100); //间隔一段时间扫描
204 P0=0x00;//消隐
205 }
206 }
207 /*******************************************************************************
208 * 函数名 :datapros()
209 * 函数功能 :数据处理函数
210 * 输入 : 无
211 * 输出 : 无
212 *******************************************************************************/
213 void datapros()
214 {
215 disp[0]=smgduan[num/1000];//千位
216 disp[1]=smgduan[num%1000/100];//百位
217 disp[2]=smgduan[num%1000%100/10];//个位
218 disp[3]=smgduan[num%1000%100%10];
219 }
220 void main()
221 {
222 while(1)
223 {
224 Keypros(); //按键处理函数
225 datapros(); //数据处理函数
226 DigDisplay();//数码管显示函数
227 }
228 }

Leave a Comment

Your email address will not be published.