第八届蓝桥杯单片机完整程序

  • Post author:
  • Post category:其他



声明:1、我发布程序旨在希望大家蓝桥杯单片机比赛可以取得好成绩




2、在看我程序之前希望大家先去B站观看完小蜜蜂老师的蓝桥杯单片机视频讲解


小蜜蜂视频链接:


【小蜜蜂笔记】蓝桥杯大赛-单片机设计与开发基础技能与进阶强化教程_哔哩哔哩_bilibili

本程序资源包采用第十三届蓝桥杯资源包



main.c

#include <reg52.h>
#include <ds1302.h>
#include <onewire.h>
sbit L1 = P0^0;
sbit S4 = P3^3;
sbit S5 = P3^2;
sbit S6 = P3^1;
sbit S7 = P3^0;
//变量区
float temp1;
signed char hour = 0;//时
signed char min = 0;//分
signed char sec = 0;//秒
unsigned char flag_s = 0;//设置时间标志位 
unsigned char flag_t = 0;//闪灭标志位
unsigned char flag_LED1 = 0;//LED闪灭标志位
unsigned char flag_LED = 0;//LED标志位
unsigned char count0 = 0;//定时器0
unsigned char count1 = 0;//0.2s
unsigned int count2 = 0;
unsigned char state = 1;//界面标志位
unsigned char Write_DS1302_Addr[3] = {0x80,0x82,0x84};
unsigned char Read_DS1302_addr[3] = {0x81,0x83,0x85};
signed char Timer[3] = {0x50,0x59,0x23};
unsigned char SMG_val[10] = {0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};
unsigned char temp;
//锁存器的选择
void SelectHC573(unsigned char 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;
	}
}
//数码管得选择
void Select_SMG(unsigned char pos,val)
{
	SelectHC573(7);
	P0 = 0xff;
	SelectHC573(6);
	P0 = 0x01 << pos;
	SelectHC573(7);
	P0 = val;
}
//关闭所有
void CloseAll()
{
	SelectHC573(6);
	P0 = 0xff;
	SelectHC573(7);
	P0 = 0xff;
}
//定时器0初始化
void Init_Timer0()
{
	TMOD = 0x01;
	TH0 = (65536 - 10000) / 256;
	TL0 = (65536 - 10000) % 256;
	ET0 = 1;
	EA = 1;
	TR0 = 1;
}
//定时器服务函数
void Service_T0() interrupt 1
{
	TH0 = (65536 - 10000) / 256;
	TL0 = (65536 - 10000) % 256;
	
	count0 ++;
	if(count0 == 100)
	{
		flag_t = !flag_t;
		count0 = 0;
	}
	if(flag_LED == 1)
	{
		count1 ++;
		count2 ++;
		if(count1 == 20)
		{
			flag_LED1 = !flag_LED1;
			count1 = 0;
		}
		if(count2 == 500)
		{
			flag_LED = 0;
			count2 = 0;
		}
	}
}
//DS1302初始化
void Init_DS1302()
{
	unsigned char i;
	Write_Ds1302_Byte(0x8e,0x00);
	for(i = 0;i < 3;i ++)
	{
		Write_Ds1302_Byte(Write_DS1302_Addr[i],Timer[i]);
	}
	Write_Ds1302_Byte(0x8e,0x80);
}
//读取数据
void Read_Time()
{
	unsigned char i;
	for(i = 0;i < 3;i ++)
	{
		Timer[i] = Read_Ds1302_Byte(Read_DS1302_addr[i]);
	}
}
//延迟函数
void Delay(unsigned int t)
{
	while(t--);
}
//温度显示
void Display_temp()
{
	temp = DS18B20_temp();
	Select_SMG(5,SMG_val[temp / 10]);
	Delay(500);
	Select_SMG(6,SMG_val[temp % 10]);
	Delay(500);
	Select_SMG(7,0xc6);
	Delay(500);
	CloseAll();
}
//时间正常显示
void Display_Time()
{
	Select_SMG(0,SMG_val[Timer[2] / 16]);
	Delay(100);
	Select_SMG(1,SMG_val[Timer[2] % 16]);
	Delay(100);
	
	Select_SMG(2,0xbf);
	Delay(100);
	
	Select_SMG(3,SMG_val[Timer[1] / 16]);
	Delay(100);
	Select_SMG(4,SMG_val[Timer[1] % 16]);
	Delay(100);
	
	Select_SMG(5,0xbf);
	Delay(100);
	
	Select_SMG(6,SMG_val[Timer[0] / 16]);
	Delay(100);
	Select_SMG(7,SMG_val[Timer[0] % 16]);
	Delay(100);
	CloseAll();
}
//秒
void Display_Time1()
{
	Select_SMG(0,SMG_val[Timer[2] / 16]);
	Delay(100);
	Select_SMG(1,SMG_val[Timer[2] % 16]);
	Delay(100);
	
	Select_SMG(2,0xbf);
	Delay(100);
	
	Select_SMG(3,SMG_val[Timer[1] / 16]);
	Delay(100);
	Select_SMG(4,SMG_val[Timer[1] % 16]);
	Delay(100);
	
	Select_SMG(5,0xbf);
	Delay(100);
	if(flag_t == 1)
	{
		Select_SMG(6,SMG_val[Timer[0] / 16]);
		Delay(100);
		Select_SMG(7,SMG_val[Timer[0] % 16]);
		Delay(100);
	}
	else if(flag_t == 0)
	{
		Select_SMG(6,0xff);
		Delay(100);
		Select_SMG(7,0xff);
		Delay(100);
	}
	CloseAll();
}
//分
void Display_Time2()
{
	Select_SMG(0,SMG_val[Timer[2] / 16]);
	Delay(100);
	Select_SMG(1,SMG_val[Timer[2] % 16]);
	Delay(100);
	
	Select_SMG(2,0xbf);
	Delay(100);
	if(flag_t == 1)
	{
		Select_SMG(3,SMG_val[Timer[1] / 16]);
		Delay(100);
		Select_SMG(4,SMG_val[Timer[1] % 16]);
		Delay(100);
	}
	else if(flag_t == 0)
	{
		Select_SMG(3,0xff);
		Delay(100);
		Select_SMG(4,0xff);
		Delay(100);
	}
	
	Select_SMG(5,0xbf);
	Delay(100);
	
	Select_SMG(6,SMG_val[Timer[0] / 16]);
	Delay(100);
	Select_SMG(7,SMG_val[Timer[0] % 16]);
	Delay(100);
	CloseAll();
}
//时
void Display_Time3()
{
	if(flag_t == 1)
	{
		Select_SMG(0,SMG_val[Timer[2] / 16]);
		Delay(100);
		Select_SMG(1,SMG_val[Timer[2] % 16]);
		Delay(100);
	}
	else if(flag_t == 0)
	{
		Select_SMG(0,0xff);
		Delay(100);
		Select_SMG(1,0xff);
		Delay(100);
	}
	
	Select_SMG(2,0xbf);
	Delay(100);
	
	Select_SMG(3,SMG_val[Timer[1] / 16]);
	Delay(100);
	Select_SMG(4,SMG_val[Timer[1] % 16]);
	Delay(100);
	
	Select_SMG(5,0xbf);
	Delay(100);
	
	Select_SMG(6,SMG_val[Timer[0] / 16]);
	Delay(100);
	Select_SMG(7,SMG_val[Timer[0] % 16]);
	Delay(100);
	CloseAll();
}
//闹钟设置按键
void Set_Clock()
{
	if(flag_s == 1)
	{
		Select_SMG(0,SMG_val[hour / 10]);
		Delay(100);
		Select_SMG(1,SMG_val[hour % 10]);
		Delay(100);
	}
	else if(flag_s == 2)
	{
		Select_SMG(3,SMG_val[min / 10]);
		Delay(100);
		Select_SMG(4,SMG_val[min % 10]);
		Delay(100);
	}
	else if(flag_s == 3)
	{
		Select_SMG(6,SMG_val[sec / 10]);
		Delay(100);
		Select_SMG(7,SMG_val[sec % 10]);
		Delay(100);
	}
	CloseAll();
}
//界面
void Display()
{
	switch(state)
	{
		case 1 : Display_Time(); break;
		case 2 : Display_Time3(); break;
		case 3 : Display_Time2(); break;
		case 4 : Display_Time1(); break;
		case 5 : Set_Clock();break;
		case 6 : Display_temp(); break;
	}
}
void Clock_TX();
//按键
void KEY_BTN()
{
	if(S7 == 0)
	{
		Delay(100);
		if(S7 == 0)
		{
			flag_LED = 0;
			if(state == 1)
			{
				state = 2;
			}
			else if(state == 2)
			{
				state = 3;
			}
			else if(state == 3)
			{
				state = 4;
			}
			else if(state == 4)
			{
				state = 1;
			}
		}
		while(S7 == 0)
		{
			Display();
			Clock_TX();
		}
	}
	if(S6 == 0)
	{
		Delay(100);
		{
			if(S6 == 0)
			{
				flag_LED = 0;
				state = 5;
				if(flag_s == 0)
				{
					flag_s = 1;
				}
				else if(flag_s == 1)
				{
					flag_s = 2;
				}
				else if(flag_s == 2)
				{
					flag_s = 3;
				}
				else if(flag_s == 3)
				{
					state = 1;
				}
			}
		}
		while(S6 == 0)
		{
			Display();
			Clock_TX();
		}
	}
	if(S5 == 0)
	{
		Delay(100);
		if(S5 == 0)
		{
			flag_LED = 0;
			if(state == 5 && flag_s == 1)
			{
				hour ++;
				if(hour > 23)
				{
					hour = 0;
				}
			}
			else if(state == 5 && flag_s == 2)
			{
				min ++;
				if(min > 59)
				{
					min = 0;
				}
			}
			 else if(state == 5 && flag_s == 3)
			{
				sec ++;
				if(sec > 59)
				{
					sec = 0;
				}
			}
			 if(state == 2)
			{
				Timer[2] ++;			
			}
			 if(state == 3)
			{
				Timer[1] ++;
			}
			 if(state == 4)
			{
				Timer[0] ++;
			}
		}
		Init_DS1302();
		while(S5 == 0)
		{
			Display();
			Clock_TX();
		}
	}
	if(S4 == 0)
	{
		Delay(100);
		{
			if(S4 == 0)
			flag_LED = 0;
			{
			if(state == 5 && flag_s == 1)
			{
				hour --;
				if(hour < 0)
				{
					hour = 23;
				}
			}
			else if(state == 5 && flag_s == 2)
			{
				min --;
				if(min < 0)
				{
					min = 59;
				}
			}
			else if(state == 5 && flag_s == 3)
			{
				sec --;
				if(sec < 0)
				{
					sec = 59;
				}
			}
			 if(state == 2)
			{
				Timer[2] --;
			}
			 if(state == 3)
			{
				Timer[1] --;
			}
			 if(state == 4)
			{
				Timer[0] --;
			}
			Init_DS1302();
			if(state == 1)
			{
				while(S4 == 0)
				{
					state = 6;
					Display();
					Clock_TX();
				}
				state = 1;
			}
		}
		while(S4 == 0)
		{
			Display();
			Clock_TX();
		}
	 }
    }
}
//闹钟提示
void Clock_TX()
{
	SelectHC573(4);
	if((Timer[2] / 16) * 10 + (Timer[2] % 16) == hour &&(Timer[1] / 16) * 10 + (Timer[1] % 16) == min && (Timer[0] / 16) * 10 + (Timer[0] % 16) == sec)
	{
		flag_LED = 1; 
	}
	
	if(flag_LED == 0)
	{
		L1 = 1;
	}
	else if(flag_LED1 == 1)
	{
		L1 = 0;
	}
	else if(flag_LED1 == 0)
	{
		L1 = 1;
	}
}
//系统初始化
void InitSystem()
{
	SelectHC573(5);
	P0 = 0x00;
	SelectHC573(4);
	P0 = 0xff;
}
//主函数
void main()
{
	Init_Timer0();
	InitSystem();
	Init_DS1302();
	while(1)
	{
		Read_Time();
		Display();
		KEY_BTN();
		Clock_TX();
	}
}


DS1302.c

#include "ds1302.h"  									

//写字节
void Write_Ds1302(unsigned  char temp) 
{
	unsigned char i;
	for (i=0;i<8;i++)     	
	{ 
		SCK = 0;
		SDA = temp&0x01;
		temp>>=1; 
		SCK=1;
	}
}   

//向DS1302寄存器写入数据
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; 
}

//从DS1302寄存器读出数据
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);			
}


OneWire.c

#include "onewire.h"

//单总线内部延时函数
void Delay_OneWire(unsigned int t)  
{
	while(t--);
}

//单总线写操作
void Write_DS18B20(unsigned char dat)
{
	unsigned char i;
	for(i=0;i<8;i++)
	{
		DQ = 0;
		DQ = dat&0x01;
		Delay_OneWire(50);
		DQ = 1;
		dat >>= 1;
	}
	Delay_OneWire(50);
}

//单总线读操作
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(50);
	}
	return dat;
}

//DS18B20初始化
bit init_ds18b20(void)
{
  	bit initflag = 0;
  	
  	DQ = 1;
  	Delay_OneWire(120);
  	DQ = 0;
  	Delay_OneWire(800);
  	DQ = 1;
  	Delay_OneWire(100); 
    initflag = DQ;     
  	Delay_OneWire(50);
  
  	return initflag;
}
//DS18B20程序
unsigned char DS18B20_temp()
{
	unsigned char LSB,MSB;
	unsigned int temp;
	
	init_ds18b20();
	Write_DS18B20(0xcc);
	Write_DS18B20(0x44);
	
	Delay_OneWire(50);
	
	init_ds18b20();
	Write_DS18B20(0xcc);
	Write_DS18B20(0xbe);
	LSB = Read_DS18B20();
	MSB = Read_DS18B20();
	temp = MSB << 8;
	temp = temp | LSB;
	temp = temp * 0.0625;
	return temp;
}


DS1302.h

#ifndef __DS1302_H
#define __DS1302_H

#include <reg52.h>
#include <intrins.h>

sbit SCK = P1^7;		
sbit SDA = P2^3;		
sbit RST = P1^3; 

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.h

#ifndef __ONEWIRE_H
#define __ONEWIRE_H

#include "reg52.h"

sbit DQ = P1^4;  

//unsigned char rd_temperature(void);  
unsigned char DS18B20_temp();
#endif



版权声明:本文为m0_56936436原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。