蓝桥杯第六届省赛试题——温度记录器

第六届的试题涉及到ds1302,ds18b20,led,数码管和独立按键。大致题目如下

蓝桥杯第六届省赛试题——温度记录器_第1张图片


我用了两个定时器,一个用于提示符的闪烁,一个用于采集温度传感器的值(其实用一个也可以),其他也...没啥难点吧,主要是会用这些模块。

#include
#include "iic.h"
#include "ds1302.h"
#include "onewire.h"
#define uchar unsigned char
sfr AUXR=0x8e;
sbit beep=P0^6;
sbit S4=P3^3;
sbit S5=P3^2;
sbit S6=P3^1;
sbit S7=P3^0;
uchar code tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf};
uchar a[10]={0,0,0,0,0,0,0,0,0,0};
uchar write[]={0x80,0x82,0x84,0x86,0x88,0x8a,0x8c};
uchar read[]={0x81,0x83,0x85,0x87,0x89,0x8b,0x8d};
uchar time[]={0x50,0x59,0x23,0x17,0x02,0x06,0x18};
uchar t=0,t1,t2=0;
uchar num=0,num1=0,jici=0,jici1=0;
void delay(int x)
{
while(x--);
}
void delay10ms(uchar x)
{
unsigned char i, j;
while(x--)
{
i = 117;
j = 184;
do
{
while (--j);
} while (--i);
}


}
void Timer0Init(void) //5毫秒@12.000MHz
{
AUXR |= 0x80; //定时器时钟1T模式
TMOD = 0x00; //设置定时器模式
TL1 = 0xA0; //设置定时初值
TH1 = 0x15; //设置定时初值
TL0 = 0xA0; //设置定时初值
TH0 = 0x15; //设置定时初值
TF0 = 0; //清除TF0标志
TF1=0;
TR0 = 1; //定时器0开始计时
TR1= 1;
ET0=0;
ET1=0;
EA=1;
}


void cls_buzz()
{
P2=(P2&0x1f)|0xa0;
beep=0;
P2&=0X1f;
}
void cls_LED(uchar x)
{
   P2=(P2&0x1f)|0x80;
P0=x;
P2&=0X1f;
}
void weixuan(uchar x)
{
P2=(P2&0x1f)|0xc0;
P0=x;
P2&=0X1F;
}
void duanxuan(uchar x)
{
P2=(P2&0x1f)|0xe0;
P0=tab[x];
P2&=0X1F;
}
void T0_TIMER() interrupt 1
{
uchar i=1;
num++;
if(num==200)
{
 while(i--)
 {
weixuan(0x20);
duanxuan(10);
delay(100);
weixuan(0x04);
duanxuan(10);
delay(100);
 }
 num=0;  
}
}


void ds1302()
{
uchar i;
Ds1302_Single_Byte_Write(0x8e,0x00);
for(i=0;i<7;i++)
{
Ds1302_Single_Byte_Write(write[i],time[i]);
}
Ds1302_Single_Byte_Write(0x8e,0x80);
}
void ds1302_read()
{
uchar n;
for(n=0;n<7;n++)
{
time[n]=Ds1302_Single_Byte_Read(read[n]);
}


}
void display1(uchar x)
{
weixuan(0x80);
P0=0XFF;
duanxuan(x%10);
P0=0xff;
delay(200);
weixuan(0x40);
P0=0xff;
duanxuan(x/10);
P0=0XFF;
delay(200);
weixuan(0x20);
P0=0xff;
duanxuan(10);
P0=0XFF;
delay(200);

}
void displaytime()
{
weixuan(0x80);
duanxuan(time[0]&0x0f);
delay(500);
weixuan(0x40);
duanxuan(time[0]/16);
delay(500);


 weixuan(0x10);
duanxuan(time[1]&0x0f);
delay(500);
weixuan(0x08);
duanxuan(time[1]/16);
delay(500);


weixuan(0x02);
duanxuan(time[2]&0x0f);
delay(500);
weixuan(0x01);
duanxuan(time[2]/16);
delay(500);

}
void key_scan()
{
if(S4==0)
{
while(S4==0);
t++;
if(t>=5)
t=1;
}
if(S5==0)
{
while(S5==0);
ds1302();
t=6;
}
if(S6==0)
{
   while(S6==0);
jici1++;
t=7;
cls_LED(0xff);
if(jici1==10)
{
jici1=0;
}
}
if(S7==0)
{
while(S7==0);
t=1;
}
}
void display2(uchar x,uchar y)
{
weixuan(0x80);
P0=0XFF;
duanxuan(x%10);
P0=0xff;
delay(1000);
weixuan(0x40);
P0=0xff;
duanxuan(x/10);
P0=0XFF;
delay(1000);
weixuan(0x20);
P0=0xff;
duanxuan(10);
P0=0XFF;
delay(1000);


weixuan(0x04);
P0=0XFF;
duanxuan(y%10);
P0=0xff;
delay(1000);
weixuan(0x02);
P0=0xff;
duanxuan(y/10);
P0=0XFF;
delay(1000);
weixuan(0x01);
P0=0xff;
duanxuan(10);
P0=0XFF;
delay(1000);

}
void displayt()
{
switch(t)
{
case 1:t1=1;display1(t1);break;
case 2:t1=5;display1(t1);break;
case 3:t1=30;display1(t1);break;
case 4:t1=60;display1(t1);break;
case 6:displaytime();
ds1302_read();
ET0=1;
ET1=1;
break;
case 7:;ET0=0;ET1=0;display2(a[jici1],jici1);break;
}
}
uchar ds18b20_read()
{
uchar low,high;
char temp;


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<<4;
temp|=(low>>4);


return temp;
}
void T1_TIMER() interrupt 3
{
num1++;
if(num1==20*t1)
{  
 
 a[jici]=ds18b20_read();
 jici++;
 if(jici==10)
 {
t=7;
cls_LED(0XFE);
jici=0;
 }
 num1=0;
}
}
void main()
{
uchar x;
cls_LED(0xff);
cls_buzz();
ds1302();
Timer0Init();
x=ds18b20_read();
while(1)
{  
 key_scan();
 displayt();  
}
}


你可能感兴趣的:(蓝桥杯)