蓝桥杯单片机 国赛 第4届 超声波测距报警实时时钟电路

本文介绍了一款基于单片机的系统,该系统集成了超声波测距和DS1302实时时钟模块。程序实现了距离测量、时间设置及显示,并通过键盘进行交互操作。此外,系统还具备超限警告功能,当测量距离低于预设阈值时,会触发警告。
摘要由CSDN通过智能技术生成

就感觉这届的题怪怪的,不好描述。应该是题目不全的原因


题目:


在这里插入图片描述


代码:


main.c

#include"reg52.h"
#include"intrins.h"
#include"ds1302.h"
sbit TX=P1^0;
sbit RX=P1^1;
int distance;
sfr AUXR=0x8e;
sbit S4=P3^3;
sbit S5=P3^2;
sbit S6=P3^1;
sbit S7=P3^0;
int smg_mode=0;
int time_mode=4;
int code write_add[]={0x80,0x82,0x84,0x86,0x88,0x8a,0x8c};
int code read_add[]={0x81,0x83,0x85,0x87,0x89,0x8b,0x8d};
unsigned int code xianshi[12]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff,0xbf};
int time[]={0x59,0x50,0x11};
int distance_alarm=30;
int time_temp;
int shanshuo=0;
void Timer0Init(void)		//5毫秒@11.0592MHz
{
	AUXR |= 0x80;		//定时器时钟1T模式
	TMOD &= 0xF0;		//设置定时器模式
	TL0 = 0x00;		//设置定时初始值
	TH0 = 0x28;		//设置定时初始值
	TF0 = 0;		//清除TF0标志
	TR0 = 1;		//定时器0开始计时
	ET0=1;
	EA=1;
}
int count_1=0;
void timer0_server() interrupt 1
{
	count_1++;
	if(count_1>=100)
	{
		count_1=0;
		shanshuo++;
		if(shanshuo>=2)
		{
			shanshuo=0;
		}
	}	
}


void choose_573(int i)
{
	switch(i)
	{
		case(0):P2=(P2&0x1f)|0x00;break;
		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 Delay1000ms()		//@11.0592MHz
{
	unsigned char i, j, k;

	_nop_();
	_nop_();
	i = 43;
	j = 6;
	k = 203;
	do
	{
		do
		{
			while (--k);
		} while (--j);
	} while (--i);
}

void init_system()
{
	choose_573(4);
	P0=0x00;
	choose_573(5);
	P0=0xff;
	choose_573(0);
	Delay1000ms();
	choose_573(4);
	P0=0xff;
	choose_573(5);
	P0=0x00;
	choose_573(0);
}
//====================================time
void set_time()
{
	int i;
	Write_Ds1302_Byte(0x8e,0x00);
	for(i=0;i<3;i++)
	{
		Write_Ds1302_Byte(write_add[i],time[i]);	
	}
	Write_Ds1302_Byte(0x8e,0x80);
}
void read_time()
{
	int i;
	for(i=0;i<3;i++)
	{
		time[i]=Read_Ds1302_Byte(read_add[i]);
	}
}
//====================================
//====================================超声波测距

void Delay12us()		//@11.0592MHz
{
	unsigned char i;

	_nop_();
	_nop_();
	_nop_();
	i = 30;
	while (--i);
}
void send_ware()
{
	int i;
	for(i=0;i<8;i++)
	{
		TX=1;
		Delay12us();
		TX=0;
		Delay12us();
	}
}
void get_distance()
{ 	
	int time;
	AUXR |= 0x40;		//定时器时钟1T模式
	TMOD &= 0x0F;		//设置定时器模式
	TL1 = 0x00;		//设置定时初始值
	TH1 = 0x00;		//设置定时初始值
	TF1 = 0;		//清除TF1标志
	TR1 = 0;		//waring
	send_ware();
	TR1=1;
	while(RX==1&&TF1==0);
	TR1=0;
	if(TF1==0)
	{
		time=TH1;
		time=(time<<8)|TL1;
		distance=time*0.017;	
	}	
	else
	{

		TF1=0;
		distance=999;	
	}
	TH1=TL1=0x00;
}
//====================================
//==================================== key_board
void Delay10ms()		//@11.0592MHz
{
	unsigned char i, j;

	i = 108;
	j = 145;
	do
	{
		while (--j);
	} while (--i);
}
void key_board()
{
	if(S7==0)
	{
		Delay10ms();
		if(S7==0)
		{
			smg_mode++;
			if(smg_mode>=2)
			{
				smg_mode=0;
			}
		}
		while(!S7);
	}  
	//S6
	if(S6==0&&(smg_mode==0||smg_mode==2))
	{
		Delay10ms();
		if(S6==0&&(smg_mode==0||smg_mode==2))
		{
			smg_mode=2;
			time_mode++;
			if(time_mode>=4)
			{
				time_mode=0;
			}
		}
		while(!S6);
	}
	if(S6==0&&smg_mode==1)
	{
		Delay10ms();
		if(S6==0&&smg_mode==1)
		{
			smg_mode=3;
		}
		while(!S6);
	}
	//S5
	
	if(S5==0)
	{
		Delay10ms();
		if(S5==0)
		{
			if(smg_mode==3)
			{
				distance_alarm++;
			}
			if(smg_mode==2)
			{
				if(time_mode==0)
				{
					time_temp=(time[2]/16*10)+time[2]%16;
					time_temp++;
					time_temp=time_temp+((time_temp/10)*6);
					time[2]=time_temp;
					set_time();	
				}
				if(time_mode==1)
				{
					time_temp=(time[1]/16*10)+time[1]%16;
					time_temp++;
					time_temp=time_temp+((time_temp/10)*6);
					time[1]=time_temp;
					set_time();	
				}
				if(time_mode==2)
				{
					time_temp=(time[0]/16*10)+time[0]%16;
					time_temp++;
					time_temp=time_temp+((time_temp/10)*6);
					time[0]=time_temp;
					set_time();	
				}
			}
		}
		while(!S5);
	}

	//S4
	if(S4==0)
	{
		Delay10ms();
		if(S4==0)
		{
			if(smg_mode==3)
			{
				distance_alarm--;
			}
			if(smg_mode==2)
			{
				if(time_mode==0)
				{
					time_temp=(time[2]/16*10)+time[2]%16;
					time_temp--;
					time_temp=time_temp+((time_temp/10)*6);
					time[2]=time_temp;
					set_time();	
				}
				if(time_mode==1)
				{
					time_temp=(time[1]/16*10)+time[1]%16;
					time_temp--;
					time_temp=time_temp+((time_temp/10)*6);
					time[1]=time_temp;
					set_time();	
				}
				if(time_mode==2)
				{
					time_temp=(time[0]/16*10)+time[0]%16;
					time_temp--;
					time_temp=time_temp+((time_temp/10)*6);
					time[0]=time_temp;
					set_time();	
				}
			}
		}
		while(!S4);
	}

}
//====================================
//====================================smg
void Delay300us()		//@11.0592MHz
{
	unsigned char i, j;

	_nop_();
	_nop_();
	i = 4;
	j = 54;
	do
	{
		while (--j);
	} while (--i);
}
void SMG(int wei,int dat)
{
	choose_573(6);
	P0=0x80>>(wei-1);
	choose_573(7);
	P0=xianshi[dat];
	choose_573(0);
	Delay300us();
}
void smg_display()
{
	if(smg_mode==0||time_mode==3)
	{
		SMG(1,time[0]%16);
		SMG(2,time[0]/16);
		SMG(3,11);
		SMG(4,time[1]%16);
		SMG(5,time[1]/16);
		SMG(6,11);
		SMG(7,time[2]%16);
		SMG(8,time[2]/16);
	}
	if(smg_mode==1)
	{
		SMG(1,distance%10);
		SMG(2,(distance%100)/10);
		SMG(3,(distance%1000)/100);
		SMG(4,10);
		SMG(5,10);
		SMG(6,10);
		SMG(7,10);
		SMG(8,10);
	}
	if(smg_mode==3)
	{
		SMG(1,distance_alarm%10);
		SMG(2,(distance_alarm%100)/10);
		SMG(3,(distance_alarm%1000)/100);
		SMG(4,11);
		SMG(5,11);
		SMG(6,10);
		SMG(7,10);
		SMG(8,10);
	}

	if(smg_mode==2)
	{
		if(time_mode==0)
		{
			SMG(1,time[0]%16);
			SMG(2,time[0]/16);
			SMG(3,11);
			SMG(4,time[1]%16);
			SMG(5,time[1]/16);
			SMG(6,11);
			if(shanshuo==1)
			{
				SMG(7,time[2]%16);
				SMG(8,time[2]/16);
			}
			else
			{
				SMG(7,10);
				SMG(8,10);
			}
		}
		if(time_mode==1)
		{
			SMG(1,time[0]%16);
			SMG(2,time[0]/16);
			SMG(3,11);
			if(shanshuo==1)
			{
				SMG(4,time[1]%16);
				SMG(5,time[1]/16);
			}
			else
			{
				SMG(4,10);
				SMG(5,10);
			}
			SMG(6,11);
			SMG(7,time[2]%16);
			SMG(8,time[2]/16);
		}
		if(time_mode==2)
		{
			if(shanshuo==1)
			{
				SMG(1,time[0]%16);
				SMG(2,time[0]/16);
			}
			else
			{
				SMG(1,10);
				SMG(1,10);
			}
			SMG(3,11);
			SMG(4,time[1]%16);
			SMG(5,time[1]/16);
			SMG(6,11);
			SMG(7,time[2]%16);
			SMG(8,time[2]/16);
		}
	}
}
//====================================
//====================================waring
int led=0xff;
void work()
{
	if(distance<distance_alarm*1.2&&shanshuo==1)
	{
		led=(led&0xfe)|0x00;
		choose_573(4);
		P0=led;
		choose_573(0);
	}
	else
	{
		led=(led&0xfe)|0x01;
		choose_573(4);
		P0=led;
		choose_573(0);
	}
}
//====================================
void main()
{
	init_system();
	set_time();
	Timer0Init();
	while(1)
	{
		get_distance();
		read_time();
		smg_display();
		key_board();
		work();
	}
}

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

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值