前言
本人曾在第十三届蓝桥杯大赛单片机组中获得国一,以下是我在准备比赛过程中的一些记录,希望能给大家带来帮助
本人持续分享更多关于嵌入式和单片机的知识,如果大家喜欢,别忘点个赞加个关注哦,让我们一起共同进步~
随着距离蓝桥杯举办日期的日益临近,今天才真正完成了第一套真题,基本就考察了几个外设的使用,已经按键和各个外设之间的灵活使用。总的来说第十届蓝桥杯省赛题目不是很难,下面是我的代码,如果有什么问题大家可以积极沟通,一起进步鸭。
# 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; //led
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); // 1001 0000
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() //定时器0计数,定时器1计时
{
TH0 = 0xff;
TH0 = 0xff;
TMOD = 0x16;
TL1 = 0xB0; //设置定时初始值
TH1 = 0x3C; //设置定时初始值
ET0 = 1;
ET1 = 1;
EA = 1;
TR1 = 1; //定时器1开始计时
TR0 = 1; //定时器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)
{
//RB2_show();
keyscan();
}
}
更多相关资源
【全套完结】蓝桥杯单片机— 从省赛到国赛
https://blog.csdn.net/amimax/article/details/128749109?spm=1001.2014.3001.5501