蓝桥杯单片机第九届国赛题目-多功能测量仪表
Posted WILLPOWER-
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了蓝桥杯单片机第九届国赛题目-多功能测量仪表相关的知识,希望对你有一定的参考价值。
题目
文件树
代码
init.c
#include "init.h"
void SL(uchar _dev, uchar _data)
{
P0 = _data; SEL(_dev);
}
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 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 BF1(uchar _0, uchar _1, uchar _2, uchar _3,
uchar _4, uchar _5, uchar _6, uchar _7)
{
uchar i = 3;
uchar buf1[8];
buf1[0] = _0; buf1[1] = _1; buf1[2] = _2; buf1[3] = _3;
buf1[4] = _4; buf1[5] = _5; buf1[6] = _6; buf1[7] = _7;
for(i = 3; i < 8; i++)
{
if(buf1[i] != 0)
break;
else
buf1[i] = F_C;
}
memcpy(buf,buf1,8);
}
uchar FR(float _data, uchar _dig)
{
uint i = 1;
while(_dig--)
{
i = i * 10;
}
return((uint)_data/i%10);
}
void Run(t_delay* time)
{
if(time->cnt++ < time->max);
else
{
time->cnt = 0;
time->ok = 1;
}
}
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 NE555_Init()
{
AUXR |= 0x80;
TMOD |= 0x05;
}
void NE555_Read()
{
if(delay_500.ok == 0)
{
delay_500.ok = 2;
TH0 = 0; TL0 = 0; TR0 = 1;
}
if(delay_500.ok == 1)
{
uint tmpVal = 0;
TR0 = 0;delay_500.ok = 0;
tmpVal = ((uint)TH0 << 8) | TL0;
neVal = tmpVal;
}
}
uchar LedStatus(uchar _data, uchar _dig)
{
return((_data >> _dig) & 1);
}
uchar LedChange(uchar _data, uchar _dig, uchar _en)
{
if(_en == 1)
{
_data &= ~(1<<_dig);
}
else if(_en == 0)
{
_data |= (1<<_dig);
}
else
{
_data ^= (1<<_dig);
}
SL(4, _data);
return _data;
}
main.c
#include "init.h"
enum{LED=4, EXT, SEL, CODE};
//dis
uchar code CA[] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99,
0x92, 0x82, 0xf8, 0x80, 0x90,
0xc0&0x7f, 0xf9&0x7f, 0xa4&0x7f, 0xb0&0x7f, 0x99&0x7f,
0x92&0x7f, 0x82&0x7f, 0xf8&0x7f, 0x80&0x7f, 0x90&0x7f,
0xff, 0xc1, 0x8e, 0xc6, 0x89, 0x8c};
uchar buf[8];
uchar curDig = 0;
//key
enum{KS_GT,KS_AS,KS_WA}keyState = KS_GT;
uchar key = 0, tmpKey =0, keyCnt = 0;
uint serCnt = 0;
//var
t_delay delay_500 = {500, 0, 0};
t_delay delay_200 = {200, 0, 0};
t_delay delay_led = {200, 0, 0};
uchar ledData = 0xff;
uchar tmpVal[3];
uint neVal;
uchar volMax = 10;
//global
enum{S_VOL, S_FRE, S_TEMP}gloSta = S_TEMP;
uchar disCnt = 0;
t_sum uintDiv = {0};
//codewrite
uchar codeData[6];
void Timer1Handle() interrupt 3
{
SL(CODE, 0xff); SL(SEL, 1 << curDig); SL(CODE, CA[buf[curDig]]);
curDig = (curDig + 1)%8;
//key
switch(keyState)
{
case KS_GT:
serCnt = 0;keyCnt = 0; tmpKey = GetKey(); keyState = KS_AS;
break;
case KS_AS:
if(keyCnt++ < 10);
else if(tmpKey == GetKey())
{
if(tmpKey != key)
{
key = tmpKey;keyState = KS_WA;
}
else if(serCnt++ < 400);
else
{
key = tmpKey;keyState = KS_WA;
}
}
else
keyState = KS_GT;
break;
}
//time
if(delay_500.ok == 2)
{
Run(&delay_500);
}
Run(&delay_200);
Run(&delay_led);
}
void keyProcess()
{
if(key == 4)
{
if(disCnt != 2)
{
gloSta = (gloSta + 1)%3;
}
}
if(key == 7)
{
if(disCnt != 2)
{
disCnt = 2;
}
else
{
codeData[5] = volMax;
Code_Write(0, codeData, 6);
Code_Write(0, codeData, 6);
disCnt = 0;
}
}
if(key == 6)
{
if(disCnt == 2)
{
if(volMax < 50)
{
volMax++;
}
else
volMax = 1;
}
else if(disCnt == 0)
disCnt = 1;
}
if(key == 5)
{
codeData[0] = (uint)adcVal;
uintDiv.ab =(uint)neVal;
codeData[1] = uintDiv.a;
codeData[2] = uintDiv.b;
uintDiv.ab =(uint)tempVal;
codeData[3] = uintDiv.a;
codeData[4] = uintDiv.b;
Code_Write(0, codeData, 6);
Code_Write(0, codeData, 6);
}
}
void SensorProcess()
{
NE555_Read();
if(delay_200.ok == 1)
{
delay_200.ok = 0;
Temp_Read();
adcVal = Adc_Read(0x43, 250);
// BF(F_C, FR(tempVal, 3), FR(tempVal, 2), FR(tempVal, 1),
// FR(tempVal, 0),F_C, F_C, F_C);
// BF(F_C, FR(adcVal, 3), FR(adcVal, 2), FR(adcVal, 1),
// FR(adcVal, 0),FR(tmpVal[0],0), FR(tmpVal[1],0), FR(tmpVal[2],0));
// SL(LED, ~(1<<curDig));
}
// BF(FR(neVal, 4), FR(neVal, 3), FR(neVal, 2), FR(neVal, 1),
// FR(neVal, 0),F_C, F_C, F_C);
}
void DisProcess()
{
if(disCnt == 0)
{
switch(gloSta)
{
case S_VOL:
BF(F_U, F_C, F_C, F_C,
F_C,F_C, FR(adcVal,1) + XS, FR(adcVal,0));
break;
case S_FRE:
BF1(F_F, F_C, F_C,FR(neVal, 4), FR(neVal, 3), FR(neVal, 2), FR(neVal, 1),
FR(neVal, 0));
break;
case S_TEMP:
BF(F_CC, F_C, F_C,F_C, FR(tempVal, 3), FR(tempVal, 2) + XS, FR(tempVal, 1),
FR(tempVal, 0));
break;
}
}
//回显显示
else if(disCnt == 1)
{
switch(gloSta)
{
case S_VOL:
BF(F_H, F_U, F_C, F_C,
F_C,F_C, FR(codeData[0],1) + XS, FR(codeData[0],0));
break;
case S_FRE:
uintDiv.a = codeData[1]; uintDiv.b = codeData[2];
BF1(F_H, F_F, F_C,FR(uintDiv.ab, 4), FR(uintDiv.ab, 3), FR(uintDiv.ab, 2), FR(uintDiv.ab, 1),
FR(uintDiv.ab, 0));
break;
case S_TEMP:
uintDiv.a = codeData[3]; uintDiv.b = codeData[4];
BF(F_H, F_CC, F_C,F_C, FR(uintDiv.ab, 3), FR(uintDiv.ab, 2) + XS, FR(uintDiv.ab, 1),
FR(uintDiv.ab, 0));
break;
}
}
else if(disCnt == 2)
{
BF(F_P, F_C, F_C,F_C, F_C, F_C, FR(volMax, 1) + XS,
FR(volMax, 0));
}
}
void LedProcess()
{
if(delay_led.ok == 1)
{
if(adcVal > volMax)
{
delay_led.ok = 0;
ledData = LedChange(ledData, 7, 2);
}
else if(!LedStatus(ledData, 7))
ledData = LedChange(ledData, 7, 0);
}
if(disCnt == 0)
{
switch(gloSta)
{
case S_TEMP:
if(LedStatus(ledData, 0))
{
ledData = LedChange(ledData, 0, 1);
ledData = LedChange(ledData, 1, 0);
ledData = LedChange(ledData, 2, 0);
}
break;
case S_FRE:
if(LedStatus(ledData, 1))
{
ledData = LedChange(ledData, 0, 0);
ledData = LedChange(ledData, 1, 1);
ledData = LedChange(ledData, 2, 0);
}
break;
case S_VOL:
if(LedStatus(ledData, 2))
{
ledData = LedChange(ledData, 0, 0);
ledData = LedChange(ledData, 1, 0);
ledData = LedChange(ledData, 2, 1);
}
break;
}
}
}
void main()
{
BPOFF; RLOFF;
SL(LED, ledData);
Timer1Init();
NE555_Init();
memset(buf, 1, 8);
Code_Read(0, codeData, 6);
Code_Read(0, codeData, 6);
volMax = codeData[5];
while(1)
{
SensorProcess();
DisProcess蓝桥杯单片机第九届国赛程序设计题--多功能测量仪表国赛