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

//main.c  串口稍有瑕疵,不太会改

#include "seg.h"
#include "onewire.h"
#include "tim.h"
#include "uart.h"
#include "key.h"
#include "ultrasonic.h"
#include "stdio.h"
#include "stc15f2k60s2.h"
#include "i2c.h"
#define u8 unsigned char
#define u16 unsigned int
u8 mode1=0;
u8 pucSeg_Buf[11]=0,ucSeg_Pos=0,pucSeg_Code[11];
u8 times=0;
u8 chao=0;
u16 time1=0;
u16 cishu=0;//记录变更次数
u16 wencan=30,jucan=35;
int te;
bit mode2=0;
u16 biangeng=0;//变更次数
bit led=0;
bit dacdo=1;
bit led1=0;
bit led2=0;
bit led3=0;
u16 wencan1=30,jucan1=35;//用来作比较判断是否产生了变动
u8 ucUart_Num;//记录串口位数
u16 ulms;//时间
u8 a[12];//存放接收数据
//u16 time2=0;//记录按下时间
void Uart_Proc(void);
void Delay100ms() //@11.0592MHz
{
unsigned char i, j, k;
i = 5;
j = 52;
k = 195;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void dac()//DAC
{

if(chao>jucan)
{
Pcf8591_dac(255/5*4);
}
else
{
Pcf8591_dac(255/5*2);
}
}
void show()
{
int i=0;
for(i=0;i<8;i++)
{
pucSeg_Buf[i]=' ';
}
if(mode2==0)//数据界面
{
if(mode1==0)//温度
{
pucSeg_Buf[0]='C';
pucSeg_Buf[1]=' ';
pucSeg_Buf[2]=' ';
pucSeg_Buf[3]=' ';
sprintf(pucSeg_Buf+3, " %04.2f", (int)te/16.0);//打入字符串,以字符形式储存
}
if(mode1==1)//超声波数据界面
{
pucSeg_Buf[0]='L';
sprintf(pucSeg_Buf+5, " %02u", (unsigned int)chao);//打入字符串,以字符形式储存
}
if(mode1==2)//变更次数
{
int i=0;
int wei=0;
int q=1;
pucSeg_Buf[0]='N';
pucSeg_Buf[1]=' ';
pucSeg_Buf[2]=' ';
sprintf(pucSeg_Buf+2, " %05d", (unsigned int)cishu);//打入字符串,以字符形式储存

for(i=0;i<5;i++)
{
if(cishu/q!=0)
{
q=q*10;
wei=wei+1;
}
}
for(i=0;i<5-wei;i++)
{
pucSeg_Buf[3+i]=' ';
}
if(cishu==0)
{
pucSeg_Buf[7]='0';
}
}
}
else//参数界面
{
pucSeg_Buf[0]='P';

if(mode1==0)
{
pucSeg_Buf[3]='1';
sprintf(pucSeg_Buf+5, " %02u", (unsigned int)wencan);//打入字符串,以字符形式储存
}
else
{
pucSeg_Buf[3]='2';
sprintf(pucSeg_Buf+5, " %02u", (unsigned int)jucan);//打入字符串,以字符形式储存
}
}
Seg_Tran(pucSeg_Buf,pucSeg_Code);
}
void keydo()//按键反应
{
if(time2>70)//长按
{
if(key_value==12)
{
cishu=0;
}
if(key_value==11)
{
dacdo=!dacdo;
}
}
else//短按
{
if(key_value==11)//切换界面
{
if(mode2==0)
{
mode2=~mode2;
jucan1=jucan;
wencan1=wencan;
}
else if(mode2==1)
{
mode2=~mode2;
if(jucan1!=jucan||wencan1!=wencan)
{
u16 w,ww;
cishu=cishu+1;
w=cishu/256;
ww=cishu%256;
write_eeprom(50,w);//存储高8位
Delay100ms();
write_eeprom(60,ww);//存储低8位
}
}
mode1=0;
}
if(key_value==12)
{
if(mode2==0)//数据界面
{
mode1+=1;
mode1%=3;
}
else//参数界面
{
mode1+=1;
mode1%=2;
}
}
if(key_value==16)
{
if(mode2==1&&mode1==0)
{
wencan-=2;
}
if(mode2==1&&mode1==1)
{
jucan-=2;
}
}
if(key_value==15)
{
if(mode2==1&&mode1==0)
{
wencan+=2;
}
if(mode2==1&&mode1==1)
{
jucan+=2;
}
}
}
key_value=0;
}
void leddo()
{
u8 q=0;
q|=led3;
q<<=1;
q|=led2;
q<<=1;
q|=led1;
Led_Disp(q);
}
void main()
{
Cls_Peripheral();
Timer1Init();
Timer0Init();
UartInit();
cishu=read_eeprom(60)+read_eeprom(50)*256;//从05.06读取数据
while(1)
{
if(te/16>wencan)
{
led1=1;
}
else
{
led1=0;
}
if(chao>jucan)
{
led2=1;
}
else
{
led2=0;
}
if(dacdo==1)
{
dac();
led3=1;
}
else
{
led3=0;
Pcf8591_dac(23);
}
Uart_Proc();
show();
Key_Scan();//矩阵检测
Key_Judge();
if(time1==100)
{
te=DS18B20_Read();
time1=0;
}
if(ulms==100)
{
chao=Wave_Recv(); //超声波
ulms = 0;
}
if(key_flag!=0)
{
u16 time3=0;
while(1)
{
key_flag=0;
Key_Scan();//矩阵检测
Key_Judge();
if(time3!=time2)
{
time3=time2;
}
else
{
break;
}
}
keydo();
time2=0;
}
}
}
void Time_1(void) interrupt 3
{
leddo();
time1++;
ulms++;
if(ulms>100)
{
ulms=0;
}
if(time1>100)
{
time1=0;
}
Seg_Disp(pucSeg_Code, ucSeg_Pos);//数码管显示功能
if(++ucSeg_Pos == 8) ucSeg_Pos = 0;
}
void Uart_Proc(void)
{
//u8 i;
if(ucUart_Num > 0)
{
if(a[ucUart_Num-1] == 0xa)//换行
{
if(a[0]=='S'&&a[1]=='T'&&(a[2]==0xd))//oxd换行
{
sprintf(a,"$%02u,%04.2f\r\n",(unsigned int)chao, (float)te/16.0);
}
else if(a[0]=='P'&&a[1]=='A'&&a[2]=='R'&&a[3]=='A'&&(a[4]==0xd))
{
sprintf(a,"$%02u,%02U\r\n",(unsigned int)jucan, (unsigned int)wencan);
//Uart_SendString(a);
}
else if(ucUart_Num>ucUart_Num-1)
{
sprintf(a,"ERROR\r\n");
//Uart_SendString("ERROR\r\n");
}
ucUart_Num=0;
Uart_SendString(a);
}
else if(ucUart_Num>6)
{
Uart_SendString("ERROR\r\n");
ucUart_Num = 0;
}
}
else if(ucUart_Num>6)
{
Uart_SendString("ERROR\r\n");
ucUart_Num = 0;
}
}
void uart_0(void) interrupt 4
{
if(RI)
{
a[ucUart_Num++] = SBUF;
RI = 0;
}
}

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