蓝桥杯单片机第八届国赛代码

//main.c 功能基本实现,暂未发现明显Bug子函数自己加

#include "tim.h"
#include "seg.h"
#include "stdio.h"
#include "key.h"
#include "i2c.h"
#include "ultrasonic.h"
#include "intrins.h"
unsigned char ucSec, ucLed;
unsigned char pucSeg_Buf[9], pucSeg_Code[8], ucSeg_Pos;//pucseg_buf储存阿拉伯数字,code储存16进制
unsigned char mode=0;
unsigned int times=0;
unsigned int chaoshengbo[4]=0;//储存4次超声波
unsigned char chaoshengbonow=0;//储存当前超声波
unsigned int mangqu=0;
unsigned long ulms, ulKey_Time,ulms2=0;//2存dac时间,3存led闪烁时间
unsigned int ulms3=0;
unsigned int ulms1=0;//检验矩阵,50ms一次
unsigned char ucKey_Dly, ucKey_Old;
unsigned char led=0,led1=0;
bit led2=1;
void Key_Proc(void);
void keydo(unsigned char key_value);
void Delay100ms() //@11.0592MHz
{
unsigned char i, j, k;
_nop_();
_nop_();
i = 5;
j = 52;
k = 195;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void leddo()
{
if(mode==0)
{
led&=0x7f;
led|=0x40;
}
if(mode==1)
{
led&=0xbf;
led|=0x80;
}
}
void show()
{
int i=0;
//Led_Disp(leddo);//LED显示功能
for(;i<10;i++)
{
pucSeg_Buf[i]=' ';
}
if(mode==0)
{
sprintf(pucSeg_Buf, "C %03u%03u", (unsigned int)chaoshengbonow,(unsigned int)chaoshengbo[1]);//打入字符串,以字符形式储存
}
if(mode==1)
{
unsigned int time=chaoshengbo[3-times];
sprintf(pucSeg_Buf, "%u %3u",(unsigned int)times,(unsigned int)time);//打入字符串,以字符形式储存

}
if(mode==2)
{
sprintf(pucSeg_Buf, "F %2u",(unsigned int)mangqu);//打入字符串,以字符形式储存

}
Seg_Tran(pucSeg_Buf, pucSeg_Code);//将阿拉伯数字转换成16进制
}
void keydo(unsigned char key_value)
{
if(key_value==4&&mode==0)
{
unsigned char i;
led1=1;

for(i=3;i>0;i--)
{
chaoshengbo[i]=chaoshengbo[i-1];
write_eeprom(i,chaoshengbo[i]);
Delay100ms();
}chaoshengbonow=Wave_Recv();
chaoshengbo[0]=chaoshengbonow;
write_eeprom(0,chaoshengbo[0]);
}
if(key_value==5&&mode!=1)
{
mode=1;
}
else if(key_value==5&&mode==1)
{
mode=0;
}
if(key_value==6&mode!=2)
{
mode=2;
}
else if(key_value==6&mode==2)
{
mode=0;
}
if(mode==2&&key_value==7)
{
mangqu+=10;
if(mangqu>30)
{
mangqu-=30;
}
}
if(mode==1&&key_value==7)
{
times++;
times%=4;
}
}
void dacdo()
{
if(chaoshengbonow<=mangqu)
{
Pcf8591_dac(0);
}
else
{
unsigned int dacshu=(chaoshengbonow-mangqu)*256.0*0.02/5+3;
if(dacshu>250)
{
dacshu=250;
}
Pcf8591_dac(dacshu);
}
}
void main(void)
{
unsigned char q=5;
unsigned char i=0;
for(i=0;i<4;i++)
{
chaoshengbo[i]=read_eeprom(i);
Delay100ms();
}
Cls_Peripheral();
Timer1Init();
Timer0Init();
times=0;
while(1)
{
leddo();
Key_Proc();
show();
if(ulms2==100)
{
dacdo();
}
}
}
void Time_1(void) interrupt 3
{
ulms++;
ulms1++;
ulms2++;
if(led1==1)
{
if(ulms3%1000==0)
{

if(led2==1)
{
led|=0x01;
}
else
{
led&=0xfe;
}
led2=~led2;
}

ulms3++;
if(ulms3>5000)
{
ulms3=0;
led1=0;
}
}
if(++ucKey_Dly == 10)
ucKey_Dly = 0;
Led_Disp(led);
Seg_Disp(pucSeg_Code, ucSeg_Pos);
if(++ucSeg_Pos == 8) ucSeg_Pos = 0;

if(ulms2==200)
{
ulms2=0;
}
}
void Key_Proc(void)
{
unsigned char ucKey_Val, ucKey_Down, ucKey_Up;
if(ucKey_Dly) return;
ucKey_Dly = 1;
ucKey_Val = Key_Read();
ucKey_Down = ucKey_Val & (ucKey_Old ^ ucKey_Val);//^按位异或
ucKey_Up = ~ucKey_Val & (ucKey_Old ^ ucKey_Val);
ucKey_Old = ucKey_Val;
if(ucKey_Down)//判断old是否和新的一样,一样说明是同一个,不会进入
{
ulKey_Time = ulms;
keydo(ucKey_Down);
}
if(ucKey_Old && (ulms - ulKey_Time > 800))
{
ulKey_Time = ulms;
ulKey_Time-=400;
keydo(ucKey_Old);
}
}

posted @ 2020-11-13 12:06  通信小九  阅读(350)  评论(0编辑  收藏  举报