📄 func_shi.lst
字号:
C51 COMPILER V7.50 FUNC_SHI 01/14/2006 15:30:02 PAGE 1
C51 COMPILER V7.50, COMPILATION OF MODULE FUNC_SHI
OBJECT MODULE PLACED IN .\HEX\func_shi.obj
COMPILER INVOKED BY: F:\Keil\C51\BIN\C51.EXE func_shi.C LARGE BROWSE DEBUG OBJECTEXTEND PRINT(.\lst\func_shi.lst) OBJECT
-(.\HEX\func_shi.obj)
line level source
1
2 /*********************************************************************
3 SHI Communication Function Version 1.2 (Copyright(c) VXIS Inc. 2002)
4 v1.0
5 Data:2002.08.02
6 by P.J. Yang
7 v1.1
8 Data:2002.08.07
9 by P.J. Yang
10 v1.2
11 Data:2002.08.14
12 by P.J. Yang
13 Modify:1. Modify DelayXms Function's input mode. char => unsigned char
14 2. Change shi_sub_write Function's variable from "buf_data_temp"
15 to "buf_data_temp1".
16 v1.3
17 Data:2002.09.12
18 by P.J. Yang
19 Modify:1. Remove delay to speed SHI's SCL clock.
20 SHI Frequency = 53kHz
21 *********************************************************************/
22
23 #include <intrins.h>
24 #include "config.h"
25 #if _debug
26 #include <stdio.h>
27 #endif
28 #include <reg51.h>
29 #include "pindef.h"
30 #include "func_shi.h"
31
32 void delay_nop();
33 void stop_con();
34 void start_con();
35 void nack_con();
36 void ack_con();
37 bit send_con(char);
38 bit send_con1(char bytedata);
39 char receive_con();
40 char receive_con1();
41
42 char bytedata = 0; // 8 bits buffer for both send and receive
43 bit ack; // Ack = 0 => Acknowledge
44 // Ack = 1 => No Acknowledge
45
46 /*********************************************************************
47 Readdata Function
48
49 Input Factor:
50 device => 7 bits , Device number (LSB must be zero)
51 address => 8 bits , Reg address
52 num_data => 8 bits , (Number of received data)-1
53 buf_data => 8 bits , A index point to receive data buffer
54
C51 COMPILER V7.50 FUNC_SHI 01/14/2006 15:30:02 PAGE 2
55 Output Factor:
56 None
57
58 Motion:
59 1.Program will read "num_data" datum from the "address" of "device"
60 to the register of "buf_data" address.
61 *********************************************************************/
62 void shi_sub_read(char device,char address,char num_data,char *buf_data)
63 {
64 1 unsigned char data count = 0;
65 1 char *buf_data_temp;
66 1 bit EA_temp;
67 1 bit com = 0; // A flag to indicate whether the read motion is complete
68 1 // com = 0 => Not complete
69 1 // com = 1 => Complete
70 1 EA_temp=EA;
71 1 EA=0;
72 1 while (!com)
73 1 {
74 2 stop_con(); // Master send Stop command
75 2 delay_nop();
76 2 start_con(); // Master send Start command
77 2 ack = send_con(device++); // Send the device number and Set the R/W bit
78 2
79 2 if (!ack)
80 2 {
81 3 ack = send_con(address); // Send the address of reg
82 3 // if(!ack){printf("read ack addr is ok\n");}
83 3 if (!ack)
84 3 {
85 4 com = 1; // Motion is complete
86 4 }
87 3 }
88 2 }
89 1 // delay_nop();
90 1
91 1 // Ready for receive data
92 1 com = 0;
93 1 while (!com)
94 1 {
95 2 buf_data_temp = buf_data;
96 2 stop_con(); // Master send Stop command
97 2 delay_nop();
98 2 start_con(); // Master send Start command
99 2 ack = send_con(device); // Send the device number and Set the R/W bit
100 2
101 2 if (!ack)
102 2 {
103 3 // ack=send_con(address);
104 3 // if(!ack){printf("read ack addr is ok\n");} while(1);
105 3 for (count = 0;count <= num_data;count++)
106 3 {
107 4 *buf_data_temp = receive_con(); // Save received data to buf_data
108 4 buf_data_temp++; // Address index increase
109 4 if (count != num_data) // Check whether the data is the last
110 4 {
111 5 ack_con();
112 5 }
113 4 else
114 4 {
115 5 nack_con();
116 5 com = 0; //printf("com=0\n");
C51 COMPILER V7.50 FUNC_SHI 01/14/2006 15:30:02 PAGE 3
117 5 goto Com_Received;
118 5 }
119 4 }
120 3 }
121 2 }
122 1 Com_Received:
123 1 delay_nop();
124 1 stop_con();
125 1 delay_nop();
126 1
127 1 EA=EA_temp;
128 1
129 1 }
130
131 /*********************************************************************
132 Writedata Function
133
134 Input Factor:
135 device => 7 bits , Device number (LSB must be zero)
136 address => 8 bits , Reg address
137 num_data => 8 bits , (Number of transmitted data) - 1
138 buf_data => 8 bits , A index to point transmit data buffer
139
140 Output Factor:
141 None
142
143 Motion:
144 1.Program will write "num_data" datum from the register of "buf_data" address
145 to the "address" of "device".
146 *********************************************************************/
147 void shi_sub_write(char device,char address,unsigned char num_data,char *buf_data)
148 {
149 1 bit com = 0;
150 1 unsigned char data count = 0;
151 1 char *buf_data_temp1;
152 1 bit EA_temp;
153 1 EA_temp=EA;
154 1 EA=0;
155 1
156 1 while (!com)
157 1 {
158 2 buf_data_temp1 = buf_data;
159 2 stop_con(); // Master send Stop command
160 2 delay_nop();
161 2 start_con(); // Master send Start command
162 2 ack = send_con(device); // Send the device number and Set the R/W bit
163 2 //if(!ack){printf("write ack addr is ok\n");} while(1);
164 2 if (!ack)
165 2 {
166 3 ack = send_con(address); // Send the address of reg
167 3
168 3 //if(!ack){printf("write ack addr is ok\n");} while(1);
169 3
170 3 for (count=0;count <= num_data;count++)
171 3 {
172 4 if (!ack)
173 4 {
174 5 ack = send_con(*buf_data_temp1); // Send the data pointed the buf_data index
175 5 buf_data_temp1++; // Address index increase
176 5 if (count == num_data && !ack )
177 5 {
178 6 com = 1;
C51 COMPILER V7.50 FUNC_SHI 01/14/2006 15:30:02 PAGE 4
179 6 //printf("com=1 exec\n");
180 6 break;
181 6 }
182 5 }
183 4 else
184 4 {
185 5 break;
186 5 }
187 4 }
188 3 }
189 2 }
190 1 delay_nop();
191 1 stop_con(); // Master send Stop command
192 1 delay_nop();
193 1
194 1 EA=EA_temp;
195 1 }
196
197 /*********************************************************************
198 Receivebyte Function
199 *********************************************************************/
200 char receive_con()
201 {
202 1 char count = 0;
203 1
204 1 for (count=0;count<8;count++)
205 1 {
206 2 bytedata <<= 1;
207 2 scl = 1; // scl high
208 2 delay_nop();
209 2 if (sda)
210 2 {
211 3 bytedata |= 0x01;
212 3 }
213 2 scl = 0; // scl low
214 2 delay_nop();
215 2 }
216 1 return (bytedata);
217 1 }
218
219 /*********************************************************************
220 Sendbyte Function
221 *********************************************************************/
222 bit send_con(char bytedata)
223 {
224 1 char data count = 0;
225 1
226 1 scl = 0;
227 1 delay_nop();
228 1 for (count=0;count<8;count++)
229 1 {
230 2 //sda = 0; // sda low
231 2 if (bytedata & 0x80)
232 2 sda = 1; // sda high
233 2 else
234 2 sda = 0;
235 2 delay_nop();
236 2 bytedata <<= 1; // bytedata left shift
237 2
238 2 // Generate SCLK
239 2 scl = 1; // scl high
240 2 //_nop_ ();
C51 COMPILER V7.50 FUNC_SHI 01/14/2006 15:30:02 PAGE 5
241 2 delay_nop();
242 2 scl = 0; // scl low
243 2 delay_nop();
244 2 }
245 1 //delay_nop(); //CSWU2005.11.6
246 1
247 1 // Readback ack
248 1 sda = 1; // sda high //CSWU2005.11.15 1->0
249 1 delay_nop();
250 1 scl = 1; // scl high
251 1 delay_nop();
252 1 delay_nop();
253 1 delay_nop();
254 1 ack = sda; // Read ack
255 1 // Ack = 0 => Acknowledge
256 1 // Ack = 1 => No Acknowledge
257 1 delay_nop();
258 1 scl = 0; // scl low
259 1 delay_nop();
260 1 return (ack);
261 1 }
262
263 /*********************************************************************
264 Acknowledge Function
265 *********************************************************************/
266 void ack_con()
267 {
268 1 sda = 0; // sda low
269 1 delay_nop();
270 1 scl = 1; // scl high
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -