📄 0.lst
字号:
C51 COMPILER V8.02 0 07/12/2010 12:11:04 PAGE 1
C51 COMPILER V8.02, COMPILATION OF MODULE 0
OBJECT MODULE PLACED IN 0.OBJ
COMPILER INVOKED BY: D:\处理软件\keil\C51\BIN\C51.EXE 0.c BROWSE DEBUG OBJECTEXTEND
line level source
1 /***************************************************************
2 “天祥电子” 倾情奉献
3 www.txmcu.com
4 www.txmcu.cn
5 *****************************************************************
6 程序功能: 向IIC总线器件24c02EEPROM中地址为0的存储单元中写入数据0xaa
7 并将其读出并送入P1口,可观察到P1口相连的LED交替被点亮。
8 主要为了练习IIC总线的驱动程序以及24C02的操作方法。
9 ***************************************************************/
10 //试验AT24C02EEPROM芯片程序
11 #include<reg51.h>
12 #include <intrins.h>
13 #define uint unsigned int
14 #define uchar unsigned char
15 sbit sda=P2^0; //IO口定义
16 sbit scl=P2^1;
17
18 //此为待写入24c02的数据。为了便于验证结果,数组的内容为周期重复的。
19 char code music[] = {
20 0x55,0xaa,0x0f,0xf0,0x55,0xaa,0x0f,0xf0,0x55,0xaa,0x0f,0xf0,
21 0x55,0xaa,0x0f,0xf0,0x55,0xaa,0x0f,0xf0,0x55,0xaa,0x0f,0xf0,
22 0x55,0xaa,0x0f,0xf0,0x55,0xaa,0x0f,0xf0,0x55,0xaa,0x0f,0xf0,
23 0x55,0xaa,0x0f,0xf0,0x55,0xaa,0x0f,0xf0,0x55,0xaa,0x0f,0xf0,
24 0x55,0xaa,0x0f,0xf0,0x55,0xaa,0x0f,0xf0,0x55,0xaa,0x0f,0xf0
25 }; //由于最后还要讲这些数据读出送到P1口,故可发现P1口相连的led有规律地闪烁15个周期
26
27 uchar data buffer[100]; //用于缓存从24c02中读取的数据。
28
29 delay(unsigned int m)
30 {
31 1 unsigned int n,p;
32 1 for(n=m;n>0;n--)
33 1 for(p=125;p>0;p--);
34 1 }
35 void nop()
36 {
37 1 _nop_();
38 1 _nop_();
39 1 }
40 /////////24C02读写驱动程序////////////////////
41 void delay1(unsigned int m)
42 { unsigned int n;
43 1 for(n=0;n<m;n++);
44 1 }
45
46 void init() //24c02初始化子程序
47 {
48 1 scl=1;
49 1 nop();
50 1 sda=1;
51 1 nop();
52 1 }
53
54 void start() //启动I2C总线
55 {
C51 COMPILER V8.02 0 07/12/2010 12:11:04 PAGE 2
56 1 sda=1;
57 1 nop();
58 1 scl=1;
59 1 nop();
60 1 sda=0;
61 1 nop();
62 1 scl=0;
63 1 nop();
64 1 }
65
66 void stop() //停止I2C总线
67 {
68 1 sda=0;
69 1 nop();
70 1 scl=1;
71 1 nop();
72 1 sda=1;
73 1 nop();
74 1 }
75
76 void writebyte(unsigned char j) //写一个字节
77 {
78 1 unsigned char i,temp;
79 1 temp=j;
80 1 for (i=0;i<8;i++)
81 1 {
82 2 temp=temp<<1;
83 2 scl=0;
84 2 nop();
85 2 sda=CY; //temp左移时,移出的值放入了CY中
86 2 nop();
87 2 scl=1; //待sda线上的数据稳定后,将scl拉高
88 2 nop();
89 2 }
90 1 scl=0;
91 1 nop();
92 1 sda=1;
93 1 nop();
94 1 }
95
96 unsigned char readbyte() //读一个字节
97 {
98 1 unsigned char i,j,k=0;
99 1 scl=0; nop(); sda=1;
100 1 for (i=0;i<8;i++)
101 1 {
102 2 nop(); scl=1; nop();
103 2 if(sda==1)
104 2 j=1;
105 2 else
106 2 j=0;
107 2 k=(k<<1)|j;
108 2 scl=0;
109 2 }
110 1 nop();
111 1 return(k);
112 1 }
113
114 void clock() //I2C总线时钟
115 {
116 1 unsigned char i=0;
117 1 scl=1;
C51 COMPILER V8.02 0 07/12/2010 12:11:04 PAGE 3
118 1 nop();
119 1 while((sda==1)&&(i<255))
120 1 i++;
121 1 scl=0;
122 1 nop();
123 1 }
124
125 ////////从24c02的地址address中读取一个字节数据/////
126 unsigned char read24c02(unsigned char address)
127 {
128 1 unsigned char i;
129 1 start();
130 1 writebyte(0xa0);
131 1 clock();
132 1 writebyte(address);
133 1 clock();
134 1 start();
135 1 writebyte(0xa1);
136 1 clock();
137 1 i=readbyte();
138 1 stop();
139 1 delay1(100);
140 1 return(i);
141 1 }
142
143 //////向24c02的address地址中写入一字节数据info/////
144 void write24c02(unsigned char address,unsigned char info)
145 {
146 1 start();
147 1 writebyte(0xa0);
148 1 clock();
149 1 writebyte(address);
150 1 clock();
151 1 writebyte(info);
152 1 clock();
153 1 stop();
154 1 delay1(5000); //这个延时一定要足够长,否则会出错。因为24c02在从sda上取得数据后,还需要一定时间的烧录过
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -