前言:
本篇主要分享关于串口方面的测评,因题目较为简单 就不做具体的分析了
试题:
代码:
#include <STC15F2K60S2.H>
#include "Nixie.h"
#include "Key.h"
#include "InitHC138.h"
#include "Uart.h"
#include "iic.h"
#include "onewire.h"
#include "Delay.h"
#include "stdio.h"
bit flag_t=0;
float t;
unsigned char temp=0x00;
unsigned int vcc;
bit turn=0;
unsigned char Uart_Send[10];
unsigned char Uart_Recv[1];
unsigned char Uart_Recv_Index;
bit lock=0;//锁住串口
bit shine=0;
bit flag_Uart=0;
unsigned char flag=0;
unsigned char Key_Val,Key_Old,Key_Down,Key_Up;
void Timer0_Init(void) //1毫秒@12.000MHz
{
AUXR &= 0x7F; //定时器时钟12T模式
TMOD &= 0xF0; //设置定时器模式
TL0 = 0x18; //设置定时初始值
TH0 = 0xFC; //设置定时初始值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
ET0 = 1; //使能定时器0中断
EA=1;
}
//LED
void Led_Config(unsigned char pin,state)
{
unsigned char i,ledstate;
static unsigned char Led[8]={0,0,0,0,0,0,0,0};
if(Led[pin]!=state)
{
Led[pin]=state;
ledstate=0xff;
for(i=0;i<8;i++)
{
ledstate=ledstate^(Led[i]<<i);
}
P0=ledstate;
InitHC138(4);
}
}
//继电器
void Relay(unsigned char flag)
{
static unsigned char temp_old=0xff;
if(flag)
temp|=0x10;
else
temp&=~0x10;
if(temp!=temp_old)
{
P0=temp;
InitHC138(5);
temp_old=temp;
}
}
//蜂鸣器
void Beep(unsigned char flag)
{
static unsigned char temp_old=0xff;
if(flag)
temp|=0x40;
else
temp&=~0x40;
if(temp!=temp_old)
{
P0=temp;
InitHC138(5);
temp_old=temp;
}
}
//初始化
void InitSystem()
{
P0=0xff;
InitHC138(4);
P0=0x00;
InitHC138(5);
P0=0xff;
InitHC138(7);
}
//温度显示界面
void Temper()
{
Nixie_SetBuf(1,16);
Nixie_SetBuf(2,1);
Nixie_SetBuf(3,17);
Nixie_SetBuf(4,17);
Nixie_SetBuf(5,17);
point[7]=1;
point[6]=0;
Nixie_SetBuf(6,(unsigned char)t/10);
Nixie_SetBuf(7,(unsigned char)t%10);
Nixie_SetBuf(8,(unsigned int)(t*10)%10);
}
//电压显示界面
void Dianya()
{
Nixie_SetBuf(1,16);
Nixie_SetBuf(2,2);
Nixie_SetBuf(3,17);
Nixie_SetBuf(4,17);
Nixie_SetBuf(5,17);
point[7]=0;
point[6]=1;
Nixie_SetBuf(6,vcc/100);
Nixie_SetBuf(7,vcc/10%10);
Nixie_SetBuf(8,vcc%10);
}
void Key_Loop()
{
Key_Val=ScanKey();
Key_Down=Key_Val&(Key_Old^Key_Val);
Key_Up=~Key_Val&(Key_Old^Key_Val);
Key_Old=Key_Val;
if(Key_Down==4)
{
lock=1;
}
else if(Key_Down==5)
{
lock=0;
}
else if(Key_Down==12)
{
if(turn==0)flag=1;
else if(turn==1)flag=2;
}
}
//数据读取
void Read_Data()
{
if(flag_t==1)
{
t=read_t();
flag_t=0;
vcc=(AD_Read(0x43)/51.0)*100;
}
if(turn==0)
{
Temper();
Led_Config(0,1);
Led_Config(1,0);
}
else if(turn==1)
{
Dianya();
Led_Config(1,1);
Led_Config(0,0);
}
if(lock==1)
{
if(shine==0)Led_Config(2,0);
else if(shine==1)Led_Config(2,1);
}
else Led_Config(2,0);
if(t>=28)Relay(1);
else Relay(0);
if((AD_Read(0x43)/51.0)>3.6)Beep(1);
else Beep(0);
}
//串口
void Uart_Loop()
{
if(Uart_Recv_Index==1)
{
if(Uart_Recv[0]=='A')turn=0;
else if(Uart_Recv[0]=='B')turn=1;
Uart_Recv_Index=0;
}
if(flag==1)
{flag=0;
printf("TEMP:%.2f℃",t);
}
else if(flag==2)
{flag=0;
printf("Voltage:%.2fV",(AD_Read(0x43)/51.0));
}
}
void main()
{
InitSystem();
Timer0_Init();
Uart1_Init();
read_t();
Delay(750);
while(1)
{
Read_Data();
if(flag_Uart==1)
{
Uart_Loop();
flag_Uart=0;
}
}
}
void Timer0_Isr() interrupt 1
{
static unsigned char count1;
static unsigned char count2;
static unsigned char count3;
static unsigned char count4;
static unsigned char count5;
count1++;
count2++;
count3++;
count5++;
if(count5>=200)
{
flag_Uart=1;
count5=0;
}
if(lock==1)
count4++;
if(count1>=2)
{
count1=0;
Nixie_Loop();
}
if(count2>=10)
{
count2=0;
Key_Loop();
}
if(count3>=200)
{
count3=0;
flag_t=1;
}
if(count4<=100)
{
shine=0;
}
else if(count4<=200)
{
shine=1;
}
if(count4>200)count4=0;
}
void Uart1_Isr(void) interrupt 4
{
if (RI==1) //检测串口1接收中断
{
if(lock==0)
{
Uart_Recv[Uart_Recv_Index]=SBUF;
Uart_Recv_Index++;
}
RI = 0; //清除串口1接收中断请求位
}
}
/*
#ifndef ___H__
#define ___H__
#endif
*/