第八届“蓝桥杯”单片机省赛——程序设计题
个人编写,代码仅供参考
如有不足,多多指教
1.题目
2.代码
main.c程序
#include "reg52.h"
#include "ds1302.h"
#include "onewire.h"
#include "intrins.h"
//=============================================参数定义
uchar code smg[] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf,0XFf,0xc6};
// 0 1 2 3 4 5 6 7 8 9 - mie C
uchar write[7] = {0x80,0x82,0x84,0x86,0x88,0x8a,0x8c};
uchar read[7] = {0x81,0x83,0x85,0x87,0x89,0x8b,0x8d};
uchar time[7] = {0x50,0x59,0x23,0x00,0x00,0x00,0x00};//写入23时59分50秒
sfr AUXR = 0X8E;
sbit S4 = P3^3;
sbit S5 = P3^2;
sbit S6 = P3^1;
sbit S7 = P3^0;
#define uchar unsigned char
#define uint unsigned int
uchar shizhongshezhi_flag = 0;//时钟显示界面开 0 小时设置 1 分钟设置 2 秒设置 3 闹钟小时设置 4 闹钟分钟设置 5 闹钟秒设置 6
uchar wengduxianshi_flag = 0; //温度显示关0 温度显示开1
uchar alarm_flag = 0;//闹钟响标志位
uchar alarm_sec = 0;//闹钟
uchar alarm_min = 0;
uchar alarm_hour = 0;
uint led_count = 0;//用于led闪烁
uchar five_sec = 0;//用于计时 5s led闪烁
uint count = 0;//用于定时器
uchar flag = 1;//用于闪烁选中的数码管
unsigned short tempr;//存温度
//===============================================函数定义
void select(uchar channel);
void cls_relay_buz_led();
void smg_delay(uint time);
void smg_display(uchar pos,uchar val);
void smg_dis_all(uchar val);
void key_scan();
void smg_show();
void check_alarm();
void Timer0Init(void); //1??@11.0592MHz
void server_timer0();
void ds1302_settime();
void ds1302_readtime();
void led_show();
//===============================================初始化函数
void select(uchar channel)
{
switch(channel)
{
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) | 0X00;;
break;
}
}
void cls_relay_buz_led()
{
select(4);
P0 = 0XFF;
select(5);
P0 &= 0XAF;
select(0);
}
//==========================================定时器
void Timer0Init(void) //1??@11.0592MHz
{
AUXR &= 0x7F; //?????12T??
TMOD &= 0xF0; //???????
TL0 = 0x66; //???????
TH0 = 0xFC; //???????
TF0 = 0; //??TF0??
ET0 = 1;
EA = 1;
TR0 = 1; //???0????
}
void server_timer0() interrupt 1
{
count++;
if(count == 1000)
{
count = 0;
flag = ~flag;
}
if(alarm_flag == 1)
{
led_count++;
if(led_count == 200)
{
led_count = 0;
five_sec++;
if(five_sec == 25)
{
five_sec = 0;
alarm_flag = 0;
}
}
}
}
//===========================================按键扫描函数
void key_scan()
{
if(S7 == 0)
{
smg_show();
if(S7 == 0)
{
if(alarm_flag == 0)
{
while(S7 == 0)
{
smg_show();
}
switch (shizhongshezhi_flag)
{
case 0: shizhongshezhi_flag = 1;
break;
case 1: shizhongshezhi_flag = 2;
break;
case 2: shizhongshezhi_flag = 3;
break;
case 3: shizhongshezhi_flag = 0;
break;
}
}
else
{
while(S7 == 0)
{
smg_show();
}
alarm_flag = 0;
}
}
}
else if(S6 == 0)
{
smg_show();
if(S6 == 0)
{
if(alarm_flag == 0)
{
while(S6 == 0)
{
smg_show();
}
switch (shizhongshezhi_flag)
{
case 0: shizhongshezhi_flag = 4;
break;
case 4: shizhongshezhi_flag = 5;
break;
case 5: shizhongshezhi_flag = 6;
break;
case 6: shizhongshezhi_flag = 0;
break;
}
}
else
{
while(S6 == 0)
{
smg_show();
}
alarm_flag = 0;
}
}
}
else if(S5 == 0)
{
smg_show();
if(S5 == 0)
{
if(alarm_flag == 0)
{
while(S5 == 0)
{
smg_show();
}
if(shizhongshezhi_flag != 0)
{
switch(shizhongshezhi_flag)
{
case 1:if(((time[2]/16)*10+time[2]%16) <= 22)
{
if(time[2] == 0x09)
{
Write_Ds1302_Byte(0x8e,0x00);
Write_Ds1302_Byte(write[2],0x10);
Write_Ds1302_Byte(0x8e,0x80);
}
else if(time[2] == 0x19)
{
Write_Ds1302_Byte(0x8e,0x00);
Write_Ds1302_Byte(write[2],0x20);
Write_Ds1302_Byte(0x8e,0x80);
}
else
{
Write_Ds1302_Byte(0x8e,0x00);
Write_Ds1302_Byte(write[2],time[2]+0x01);
Write_Ds1302_Byte(0x8e,0x80);
}
}
else
{
Write_Ds1302_Byte(0x8e,0x00);
Write_Ds1302_Byte(write[2],0x00);
Write_Ds1302_Byte(0x8e,0x80);
}break;
case 2:if(((time[1]/16)*10+time[1]%16)<=58)
{
if(time[1] == 0x09)
{
Write_Ds1302_Byte(0x8e,0x00);
Write_Ds1302_Byte(write[1],0x10);
Write_Ds1302_Byte(0x8e,0x80);
}
else if(time[1] == 0x19)
{
Write_Ds1302_Byte(0x8e,0x00);
Write_Ds1302_Byte(write[1],0x20);
Write_Ds1302_Byte(0x8e,0x80);
}
else if(time[1] == 0x29)
{
Write_Ds1302_Byte(0x8e,0x00);
Write_Ds1302_Byte(write[1],0x30);
Write_Ds1302_Byte(0x8e,0x80);
}
else if(time[1] == 0x39)
{
Write_Ds1302_Byte(0x8e,0x00);
Write_Ds1302_Byte(write[1],0x40);
Write_Ds1302_Byte(0x8e,0x80);
}
else if(time[1] == 0x49)
{
Write_Ds1302_Byte(0x8e,0x00);
Write_Ds1302_Byte(write[1],0x50);
Write_Ds1302_Byte(0x8e,0x80);
}
else
{
Write_Ds1302_Byte(0x8e,0x00);
Write_Ds1302_Byte(write[1],time[1]+0x01);
Write_Ds1302_Byte(0x8e,0x80);
}
}
else
{
Write_Ds1302_Byte(0x8e,0x00);
Write_Ds1302_Byte(write[1],0x00);
Write_Ds1302_Byte(0x8e,0x80);
}break;
case 3:if(((time[0]/16)*10+time[0]%16)<=58)
{
if(time[0] == 0x09)
{
Write_Ds1302_Byte(0x8e,0x00);
Write_Ds1302_Byte(write[0],0x10);
Write_Ds1302_Byte(0x8e,0x80);
}
else if(time[0] == 0x19)
{
Write_Ds1302_Byte(0x8e,0x00);
Write_Ds1302_Byte(write[0],0x20);
Write_Ds1302_Byte(0x8e,0x80);
}
else if(time[0] == 0x29)
{
Write_Ds1302_Byte(0x8e,0x00);
Write_Ds1302_Byte(write[0],0x30);
Write_Ds1302_Byte(0x8e,0x80);
}
else if(time[0] == 0x39)
{
Write_Ds1302_Byte(0x8e,0x00);
Write_Ds1302_Byte(write[0],0x40);
Write_Ds1302_Byte(0x8e,0x80);
}
else if(time[0] == 0x49)
{
Write_Ds1302_Byte(0x8e,0x00);
Write_Ds1302_Byte(write[0],0x50);
Write_Ds1302_Byte(0x8e,0x80);
}
else
{
Write_Ds1302_Byte(0x8e,0x00);
Write_Ds1302_Byte(write[0],time[0]+0x01);
Write_Ds1302_Byte(0x8e,0x80);
}
}
else
{
Write_Ds1302_Byte(0x8e,0x00);
Write_Ds1302_Byte(write[0],0x00);
Write_Ds1302_Byte(0x8e,0x80);
}break;
case 4:if(alarm_hour<=22){alarm_hour++;}else{alarm_hour = 0;}break;
case 5:if(alarm_min<=58){alarm_min++;}else{alarm_min = 0;}break;
case 6:if(alarm_sec<=58){alarm_sec++;}else{alarm_sec = 0;}break;
}
}
}
else
{
while(S5 == 0)
{
smg_show();
}
alarm_flag = 0;
}
}
}
else if(S4 == 0)
{
smg_show();
if(S4 == 0)
{
if(alarm_flag == 0)
{
if(shizhongshezhi_flag != 0)
{
while(S4 == 0)
{
smg_show();
}
switch(shizhongshezhi_flag)
{
case 1:if(((time[2]/16)*10+time[2]%16)>=1)
{
if(time[2] == 0x10)
{
Write_Ds1302_Byte(0x8e,0x00);
Write_Ds1302_Byte(write[2],0x09);
Write_Ds1302_Byte(0x8e,0x80);
}
else if(time[2] == 0x20)
{
Write_Ds1302_Byte(0x8e,0x00);
Write_Ds1302_Byte(write[2],0x19);
Write_Ds1302_Byte(0x8e,0x80);
}
else
{
Write_Ds1302_Byte(0x8e,0x00);
Write_Ds1302_Byte(write[2],time[2]-0x01);
Write_Ds1302_Byte(0x8e,0x80);
}
}
else
{
Write_Ds1302_Byte(0x8e,0x00);
Write_Ds1302_Byte(write[2],0x23);
Write_Ds1302_Byte(0x8e,0x80);
}break;
case 2:if(((time[1]/16)*10+time[1]%16)>=1)
{
if(time[1] == 0x10)
{
Write_Ds1302_Byte(0x8e,0x00);
Write_Ds1302_Byte(write[1],0x09);
Write_Ds1302_Byte(0x8e,0x80);
}
else if(time[1] == 0x20)
{
Write_Ds1302_Byte(0x8e,0x00);
Write_Ds1302_Byte(write[1],0x19);
Write_Ds1302_Byte(0x8e,0x80);
}
else if(time[1] == 0x30)
{
Write_Ds1302_Byte(0x8e,0x00);
Write_Ds1302_Byte(write[1],0x29);
Write_Ds1302_Byte(0x8e,0x80);
}
else if(time[1] == 0x40)
{
Write_Ds1302_Byte(0x8e,0x00);
Write_Ds1302_Byte(write[1],0x39);
Write_Ds1302_Byte(0x8e,0x80);
}
else if(time[1] == 0x50)
{
Write_Ds1302_Byte(0x8e,0x00);
Write_Ds1302_Byte(write[1],0x49);
Write_Ds1302_Byte(0x8e,0x80);
}
else
{
Write_Ds1302_Byte(0x8e,0x00);
Write_Ds1302_Byte(write[1],time[1]-0x01);
Write_Ds1302_Byte(0x8e,0x80);
}
}
else
{
Write_Ds1302_Byte(0x8e,0x00);
Write_Ds1302_Byte(write[1],0x59);
Write_Ds1302_Byte(0x8e,0x80);
}break;
case 3:if(((time[0]/16)*10+time[0]%16)>=1)
{
if(time[0] == 0x10)
{
Write_Ds1302_Byte(0x8e,0x00);
Write_Ds1302_Byte(write[0],0x09);
Write_Ds1302_Byte(0x8e,0x80);
}
else if(time[0] == 0x20)
{
Write_Ds1302_Byte(0x8e,0x00);
Write_Ds1302_Byte(write[0],0x19);
Write_Ds1302_Byte(0x8e,0x80);
}
else if(time[0] == 0x30)
{
Write_Ds1302_Byte(0x8e,0x00);
Write_Ds1302_Byte(write[0],0x29);
Write_Ds1302_Byte(0x8e,0x80);
}
else if(time[0] == 0x40)
{
Write_Ds1302_Byte(0x8e,0x00);
Write_Ds1302_Byte(write[0],0x39);
Write_Ds1302_Byte(0x8e,0x80);
}
else if(time[0] == 0x50)
{
Write_Ds1302_Byte(0x8e,0x00);
Write_Ds1302_Byte(write[0],0x49);
Write_Ds1302_Byte(0x8e,0x80);
}
else
{
Write_Ds1302_Byte(0x8e,0x00);
Write_Ds1302_Byte(write[0],time[0]-0x01);
Write_Ds1302_Byte(0x8e,0x80);
}
}
else
{
Write_Ds1302_Byte(0x8e,0x00);
Write_Ds1302_Byte(write[0],0x59);
Write_Ds1302_Byte(0x8e,0x80);
}break;
case 4:if(alarm_hour>=1){alarm_hour--;}else{alarm_hour = 23;}break;
case 5:if(alarm_min>=1){alarm_min--;}else{alarm_min = 59;}break;
case 6:if(alarm_sec>=1){alarm_sec--;}else{alarm_sec = 59;}break;
}
}
if(shizhongshezhi_flag == 0)
{
while(S4 == 0)
{
wengduxianshi_flag = 1;
smg_show();
}
wengduxianshi_flag = 0;
}
}
}
else
{
while(S4 == 0)
{
smg_show();
}
alarm_flag = 0;
}
}
}
//=================================================数码管显示函数
void smg_delay(uint time)
{
while(time--);
}
void smg_display(uchar pos,uchar val)
{
select(7);
P0 = 0xff;
select(6);
P0 = 0X01 << pos;
select(7);
P0 = smg[val];
select(0);
smg_delay(500);
}
void smg_dis_all(uchar val)
{
select(7);
P0 = 0XFF;
select(6);
P0 = 0XFF;
select(7);
P0 = smg[val];
select(0);
smg_delay(500);
}
void smg_show_hour()
{
if(shizhongshezhi_flag == 1)
{
uchar hour = ((time[2]/16)*10+time[2]%16);
Write_Ds1302_Byte(0x80,time[2]|0x80);//停止计时小时位
if(flag == 1)
{
smg_display(1,hour%10);
smg_display(0,hour/10);
}
else
{
smg_display(1,11);
smg_display(0,11);
}
}
else if(shizhongshezhi_flag == 4)
{
if(flag == 1)
{
smg_display(1,alarm_hour%10);
smg_display(0,alarm_hour/10);
}
else
{
smg_display(1,11);
smg_display(0,11);
}
}
else if(shizhongshezhi_flag != 1 && shizhongshezhi_flag != 4)
{
if(shizhongshezhi_flag == 2 || shizhongshezhi_flag == 3 || shizhongshezhi_flag == 0)
{
uchar hour = ((time[2]/16)*10+time[2]%16);
smg_display(1,hour%10);
smg_display(0,hour/10);
}
else if(shizhongshezhi_flag == 5 || shizhongshezhi_flag == 6)
{
smg_display(1,alarm_hour%10);
smg_display(0,alarm_hour/10);
}
}
}
void smg_show_min()
{
if(shizhongshezhi_flag == 2)
{
uchar min = ((time[1]/16)*10+time[1]%16);
Write_Ds1302_Byte(0x80,time[1]|0x80);//停止计时分钟位
if(flag == 1)
{
smg_display(4,min%10);
smg_display(3,min/10);
}
else
{
smg_display(4,11);
smg_display(3,11);
}
}
else if(shizhongshezhi_flag == 5)
{
if(flag == 1)
{
smg_display(4,alarm_min%10);
smg_display(3,alarm_min/10);
}
else
{
smg_display(4,11);
smg_display(3,11);
}
}
else if(shizhongshezhi_flag != 2 && shizhongshezhi_flag != 5)
{
if(shizhongshezhi_flag == 1 || shizhongshezhi_flag == 3 || shizhongshezhi_flag == 0)
{
uchar min = ((time[1]/16)*10+time[1]%16);
smg_display(4,min%10);
smg_display(3,min/10);
}
else if(shizhongshezhi_flag == 4 || shizhongshezhi_flag == 6)
{
smg_display(4,alarm_min%10);
smg_display(3,alarm_min/10);
}
}
}
void smg_show_sec()
{
if(shizhongshezhi_flag == 3)
{
uchar sec = ((time[0]/16)*10+time[0]%16);
Write_Ds1302_Byte(0x80,time[0]|0x80);//停止计时分钟位
if(flag == 1)
{
smg_display(7,sec%10);
smg_display(6,sec/10);
}
else
{
smg_display(7,11);
smg_display(6,11);
}
}
else if(shizhongshezhi_flag == 6)
{
if(flag == 1)
{
smg_display(7,alarm_sec%10);
smg_display(6,alarm_sec/10);
}
else
{
smg_display(7,11);
smg_display(6,11);
}
}
else if(shizhongshezhi_flag != 3 && shizhongshezhi_flag != 6)
{
if(shizhongshezhi_flag == 1 || shizhongshezhi_flag == 2 || shizhongshezhi_flag == 0)
{
uchar sec = ((time[0]/16)*10+time[0]%16);
smg_display(7,sec%10);
smg_display(6,sec/10);
}
else if(shizhongshezhi_flag == 4 || shizhongshezhi_flag == 5)
{
smg_display(7,alarm_sec%10);
smg_display(6,alarm_sec/10);
}
}
}
void smg_show()
{
if(wengduxianshi_flag == 0)
{
smg_show_sec();
smg_display(5,10);
smg_show_min();
smg_display(2,10);
smg_show_hour();
smg_dis_all(0xff);
}
else
{
smg_display(7,12);
tempr = rd_tempreture_zhengshu();
smg_display(6,tempr%10);
smg_display(5,tempr/10);
smg_display(4,11);
smg_display(3,11);
smg_display(2,11);
smg_display(1,11);
smg_display(0,11);
smg_dis_all(0xff);
}
}
//==========================================检查是否到闹钟
void check_alarm()
{
if(alarm_hour == ((time[2]/16)*10+time[2]%16) && alarm_min == ((time[1]/16)*10+time[1]%16) && alarm_sec == ((time[0]/16)*10+time[0]%16) && shizhongshezhi_flag == 0)
{
alarm_flag = 1;
}
}
//=======================================写入和读出时间函数
void ds1302_settime()
{
char i;
Write_Ds1302_Byte(0x8e,0x00);
for(i = 0; i < 7; i++)
{
Write_Ds1302_Byte(write[i],time[i]);
}
Write_Ds1302_Byte(0x8e,0x80);
}
void ds1302_readtime()
{
char i;
for(i = 0; i < 7;i++)
{
time[i] = Read_Ds1302_Byte(read[i]);
}
}
//=======================================LED函数
void led_show()
{
if(alarm_flag == 1)
{
if(five_sec % 2 == 0)
{
select(4);
P0 = 0xff;
select(0);
}
else if(five_sec % 2 == 1)
{
select(4);
P0 = 0XFE;
select(0);
}
}
else if(alarm_flag == 0)
{
select(4);
P0 = 0XFF;
select(0);
}
}
//===================================主函数
void main()
{
cls_relay_buz_led();
ds1302_settime();
Timer0Init(); //1??@11.0592MHz
while(1)
{
ds1302_readtime();
smg_show();
check_alarm();
key_scan();
led_show();
}
}
ds1302.c 程序
/*
程序说明: DS1302驱动程序
软件环境: Keil uVision 4.10
硬件环境: CT107单片机综合实训平台 8051,12MHz
日 期: 2011-8-9
*/
#include <reg52.h>
#include <intrins.h>
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);
}
ds1302.h程序
#ifndef __DS1302_H
#define __DS1302_H
void Write_Ds1302(unsigned char temp);
void Write_Ds1302_Byte( unsigned char address,unsigned char dat );
unsigned char Read_Ds1302_Byte( unsigned char address );
#endif
onewire.c程序
/*
程序说明: 单总线驱动程序
软件环境: Keil uVision 4.10
硬件环境: CT107单片机综合实训平台(外部晶振12MHz) STC89C52RC单片机
日 期: 2011-8-9
*/
#include "reg52.h"
sbit DQ = P1^4; //单总线接口
uchar i;
//单总线延时函数
void Delay_OneWire(unsigned int t) //STC89C52RC
{
while(t--);
for(i=0;i<12;i++);
}
//通过单总线向DS18B20写一个字节
void Write_DS18B20(unsigned char dat)
{
unsigned char i;
for(i=0;i<8;i++)
{
DQ = 0;
DQ = dat&0x01;
Delay_OneWire(20);//原来是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(20);//原来是5
}
return dat;
}
//DS18B20设备初始化
bit init_ds18b20(void)
{
bit initflag = 0;
// DQ = 1;
// Delay_OneWire(12);
DQ = 0;
Delay_OneWire(200);//原来是80
DQ = 1;
Delay_OneWire(20); //原来是10
initflag = DQ;
Delay_OneWire(100);//原来是5
return initflag;
}
//==============================温度读取函数(小数)
unsigned short rd_temperature_xiaoshu()
{
unsigned short temp = 0x0000;
uchar msb,lsb;
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0x44);
Delay_OneWire(200);
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0xbe);
lsb = Read_DS18B20();
msb = Read_DS18B20();
init_ds18b20();
temp = msb;
temp = (temp << 8)|lsb;
if((temp&0xf800)==0x0000)
{
temp>>=4;
temp=temp*10;
temp=temp+(lsb &0x0f)*0.625;
}
return temp;
}
//===========================================温度读取函数(整数)
unsigned short rd_tempreture_zhengshu()
{
unsigned short temp = 0x0000;
uchar msb,lsb;
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0x44);
Delay_OneWire(200);
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0xbe);
lsb = Read_DS18B20();
msb = Read_DS18B20();
init_ds18b20();
temp = msb;
temp = (temp << 8)|lsb;
if((temp&0xf800)==0x0000)
{
temp>>=4;
}
return temp;
}
onewire.h程序
#ifndef __ONEWIRE_H
#define __ONEWIRE_H
unsigned short rd_temperature_xiaoshu(void); //; ;
unsigned short rd_tempreture_zhengshu();
bit init_ds18b20(void);
unsigned char Read_DS18B20(void);
void Write_DS18B20(unsigned char dat);
void Delay_OneWire(unsigned int t); //STC89C52RC
#endif