eeprom_ht1380.lst
来自「自己的平时一些制作 现在整理一下 与大家一起分享」· LST 代码 · 共 530 行 · 第 1/2 页
LST
530 行
C51 COMPILER V7.20 EEPROM_HT1380 06/19/2007 13:42:25 PAGE 1
C51 COMPILER V7.20, COMPILATION OF MODULE EEPROM_HT1380
OBJECT MODULE PLACED IN eeprom_ht1380.OBJ
COMPILER INVOKED BY: d:\Keil\C51\BIN\C51.EXE eeprom_ht1380.c BROWSE DEBUG OBJECTEXTEND
line level source
1 #pragma ot(5,SIZE)
2 #include <reg51.h>
3 #include <intrins.h>
4
5 /******定义蜂鸣器接口******/
6 sbit beep=P1^7;
7 /*****定义EEPROM接口及相关参数******/
8 #define ERRORCOUNT 10
9 sbit SDA=P3^3;
10 sbit SCL=P3^5;
11 enum eepromtype {M2401,M2402,M2404,M2408,M2416,M2432,M2464,M24128,M24256};
12 enum eepromtype EepromType; /* 器件类型枚举变量取值为0-8 */
13
14 /*****定义ht1380芯片接口********/
15 // ht1380 ht1380_sclk --->P1.0
16 // ht1380 I/O --->P1.1
17 // ht1380 ht1380_rest --->P1.2
18 sbit ht1380_sclk=P1^0;
19 sbit ht1380_data=P1^1;
20 sbit ht1380_rest=P1^2;
21 /**********定义两个全局变量*********/
22 //定义数组变量用于存放四个数码管的显示数据
23 //定义number存放动态显示是第几个数码管
24 unsigned char d[4],number=0;
25
26 /*********定义时间变量年月日时分秒***********/
27 unsigned char year,month,day,hour,minute,second;
28 /**********定义冒号变量 distime=1 是数码管冒号显示出来************/
29 bit distime=1;
30
31 /*******与数码管相关的两个表*********/
32 //一个显示译码表
33 //一个使能译码表
34 unsigned char code tab[10]={0x3f,6,0x5b,0x4f,0x66,0x6d,0x7d,7,0x7f,0x6f};
35 unsigned char code digit[4]={0xf8,0xf4,0xf2,0xf1};
36
37 /******延时程序***********/
38 void delay(unsigned char dy)
39 {
40 1 while(--dy);
41 1 }
42 /******* ht1380 子程序*****************/
43 //------------- sent_char to ht1380----------
44 //上升沿写入
45 void sent_char(unsigned char sentchar)
46 {
47 1 unsigned char ii;
48 1
49 1 for(ii=0;ii<8;ii++)
50 1 {
51 2 ht1380_sclk=0;
52 2 if ((sentchar & 1)!=0) ht1380_data=1;
53 2 else ht1380_data=0;
54 2 ht1380_sclk=1;
55 2 sentchar=sentchar>>1;
C51 COMPILER V7.20 EEPROM_HT1380 06/19/2007 13:42:25 PAGE 2
56 2 }
57 1 }
58 //------------- get_char from ht1380---------
59 unsigned char get_char()
60 {
61 1 unsigned char getchar,ii,temp;
62 1 getchar=0;temp=1;
63 1 for(ii=0;ii<8;ii++)
64 1 {
65 2 ht1380_sclk=0;
66 2 ht1380_data=1;
67 2 if(ht1380_data==1) getchar=getchar |temp;
68 2 ht1380_sclk=1;
69 2 temp=temp<<1;
70 2 }
71 1 return(getchar);
72 1 }
73
74 //-------------set_wp_off------------------
75 void set_wp_off()
76 {
77 1 ht1380_rest=1;
78 1 sent_char(0x8e); //10001110b
79 1 sent_char(0); //00000000b
80 1 ht1380_sclk=0;
81 1 ht1380_rest=0;
82 1 }
83 //-----------set_wp_on--------------------
84 void set_wp_on()
85 {
86 1 ht1380_rest=1;
87 1 sent_char(0x8e); //10001110b
88 1 sent_char(0x80); //10000000b
89 1 ht1380_sclk=0;
90 1 ht1380_rest=0;
91 1 }
92 //----------read_time --------------------
93 void read_time()
94 {
95 1 unsigned char temp;
96 1 ht1380_rest=1;
97 1 sent_char(0xbf); //10111111b
98 1 second=get_char();
99 1 minute=get_char();
100 1 hour= get_char();
101 1 day= get_char();
102 1 month= get_char();
103 1 temp= get_char(); /* week day */
104 1 year= get_char();
105 1 temp= get_char();
106 1 ht1380_sclk=0;
107 1 ht1380_rest=0;
108 1
109 1 }
110 //--------------set time-----------------
111 void set_time()
112 {
113 1 set_wp_off();
114 1 ht1380_rest=1;
115 1 sent_char(0xbe); //10111110b
116 1 sent_char(second);
117 1 sent_char(minute);
C51 COMPILER V7.20 EEPROM_HT1380 06/19/2007 13:42:25 PAGE 3
118 1 sent_char(hour);
119 1 sent_char(day);
120 1 sent_char(month);
121 1 sent_char(1); /* week day */
122 1 sent_char(year);
123 1 sent_char(0);
124 1 ht1380_sclk=0;
125 1 ht1380_rest=0;
126 1 set_wp_on();
127 1 }
128
129 /******EEPROM 24cxx**********/
130 /***********************************************************************************/
131 bit RW24XX(unsigned char *DataBuff,unsigned char ByteQuantity,unsigned int Address,
132 unsigned char ControlByte,enum eepromtype EepromType)
133 {
134 1 void Delay(unsigned char DelayCount);
135 1 void IICStart(void);
136 1 void IICStop(void);
137 1 bit IICRecAck(void);
138 1 void IICNoAck(void);
139 1 void IICAck(void);
140 1 unsigned char IICReceiveByte(void);
141 1 void IICSendByte(unsigned char sendbyte);
142 1 unsigned char data j,i=ERRORCOUNT;
143 1 bit errorflag=1; /* clr errorflag */
144 1 while(i--) /* 启动IIC总线并发送EEPROM从器件地址 */
145 1 {
146 2 IICStart(); /* 启动IIC总线 */
147 2 IICSendByte(ControlByte&0xfe); /* 发送EEPROM地址 */
148 2 if(IICRecAck()) /* 接收EEPROM应答信号 */
149 2 continue; /* 接收EEPROM应答信号,为1重新启动 */
150 2 if(EepromType>M2416) /* EEPROM为24C32以上器件内地址为2字节 */
151 2 {
152 3 IICSendByte((unsigned char)(Address>>8));/* 先发送高字节,参考硬件相关说明 */
153 3 if(IICRecAck())
154 3 continue;
155 3 }
156 2 IICSendByte((unsigned char)Address);/* 再发送低字节或单字节地址 */
157 2 if(IICRecAck())
158 2 continue;
159 2 if(!(ControlByte&0x01)) /* 以下为写操作 */
160 2 {
161 3 j=ByteQuantity; /* ByteQuantity为写入字节数 */
162 3 errorflag=0;
163 3 while(j--)
164 3 {
165 4 IICSendByte(*DataBuff++); /* DataBuff写操作缓冲区 */
166 4 if(!IICRecAck())
167 4 continue;
168 4 errorflag=1;
169 4 break;
170 4 }
171 3 if(errorflag==1)
172 3 continue;
173 3 break;
174 3 }
175 2 else /* 以下为读操作 */
176 2 {
177 3 IICStart(); /* 重新启动IIC总线并发送EEPROM从器件地址 */
178 3 IICSendByte(ControlByte);
179 3 if(IICRecAck())
C51 COMPILER V7.20 EEPROM_HT1380 06/19/2007 13:42:25 PAGE 4
180 3 continue;
181 3 while(--ByteQuantity) /* 每次读一字节,共ByteQuantity字节 */
182 3 {
183 4 *DataBuff++=IICReceiveByte(); /*读1字节并存入DataBuff为指针的存储单元 */
184 4 IICAck(); /* 发送应答信号 */
185 4 }
186 3 *DataBuff=IICReceiveByte(); /*read last byte data*/
187 3 IICNoAck(); /* 读入最后一字节无须发送应答信号 */
188 3 errorflag=0;
189 3 break;
190 3 }
191 2 }
192 1 IICStop(); /* 停止IIC总线信号 */
193 1 if(!(ControlByte&0x01)) /* 写入操作需延时,确保足够长写入时间 */
194 1 {
195 2 Delay(255);
196 2 Delay(255);
197 2 Delay(255);
198 2 Delay(255);
199 2 }
200 1 return(errorflag);
201 1 }
202
203 /*******************
204 以下是对IIC操作子程序
205 *******************/
206 /*******************
207 启动IIC
208 *******************/
209 void IICStart(void)
210 {
211 1 SCL=0;
212 1 SDA=1;
213 1 SCL=1;
214 1 _nop_();
215 1 _nop_();
216 1 _nop_();
217 1 SDA=0;
218 1 _nop_();
219 1 _nop_();
220 1 _nop_();
221 1 _nop_();
222 1 SCL=0;
223 1 SDA=1;
224 1 }
225 /*****************
226 停止IIC总线
227 *****************/
228 void IICStop(void)
229 {
230 1 SCL=0;
231 1 SDA=0;
232 1 SCL=1;
233 1 _nop_();
234 1 _nop_();
235 1 _nop_();
236 1 SDA=1;
237 1 _nop_();
238 1 _nop_();
239 1 _nop_();
240 1 SCL=0;
241 1 }
C51 COMPILER V7.20 EEPROM_HT1380 06/19/2007 13:42:25 PAGE 5
242 /**************
243 检查应答位
244 **************/
245 bit IICRecAck(void)
246 {
247 1 SCL=0;
248 1 SDA=1;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?