?? 12.lst
字號:
C51 COMPILER V8.02 12 10/10/2008 16:00:14 PAGE 1
C51 COMPILER V8.02, COMPILATION OF MODULE 12
OBJECT MODULE PLACED IN 12.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE 12.c COMPACT BROWSE DEBUG OBJECTEXTEND CODE
line level source
1 /**********************************************
2 顯示和鍵盤都已經正常,鍵盤采用的是中斷方式
3
4 2008.7.2
5
6 **************************************/
7 #include <P89V51.h>
8 #include <absacc.h>
9 #include <intrins.h>
10 #include <stdlib.h>
11 #include <stdio.h>
12 #define uchar unsigned char
13 //SYSTEM ADDRESS ALLOCATION ******************
14 #define KEYCOLUM XBYTE[0xEFFF]
15 #define KEYROW XBYTE[0xDFFF]
16 #define ALARM XBYTE[0xCFFF]
17 #define HC245C1 XBYTE[0xBFFF]
18 #define HC245C2 XBYTE[0xAFFF]
19 #define HC573C1 XBYTE[0x9FFF]
20 #define HC573C2 XBYTE[0x8FFF]
21 #define HC573C3 XBYTE[0x7FFF]
22 #define ADCS XBYTE[0x6FFF]
23 #define DACS XBYTE[0x5FFF]
24 #define DACLOAD XBYTE[0x4FFF]
25 //***************************************
26
27 //static unsigned char keyin=0;
28
29
30 /*-------------LCD1602 definitions and functions--------------*/
31 #define nops() {_nop_();_nop_();_nop_();}
32 typedef bit BOOL;
33
34 #define FALSE (bit)0
35 #define TRUE (bit)1
36
37 sbit lcd_cs = P2^0; //控制位
38 sbit lcd_rw = P2^1;
39 sbit lcd_rs = P2^2;
40 #define DataPort P0
41 #define DataDirPort P0
42 #define StatePort P0
43
44 #define Busy 0x80 // 忙判別位
45 uchar keystate,keyin;
46 unsigned char ucKeyState,flag,c_0,c_1,c_2;
47 unsigned int n=0,b_out,tick=0,second=0, minute=0;
48 unsigned char mai[8]={0x11,0x33,0x22,0x66,0x44,0xcc,0x88,0x99};
49
50 void delay(long t)
51 {
52 1 while(t)
53 1 t--;
54 1 }
55
C51 COMPILER V8.02 12 10/10/2008 16:00:14 PAGE 2
56 code unsigned char* WrtieLcdErr = "Display char outof range.";
57
58 // ------ Private function prototypes --------------------------
59 static BOOL WaitTillNotBusy();
60 static BOOL LocateXY(unsigned char posx, unsigned char posy);
61 static BOOL LcdWData(unsigned char dataW);
62 static BOOL LcdWCMD(unsigned char CMD);
63 BOOL LCDInit() ;
64 BOOL DisplayOneChar(unsigned char x ,unsigned char y ,unsigned char Wdata) ;
65 BOOL DisplayString(unsigned char posx, unsigned char posy, unsigned char* str);
66 unsigned char keyscan(void);
67 unsigned char Gatkey(char keystate);
68
69 BOOL WaitTillNotBusy()
70 {
71 1 unsigned int delay = 0xffff;
72 1
73 1 DataDirPort = 0xff;
74 1
75 1 lcd_cs = 1;
76 1 lcd_rs = 0;
77 1 lcd_rw = 1;
78 1 while( (StatePort & Busy) && delay)
79 1 delay--;
80 1 lcd_cs = 0;
81 1 if (!delay)
82 1 return FALSE ;
83 1
84 1 return TRUE;
85 1 }
86
87 BOOL LocateXY( unsigned char posx,unsigned char posy)
88 {
89 1 posy &= 0x01;
90 1 if (posy == 1)
91 1 posx |= 0x40;
92 1 posx |= 0x80;
93 1
94 1 return LcdWCMD(posx);
95 1 }
96
97
98 BOOL LcdWCMD(unsigned char CMD)
99 {
100 1 unsigned char i=1;
101 1
102 1 if (!WaitTillNotBusy()) // 檢測忙信號?
103 1 return FALSE;
104 1
105 1 DataPort = CMD;
106 1 lcd_rs = 0;
107 1 lcd_rw = 0;
108 1 lcd_cs = 1;
109 1 while(i++);
110 1 lcd_cs = 0;
111 1
112 1 return TRUE;
113 1 }
114
115
116 BOOL LcdWData( unsigned char dataW )
117 {
C51 COMPILER V8.02 12 10/10/2008 16:00:14 PAGE 3
118 1
119 1 unsigned char i = 1;
120 1
121 1 if (!WaitTillNotBusy()) // 檢測忙信號?
122 1 return FALSE;
123 1
124 1
125 1 DataPort = dataW;
126 1 lcd_rs = 1;
127 1 lcd_rw = 0;
128 1 lcd_cs = 1;
129 1 while(i++);
130 1 lcd_cs = 0;
131 1
132 1 return TRUE;
133 1 }
134
135 // LCD輸出用函數
136 //LCDInit--LCD初始化
137 //DisplayOneChar(x,y,char) --在指定x,y顯示一個字符
138 //DisplayString(x,y,str)--在指定位置x,y開始顯示一個字符串
139
140 BOOL LCDInit()
141 {
142 1
143 1 // 顯示模式設置,光標移動設置,顯示開及光標設置,顯示清屏
144 1 return (LcdWCMD( 0x38) && LcdWCMD( 0x06) && LcdWCMD( 0x0c) && LcdWCMD( 0x01));
145 1
146 1 }
147
148 BOOL DisplayOneChar(unsigned char x ,unsigned char y ,unsigned char Wdata)
149 {
150 1
151 1 if( (x >= 0 && x < 16) && (y >= 0 && y < 2) )
152 1 return (LocateXY( x, y ) && LcdWData( Wdata )) ;// 定位顯示地址,寫字符
153 1 else
154 1 return ( LCDInit() && DisplayString(0, 0, WrtieLcdErr)); //顯示越界消息
155 1 }
156
157 BOOL DisplayString(unsigned char posx, unsigned char posy, unsigned char* str)
158 {
159 1 while ( (*str) != '\0' )
160 1 {
161 2 if (!DisplayOneChar(posx, posy, (*str)))
162 2 return FALSE;
163 2 str++;
164 2 posx++;
165 2 if( posx == 16 )
166 2 {
167 3 posx = 0;
168 3 posy++;
169 3
170 3 }
171 2 }
172 1
173 1 return TRUE;
174 1 }
175 /*--------------End of LCD1602 macros and functions------------------*/
176
177 /*-------------- ----SPI protocols----------------------------------- */
178
179 void SpiInit(void)
C51 COMPILER V8.02 12 10/10/2008 16:00:14 PAGE 4
180 {
181 1 SPCR = 0x51;
182 1 SPSR = 0x00;
183 1 }
184
185 void SpiClose(void) {SPCR = 0x00;}
186
187 /*----------------End of SPI protocols---------------------------*/
188
189 //-----------ADC MCP3208 functions--------------------
190 // ReadAD(chx)讀AD,chx為通道號
191 //chx=0x00----0x07對應AD0---AD7通道
192 unsigned int ReadAD(unsigned char chx)
193 {
194 1
195 1 unsigned int lsb,msb;
196 1 char sbuf;
197 1 lsb=0;msb=0;
198 1
199 1 ADCS = 0x00;
200 1 sbuf = 0x06 | (chx>>2);
201 1 SPDAT = sbuf;
202 1 while(!(SPSR&0x80) );
203 1 SPSR = 0x00;
204 1 sbuf = SPDAT;
205 1
206 1 sbuf = (chx&0x03)<<6;
207 1 SPDAT = sbuf;
208 1 while(!(SPSR&0x80) );
209 1 SPSR = 0x00;
210 1 sbuf = SPDAT;
211 1 msb = sbuf&0x0F;
212 1
213 1 sbuf = 0x00;
214 1 SPDAT = sbuf;
215 1 while(!(SPSR&0x80) );
216 1 SPSR = 0x00;
217 1 lsb = SPDAT;
218 1
219 1 XBYTE[0xFFFF] = 0x00;
220 1
221 1 return msb*256+lsb;
222 1
223 1 }
224 //------------End ADC MCP3208 functions-----------------
225
226 //-----------------DAC MCP4922 functions----------------------
227 void DACout( unsigned char DACH, unsigned int DAValue)
228 {
229 1 unsigned char msb,lsb,temp;
230 1
231 1 XBYTE[0xFFFF] = 0;
232 1
233 1 DAValue&=0x0FFF;
234 1
235 1 temp = (DAValue/256)&0x0F;
236 1
237 1 DACS = 0x00;
238 1
239 1 if(DACH==1)
240 1 {
241 2 msb =0x70|temp;
C51 COMPILER V8.02 12 10/10/2008 16:00:14 PAGE 5
242 2 lsb = (unsigned char)(DAValue & 0xFF) ;
243 2 }
244 1 else if(DACH==2)
245 1 {
246 2 msb =0xF0|temp;
247 2 lsb = (unsigned char)(DAValue & 0xFF) ;
248 2 }
249 1 else
250 1 { }
251 1
252 1
253 1 SPDAT = msb;
254 1 while(!(SPSR&0x80) );
255 1 SPSR = 0x00;
256 1
257 1 SPDAT = lsb;
258 1 while(!(SPSR&0x80) );
259 1 SPSR = 0x00;
260 1
261 1 XBYTE[0xFFFF] = 0;
262 1 DACLOAD = 0x00;
263 1 nops();
264 1 XBYTE[0xFFFF] = 0;
265 1
266 1 }
267
268
269
270 //**********************按鍵中斷響應**********************
271
272 void Int1_keyscan( ) interrupt 0
273 {
274 1 unsigned char ucSKey;
275 1 unsigned int uiI;
276 1 EA=0;
277 1 for (uiI=0;uiI<100;uiI++); //延時,去掉抖動
278 1 if(INT1==1)
279 1 goto end01;
280 1 for (uiI=0;uiI<100;uiI++); //延時,去掉抖動
281 1 if(INT1==1)
282 1 goto end01; //關閉外部中斷
283 1 LcdWCMD( 0x01) ; //顯示清屏
284 1 keyscan(); //獲取按鍵狀態
285 1 ucSKey=Gatkey(keystate);
286 1 ucKeyState=ucSKey;
287 1 //DisplayOneChar(1,1,keyin); //顯示按鍵對應的字符
288 1 //delay(500);
289 1 end01:;
290 1 KEYROW=0xE0;
291 1 EA=1; // 開啟中斷
292 1
293 1 }
294 //********************************平衡************************
295 void BLB(void)
296 {
297 1 int value_buf[8],temp;
298 1 char j,i;
299 1
300 1 for(i=0;i<8;i++)
301 1 {
302 2 temp = ReadAD(0x01);
303 2 if((temp>10)&&(temp<4500))
C51 COMPILER V8.02 12 10/10/2008 16:00:14 PAGE 6
304 2 {
305 3 value_buf[i]=temp; //獲取采樣值
306 3 }
307 2 delay(3);
308 2 }
309 1
310 1 for(j=0;j<7;j++) //采樣值由小到大排列,排序采用冒泡法
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -