赞
踩
去年14届的国赛题,个人感觉难度适中。主要的难点在于,S8\S9按键长按清楚数据,以及同时按下时,记录超声波的测量值。 还有一个值得注意的是,在校准值中,引入了负数(这点需要特别注意)。当在-5再减到-10时,数码管的处理是一个小难点。LED灯也不同于往年的考法,结合了二进制的知识点。
此次试题,本人用了几个巧妙地方法完美实现以上问题。以下为代码实现部分,所有都为本人一人编写,仅供大家学习交流。
矩阵键盘部分
- #include <STC15F2K60S2.H>
-
- unsigned char Key_read()
- {
- unsigned char temp=0;
-
- P44=0;P42=1;P35=1;P34=1;
- if(P30==0)temp=7;
- if(P31==0)temp=6;
- if(P32==0)temp=5;
- if(P33==0)temp=4;
-
- P44=1;P42=0;P35=1;P34=1;
- if(P30==0)temp=11;
- if(P31==0)temp=10;
- if(P32==0)temp=9;
- if(P33==0)temp=8;
-
- P44=1;P42=1;P35=0;P34=1;
- if(P30==0)temp=15;
- if(P31==0)temp=14;
- if(P32==0)temp=13;
- if(P33==0)temp=12;
-
- P44=1;P42=1;P35=1;P34=0;
- if(P30==0)temp=19;
- if(P31==0)temp=18;
- if(P32==0)temp=17;
- if(P33==0)temp=16;
-
- return temp;
- }
数码管部分
- #include <STC15F2K60S2.H>
-
- code unsigned char Shu[]=
- {
- 0xc0,
- 0xf9,
- 0xa4,
- 0xb0,
- 0x99,
- 0x92,
- 0x82,
- 0xf8,
- 0x80,
- 0x90,
- 0xff,
- 0xbf, //-
- 0x8c, //P
- 0x8e //F
- };
-
- void InitHC138(unsigned char u,num)
- {
- P0=num;
- switch(u)
- {
- case 4:P2=P2&0x1f|0x80;break;
- case 5:P2=P2&0x1f|0xa0;break;
- case 6:P2=P2&0x1f|0xc0;break;
- case 7:P2=P2&0x1f|0xe0;break;
- }
- P2=P2&0x1f|0x00;
- }
-
- void SMG(unsigned char location,num,point)
- {
- InitHC138(7,0xff);
- InitHC138(6,0x01<<location);
-
- P0=Shu[num];
- if(point)
- P0&=0x7f;
- P2=P2&0x1f|0xe0;
- P2=P2&0x1f|0x00;
- }
LED、继电器模块
- #include <STC15F2K60S2.H>
-
- static unsigned char temp1=0x00;
- static unsigned char old1=0xff;
-
- void Led_read(unsigned char addr,able)
- {
- static unsigned char temp=0x00;
- static unsigned char old=0xff;
-
- if(able)
- temp|=0x01<<addr;
- else
- temp&=~(0x01<<addr);
-
- if(temp!=old)
- {
- P0=~temp;
- P2=P2&0x1f|0x80;
- P2=P2&0x1f|0x00;
- old=temp;
- }
- }
-
- void Relay(unsigned char able)
- {
- if(able)
- temp1|=0x10;
- else
- temp1&=~0x10;
-
- if(temp1!=old1)
- {
- P0=temp1;
- P2=P2&0x1f|0xa0;
- P2=P2&0x1f|0x00;
- old1=temp1;
- }
- }
温度读取模块(省去onewire给的代码)
- float Temperture()
- {
- unsigned char low,high;
- unsigned int temp;
- float t;
-
- init_ds18b20();
- Write_DS18B20(0xcc);
- Write_DS18B20(0x44);
- Delay_OneWire(200);
- init_ds18b20();
- Write_DS18B20(0xcc);
- Write_DS18B20(0xbe);
- low=Read_DS18B20();
- high=Read_DS18B20();
- temp=(high<<8)|low;
- t=temp*0.0625;
-
- return t;
- }
超声波模块
- #include <STC15F2K60S2.H>
- #include "intrins.h"
-
- sbit TX=P1^0;
- sbit RX=P1^1;
-
- void Delay12us(void) //@12.000MHz
- {
- unsigned char data i;
- _nop_();
- _nop_();
- i = 40;
- while (--i);
- }
-
- void Wave_Init()
- {
- unsigned char i;
- for(i=0;i<8;i++)
- {
- TX=1;
- Delay12us();
- TX=0;
- Delay12us();
- }
- }
-
- unsigned char Wave_get()
- {
- unsigned int time;
- TMOD&=0x0f;
- TH1=TL1=0;
- Wave_Init();
- TR1=1;
- while((RX==1) && (TF1==0));
- TR1=0;
-
- if(TF1==0)
- {
- time=(TH1<<8)|TL1;
- return time*0.017;
- }
- else
- {
- TF1=0;
- return 0;
- }
- }
-
DA输出模块(省去iic给的代码)
- void DA(unsigned char dat)
- {
- I2CStart();
- I2CSendByte(0x90);
- I2CWaitAck();
- I2CSendByte(0x43);
- I2CWaitAck();
- I2CSendByte(dat);
- I2CWaitAck();
- I2CStop();
- }
主函数
- #include <STC15F2K60S2.H>
- #include "intrins.h"
- #include "Key.h"
- #include "Seg.h"
- #include "Led.h"
- #include "onewire.h"
- #include "Wave.h"
- #include "iic.h"
-
- unsigned char Key_time;
- unsigned int Seg_time;
-
- unsigned char Key_value,Key_dowm,Key_up,Key_old;
- unsigned char Seg_buf[]={10,10,10,10,10,10,10,10,10,10};
- unsigned char Seg_point[]={0,0,0,0,0,0,0,0};
- unsigned char Seg_pos;
- unsigned char Led_buf[]={0,0,0,0,0,0,0,0};
-
- float T;
- int L;
- unsigned char Mode;
- unsigned char Show_L=40; //¾àÀë²ÎÊý
- unsigned char Show_T=30; //ζȲÎÊý
- unsigned char Mode_can; //²ÎÊýģʽ1/2
- unsigned char Mode_can3; //²ÎÊýģʽ3
- unsigned char Change_can; //У׼ֵ
- unsigned char Fu; //¸ºÊýУ׼ֵ
- int Speed=340; //´«ÊäËÙÂÊ
- unsigned char V=10; //µçѹ
- float V_use;
- unsigned char HEX[8]={128,64,32,16,8,4,2,1};
- unsigned char Ad_num;
- unsigned char Time_100ms;
- unsigned char State; //µÆ±ä»»ÁÁÃð
-
- void Key_get()
- {
- if(Key_time)return;
- Key_time=1;
-
- Key_value=Key_read();
- Key_dowm=Key_value&(Key_value^Key_old);
- Key_up=~Key_value&(Key_value^Key_old);
- Key_old=Key_value;
-
- switch(Key_dowm)
- {
- case 4:
- if(++Mode==3)Mode=0;
- Mode_can=0;
- Mode_can3=0;
- break;
-
- case 5:
- if(Mode==0 || Mode==1)Mode_can^=1;
- if(Mode==2)
- {
- if(++Mode_can3==3)Mode_can3=0;
- }
- break;
-
- case 8: //¼Ó
- if(Mode==1)
- {
- if(Mode_can==0)
- {
- Show_L+=10;
- if(Show_L==100)Show_L=90;
- }
- else
- {
- if(++Show_T==81)Show_T=80;
- }
- }
- else if(Mode==2)
- {
- if(Mode_can3==0)
- {
- if(Fu==0 && Change_can>=0)
- {
- Change_can+=5;
- if(Change_can==95)Change_can=90;
- }
- if(Fu>0)
- Fu-=5;
- }
- else if(Mode_can3==1)
- {
- Speed+=10;
- if(Speed==9995)Change_can=9990;
- }
- else
- {
- if(++V==21)V=20;
- }
- }
- break;
-
- case 9:
- if(Mode==1) //²ÎÊý¼õ
- {
- if(Mode_can==0)
- {
- Show_L-=10;
- if(Show_L==0)Show_L=10;
- }
- else
- {
- if(--Show_T==255)Show_T=0;
- }
- }
- else if(Mode==2) //¹¤³§Ä£Ê½
- {
- if(Mode_can3==0)
- {
- if(Change_can==0 && Fu>=0)
- {
- Fu+=5;
- if(Fu==95)Fu=90;
- }
- if(Change_can>0)
- Change_can-=5;
- }
- else if(Mode_can3==1)
- {
- Speed-=10;
- if(Speed==0)Change_can=10;
- }
- else
- {
- if(--V==0)V=1;
- }
- }
- break;
- }
-
- }
-
- void Seg_get()
- {
- if(Seg_time)return;
- Seg_time=1;
-
- T=Temperture();
- L=(Speed/340)*Wave_get()+(Change_can>Fu?Change_can:-Fu);
-
- switch(Mode)
- {
- case 0:
- Seg_buf[0]=T/10;
- Seg_buf[1]=(unsigned char)T%10;
- Seg_buf[2]=(unsigned int)(T*10)%10;
- Seg_buf[3]=11;
- Seg_point[1]=1;
- Seg_point[6]=0;
- if(Mode_can==0)
- {
- Seg_buf[5]=L>99?L/100%10:10;
- Seg_buf[6]=L/10%10;
- Seg_buf[7]=L%10;
- Seg_point[5]=0;
- }
- else
- {
- Seg_buf[5]=L>99?L/100%10:0;
- Seg_buf[6]=L/10%10;
- Seg_buf[7]=L%10;
- Seg_point[5]=1;
- }
- break;
-
- case 1:
- Seg_buf[0]=12;
- Seg_buf[2]=10;
- Seg_buf[3]=10;
- Seg_buf[5]=10;
- Seg_point[1]=0;
- Seg_point[5]=0;
- if(Mode_can==0)
- {
- Seg_buf[1]=1;
- Seg_buf[6]=Show_L/10;
- Seg_buf[7]=Show_L%10;
- }
- else
- {
- Seg_buf[1]=2;
- Seg_buf[6]=Show_T/10;
- Seg_buf[7]=Show_T%10;
- }
- break;
-
- case 2:
- Seg_buf[0]=13;
- if(Mode_can3==0)
- {
- Seg_buf[1]=1;
- Seg_point[6]=0;
- if(Change_can>0 || Change_can==Fu)
- {
- Seg_buf[5]=10;
- Seg_buf[6]=Change_can<10?10:Change_can/10;
- Seg_buf[7]=Change_can%10;
- }
- if(Fu>0)
- {
- Seg_buf[5]=Fu>=10?11:10;
- Seg_buf[6]=Fu<10?11:Fu/10;
- Seg_buf[7]=Fu%10;
- }
- }
- else if(Mode_can3==1)
- {
- Seg_buf[1]=2;
- Seg_buf[4]=Speed>999?Speed/1000%10:10;
- Seg_buf[5]=Speed>99?Speed/100%10:10;
- Seg_buf[6]=Speed/10%10;
- Seg_buf[7]=Speed%10;
- }
- else
- {
- Seg_buf[1]=3;
- Seg_buf[4]=10;
- Seg_buf[5]=10;
- Seg_point[6]=1;
- if(V>=10)
- {
- Seg_buf[6]=V/10;
- Seg_buf[7]=V%10;
- }
- else
- {
- Seg_buf[6]=0;
- Seg_buf[7]=V%10;
- }
- }
- break;
- }
-
- }
-
- void Led_get()
- {
- unsigned char i;
- int temp=L;
- if(Mode==1)
- {
- for(i=0;i<7;i++) //ϨÃðÆäÓà7ÕµµÆ£¬ÁôL8
- Led_buf[i]=0;
- Led_buf[7]=1;
- }
- else if(Mode==2)
- {
- for(i=1;i<8;i++)
- Led_buf[i]=0;
- Led_buf[0]=State;
- }
- else
- {
- for(i=0;i<8;i++)
- {
- if(temp>HEX[i])
- {
- Led_buf[7-i]=1;
- temp-=HEX[i];
- }
- else if(temp==HEX[i]) //¸ÕºÃµÈÓÚ1¡¢2¡¢4¡¢8¡¢16...
- {
- Led_buf[7-i]=1;
- temp=0;
- }
- else
- Led_buf[7-i]=0;
- }
- }
-
- V_use=V/10;
- if(L>=90)Ad_num=5;
- if(L<=10)Ad_num=V_use;
- if(L>10 && L<90)Ad_num=((5-V_use)/80)*L+(9*V_use-5)/8;
- DA(Ad_num*51);
-
- if((Show_L-5<=L && Show_L+5>=L) && T<=Show_T)
- Relay(1);
- else
- Relay(0);
- }
-
- void Timer0_Init()
- {
- AUXR &= 0x7F;
- TMOD &= 0xF0;
- TL0 = 0x18;
- TH0 = 0xFC;
- TF0 = 0;
- TR0 = 1;
- EA=1;
- ET0=1;
- }
-
- void Delay750ms(void)
- {
- unsigned char data i, j, k;
- _nop_();
- _nop_();
- i = 35;
- j = 51;
- k = 182;
- do
- {
- do
- {
- while (--k);
- } while (--j);
- } while (--i);
- }
-
- void main()
- {
- InitHC138(4,0xff);
- InitHC138(5,0x00);
- Timer0_Init();
- Temperture();
- Delay750ms();
- while(1)
- {
- Key_get();
- Seg_get();
- Led_get();
- }
- }
-
- void Init_T0() interrupt 1
- {
- if(++Key_time==10)Key_time=0;
- if(++Seg_time==200)Seg_time=0;
- if(++Seg_pos==8)Seg_pos=0;
- SMG(Seg_pos,Seg_buf[Seg_pos],Seg_point[Seg_pos]);
- Led_read(Seg_pos,Led_buf[Seg_pos]);
-
- if(++Time_100ms==100)
- {
- Time_100ms=0;
- State^=1;
- }
- }
十四届国赛
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。