蓝桥杯单片机_第七届决赛

题目要求太多了就不放了;

 

  1 #include "stc15.h"
  2 #include "intrins.h"
  3 #include "stdio.h"
  4 #include "key.h"
  5 #include "iic.h"
  6 #include "ds1302.h"
  7 #include "intrins.h"
  8 #define uchar unsigned char
  9 #define uint  unsigned int
 10 
 11 #define KEYS4   4
 12 #define KEYS5   5
 13 #define KEYS6   6
 14 #define KEYS7   7   
 15 #define KEYS8   8
 16 #define KEYS9   9
 17 #define KEYS10  10
 18 #define KEYS11  11
 19 
 20 uchar code smg_dis[]    = {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x00,0x40};
 21 uchar code Store_add[]  = {0x05,0x06,0x07,0x08};//地址索引
 22 uchar code Time_Init[]  = {23,59,10};
 23 uchar code TIme_Limit[] = {23,59,59};
 24 
 25 uchar Smg_Buf[8]={10,10,10,10,10,10,10,10};//显存
 26 uchar Time_rd_Buf[3] = {0};//时间读
 27 uchar Time_wr_Buf[3]= {0};//时间写
 28 uchar Event[4]= {0};//事件存储
 29 uint  Limit[2]= {2000,1000};//电压阈值
 30 
 31 bit KeyFlag  = 0;//案件扫描
 32 bit MeasFlag = 0;//扫描
 33 
 34 bit SetFlag  = 0;//闪烁使能
 35 bit Blink    = 0;//1s闪烁
 36 bit TimeSet  = 0;//时间设置
 37 bit LimitSet = 0;//阈值设置
 38 
 39 uchar AT_Read(uchar add);
 40 void AT_Write(uchar add ,uchar dat);
 41 void Delay2ms(void);
 42 void Time_Set(uchar *arr);
 43 void Time_Read(uchar *arr);
 44 void Time_Show(uchar *arr);
 45 uint Fre_Read (void);//采集频率
 46 uint ADC_Read(uchar add);//采集电压
 47 void UartInit(void);
 48 void Timer0Init(void);
 49 
 50 uchar Value_Dispose (uchar sum , uchar val ,uchar max , uchar min);
 51 
 52 //功能处理 
 53 void Key_Dispose1 (uchar val);
 54 void Key_Dispose2 (uchar val);
 55 void Key_Dispose3 (uchar val);
 56 void Key_Dispose4 (uchar val);
 57 
 58 void VoltMeasure (void);//后台电压扫描
 59 
 60 void EEPROM_Init (void);//初始化EEPROM中的阈值
 61 void EEPROM_Read(void);//初始化读取EEPROM保存
 62 void EEPROM_Write(uchar *arr);//事件保存
 63 
 64 /* 
 65 EEPROM 存储格式:
 66 
 67     0x01~0x02 电压上限
 68     0x03~0x04 电压下限
 69     0x05 时 [0]
 70     0x06 分 [1]
 71     0x07 秒    [2]
 72     0x08 事件类型[3]
 73     0x10 0xAA  校验位
 74     
 75 */
 76 
 77 uint FreTemp = 0;//频率
 78 uchar ShowMode = 0;
 79 //0 --> 时间
 80 //1 --> 电压
 81 //2 --> 频率
 82 //3 --> 查询
 83 
 84 void main ()
 85 {
 86     KeyStruct KeyVal;//按键结构体
 87     P0 = ~0X00;P2=0X80;P2=0X00;
 88     P0 =  0X00;P2=0Xa0;P2=0X00;
 89     Timer0Init();
 90     Time_Set(Time_Init);
 91 //    UartInit();
 92 //    AT_Write(0x10,0x23);//test
 93     EA = 0;
 94     EEPROM_Init();
 95     EA = 1;
 96     while(1)
 97     {
 98         if(KeyFlag)
 99         {
100             KeyFlag = 0;
101             KeyVal = KeyRead();
102             Key_Dispose1(KeyVal.knew);//入参键值
103             Key_Dispose2(KeyVal.knew);
104             Key_Dispose3(KeyVal.knew);
105             Key_Dispose4(KeyVal.knew);
106         }
107         if(MeasFlag)
108         {
109             MeasFlag = 0;
110             if(ShowMode == 2) 
111             {
112                 FreTemp = Fre_Read();
113             }
114             Time_Read(Time_rd_Buf);
115             VoltMeasure();//后台采集电压
116             if(ShowMode == 0 && !SetFlag) Time_Show(Time_rd_Buf);//仅在显示状态下
117         }    
118     }
119 }
120 
121 void VoltMeasure (void)
122 {
123     uint temp;
124     temp = ADC_Read(0x03);
125     if(temp>=Limit[0])//100ms 一次电压采集 
126     {
127         Event[3] = 1;//大于上限
128         Event[2] = Time_rd_Buf[2];
129         Event[1] = Time_rd_Buf[1];
130         Event[0] = Time_rd_Buf[0];
131         EEPROM_Write(Event);
132     }
133     else if(temp<=Limit[1]) 
134     {
135         Event[3] = 0;//小于下限
136         Event[2] = Time_rd_Buf[2];
137         Event[1] = Time_rd_Buf[1];
138         Event[0] = Time_rd_Buf[0];
139         EEPROM_Write(Event);
140     }
141     if(ShowMode == 1 && !SetFlag) 
142     {
143         Smg_Buf[0] = 11;
144         Smg_Buf[1] = 1;
145         Smg_Buf[2] = 11;
146         Smg_Buf[3] = 10;
147         Smg_Buf[4] = temp/1000;
148         Smg_Buf[5] = temp/100%10;
149         Smg_Buf[6] = temp/10%10;
150         Smg_Buf[7] = temp%10;
151     }    
152 }
153 
154 
155 void TIME0 (void) interrupt 1
156 {
157     static uchar SmgCount  = 0;
158     static uchar KeyCount  = 0;
159     static uchar MeasCount = 0;
160     static uchar SmgLen    = 0;
161     static uint  count = 0;
162     if(SetFlag)
163     {
164         if(++ count > 999)
165         {
166             count = 0;
167             Blink = ~Blink;
168         }
169     }
170     else
171     {
172         count = 0;
173         Blink = 0;
174     }
175      
176     if( ++SmgCount > 1)//数码管
177     {
178         SmgCount = 0;
179         P0=~smg_dis[Smg_Buf[SmgLen]];
180         P2=0xe0;P2=0X00;
181         P0=1<0xc0;P2=0X00;
182         if(++SmgLen > 7) SmgLen=0;
183     }
184     
185     if(++MeasCount > 80)//采集间隔
186     {
187         MeasCount = 0;
188         MeasFlag = 1;
189     }
190         
191     if( ++KeyCount > 10)//按键
192     {
193         KeyCount=0;
194         KeyFlag=1;
195     }
196 }
197 
198 void Timer0Init(void)        //[email protected]
199 {
200     AUXR |= 0x80;
201     TMOD &= 0xF0;
202     TL0 = 0xCD;
203     TH0 = 0xD4;
204     TF0 = 0;
205     TR0 = 1;
206     ET0 = 1;
207     PT0 = 0;
208     EA = 1;
209 }
210 
211 uint Fre_Read (void)
212 {
213     uint temp;
214     TH1 = 0;
215     TL1 = 0;
216 //    EA = 0;
217     while(P34);
218     while(!P34);
219     TR1 = 1;
220     while(P34);
221     while(!P34);
222     TR1 = 0;
223     temp = TH1<<8|TL1;
224 //    EA = 1;
225     return 1000000/temp*0.93;// hz
226 }
227 
228 uint ADC_Read(uchar add)
229 {
230     uchar temp;
231     IIC_Start();
232     IIC_SendByte(0x90);
233     IIC_WaitAck();
234     IIC_SendByte(add);
235     IIC_WaitAck();
236     IIC_Start();
237     IIC_SendByte(0x91);
238     IIC_WaitAck();
239     temp = IIC_RecByte();
240     IIC_WaitAck();
241     IIC_Stop();
242     return temp *5.0/256*1000;// mv
243 }
244 
245 void Time_Read(uchar *arr)
246 {
247     arr[2] = Read_Ds1302(0x81);
248     arr[1] = Read_Ds1302(0x83);
249     arr[0] = Read_Ds1302(0x85);//
250     arr[0] = arr[0]/16*10 + arr[0]%16; 
251     arr[1] = arr[1]/16*10 + arr[1]%16; 
252     arr[2] = arr[2]/16*10 + arr[2]%16; 
253 }
254 
255 void Time_Show (uchar *arr)
256 {
257     Smg_Buf[0] = arr[0]/10;
258     Smg_Buf[1] = arr[0]%10;
259     Smg_Buf[2] = 11;
260     Smg_Buf[3] = arr[1]/10;
261     Smg_Buf[4] = arr[1]%10;
262     Smg_Buf[5] = 11;
263     Smg_Buf[6] = arr[2]/10;
264     Smg_Buf[7] = arr[2]%10;
265 }
266 
267 void Time_Set(uchar *arr)
268 {
269     Write_Ds1302(0x8e,0x00);
270     Write_Ds1302(0x80,arr[2]/10*16+arr[2]%10);//
271     Write_Ds1302(0x82,arr[1]/10*16+arr[1]%10);
272     Write_Ds1302(0x84,arr[0]/10*16+arr[0]%10);
273     Write_Ds1302(0x8e,0x80);
274 }
275 
276 uchar AT_Read(uchar add)
277 {
278     uchar temp;
279     IIC_Start();
280     IIC_SendByte(0xa0);
281     IIC_WaitAck();
282     IIC_SendByte(add);
283     IIC_WaitAck();
284     IIC_Start();
285     IIC_SendByte(0xa1);
286     IIC_WaitAck();
287     temp=IIC_RecByte();
288     IIC_WaitAck();
289     IIC_Stop();
290     Delay2ms();
291     return temp;    
292 }
293 
294 void AT_Write(uchar add ,uchar dat)
295 {
296     IIC_Start();
297     IIC_SendByte(0xa0);
298     IIC_WaitAck();
299     IIC_SendByte(add);
300     IIC_WaitAck();
301     IIC_SendByte(dat);
302     IIC_WaitAck();
303     IIC_Stop();
304     Delay2ms();    
305 }
306 
307 void EEPROM_Write(uchar *arr)//保存事件时间
308 {
309     uchar i = 0;
310     for(;i<4;i++)
311     {
312         AT_Write(Store_add[i],arr[i]);
313     }
314 }
315 
316 void EEPROM_Init (void)
317 {
318     //初始化EEPROM中的阈值
319     //高位在后
320     if( AT_Read(0x10)== 0xaa)//判断校验位
321     {
322         EEPROM_Read();
323         P0 = ~0X01;P2=0X80;P2=0X00;
324     }
325     else
326     {
327         AT_Write(0x10,0xaa);//写入校验位
328         AT_Write(0x01,Limit[0]);
329         AT_Write(0x02,Limit[0]>>8);
330         AT_Write(0x03,Limit[1]);
331         AT_Write(0x04,Limit[1]>>8);    
332         P0 = ~0X02;P2=0X80;P2=0X00;
333     } 
334 }
335 
336 void EEPROM_Read(void)//初始化读取EEPROM保存
337 {
338     uchar i = 0;
339     Limit[0] = AT_Read(0x02) << 8 | AT_Read(0x01);
340     Limit[1] = AT_Read(0x04) << 8 | AT_Read(0x03);//读取阈值
341     for(;i<4;i++)//读取事件
342     {
343         Event[i] = AT_Read(Store_add[i]);    
344     }
345 }
346 
347 void UartInit(void)//Uart1 9600bps 11.0592Mhz time2
348 {
349     SCON = 0x50;        
350     AUXR |= 0x01;        
351     AUXR |= 0x04;        
352     T2L = 0xE0;        
353     T2H = 0xFE;        
354     AUXR |= 0x10;
355 }
356 
357 void Delay2ms(void)        //@11.0592MHz
358 {
359     unsigned char i, j;
360 
361     _nop_();
362     _nop_();
363     i = 22;
364     j = 128;
365     do
366     {
367         while (--j);
368     } while (--i);
369 }
370 
371 void Key_Dispose1 (uchar val)//功能处理 1 时间设置
372 {
373     static uchar mode = 0;
374     static uchar len = 0;
375     static uchar temp[3];
376     if( !LimitSet && val == KEYS7 )
377     {
378         if(ShowMode == 0)mode++;
379         else 
380         {
381             ShowMode = 0;
382         }
383         if(mode == 1)
384         {
385             SetFlag  = 1;//进入时间设置
386             TimeSet  = 1;
387             ShowMode = 0;
388             Time_wr_Buf[0] = Time_rd_Buf[0];
389             Time_wr_Buf[1] = Time_rd_Buf[1];
390             Time_wr_Buf[2] = Time_rd_Buf[2];
391             temp[0]=Time_wr_Buf[0];
392             temp[1]=Time_wr_Buf[1];
393             temp[2]=Time_wr_Buf[2];
394             mode++;
395         }
396         else if(mode == 3)
397         {
398             SetFlag  = 0; 
399             TimeSet  = 0;
400             if(temp[0]!=Time_wr_Buf[0]||temp[1]!=Time_wr_Buf[1]||temp[2]!=Time_wr_Buf[2])
401                 Time_Set(Time_wr_Buf);
402             mode = 0;
403         }
404     }
405     if( TimeSet )//时间调整
406     {
407         Time_Show(Time_wr_Buf);
408         if(val==KEYS4)
409         {
410             if(++len > 2 )len = 0;
411         }
412         if(Blink)
413         {
414             Smg_Buf[len*3]   = 10;
415             Smg_Buf[len*3+1] = 10;
416         }
417         Time_wr_Buf[len] = Value_Dispose(Time_wr_Buf[len],val,TIme_Limit[len],0);
418     }
419     else
420     {
421         len = 0;
422     }
423 }
424 
425 void Key_Dispose2 (uchar val)//电压
426 {
427     static uchar mode = 0;
428     static uchar len = 0;
429     if( !TimeSet && val == KEYS6 )
430     {
431         mode++;
432         if(mode==1)
433         {
434             ShowMode = 1;
435         }
436         else if(mode==2)
437         {
438             SetFlag  = 1;//开闪烁
439             LimitSet = 1;
440         }
441         else
442         {
443             SetFlag  = 0;
444             LimitSet = 0;
445             mode = 1;
446             AT_Write(0x01,Limit[0]);
447             AT_Write(0x02,Limit[0]>>8);    
448             AT_Write(0x03,Limit[1]);
449             AT_Write(0x04,Limit[1]>>8);    
450         }
451     } 
452     if(ShowMode!=1 ) mode = 0;//直接退出到时间显示后清0
453     if( LimitSet )
454     {
455         Smg_Buf[0] = Limit[0]/1000;
456         Smg_Buf[1] = Limit[0]/100%10;
457         Smg_Buf[2] = Limit[0]/10%10;
458         Smg_Buf[3] = Limit[0]%10;
459         
460         Smg_Buf[4] = Limit[1]/1000;
461         Smg_Buf[5] = Limit[1]/100%10;
462         Smg_Buf[6] = Limit[1]/10%10;
463         Smg_Buf[7] = Limit[1]%10;
464         
465         if(val==KEYS4)
466         {
467             if(++len > 1 )len = 0;
468         }
469         if(Blink)
470         {
471             Smg_Buf[len*4]   = 10;
472             Smg_Buf[len*4+1] = 10;
473             Smg_Buf[len*4+2] = 10;
474             Smg_Buf[len*4+3] = 10;
475         }
476         Limit[len] = Value_Dispose(Limit[len]/500,val,10,0)*500;//步进500
477     }
478     else
479     {
480         len = 0;
481     }       
482 }
483 
484 void Key_Dispose3 (uchar val)//频率
485 {
486     static uchar mode = 0;
487     static uchar len = 0;
488     uint temp;
489     if( !TimeSet && !LimitSet && val == KEYS5 )
490     {
491         mode++;
492         if(mode==1)
493         {
494             ShowMode = 2;
495         }
496         else if(mode==2)
497         {
498             ShowMode = 0;
499         }
500     } 
501     if(ShowMode != 2) mode = 0;//直接退出到时间显示后清0
502     if( ShowMode == 2 )
503     {
504         Smg_Buf[0] = 11;
505         Smg_Buf[1] = 2;
506         Smg_Buf[2] = 11;
507         if(val==KEYS4)
508         {
509             if(++len > 1 )len = 0;
510         }
511         if(len==0)
512         {
513             Smg_Buf[3] = FreTemp/10000;
514             Smg_Buf[4] = FreTemp/1000%10;
515             Smg_Buf[5] = FreTemp/100%10;
516             Smg_Buf[6] = FreTemp/10%10;
517             Smg_Buf[7] = FreTemp%10;
518         }
519         else
520         {
521             temp = 100000/FreTemp;
522             Smg_Buf[3] = temp/10000;
523             Smg_Buf[4] = temp/1000%10;
524             Smg_Buf[5] = temp/100%10;
525             Smg_Buf[6] = temp/10%10;
526             Smg_Buf[7] = temp%10;
527         }  
528     }
529     else
530     {
531         len = 0;
532     }       
533 }
534 
535 void Key_Dispose4 (uchar val)//查询
536 {
537     static uchar mode = 0;
538     static uchar len = 0;
539     if( !TimeSet && !LimitSet && val == KEYS9 )
540     {
541         mode++;
542         if(mode==1)
543         {
544             ShowMode = 3; 
545         }
546         else if(mode==2)
547         {
548              ShowMode = 0; 
549         }
550     } 
551     if(ShowMode != 3) mode = 0;//直接退出到时间显示后清0
552     if( ShowMode == 3 )
553     {
554         if(val==KEYS4)
555         {
556             if(++len > 1 )len = 0;
557         }
558         if(len==0)
559         {
560             Smg_Buf[0] = 10;
561             Smg_Buf[1] = 10;
562             Smg_Buf[2] = 10;
563             Smg_Buf[3] = 10;
564             Smg_Buf[4] = 10;
565             Smg_Buf[5] = 10;
566             Smg_Buf[6] = 10;
567             Smg_Buf[7] = Event[3];
568         }
569         else
570         {
571             Time_Show(Event);
572         } 
573     }
574     else
575     {
576         len = 0;
577     }       
578 }
579 
580 uchar Value_Dispose (uchar sum , uchar val ,uchar max , uchar min)
581 {
582     char temp;
583     temp = sum;
584     if(val == KEYS11) 
585     {
586         if(++temp >= max) temp = max;
587     }
588     else if(val == KEYS10) 
589     {
590         if(--temp <= min) temp = min;
591     }
592     return temp;  
593 }

 

转载于:https://www.cnblogs.com/nsss/p/10886566.html

你可能感兴趣的:(蓝桥杯单片机_第七届决赛)