//串口通讯驱动程序
#include <iom128v.h>
#include <macros.h>
#include "Usart.h"
#include "DS1302.h"
#include "Buzzer.h"
#include "Print.h"
#include "SED1335.h"
unsigned char data[17];
unsigned char Datalength;
unsigned char Timeability=0,Testability=0,Testfanhui=0;
extern unsigned char day;
extern unsigned char date;
extern unsigned char month;
extern unsigned char year;
extern unsigned char sampleNumber;
extern unsigned char ucSettime[8];
extern unsigned int SettingTemp; //温度采样设定值
extern unsigned int airPressure;//大气压
extern unsigned char status;//菜单号
extern unsigned char OldStatus;//上次菜单号
extern unsigned char shi[2];//修改小时
extern unsigned char fen[2];//修改分钟
extern unsigned char miao[2];//修改秒
extern unsigned char nian[2];//修改年
extern unsigned char yue[2];//修改月
extern unsigned char ri[2];//修改日
extern unsigned char fh[1];//修改所用符号
extern unsigned char second;//秒
extern unsigned char minute;//分
extern unsigned char hour;//时
//UART0 initialize
// desired baud rate: 9600
// actual: baud rate:9600 (0.0%)
// char size: 8 bit
// parity: Disabled
void uart0_init(void)
{
UCSR0B = 0x00; //disable while setting baud rate
UCSR0A = 0x00;
UCSR0C = 0x06;
UBRR0L = 0x47; //set baud rate lo
UBRR0H = 0x00; //set baud rate hi
UCSR0B = 0x98;
}
void sendchar(unsigned char data1)//**********串口以查询方式发送数据*/
{
while(!(UCSR0A & (1<<UDRE0)));
UDR0=data1;
}
#pragma interrupt_handler uart0_rx_isr:19/*****串口以中断方式接受数据*/
void uart0_rx_isr(void)
{
//uart has received a character in UDR
unsigned char j=0;
CLI();
data[0]=UDR0;
if(data[0]==0x68)/**起始位检测*/
{
do
{
while(!(UCSR0A & (1<<RXC0)));
j++;
data[j]=UDR0;
if(data[j]==0x16)
{
if(j>=8)
{
Datalength=j;
break;/**结束位检测*/
}
}
}
while(1);
}
Dataprocessing();/***********数据处理*/
SEI();
}
void Dataprocessing(void)//**********接收到PC用串口传送过来的数据,进行相应的处理。*/
{
unsigned char Datacheck=0,k=0,h=0;
unsigned char temp=0;
for(k=0;k<=(Datalength-2);k++)
{
Datacheck=Datacheck+data[k];
}
if(data[Datalength-1]==Datacheck)
{
if((data[4]==0x01) && (data[5]==0x01))//**进入正在测试菜单
{
status=11;
Testability=1;
}
if((data[4]==0x03) && (data[5]==0x01))//**预闪值的设定**/
{
temp=0;
status=1;//**跳转到进入测试菜单*/
OldStatus=2;
SettingTemp=data[6]*10;
if(SettingTemp>3000 || SettingTemp<300)//**预闪值设定出现异常*/
{
if(SettingTemp>3000)
{
SettingTemp=300;
}
if(SettingTemp<300)
{
SettingTemp=3000;
}
data[6]=0x11;
Datacheck=0;
for(k=0;k<=(Datalength-2);k++)
{
Datacheck=Datacheck+data[k];
}
data[7]=Datacheck;
for(h=0;h<=Datalength;h++)
{
sendchar(data[h]);
}
}
else//**预闪值设定完毕无误*/
{
data[6]=0x10;
Datacheck=0;
for(k=0;k<=(Datalength-2);k++)
{
Datacheck=Datacheck+data[k];
}
data[7]=Datacheck;
for(h=0;h<=Datalength;h++)
{
sendchar(data[h]);
}
}
//***********************保存新修改的预闪值的设定****begin*/
write_byte(w_protect,0x00);
temp=(unsigned char)(SettingTemp>>8);
W1302(0xc8, temp);
temp=(unsigned char)SettingTemp & 0x00ff;
W1302(0xca, temp);
write_byte(w_protect,0x80);
//***********************保存新修改的预闪值的设定****end*/
}
if((data[4]==0x04) && (data[5]==0x02))//**大气压值的设定***/
{
temp=0;
status=1;//**跳转到进入测试菜单*/
OldStatus=2;
airPressure=data[6]+data[7]*10;
if(airPressure>1200 || airPressure<640 || data[6]>=10)//**大气压值设定出现异常*/
{
if(airPressure>1200)
{
airPressure=1200;
}
if(airPressure<640)
{
airPressure=640;
}
if(data[6]>=10)
{
airPressure=640;
}
data[6]=0x11;
Datacheck=0;
for(k=0;k<=(Datalength-3);k++)
{
Datacheck=Datacheck+data[k];
}
data[7]=Datacheck;
data[8]=0x16;
for(h=0;h<=8;h++)
{
sendchar(data[h]);
}
}
else//**大气压值设定完毕无误*/
{
data[6]=0x10;
Datacheck=0;
for(k=0;k<=(Datalength-3);k++)
{
Datacheck=Datacheck+data[k];
}
data[7]=Datacheck;
data[8]=0x16;
for(h=0;h<=8;h++)
{
sendchar(data[h]);
}
}
//***********************保存新修改的大气压值的设定****begin*/
write_byte(w_protect,0x00);
temp=(unsigned char)(airPressure>>8);
W1302(0xcc, temp);
temp=(unsigned char)airPressure & 0x00ff;
W1302(0xce, temp);
write_byte(w_protect,0x80);
//***********************保存新修改的大气压值的设定****end */
}
if((data[3]==0x02) && (data[4]==0x06))//**时钟设置*/
{
Timeability=1;
status=4;//**跳转到修改当前时间菜单*/
OldStatus=40;
if((data[5]>59)||(data[5]<0)||(data[6]>59)||(data[6]<0)||(data[7]>23)||(data[7]<0))
{
//**时钟设置设定异常有误*/
//**************秒的判断*/
if(data[5]>59)
{
data[5]=0;
}
else if(data[5]<0)
{
data[5]=59;
}
//**************分的判断*/
if(data[6]>59)
{
data[6]=0;
}
else if(data[6]<0)
{
data[6]=59;
}
//*************小时的判断*/
if(data[7]>23)
{
data[7]=0;
}
else if(data[7]<0)
{
data[7]=23;
}
hour=data[7];
minute=data[6];
second=data[5];
data[1]=0x01;
data[5]=0x11;
Datacheck=0;
for(k=0;k<=(Datalength-4);k++)
{
Datacheck=Datacheck+data[k];