随着距离蓝桥杯举办日期的日益临近,今天才真正完成了第一套真题,基本就考察了几个外设的使用,已经按键和各个外设之间的灵活使用。总的来说第十届蓝桥杯省赛题目不是很难,下面是我的代码,如果有什么问题大家可以积极沟通,一起进步鸭。
# include "reg52.h"
# include "iic.h"
sbit S4 = P3^3;
sbit S5 = P3^2;
sbit S6 = P3^1;
sbit S7 = P3^0;
unsigned char sin_S5 = 0;
unsigned char sin_S6 = 0;
unsigned char sin_S7 = 0;
int dataint_RB2 ;
unsigned char a = 1;
unsigned t_cout = 0;
unsigned f_cout = 0;
unsigned c_cout = 0;
unsigned char data_RB2 = 0;
unsigned char SMG_NoDot[12]={0xc0,0xf9,
0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,
0xbf,0x7f};
unsigned char SMG_Dot[10]={0x40,0x79,
0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10,
};
void SMG_delay(unsigned t)
{
while(t--);
}
void select_HC138(unsigned char n)
{
switch(n){
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;
case 0 :
P2 = P2&0x1f ;
}
}
void system_init()
{
select_HC138(4);
P0 = 0xff;
select_HC138(5);
P0 = 0x00;
select_HC138(7);
P0 = 0xff;
select_HC138(0);
}
void SMG_show(unsigned char value , unsigned char pos)
{
P0 = 0xff;
select_HC138(6);
P0 = 0x01<< pos;
select_HC138(7);
P0 = value;
}
void RB2_show()
{
if (sin_S7){
select_HC138(6);
P0 = 0x00;
select_HC138(7);
P0 = 0xff;
select_HC138(0);
}
else{
IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(0x03);
IIC_WaitAck();
IIC_Stop();
IIC_Start();
IIC_SendByte(0x91);
IIC_WaitAck();
data_RB2 = IIC_RecByte();
IIC_SendAck(1);
IIC_Stop();
dataint_RB2 = (int)data_RB2;
dataint_RB2 = (float)dataint_RB2*500/255;
SMG_show(0xc1,0);
SMG_delay(500);
SMG_show(0xff,1);
SMG_delay(500);
SMG_show(0xff,2);
SMG_delay(500);
SMG_show(0xff,3);
SMG_delay(500);
SMG_show(0xff,4);
SMG_delay(500);
if(sin_S5){
SMG_show(SMG_Dot[2],5);
SMG_delay(500);
SMG_show(SMG_NoDot[0],6);
SMG_delay(500);
SMG_show(SMG_NoDot[0],7);
SMG_delay(500);
}
else{
SMG_show(SMG_Dot[dataint_RB2/100],5);
SMG_delay(500);
SMG_show(SMG_NoDot[(dataint_RB2%100)/10],6);
SMG_delay(500);
SMG_show(SMG_NoDot[dataint_RB2%10],7);
SMG_delay(500);} }
}
void Time_init()
{
TH0 = 0xff;
TH0 = 0xff;
TMOD = 0x16;
TL1 = 0xB0;
TH1 = 0x3C;
ET0 = 1;
ET1 = 1;
EA = 1;
TR1 = 1;
TR0 = 1;
}
void time0_server()interrupt 1
{
c_cout ++ ;
}
void time1_server()interrupt 3
{
TL1 = 0xB0;
TH1 = 0x3C;
t_cout ++;
if (t_cout == 20){
f_cout = c_cout;
t_cout = 0;
c_cout = 0;
}
}
void fre_show()
{ if (sin_S7){
select_HC138(6);
P0 = 0x00;
select_HC138(7);
P0 = 0xff;
select_HC138(0);
}
else{
SMG_show(0x8e,0);
SMG_delay(500);
SMG_show(0xff,1);
SMG_delay(500);
SMG_show(0xff,2);
SMG_delay(500);
if(f_cout>9999)
{SMG_show(SMG_NoDot[f_cout/10000],3);
SMG_delay(500);}
if(f_cout>999)
{SMG_show(SMG_NoDot[(f_cout/1000)%10],4);
SMG_delay(500);}
if(f_cout>99)
{SMG_show(SMG_NoDot[(f_cout/100)%10],5);
SMG_delay(500);}
if(f_cout>9)
{SMG_show(SMG_NoDot[(f_cout/10)%10],6);
SMG_delay(500);}
SMG_show(SMG_NoDot[f_cout%10],7);
SMG_delay(500);
}
}
void led_cor()
{
if (sin_S6){
select_HC138(4);
P0 = 0xff;
select_HC138(0);
}
else{
P0 = 0xff;
if(a==1){
select_HC138(4);
P0 = (P0&0xfc)|0x02;
}
else {
select_HC138(4);
P0 = (P0&0xfc)|0x01;
}
if(f_cout<1000) P0 = (P0&0xf7)|0x08;
else if((f_cout<5000)&&(f_cout>=1000))P0 = (P0&0xf7)|0x00;
else if((f_cout<10000)&&(f_cout>=5000)) P0=(P0&0xf7)|0x08;
else if(f_cout>=100000) P0 = (P0&0xf7)|0x00;
if(dataint_RB2<150) P0 = (P0&0xfb)|0x04;
else if((dataint_RB2<250)&&(dataint_RB2>=150))P0 = (P0&0xfb)|0x00;
else if((dataint_RB2<350)&&(dataint_RB2>=250)) P0=(P0&0xfb)|0x04;
else if(dataint_RB2>=350) P0 = (P0&0xfb)|0x00;
if(dataint_RB2 == 200) P0 = (P0&0xef)|0x10;
else P0 =(P0&0xef)|0x00;
select_HC138(0); }
}
void keyscan()
{ led_cor();
if(a==1)RB2_show();
else fre_show();
if(S4==0){
SMG_delay(2000);
while(S4 == 0){
if(a)RB2_show();
else fre_show();
}
if(a==1) a = 0;
else a =1;
}
if(S5==0){
SMG_delay(2000);
while(S5 == 0){
if(a)RB2_show();
else fre_show();
}
if(sin_S5==1) sin_S5 = 0;
else sin_S5 =1;
}
if(S6==0){
SMG_delay(2000);
while(S6 == 0){
if(a)RB2_show();
else fre_show();
}
if(sin_S6==1) sin_S6 = 0;
else sin_S6 =1;
}
if(S7==0){
SMG_delay(2000);
while(S7 == 0){
if(a)RB2_show();
else fre_show();
}
if(sin_S7==1) sin_S7 = 0;
else sin_S7 =1;
}
}
void main()
{
system_init();
Time_init();
while(1)
{
keyscan();
}
}
|