蓝桥杯--第八届省赛试题-电子钟程序设计

蓝桥杯—第八届省赛试题-电子钟程序设计

题目:

蓝桥杯--第八届省赛试题-电子钟程序设计_第1张图片
蓝桥杯--第八届省赛试题-电子钟程序设计_第2张图片
蓝桥杯--第八届省赛试题-电子钟程序设计_第3张图片
蓝桥杯--第八届省赛试题-电子钟程序设计_第4张图片

思路

1.显示温度使用大赛提供的onewire.c,只需自己添加读取函数。
2.设计时钟程序,或者使用DS1302.c(这里使用的是自己写的函数)
3.键扫描函数
4.进行逻辑判断,都是if语句。哪个按键和模式同时满足,执行相应的操作。
代码中注释很清楚相信大家能看懂

main.c

#include 
#include 
#define uchar unsigned char
#define uint unsigned int
code unsigned char tab[] = {
      0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90, 0xff, 0xbf };
uchar dspbuf[8] = {
      10, 10, 10, 10, 10, 10, 10, 10 };
uchar temper_flag = 0;	//温度扫描标志位
uchar hour, minute, sec, alarmHour, alarmMinute, alarmSec; 		//时分秒 闹钟的时分秒
uint count = 0, miao = 0;		//count == 25则为5秒, miao == 200 0.2秒
uchar temperature, key_value;		//温度数值 ;键扫描返回值
uchar mode = 0;	//模式
uchar tt = 0;	//显示转变温度的标志
uchar tn = 0;	//时钟和闹钟转换的标志位
uchar ledshan = 0, ledflag = 0; 		//时钟 == 闹钟,led闪烁

void display();
void init();
unsigned char read_key();
void load();
void tempera();
//关闭蜂鸣器和LED灯
void cls_buzz()
{
     
	P2 = (P2 & 0x1f) | 0xa0;
	P0 = 0x00;
	P2 = 0x1f;
}

void cls_led()
{
     
	P2 = (P2 & 0x1f) | 0x80;
	P0 = 0xff;
	P2 = 0x1f;
}

//主函数
void main()
{
     
	cls_buzz();
	cls_led();
	AUXR = 0x80;
	TMOD = 0xf0;
	TL0 = 0xcd;
	TH0 = 0xd4;
	TF0 = 0;
	TR0 = 1;
	ET0 = 1;
	EA = 1;
	init();
	while (1)
	{
     
		if (temper_flag)
		{
     
			temper_flag = 0;
			temperature = rd_temperature();  //读温度         
		}
		key_value = read_key();
	}
}
//温度赋值函数
void tempera()
{
     
	uchar i;
	for (i = 0; i < 6; i++)
		dspbuf[i] = 10;
	dspbuf[7] = temperature % 10;
	dspbuf[6] = (temperature - dspbuf[7]) / 10;
}

void time0(void) interrupt 1
{
     
	static unsigned char intr;
	if (++intr == 10)  //1ms执行一次
	{
     
		intr = 0;
		temper_flag = 1;  //10ms温度读取标志位置1
	}

	if (count == 1000)		//1s
	{
     
		sec++;
		count = 0;
		miao = (miao + 1) % 2;
	}
	count += 1;
//时钟时间限制条件
	if (sec == 60) {
     	
		sec = 0;
		minute += 1;
	}
	if (minute == 60) {
     
		minute = 0;
		hour += 1;
	}
	if (hour == 24) {
     
		hour = 0;
	}


//闹钟时间限制条件
	if (alarmSec == 60) {
     
		alarmSec = 0;
		alarmMinute += 1;
	}
	if (alarmMinute == 60) {
     
		alarmMinute = 0;
		alarmHour += 1;
	}
	if (alarmHour == 24) {
     
		alarmHour = 0;
	}

	if (ledshan == 200)
	{
     
		ledshan = 0;
		ledflag = (ledflag + 1) % 2;
	}
	ledshan += 1;
	display();
}

void load()
{
     
		if (key_value == 7) {
     
			mode += 1;
		}
	if (tt == 0) {
      	
		if (tn == 0)
		{
     
			//模式1段码值赋值
			if (mode == 1)
			{
     
				dspbuf[2] = dspbuf[5] = 11;
				dspbuf[1] = hour % 10;
				dspbuf[0] = hour / 10;

				dspbuf[4] = minute % 10;
				dspbuf[3] = minute / 10;
				if (miao == 1)
					dspbuf[6] = dspbuf[7] = 10;
				else
				{
     
					dspbuf[7] = sec % 10;
					dspbuf[6] = sec / 10;
				}
			}

			if (mode == 2)
			{
     
				dspbuf[2] = dspbuf[5] = 11;
				dspbuf[1] = hour % 10;
				dspbuf[0] = hour / 10;
				dspbuf[7] = sec % 10;
				dspbuf[6] = sec / 10;
				if (miao == 1)
					dspbuf[3] = dspbuf[4] = 10;
				else
				{
     
					dspbuf[4] = minute % 10;
					dspbuf[3] = minute / 10;
				}
			}
			if (mode == 3)
			{
     
				dspbuf[2] = dspbuf[5] = 11;

				dspbuf[4] = minute % 10;
				dspbuf[3] = minute / 10;

				dspbuf[7] = sec % 10;
				dspbuf[6] = sec / 10;
				if (miao == 1)
					dspbuf[1] = dspbuf[0] = 10;
				else
				{
     
					dspbuf[1] = hour % 10;
					dspbuf[0] = hour / 10;
				}
			}

			//时钟+设置
			if (key_value == 4 && mode == 1)
			{
     
				key_value = 0;
				sec += 1;
			}
			if (key_value == 4 && mode == 2)
			{
     
				key_value = 0;
				minute++;
			}
			if (key_value == 4 && mode == 3)
			{
     
				key_value = 0;
				hour += 1;
			}
			//时钟-设置
			if (key_value == 5 && mode == 1)
			{
     
				key_value = 0;
				if (sec == 0)
					sec = 59;
				else
					sec -= 1;
			}
			if (key_value == 5 && mode == 2)
			{
     
				key_value = 0;
				if (minute == 0)
					minute = 59;
				else
					minute--;
			}
			if (key_value == 5 && mode == 3)
			{
     
				key_value = 0;
				if (hour == 0)
					hour = 23;
				else
					hour -= 1;
			}
			if (mode == 0) {
     
				dspbuf[2] = dspbuf[5] = 11;
				dspbuf[1] = hour % 10;
				dspbuf[0] = hour / 10;

				dspbuf[4] = minute % 10;
				dspbuf[3] = minute / 10;

				dspbuf[7] = sec % 10;
				dspbuf[6] = sec  / 10;
			}

		}
		if (tn == 1)		//时钟转闹钟标志位
		{
     
			if (mode == 1)
			{
     
				dspbuf[2] = dspbuf[5] = 11;
				dspbuf[1] = alarmHour % 10;
				dspbuf[0] = alarmHour / 10;

				dspbuf[4] = alarmMinute % 10;
				dspbuf[3] = alarmMinute / 10;
				if (miao == 1)
					dspbuf[6] = dspbuf[7] = 10;
				else
				{
     
					dspbuf[7] = alarmSec % 10;
					dspbuf[6] = alarmSec / 10;
				}
			}

			if (mode == 2)
			{
     
				dspbuf[2] = dspbuf[5] = 11;
				dspbuf[1] = alarmHour % 10;
				dspbuf[0] = alarmHour / 10;
				dspbuf[7] = alarmSec % 10;
				dspbuf[6] = alarmSec / 10;
				if (miao == 1)
					dspbuf[3] = dspbuf[4] = 10;
				else
				{
     
					dspbuf[4] = alarmMinute % 10;
					dspbuf[3] = alarmMinute / 10;
				}
			}
			if (mode == 3)
			{
     
				dspbuf[2] = dspbuf[5] = 11;

				dspbuf[4] = alarmMinute % 10;
				dspbuf[3] = alarmMinute / 10;

				dspbuf[7] = alarmSec % 10;
				dspbuf[6] = alarmSec / 10;
				if (miao == 1)
					dspbuf[1] = dspbuf[0] = 10;
				else
				{
     
					dspbuf[1] = alarmHour % 10;
					dspbuf[0] = alarmHour / 10;
				}
			}


			if (key_value == 4 && mode == 1)
			{
     
				key_value = 0;
				alarmSec += 1;
			}
			if (key_value == 4 && mode == 2)
			{
     
				key_value = 0;
				alarmMinute++;
			}
			if (key_value == 4 && mode == 3)
			{
     
				key_value = 0;
				alarmHour += 1;
			}

			if (key_value == 5 && mode == 1)
			{
     
				key_value = 0;
				if (alarmSec == 0)
					alarmSec = 59;
				else
					alarmSec -= 1;
			}
			if (key_value == 5 && mode == 2)
			{
     
				key_value = 0;
				if (alarmMinute == 0)
					alarmMinute = 59;
				else
					alarmMinute--;
			}
			if (key_value == 5 && mode == 3)
			{
     
				key_value = 0;
				if (alarmHour == 0)
					alarmHour = 23;
				else
					alarmHour -= 1;
			}

			if (mode == 0) {
     
				dspbuf[2] = dspbuf[5] = 11;
				dspbuf[1] = alarmHour % 10;
				dspbuf[0] = alarmHour / 10;

				dspbuf[4] = alarmMinute % 10;
				dspbuf[3] = alarmMinute  / 10;

				dspbuf[7] = alarmSec % 10;
				dspbuf[6] = alarmSec / 10;
			}
		}
		if (hour == alarmHour && minute == alarmMinute && sec <= (alarmSec + 4))		//到达闹钟时间led闪烁五秒
		{
     

			if (ledflag == 1)		// == 1 L1亮
			{
     
				P2 = (P2 & 0x1f) | 0x80;
				P0 = 0xfe;
				P2 = 0x1f;
			}
			else
			{
     
				P2 = (P2 & 0x1f) | 0x80;
				P0 = 0xff;
				P2 = 0x1f;
			}
		}
		else
		{
     
			P2 = (P2 & 0x1f) | 0x80;
			P0 = 0xff;
			P2 = 0x1f;
		}
	}
	else if (mode == 0 && key_value == 4 && tt == 1)	//条件满足显示温度
		{
     
			tempera();
		}
	
	if (mode > 3) mode = 0; //模式转换
}

void init()	//初始赋值 时分秒 闹钟时分秒
{
     
	hour = 23;
	minute = 59;
	sec = 50;
	alarmHour = 0;
	alarmMinute = 0;
	alarmSec = 0;
}
//键扫描
unsigned char read_key(void)
{
     	//state0:充当消抖 state1:有按键按下 state2:检查按键是否松开
	uchar temp;
	static unsigned char state = 0;
	uchar key_value = 0;
	temp = P3 & 0x0f;
	switch (state)
	{
     
	case 0:
	{
     
		if (P3 != 0x0f) //有按键按下
			state = 1;
	}break;
	case 1:
	{
     
		if (P3 != 0x0f) {
     
			switch (temp)
			{
     
			case 0x0e:
			{
     
				key_value = 7; state = 2;  //S7
			}break;
			case 0x0d:
			{
     
				key_value = 6; state = 2;						//S6
				tn = (tn + 1) % 2;
			}break;
			case 0x0b:
			{
     
				key_value = 5; state = 2;			//S5
			}break;
			case 0x07:
			{
     
				key_value = 4; state = 2; 						//S4 
				if(mode == 0)
					tt = 1;
			}break;
			}
		}
		else
			state = 0;

	}break;
	case 2:
	{
     
		if (P3 == 0xff)
		{
     
			tt = 0;
			state = 0;
			key_value = 0;
		}
	}break;
	}
	return key_value;
}

//显示函数
void display()
{
     
	static unsigned char dspcom = 0;
	load();
	P2 = (P2 & 0x1f) | 0xe0;	//消影
	P0 = 0xff;
	P2 = 0x1f;

	P2 = (P2 & 0x1f) | 0xc0;	//位选
	P0 = 1 << dspcom;
	P2 = 0x1f;

	P2 = (P2 & 0x1f) | 0xe0;	//显示
	P0 = tab[dspbuf[dspcom]];
	P2 = 0x1f;

	if (++dspcom == 8) dspcom = 0;

}

onewire.h

#include "reg52.h"

sbit DQ = P1^4;  //单总线接口

//单总线延时函数
void Delay_OneWire(unsigned int t)  
{
     
	unsigned char i;
	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(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 char rd_temperature(void)
{
     
	unsigned char high,low;
	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;
}

感谢丁莉老师的指导!!!

你可能感兴趣的:(51单片机)