访问手机版页面
你的位置:老古开发网 > 51单片机 > 51单片机Keil C51编程 > 正文  
IIC总线模拟程序
内容导读:
文件: PCF8563T.C
1 /**————————————————————
2 〖说明〗I2C总线驱动程序(用两个普通IO模拟I2C总线)
3 包括100Khz(T=10us)的标准模式(慢速模式)选择,
4 和400Khz(T=2.5us)的快速模式选择,
5 默认11.0592Mhz的晶振。

10 —————————————————————*/
11
12 #ifndef SDA
13 #define SDA P0_0
14 #define SCL P0_1
15 #endif
16
17 extern uchar SystemError;
18
19 #define uchar unsigned char
20 #define uint unsigned int
21 #define Byte unsigned char
22 #define Word unsigned int
23 #define bool bit
24 #define true 1
25 #define false 0
26
27 #define SomeNOP(); _nop_();_nop_();_nop_();_nop_();
28
29 /**--------------------------------------------------------------------------------
30 调用方式:void I2CStart(void) ﹫2001/07/0 4
31 函数说明:私有函数,I2C专用
32 ---------------------------------------------------------------------------------*/
33 void I2CStart(void)
34 {
35 EA=0;
36 SDA=1; SCL=1; SomeNOP();//INI
37 SDA=0; SomeNOP(); //START
38 SCL=0;
39 }
40
41 /**--------------------------------------------------------------------------------
42 调用方式:void I2CStop(void) ﹫2001/07/0 4
43 函数说明:私有函数,I2C专用
44 ---------------------------------------------------------------------------------*/
45 void I2CStop(void)
46 {
47 SCL=0; SDA=0; SomeNOP(); //INI
48 SCL=1; SomeNOP(); SDA=1; //STOP
49 EA=1;
50 }
51
52 /**--------------------------------------------------------------------------------
53 调用方式:bit I2CAck(void) ﹫2001/07/0 4
54 函数说明:私有函数,I2C专用,等待从器件接收方的应答
55 ---------------------------------------------------------------------------------*/
56 bool WaitAck(void)
57 {
58 uchar errtime=255;//因故障接收方无ACK,超时值为255。
59 SDA=1;SomeNOP();
60 SCL=1;SomeNOP();
61 while(SDA) {errtime--; if (!errtime) {I2CStop();SystemError=0x11;return false;}}
62 SCL=0;
63 return true;
第1 页
文件: PCF8563T.C 2001-11-27, 18:39:20
64 }
65
66 /**--------------------------------------------------------------------------------
67 调用方式:void SendAck(void) ﹫2001/07/0 4
68 函数说明:私有函数,I2C专用,主器件为接收方,从器件为发送方时,应答信号。
69 ---------------------------------------------------------------------------------*/
70 void SendAck(void)
71 {
72 SDA=0; SomeNOP();
73 SCL=1; SomeNOP();
74 SCL=0;
75 }
76
77 /**--------------------------------------------------------------------------------
78 调用方式:void SendAck(void) ﹫2001/07/0 4
79 函数说明:私有函数,I2C专用,主器件为接收方,从器件为发送方时,非应答信号。
80 }**--------------------------------------------------------------------------------
81 void SendNotAck(void)
82 {
83 SDA=1; SomeNOP();
84 SCL=1; SomeNOP();
85 SCL=0;
86 }
87
88 /**--------------------------------------------------------------------------------
89 调用方式:void I2CSend(uchar ch) ﹫2001/07/0 5
90 函数说明:私有函数,I2C专用
91 ---------------------------------------------------------------------------------*/
92 void I2CSendByte(Byte ch)
93 {
94 uchar i=8;
95 while (i--)
96 {
97 SCL=0;_nop_();
98 SDA=(bit)(ch&0x80); ch<<=1; SomeNOP();
99 SCL=1; SomeNOP();
100 }
101 SCL=0;
102 }
103
104 /**--------------------------------------------------------------------------------
105 调用方式:uchar I2CReceive(void) ﹫2001/07/0 5
106 函数说明:私有函数,I2C专用
107 ---------------------------------------------------------------------------------*/
108 Byte I2CReceiveByte(void)
109 {
110 uchar i=8;
111 Byte ddata=0;
112 SDA=1;
113 while (i--)
114 {
115 ddata<<=1;
116 SCL=0;SomeNOP();
117 SCL=1;SomeNOP();
118 ddata|=SDA;
119 }
120 SCL=0;
121 return ddata;
122 }
123
124
125 //---------------------------------------------------------------------------
126 //开始PCF8563T驱动程序
第2 页
文件: PCF8563T.C 2001-11-27, 18:39:20
127 /**--------------------------------------------------------------------------------
128 调用方式:void GetPCF8563(uchar firsttype,uchar count,uchar *buff) ﹫2001/08/0 7
129 函数说明:读取时钟芯片PCF8563的时间,设置要读的第一个时间类型firsttype,并设置读取
130 的字节数,则会一次把时间读取到buff中。顺序是:
131 0x02:秒/0x03:分/0x04:小时/0x05:日/0x06:星期/0x07:月(世纪)/0x08:年
132 ---------------------------------------------------------------------------------*/
133 void GetPCF8563(uchar firsttype,uchar count,uchar *buff)
134 {
135 uchar i;
136 I2CStart();
137 I2CSendByte(0xA2);
138 WaitAck();
139 I2CSendByte(firsttype);
140 WaitAck();
141
142 I2CStart();
143 I2CSendByte(0xA3);
144 WaitAck();
145
146 for (i=0;i 147 {
148 buff[i]=I2CReceiveByte();
149 if (i!=count-1) SendAck();//除最后一个字节外,其他都要从MASTER发应答。
150 }
151
152 SendNotAck();
153 I2CStop();
154 }
155
156
157 /**--------------------------------------------------------------------------------
158 调用方式:void SetPCF8563(uchar timetype,uchar value) ﹫2001/08/0 7
159 函数说明:调整时钟。timetype是要改的时间类型,value是新设置的时间值(BCD格式)。
160 0x02:秒/0x03:分/0x04:小时/0x05:日/0x06:星期/0x07:月(世纪)/0x08:年
161 ---------------------------------------------------------------------------------*/
162 void SetPCF8563(uchar timetype,uchar value)
163 {
164 I2CStart();
165 I2CSendByte(0xA2);
166 WaitAck();
167 I2CSendByte(timetype);
168 WaitAck();
169 I2CSendByte(value);
170 WaitAck();
171 I2CStop();
172 }
173
174 /**--------------------------------------------------------------------------------
175 调用方式:void SetAlarmHour(uchar count) ﹫2001/08/0 7
176 函数说明:设置报警闹钟在一天的第count点报警。例如:count=23,则在晚上11点报警。
177 ---------------------------------------------------------------------------------*/
178 void SetAlarm(uchar alarmtype,uchar count)
179 {
180 SetPCF8563(0x01,0x02);
181 SetPCF8563(alarmtype,count);
182 }
183
184 /**--------------------------------------------------------------------------------
185 调用方式:void CleanAlarm(void) ﹫2001/08/0 7
186 函数说明:清除所有报警设置。
187 ---------------------------------------------------------------------------------*/
188 void CleanAlarm(void)
189 {
第3 页
文件: PCF8563T.C 2001-11-27, 18:39:20
190 SetPCF8563(0x01,0x00);
191 SetPCF8563(0x09,0x80);
192 SetPCF8563(0x0A,0x80);
193 SetPCF8563(0x0B,0x80);
194 SetPCF8563(0x0C,0x80);
195 // SetPCF8563(0x0D,0x00);
196 // SetPCF8563(0x0E,0x03);
197 }
198
199
200 /*--------------------------------------------------------------------------------
201 调用方式:uchar read1380(uchar command )
202 函数说明:read1380()返回当前时间, command指要返回的时间类型。
203 秒:81H 分钟:83H 小时:85H 日期:87H 星期:89H 星期几:8BH 年:8D H
204 ---------------------------------------------------------------------------------*/
205 uchar read1380 (uchar command)
206 {
207 uchar time;
208 GetPCF8563(command,1,&time);
209 return time;
210 }
211
212 /*--------------------------------------------------------------------------------
213 调用方式:void write1380(uchar command ,uchar time )
214 函数说明:write1380()往HT1380写命令和数据,command是命令字, time是后写入的数据
215 ---------------------------------------------------------------------------------*/
216 void write1380(uchar command ,uchar time)
217 {
218 SetPCF8563(command,time);
219 }
220
221
222 /*--------------------------------------------------------------------------------
223 调用方式:void time_display(uchar x0,uchar y0 )
224 函数说明:time_display()在指定的x0,y0坐标,以00:00:00格式显示当前时间。
225 ---------------------------------------------------------------------------------*/
226 //uchar time[]="00:11:11";
227
228 void time_display(uchar x0,uchar y0,bit type) //液晶时间显示
229 {
230 uchar time[]="00:00:00";
231 uchar con[3];
232 uchar time_type;
233 GetPCF8563(0x02,3,con);
234
235 time[0]=(con[2]>>4)+'0';
236 time[1]=(con[2]&0x0f)+'0';
237 time[3]=(con[1]>>4)+'0';
238 time[4]=(con[1]&0x0f)+'0';
239 time[6]=(con[0]>>4)+'0';
240 time[7]=(con[0]&0x0f)+'0';
241
242 time[8]=0;
243 if(type==1)
244 {
245 time_type=0xff;
246 }
247 else
248 {
249 time_type=0;
250 }
251 dipchar0(x0,y0,F57,1,time_type,time);
252 }
标签:
来源: 作者: 时间:2005/2/19 2:32:54
相关阅读
推荐阅读
阅读排行
最近更新
商品推荐