蓝桥杯单片机 国赛 第8届 超声波测距机的功能设计与实现

这次题目中,我觉得那个循环存储四个数据和查看确实有点难度,写起来感觉很绕,但是我觉得我应该所有功能全部实现了。如果有错误,欢迎指正!!!

题目:

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

代码:

main.c

#include"reg52.h"
#include"iic.h"
#include"intrins.h"
int smg_mode=0;
int distance_work=0;
sbit TX=P1^0;
sbit RX=P1^1;
sfr AUXR=0x8e;
int distance=0;
int judge_distance=20;
sbit S4=P3^3;
sbit S5=P3^2;
sbit S6=P3^1;
sbit S7=P3^0;
int distance_history[]={0,0,0,0};
int distance_time=3;
int distance_temp_time=0;
int led=0xff;
int L1_temp=0;
char code xianshi[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff,0xc6,0x8e};
void smg_display();
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 init_system()
{
	choose_573(4);
	P0=0xff;
	choose_573(5);
	P0=0x00;
	choose_573(0);
}
//===============================超声波
void Delay12us()		//@12.000MHz
{
	unsigned char i;

	_nop_();
	_nop_();
	i = 33;
	while (--i);
}
void send_wave()
{
	int i;
	for(i=0;i<8;i++)
	{
		TX=1;
		Delay12us();
		TX=0;
		Delay12us();
	}
}
void get_distance()
{
	if(distance_work==1)
	{
		int time;
		AUXR |= 0x40;		//定时器时钟1T模式
		TMOD &= 0x0F;		//设置定时器模式
		TL1 = 0x00;		//设置定时初始值
		TH1 = 0x00;		//设置定时初始值
		TF1 = 0;		//清除TF1标志
		TR1 = 0;		//定时器1开始计时
		send_wave();
		TR1=1;
		while(RX==1&&TF1==0);
		TR1=0;
		if(TF1==0)
		{
			time=TH1;
			time=(time<<8)|TL1;
			distance=time*0.0017;
		}
		else
		{
			TF1=0;
			distance=999;
		}
		TH1=TL1=0x00;
		L1_temp=1;
		distance_work=0;
		
	}
}
//===============================
//===============================按键
void Delay10ms()		//@11.0592MHz
{
	unsigned char i, j;

	i = 108;
	j = 145;
	do
	{
		while (--j);
	} while (--i);
}
void key_board()
{
	//S4
	if(S4==0)
	{
		Delay10ms();
		if(S4==0)
		{
			smg_mode=0;
			distance_history[distance_temp_time]=distance;
			distance_temp_time++;
			if(distance_temp_time>=4)
			{
				distance_temp_time=0;
			}
			distance_work=1;
		}
		while(!S4)
		{
			smg_display();
		}
	}
	//S5
	if(S5==0)
	{
		Delay10ms();
		if(S5==0)
		{
			if(smg_mode==1)
			{
				smg_mode=0;
			}
			else
			{
				smg_mode=1;
			}
		}
		while(!S5)
		{
			smg_display();
		}
	}
	//S6
	if(S6==0)
	{
		Delay10ms();
		if(S6==0)             //参数保存
		{
			if(smg_mode==2)
			{
				smg_mode=0;
				write_EEPROM(0x01,judge_distance);
			}
			else
			{
				smg_mode=2;
			}
		}
		while(!S6)
		{
			smg_display();
		}
	}
	//S7
	if(S7==0)
	{
		Delay10ms();
		if(S7==0)
		{
			if(smg_mode==1)       //查询保存
			{
				distance_time++;
				if(distance_time>=4)
				{
					distance_time=0;
				}
			}
			if(smg_mode==2)      //参数设置
			{
				judge_distance=judge_distance+10;
				if(judge_distance>=40)
				{
					judge_distance=0;
				}
			}
		}
		while(!S7)
		{
			smg_display();
		}
	}
}
//===============================
//===============================数码管
void Delay400us()		//@11.0592MHz
{
	unsigned char i, j;

	i = 5;
	j = 74;
	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);
	Delay400us();
	choose_573(7);
	P0=xianshi[10];
	choose_573(0);
}
void smg_display()
{
	if(smg_mode==0)
	{
		if(distance_temp_time==0)
		{
			SMG(1,distance_history[3]%10);
			SMG(2,(distance_history[3]%100)/10);
			SMG(3,(distance_history[3]%1000)/100);
		}
		else
		{
			SMG(1,distance_history[distance_temp_time-1]%10);
			SMG(2,(distance_history[distance_temp_time-1]%100)/10);
			SMG(3,(distance_history[distance_temp_time-1]%1000)/100);
		}
		SMG(4,distance%10);
		SMG(5,(distance%100)/10);
		SMG(6,(distance%1000)/100);
		SMG(7,10);
		SMG(8,11);
	}
	
	if(smg_mode==1)
	{
		SMG(1,distance_history[distance_time]%10);
		SMG(2,(distance_history[distance_time]%100)/10);
		SMG(3,(distance_history[distance_time]%1000)/100);
		SMG(4,10);
		SMG(5,10);
		SMG(6,10);
		SMG(7,10);
		SMG(8,2);
	}
	if(smg_mode==2)
	{
		SMG(1,judge_distance%10);
		SMG(2,(judge_distance%100)/10);
		SMG(3,(judge_distance%1000)/100);
		SMG(4,10);
		SMG(5,10);
		SMG(6,10);
		SMG(7,10);
		SMG(8,12);
	}
}
//===============================
//===============================DA输出
void DA_out()
{
	if(distance<=judge_distance)
	{
		DA(0);
	}
	else
	{
		if(((distance-judge_distance)*0.02)>255)
		{
			DA(255);
		}
		else
		{
			DA((distance-judge_distance)*0.02);
		}
	}
}
//===============================
//===============================LED
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;int L1_work=0;int i=0;
void time0_server() interrupt 1
{
	if(L1_temp==1)
	{
		count++;
		if(count>=200)
		{
			i++;
			count=0;
			if(i>=7)
			{
				i=1;
				L1_temp=0;
			}
		}
	}
}
void led_work()
{
	//L8
	if(smg_mode==1)
	{
		led=(led&0x7f)|0x00;
		choose_573(4);
		P0=led;
		choose_573(0);
	}
	else
	{
		led=(led&0x7f)|0x80;
		choose_573(4);
		P0=led;
		choose_573(0);
	}
	//L7
	if(smg_mode==2)
	{
		led=(led&0xbf)|0x00;
		choose_573(4);
		P0=led;
		choose_573(0);
	}
	else
	{
		led=(led&0xbf)|0x40;
		choose_573(4);
		P0=led;
		choose_573(0);
		
	}
	//L1
	if(L1_temp==1)
	{
//		led=(led&0xfe)|0x00;
//		choose_573(4);
//		P0=led;
//		choose_573(0);
//		
		if(i%2==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();
	judge_distance=read_EEPROM(0x01);
	Timer0Init();
	while(1)
	{
		get_distance();
		smg_display();
		key_board();
		DA_out();
		led_work();
	}
}

iic.c

/*
  程序说明: IIC总线驱动程序
  软件环境: Keil uVision 4.10 
  硬件环境: CT107单片机综合实训平台 8051,12MHz
  日    期: 2011-8-9
*/

#include "reg52.h"
#include "intrins.h"

#define DELAY_TIME 5

#define SlaveAddrW 0xA0
#define SlaveAddrR 0xA1

//总线引脚定义
sbit SDA = P2^1;  /* 数据线 */
sbit SCL = P2^0;  /* 时钟线 */

void IIC_Delay(unsigned char i)
{
    do{_nop_();}
    while(i--);        
}
//总线启动条件
void IIC_Start(void)
{
    SDA = 1;
    SCL = 1;
    IIC_Delay(DELAY_TIME);
    SDA = 0;
    IIC_Delay(DELAY_TIME);
    SCL = 0;	
}

//总线停止条件
void IIC_Stop(void)
{
    SDA = 0;
    SCL = 1;
    IIC_Delay(DELAY_TIME);
    SDA = 1;
    IIC_Delay(DELAY_TIME);
}

//发送应答
void IIC_SendAck(bit ackbit)
{
    SCL = 0;
    SDA = ackbit;  					// 0:应答,1:非应答
    IIC_Delay(DELAY_TIME);
    SCL = 1;
    IIC_Delay(DELAY_TIME);
    SCL = 0; 
    SDA = 1;
    IIC_Delay(DELAY_TIME);
}

//等待应答
bit IIC_WaitAck(void)
{
    bit ackbit;
	
    SCL  = 1;
    IIC_Delay(DELAY_TIME);
    ackbit = SDA;
    SCL = 0;
    IIC_Delay(DELAY_TIME);
    return ackbit;
}

//通过I2C总线发送数据
void IIC_SendByte(unsigned char byt)
{
    unsigned char i;

    for(i=0; i<8; i++)
    {
        SCL  = 0;
        IIC_Delay(DELAY_TIME);
        if(byt & 0x80) SDA  = 1;
        else SDA  = 0;
        IIC_Delay(DELAY_TIME);
        SCL = 1;
        byt <<= 1;
        IIC_Delay(DELAY_TIME);
    }
    SCL  = 0;  
}

//从I2C总线上接收数据
unsigned char IIC_RecByte(void)
{
    unsigned char i, da;
    for(i=0; i<8; i++)
    {   
    	SCL = 1;
	IIC_Delay(DELAY_TIME);
	da <<= 1;
	if(SDA) da |= 1;
	SCL = 0;
	IIC_Delay(DELAY_TIME);
    }
    return da;    
}
void write_EEPROM(int add,int dat)
{
	IIC_Start();
	IIC_SendByte(0xa0);
	IIC_WaitAck();
	IIC_SendByte(add);
	IIC_WaitAck();
	IIC_SendByte(dat);
	IIC_WaitAck();
	IIC_Stop();
}
int read_EEPROM(int add)
{
	int temp;
	IIC_Start();
	IIC_SendByte(0xa0);
	IIC_WaitAck();
	IIC_SendByte(add);
	IIC_WaitAck();
	
	IIC_Start();
	IIC_SendByte(0xa1);
	IIC_WaitAck();
	temp=IIC_RecByte();
	IIC_SendAck(1);
	IIC_Stop();
	return temp;
}
void DA(int dat)
{
	IIC_Start();
	IIC_SendByte(0x90);
	IIC_WaitAck();
	IIC_SendByte(0x40);
	IIC_WaitAck();
	IIC_SendByte(dat);
	IIC_WaitAck();
	IIC_Stop();
}

iic.h

#ifndef _IIC_H
#define _IIC_H

void IIC_Start(void); 
void IIC_Stop(void);  
bit IIC_WaitAck(void);  
void IIC_SendAck(bit ackbit); 
void IIC_SendByte(unsigned char byt); 
unsigned char IIC_RecByte(void); 
int read_EEPROM(int add);
void write_EEPROM(int add,int dat);
void DA(int dat);
#endif
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值