- 一、13届蓝桥杯省赛题目
- 二、程序代码
- 1.主函数
- 2.DS18B20
- 3.DS1302.C
以下是13届蓝桥杯的单片机程序题解答(含题目),博主是个还在备赛省赛的苦命娃(因疫情推迟),有不妥的地方欢迎大家指正。
一、13届蓝桥杯省赛题目
main:
#include
#include
#include
void Display(void);
//sbit J=P0^4;
sbit C1=P3^2;
sbit C2=P3^3;
sbit C3=P3^4;
sbit C4=P3^5;
sbit L3=P0^2;
unsigned char S12_flag,S13_flag,S16_flag,S17_flag;
unsigned char L10flag,js_flag,time1_flag,time5_flag,L_flag,fm_flag,L,L1_flag;
int t_canshu,cs_bu;
unsigned int temp;
unsigned char shi,fen,miao;
unsigned char SEG[11]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xc1};
void init()
{
P0=0x00;
P2=0xa0;
P2=0x00;
P0=0xff;
P2=0x80;
P2=0x00;
}
void Delay1ms() //@12.000MHz
{
unsigned char i, j;
i = 12;
j = 169;
do
{
while (--j);
} while (--i);
}
void Delay5ms() //@12.000MHz
{
unsigned char i, j;
i = 59;
j = 90;
do
{
while (--j);
} while (--i);
}
void Timer0Init(void) //50??@12.000MHz
{
AUXR &= 0x7F; //?????12T??
TMOD &= 0xF0; //???????
TL0 = 0xB0; //??????
TH0 = 0x3C; //??????
TF0 = 0; //??TF0??
EA = 1; //???0????
ET0=1;
}
void time_5s() interrupt 1
{
js_flag++;
if(js_flag%2==0)
{
time1_flag=1;
}
if(js_flag==100)
{
js_flag=0;
L10flag=1;
time5_flag=1;
}
}
void seg(int wei,int duan)
{
P2=0xc0;
P0=0x00;
P0=0x01<<(wei-1);
P2=0xe0;
P0=0xff;
P0=SEG[duan];
Delay1ms();
P0=0xff;
P2=0x00;
}
void seg_d(int wei,int duan)
{
P2=0xc0;
P0=0x00;
P0=0x01<<(wei-1);
P2=0xe0;
P0=0xff;
P0=SEG[duan]&0x7f;
Delay1ms();
P0=0xff;
P2=0x00;
}
void seg_u()
{
P0=0xff;
P2=0xc0;
P0=0x00;
P0=0x01;
P2=0xe0;
P0=0xff;
P0=SEG[10];
Delay1ms();
P0=0xff;
P2=0x00;
}
void seg_f()
{
P2=0xc0;
P0=0x00;
P0=0x20;
P2=0xe0;
P0=0xff;
P0=0xbf;
Delay1ms();
P0=0xff;
P2=0x00;
}
void display_temp()
{
seg_u();
seg(2,1);
seg(6,(temp*0.625)/100);
seg_d(7,(temp*10)/16%100/10);
seg(8,(temp*10)/16%10);
}
void display_time(int time1,int time2)
{
seg_u();
seg(2,2);
seg(4,time1/16);
seg(5,time1%16);
seg_f();
seg(7,time2/16);
seg(8,time2%16);
}
void display_sz(int tsz)
{
seg_u();
seg(2,3);
seg(7,tsz/10);
seg(8,tsz%10);
}
void read_sfm()
{
shi=Read_Ds1302_Byte (0x85);
fen=Read_Ds1302_Byte (0x83);
miao=Read_Ds1302_Byte (0x81);
}
void key()
{
C1=C2=C3=C4=1;
C1=0;
if(C3==0)
{
Delay5ms();
if(C3==0)
{
while(!C3)
{
fm_flag=1;
Display();
}
fm_flag=0;
S17_flag=1;
}
}
else if(C4==0)
{
Delay5ms();
if(C4==0)
{
while(!C4)
{
Display();
}
S13_flag++;
}
}
C1=1;
C2=0;
if(C3==0)
{
Delay5ms();
if(C3==0)
{
while(!C3)
{
Display();
}
S16_flag++;
}
}
else if(C4==0)
{
Delay5ms();
if(C4==0)
{
while(!C4)
{
Display();
}
S12_flag++;
}
}
}
void Display(void)
{
read_sfm();
S12_flag=S12_flag%3;
if(S12_flag==0)
{
display_temp();
}
else if(S12_flag==1)
{
if(fm_flag==0)
display_time(shi,fen);
if(fm_flag)
{
cs_bu=1;
display_time(fen,miao);
}
}
else if(S12_flag==2)
{
//S17_flag=0;
display_sz(t_canshu);
}
}
void mod()
{
if(S13_flag==3)
{
S13_flag=1;
}
if(S13_flag==1)
{
P0&=0xfd;
P2=0x80;
P2=0x00;
//P0|=0x02;
//P0=0xff;
if((temp/16.0)>t_canshu)
{
TR0=1;
L=P0;
P0=0x10;
P2=0xa0;
P2=0x00;
P0=L;
if(time1_flag)
{
time1_flag=0;
L3=~L3;
P2=0x80;
P2=0x00;
}
}
else
{
//TR0=0;
L3=1;
P2=0x80;
P2=0x00;
L=P0;
P0=0x00;
P2=0xa0;
P2=0x00;
P0=L;
}
}
else if(S13_flag==2)
{
P0|=0x02;
P2=0x80;
P2=0x00;
//P0=0xff;
if(fen==0&miao==0)
{
L_flag=1;
}
if(L_flag)
{
L=P0;
P2=0xa0;
P0=0x10;
P2=0x00;
P0=L;
TR0=1;
if(time1_flag)
{
time1_flag=0;
L3=~L3;
P2=0x80;
P2=0x00;
}
}
if(L10flag)
{
L=P0;
P2=0xa0;
P0=0x00;
P2=0x00;
P0=L;
L3=1;
P2=0x80;
P2=0x00;
L_flag=0;
L10flag=0;
TR0=0;
}
}
}
void LED() //L1
{
if(fen==0&miao==0)
{
TR0=1;
L1_flag=1;
}
if(time5_flag)
{
time5_flag=0;
L1_flag=0;
TR0=0;
}
if(L1_flag==1)
{
P0&=0xfe;
P2=0x80;
P2=0x00;
}
if(L1_flag==0)
{
P0|=0x01;
P2=0x80;
P2=0x00;
}
}
void main (void)
{
init();
Timer0Init();
S12_flag=0;
S13_flag=1;
t_canshu=23;
w_sfm(1,59,50);
while(1)
{
temp=read_temp();
key();
Display();
LED();
mod();
if(S12_flag==2)
{
if(S16_flag==1&t_canshu<90)
{
S16_flag=0;
t_canshu++;
}
else if(S17_flag==1&t_canshu>10)
{
S17_flag=0;
t_canshu--;
if(cs_bu)
{
t_canshu=t_canshu+cs_bu;
cs_bu=0;
}
}
}
}
}
2.DS18B20
DS18B20.C
#include
#include
sbit DQ = P1^4; //?????
void Delay_OneWire(unsigned int t) //STC89C52RC
{
char z;
while(t--)
for(z=0;z<12;z++);
}
void Delay200us() //@12.000MHz
{
unsigned char i, j;
i = 3;
j = 82;
do
{
while (--j);
} while (--i);
}
//??????DS18B20?????
void Write_DS18B20(unsigned char dat)
{
unsigned char i;
for(i=0;i<8;i++)
{
DQ = 0;
DQ = dat&0x01;
Delay_OneWire(5);
DQ = 1;
dat >>= 1;
}
Delay_OneWire(5);
}
//?DS18B20??????
unsigned char Read_DS18B20(void)
{
unsigned char i;
unsigned char dat;
for(i=0;i<8;i++)
{
DQ = 0;
dat >>= 1;
DQ = 1;
if(DQ)
{
dat |= 0x80;
}
Delay_OneWire(5);
}
return dat;
}
//DS18B20?????
bit init_ds18b20(void)
{
bit initflag = 0;
DQ = 1;
Delay_OneWire(12);
DQ = 0;
Delay_OneWire(80);
DQ = 1;
Delay_OneWire(10);
initflag = DQ;
Delay_OneWire(5);
return initflag;
}
unsigned int read_temp()
{
unsigned int temp;
unsigned char low,high;
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0x44);
//Delay200us();
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0xbe);
low=Read_DS18B20();
high=Read_DS18B20();
temp=(high<<8)+low;
return temp;
}
3.DS1302.C
#include
#include
#include
sbit SCK=P1^7;
sbit SDA=P2^3;
sbit RST = P1^3; // DS1302??
void Write_Ds1302(unsigned char temp)
{
unsigned char i;
for (i=0;i<8;i++)
{
SCK=0;
SDA=temp&0x01;
temp>>=1;
SCK=1;
}
}
void Write_Ds1302_Byte( unsigned char address,unsigned char dat )
{
RST=0; _nop_();
SCK=0; _nop_();
RST=1; _nop_();
Write_Ds1302(address);
Write_Ds1302(dat);
RST=0;
}
unsigned char Read_Ds1302_Byte ( unsigned char address )
{
unsigned char i,temp=0x00;
RST=0; _nop_();
SCK=0; _nop_();
RST=1; _nop_();
Write_Ds1302(address);
for (i=0;i<8;i++)
{
SCK=0;
temp>>=1;
if(SDA)
temp|=0x80;
SCK=1;
}
RST=0; _nop_();
SCK=0; _nop_();
SCK=1; _nop_();
SDA=0; _nop_();
SDA=1; _nop_();
return (temp);
}
void w_sfm(int shi,int fen,int miao)
{
Write_Ds1302_Byte(0x8e,0x00);
Write_Ds1302_Byte(0x84,(shi/10)*16+shi%10);
Write_Ds1302_Byte(0x82,(fen/10)*16+fen%10);
Write_Ds1302_Byte(0x80,(miao/10)*16+miao%10);
Write_Ds1302_Byte(0x8e,0x80);
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)