蓝桥杯单片机第八届国赛题目-超声波测距机的功能设计与实现
Posted WILLPOWER-
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了蓝桥杯单片机第八届国赛题目-超声波测距机的功能设计与实现相关的知识,希望对你有一定的参考价值。
题目
文件树
代码
init.c
#include "init.h"
void Timer1Init(void) //2毫秒@12.000MHz
{
AUXR |= 0x40; //定时器时钟1T模式
TMOD &= 0x0F; //设置定时器模式
TL1 = 0x40; //设置定时初值
TH1 = 0xA2; //设置定时初值
TF1 = 0; //清除TF1标志
TR1 = 1; //定时器1开始计时
ET1 = 1;
EA = 1;
}
void SL(uchar _dev, uchar _data)
{
P0 = _data; SEL(_dev);
}
uchar FR(float _data, uchar _dig)
{
uint i = 1;
while(_dig--)
{
i = i * 10;
}
return((uint)_data/i%10);
}
void BF(uchar _0, uchar _1, uchar _2, uchar _3,
uchar _4, uchar _5, uchar _6, uchar _7)
{
buf[0] = _0; buf[1] = _1; buf[2] = _2; buf[3] = _3;
buf[4] = _4; buf[5] = _5; buf[6] = _6; buf[7] = _7;
}
void Run(t_delay* time)
{
if(time->cnt++ < time->max);
else
{
time->cnt = 0;
time->ok = 1;
}
}
void Delay14us() //@12.000MHz
{
unsigned char i;
_nop_();
_nop_();
i = 39;
while (--i);
}
void Sonic_T()
{
uchar i = 10;
while(i--)
{
P10 = 1;
Delay14us();
P10 = 0;
Delay14us();
}
}
void Sonic_R()
{
CH = 0; CL = 0;
Sonic_T();CR = 1;
while(!CF && P11);
CR = 0;
if(CF)
{
CF = 0;
preDistance = distance;
distance = maxDistance;
Adc_Read(0x43, 0);
}
else
{
uint tmpVal;
tmpVal = ((uint)CH << 8) | CL;
preDistance = distance;
if(tmpVal*0.017/12 > maxDistance)
{
Adc_Read(0x43, 0);
distance = maxDistance;
}
else
{
uchar tmpVal;
distance = tmpVal*0.017/12;
tmpVal = (maxDistance - distance)*0.02*51;
if(tmpVal > 5)
Adc_Read(0x43, 0);
else
Adc_Read(0x43, tmpVal);
}
}
}
uchar LedChange(uchar _data, uchar _dig, uchar _en)
{
if(_en == 0)
{
_data |= (1 << _dig);
}
else if(_en == 1)
{
_data &= ~(1 << _dig);
}
else
{
_data ^= (1 << _dig);
}
SL(4, _data);
return(_data);
}
uchar GetKey()
{
if(K1 == 0) return 7;
if(K2 == 0) return 6;
if(K3 == 0) return 5;
if(K4 == 0) return 4;
return 0;
}
//void UartInit(void) //9600bps@12.000MHz
//{
// SCON = 0x50; //8位数据,可变波特率
// AUXR |= 0x01; //串口1选择定时器2为波特率发生器
// AUXR |= 0x04; //定时器2时钟为Fosc,即1T
// T2L = 0xC7; //设定定时初值
// T2H = 0xFE; //设定定时初值
// AUXR |= 0x10; //启动定时器2
// ES = 1;
//}
void Print(uchar *string)
{
ES = 0;TI = 1;
printf("%s", string);
TI = 0; ES = 1;
}
//void Delay8ms() //@12.000MHz
//{
// unsigned char i, j;
// i = 94;
// j = 95;
// do
// {
// while (--j);
// } while (--i);
//}
uchar Check(uchar _data, uchar _dig)
{
return(!((_data >> _dig) & 1));
}
main.c
#include "init.h"
uchar ledData = 0xff;
enum{LED=4, EXT, SEL, CODE};
//dis
uchar buf[8];
uchar code CA[] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99,
0x92, 0x82, 0xf8, 0x80, 0x90,
0xff, 0x8e};
uchar curDig = 0;
enum{D_DIS, D_BACK, D_SET}gloSta = D_DIS;
//key
uchar key = 0, tmpKey = 0, keyCnt = 0;
enum{KS_GT, KS_AS, KS_WA}keyState = KS_GT;
//var
t_delay delay_200 = {200, 0, 0};
t_delay delay_300 = {300, 0, 0};
t_delay delay_100 = {100, 0, 0};
float distance;
float preDistance;
float adcVal;
uchar caoZuo = 0;
uchar maxDistance = 20;
uchar pageCnt = 0;
uchar saveData[10];
uchar index = 0;
uchar revBuf[20];
uchar sonicok = 0;
uchar soniccnt = 0;
void Timer1Handle() interrupt 3
{
SL(CODE, 0xff); SL(SEL, 1 << curDig); SL(CODE, CA[buf[curDig]]);
curDig = (1 + curDig)%8;
//按键
switch(keyState)
{
case KS_GT:
keyCnt = 0; tmpKey = GetKey(); keyState = KS_AS;
break;
case KS_AS:
if(keyCnt++ < 10);
else if(tmpKey == GetKey())
{
if(key != tmpKey)
{
key = tmpKey;
keyState = KS_WA;
}
else
keyState = KS_GT;
}
else
keyState = KS_GT;
break;
}
//timerun
Run(&delay_200);
if(delay_300.ok == 2)
{
Run(&delay_300);
}
Run(&delay_100);
}
void SensorProcess()
{
if(delay_200.ok == 1)
{
delay_200.ok = 0;
adcVal = Adc_Read(0x43, 250);
// BF(F_C, F_C, F_C, F_C,
// FR(distance, 3), FR(distance, 2),FR(distance, 1),FR(distance, 0));
BF(F_C, F_C, F_C, F_C,
FR(adcVal, 3), FR(adcVal, 2),FR(adcVal, 1),FR(adcVal, 0));
}
}
void DisProcess()
{
switch(gloSta)
{
case D_DIS:
if(caoZuo == 1)
{
BF(1, F_C, F_C, FR(preDistance, 1),FR(preDistance, 0),
FR(distance, 2),FR(distance, 1),FR(distance, 0));
}
else
{
float sum;
sum = preDistance + distance;
BF(1, F_C, F_C, FR(sum, 1),FR(sum, 0),
FR(distance, 2),FR(distance, 1),FR(distance, 0));
}
break;
case D_BACK:
BF(FR(pageCnt+1, 1), FR(pageCnt+1, 0), F_C,F_C,F_C,
FR(saveData[pageCnt], 2),FR(saveData[pageCnt], 1),FR(saveData[pageCnt], 0));
break;
case D_SET:
BF(F_F, F_C, F_C,F_C,F_C,
F_C,FR(maxDistance, 1),FR(maxDistance, 0));
break;
}
}
void KD_dis()
{
if(key == 7)
{
caoZuo = (caoZuo + 1)%2;
}
}
void KD_back()
{
if(key == 7)
{
pageCnt = (pageCnt + 1)%10;
}
}
void KD_SET()
{
if(key == 7)
{
if(maxDistance < 90)
{
maxDistance = maxDistance + 10;
}
else
maxDistance = 0;
}
}
void keyProcess()
{
if(key == 4)
{
Sonic_R();
sonicok = 1;soniccnt = 0;
Rotate_Save((uchar)distance);
Code_Write(0, saveData, 8);
Code_Write(1, &saveData[8], 2);
Code_Write(0, saveData, 8);
Code_Write(1, &saveData[8], 2);
}
if(key == 5)
{
gloSta = (gloSta != D_BACK)? D_BACK:D_DIS;
if(gloSta == D_BACK)
{
pageCnt = 0;
}
}
if(key == 6)
{
gloSta = (gloSta != D_SET)? D_SET:D_DIS;
}
switch(gloSta)
{
case D_DIS:
KD_dis();
break;
case D_BACK:
KD_back();
break;
case D_SET:
KD_SET();
break;
}
}
void Rotate_Save(uchar _dat)
{
uchar i;
for(i = 0; i < 9; i++)
{
saveData[i] = saveData[i+1];
}
saveData[i] = _dat;
}
void UartHandle() interrupt 4
{
if(TI) TI = 0;
if(RI)
{
RI = 0;
if(delay_300.ok == 0)
delay_300.ok = 2;
if(index < 20)
{
revBuf[index++] = SBUF;
}
}
}
void UartProcess()
{
if(delay_300.ok == 1)
{
delay_300.ok = 0;
if(index == 3)
{
SL(LED, 0x0);
if(strncmp(revBuf, "hel", 3) == 0)
{
uchar tmpVal[50];
sprintf(tmpVal, "%bd-%bd-%bd-%bd-%bd-%bd-%bd-%bd-%bd-%bd",
saveData[0],saveData[1],saveData[2],saveData[3],saveData[4],saveData[5],saveData[6],saveData[7],saveData[8],saveData[9]);
Print(tmpVal);
}
}
index = 0;
}
}
void LedProcess()
{
if(sonicok == 1)
{
if(delay_100.ok == 1)
{
delay_100.ok = 0;
if(soniccnt++ < 10)
{
ledData = LedChange(ledData, 0, 2);
}
else
{
ledData = LedChange(ledData, 0, 0);
sonicok = 0;
}
}
}
switch(gloSta)
{
case D_SET:
if(!Check(ledData, 6))
{
ledData = LedChange(ledData, 7, 0);
ledData = LedChange(ledData, 6, 1);
}
break;
case D_BACK:
if(!Check(ledData, 7))
{
ledData = LedChange(ledData, 6, 0);
ledData = LedChange(ledData, 7, 1);
}
break;
}
}
void main()
{
BPOFF; RLOFF;
SL(LED, ledData);
Timer1Init();
memset(buf, F_C, 8);
//UartInit();
//超声波初始化
CMOD |= 0x08;
蓝桥杯单片机第九届国赛程序设计题--多功能测量仪表国赛