📄 hal.lst
字号:
C51 COMPILER V7.06 HAL 03/02/2009 16:29:21 PAGE 1
C51 COMPILER V7.06, COMPILATION OF MODULE HAL
OBJECT MODULE PLACED IN HAL.OBJ
COMPILER INVOKED BY: D:\Program Files\Keil\C51\BIN\C51.EXE HAL.C BROWSE DEBUG OBJECTEXTEND
stmt level source
1
2 #include "HAL.H"
3
4 //============================================================
5 //往SD卡定一字节
6 void SdWrite(unsigned char n)
7 {
8 1 unsigned char i;
9 1 for(i=8;i;i--)
10 1 {
11 2 SD_CLK=0;
12 2 SD_DI=(n&0x80);
13 2 n<<=1;
14 2 SD_CLK=1;
15 2 }
16 1 SD_DI=1;
17 1 }
18 //================================================================
19 //从SD卡读一字节
20 unsigned char SdRead()
21 {
22 1 unsigned char n,i;
23 1 for(i=8;i;i--)
24 1 {
25 2 SD_CLK=0;
26 2 SD_CLK=1;
27 2 n<<=1;
28 2 if(SD_DO) n|=1;
29 2
30 2 }
31 1 return n;
32 1 }
33 //================================================================
34 //读SD卡响应
35 unsigned char SdResponse()
36 {
37 1 unsigned char i=0,response;
38 1 while(i<=8)
39 1 {
40 2 response=SdRead();
41 2 if(response==0x00)
42 2 break;
43 2 if(response==0x01)
44 2 break;
45 2 i++;
46 2 }
47 1 return response;
48 1 }
49 //================================================================
50 void SdCommand(unsigned char command, unsigned long argument, unsigned char CRC)
51 {
52 1 SdWrite(command|0x40);
53 1 SdWrite(((unsigned char *)&argument)[0]);
54 1 SdWrite(((unsigned char *)&argument)[1]);
55 1 SdWrite(((unsigned char *)&argument)[2]);
C51 COMPILER V7.06 HAL 03/02/2009 16:29:21 PAGE 2
56 1 SdWrite(((unsigned char *)&argument)[3]);
57 1 SdWrite(CRC);
58 1 }
59 //================================================================
60 unsigned char SdInit(void)
61 {
62 1 int delay=0, trials=0;
63 1 unsigned char i;
64 1 unsigned char response=0x01;
65 1 SD_CS=1;
66 1 for(i=0;i<=9;i++)
67 1 SdWrite(0xff);
68 1 SD_CS=0;
69 1
70 1 //Send Command 0 to put MMC in SPI mode
71 1 SdCommand(0x00,0,0x95);
72 1
73 1
74 1 response=SdResponse();
75 1
76 1 if(response!=0x01)
77 1 {
78 2 return 0;
79 2 }
80 1
81 1 while(response==0x01)
82 1 {
83 2 SD_CS=1;
84 2 SdWrite(0xff);
85 2 SD_CS=0;
86 2 SdCommand(0x01,0x00ffc000,0xff);
87 2 response=SdResponse();
88 2 }
89 1
90 1 SD_CS=1;
91 1 SdWrite(0xff);
92 1 return 1;
93 1 }
94 //================================================================
95 unsigned char SdWriteBlock(unsigned char *Block, unsigned long address)
96 {
97 1 unsigned char count;
98 1 unsigned char dataResp;
99 1 //Block size is 512 bytes exactly
100 1 //First Lower SS
101 1
102 1 SD_CS=0;
103 1 //Then send write command
104 1 SdCommand(0x18,address,0xff);
105 1
106 1 if(SdResponse()==00)
107 1 {
108 2 SdWrite(0xff);
109 2 SdWrite(0xff);
110 2 SdWrite(0xff);
111 2 //command was a success - now send data
112 2 //start with DATA TOKEN = 0xFE
113 2 SdWrite(0xfe);
114 2 //now send data
115 2 for(count=0;count<128;count++)
116 2 {
117 3 SdWrite(*Block++);
C51 COMPILER V7.06 HAL 03/02/2009 16:29:21 PAGE 3
118 3 SdWrite(*Block++);
119 3 SdWrite(*Block++);
120 3 SdWrite(*Block++);
121 3 }
122 2 //data block sent - now send checksum
123 2 SdWrite(0xff);
124 2 SdWrite(0xff);
125 2 //Now read in the DATA RESPONSE token
126 2 dataResp=SdRead();
127 2 //Following the DATA RESPONSE token
128 2 //are a number of BUSY bytes
129 2 //a zero byte indicates the MMC is busy
130 2
131 2 while(SdRead()==0);
132 2
133 2 dataResp=dataResp&0x0f; //mask the high byte of the DATA RESPONSE token
134 2 SD_CS=1;
135 2 SdWrite(0xff);
136 2 if(dataResp==0x0b)
137 2 {
138 3 //DATA WAS NOT ACCEPTED BY CARD -- CRC ERROR
139 3 return 0;
140 3 }
141 2 if(dataResp==0x05)
142 2 return 1;
143 2
144 2 //Invalid data Response token.
145 2 return 0;
146 2 }
147 1 //Command 0x18 (Write) was not received by the MMC
148 1 return 0;
149 1 }
150 //=======================================================================
151 unsigned char SdReadBlock(unsigned char *Block, unsigned long address)
152 {
153 1 unsigned char count;
154 1 //Block size is 512 bytes exactly
155 1 //First Lower SS
156 1
157 1 // MMC_read_block
158 1
159 1 SD_CS=0;
160 1
161 1 //SdWrite(0xff);
162 1 //Then send write command
163 1 SdCommand(0x11,address,0xff);
164 1
165 1 if(SdResponse()==00)
166 1 {
167 2 //command was a success - now send data
168 2 //start with DATA TOKEN = 0xFE
169 2 while(SdRead()!=0xfe);
170 2
171 2 for(count=0;count<128;count++)
172 2 {
173 3 *Block++=SdRead();
174 3 *Block++=SdRead();
175 3 *Block++=SdRead();
176 3 *Block++=SdRead();
177 3 }
178 2 //data block sent - now send checksum
179 2 SdRead();
C51 COMPILER V7.06 HAL 03/02/2009 16:29:21 PAGE 4
180 2 SdRead();
181 2 //Now read in the DATA RESPONSE token
182 2 SD_CS=1;
183 2 SdRead();
184 2 return 1;
185 2 }
186 1 //Command 0x11 (Read) was not received by the MMC
187 1 return 0;
188 1 }
189
190
191 unsigned char SdReadSector(unsigned long sector,unsigned char len,unsigned char *pBuffer)
192 {
193 1 while(len--)
194 1 {
195 2 if(SdReadBlock(pBuffer,sector<<9)==0)
196 2 return 0;
197 2 pBuffer+=512;
198 2 }
199 1 return 1;
200 1 }
201
202 unsigned char SdWriteSector(unsigned long sector,unsigned char len,unsigned char *pBuffer)
203 {
204 1 while(len--)
205 1 {
206 2 if(SdWriteBlock(pBuffer,sector<<9)==0) return 0;
207 2 pBuffer+=512;
208 2 }
209 1 return 1;
210 1 }
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 632 ----
CONSTANT SIZE = ---- ----
XDATA SIZE = ---- ----
PDATA SIZE = ---- ----
DATA SIZE = ---- 40
IDATA SIZE = ---- ----
BIT SIZE = ---- ----
END OF MODULE INFORMATION.
C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -