要求:
(1)组成2~3人团队,在室温、板载电压、ROM密钥等目标中选定PC串口查询系统的功能,并确定本人的分工;
(2)创建KeilC工程,使开发板接收PC串口发来的数据并原样返回,实现echo功能;
(3)编写代码,使PC串口工具对话框中输入特定指令能获得开发板回复的数据;
(4)编写代码,使PC串口工具对话框中输入的指令能正确获得分工部分的数据。
#include <REGX52.H>
#include "XPT2046.h"
#include "UART.h"
#include "Delay.h"
#include "string.h"
#include "math.h"
#include "AT24C02.h"
#define MAX_NUM 3 //转换成字符串数字的位数,默认为3位,即0-255
unsigned char ADValue;//从XPT2046芯片获取到的模拟值
unsigned char write_data,read_data;
/**
* @brief 将数字转成字符串形式,并打印在屏幕上
* @param Num数字
* @retval 指向字符串的指针
*/
void Num_To_String(unsigned char Num)
{
unsigned char num = 0;
unsigned char Digit[MAX_NUM + 1] = { 0 };
unsigned char ind = MAX_NUM - 1;
while(Num)
{
num = Num % 10;
Digit[ind--] = num + '0';
Num /= 10;
}
UART_SendString(Digit);
UART_SendString(" ");
}
unsigned char option,i,t1,t2,t3,t4,t5;
void main()
{
UART_Init(); //串口初始化
while(1);
}
//串口中断函数模板
void UART_Routine() interrupt 4
{
unsigned int temp=0; //接收温度
// unsigned char c; //c摄氏度*10
double C; //C摄氏度
unsigned char Byte_data = 0; //用来存储我们接收到的1字节数据
static unsigned char rec_str[80] = {0}; //开80个字节的数组,用来存储我们接收到的数据(电脑)
if(RI==1) //接收中断发生
{
Byte_data = SBUF; //将SBUF缓冲器里面的内容存到Byte_data中
i++;
option = SBUF;
/*--------------------------------写入数据到rom-------------------------------------*/
if(t1==1 && t2==1 && t3==1 && t4==1 && t5==1)
{
t1=0;t2=0;t3=0;t4=0;t5=0;i=0;
write_data=Byte_data;
AT24C02_WriteByte(1,write_data);
memset(rec_str,0,sizeof(rec_str));
}
//write
if(i==1 && option=='w')
{
t1=1;
}
if(i==2 && option=='r' && t1==1)
{
t2=1;
}
if(i==3 && option=='i' && t2==1)
{
t3=1;
}
if(i==4 && option=='t' && t3==1)
{
t4=1;
}
if(i==5 && option=='e' && t4==1)
{
t5=1;
}
if(t1==1 && t2==1 && t3==1 && t4==1 && t5==1)
{
UART_SendString("input rom:\r");
}
/*------------------------------------------------------------------------------*/
if(Byte_data!='?')
{
strncat(rec_str,&Byte_data,1);
}
else
{
/*--------------------------------显示电压-------------------------------------*/
if (!strcmp(rec_str,"vbat"))
{
ADValue = LOAD_AD(XPT2046_VBAT_8); //显示电压存到ADValue中
UART_SendString("vbat value:\r");
Num_To_String(ADValue);i=0;
}
/*--------------------------------显示温度-------------------------------------*/
else if (!strcmp(rec_str,"temperature"))
{
UART_SendString("The temperature is:\r");
temp = LOAD_AD(XPT2046_YP_12);
C = ((125.000/4095)*temp)-40; //XPT2046测量范围-40到+85
/** c = (char)(C*10); //不显示小数点20.5->205**/
Num_To_String(C); i=0;
}
/*--------------------------------读取rom-------------------------------------*/
else if (!strcmp(rec_str,"read"))
{
UART_SendString("data of rom:\r");
read_data=AT24C02_ReadByte(1);
UART_SendByte(read_data);
}
memset(rec_str,0,sizeof(rec_str)); //发送完数据后,rec_str数组清空,重新接收新的数据
ADValue = 0; //AdValue阻值清零
}
RI = 0;
}
}