#include <reg52.h>
#include<MATH.H>
#include <intrins.h>
#define nop _nop_();_nop_();_nop_();_nop_();_nop_(); //宏定义
#define uchar unsigned char
#define uint unsigned int
sbit Key_Forw=P1^7;
sbit Key_BackOff=P1^3;
sbit Key_Left=P1^6;
sbit Key_Right=P1^4;
sbit Key_Stop=P1^5;
uchar gSysDat[10] = {0,0,0,0,0,0,0,0,0,0};
uchar receIndex=0,receByte=0,receState=0,Header=0,Soucheck=0,i=0,datarece=0,Bytec=0,Bytecount=0,receNew=0;
uchar RecBuf[10]= {0,0,0,0,0,0,0,0,0,0};
uchar jsnum = 0;
bit Qued = 0;
uchar CarSta = 0;
void InitUART(void)
{
TMOD = 0x21;
SCON = 0x50;
TH1 = 0xFA;
TL1 = TH1;
PCON = 0x80;
ES = 1;
TR1 = 1;
}
void UARTInterrupt(void) interrupt 4
{
if(RI) {
RI=0;
receByte = SBUF;
if(receByte==0xCC){ //帧头校验
if(receState++==5){
Header=1;
receState=0;
Soucheck = 0;
}else if(receState>5){
receState=0;
Header=0;
}
}else{
receState = 0;
}
if(Header==1){ //目标地址校验
if(receByte==0x00){
Soucheck=1;
Header=0;
}
}
if(Soucheck==1&&Header==0){
if(receByte==0x01){
Soucheck=0;
Bytec = 0;
datarece = 0;
i=0;
Bytec=1;
}
}
if(Bytec==1&&i++==1){
Bytec=0;
Bytecount=receByte; //读取命令字节数(除检验位)
receIndex = 0;
datarece=1;
for(i=0;i<Bytecount;i++)
RecBuf[i] = 0x00;
i=0;
}
if(datarece==1&&Bytec==0&&i++==1){
i=1;
if(receIndex <Bytecount){
RecBuf[receIndex++] = receByte;
if(receIndex == Bytecount){ //进行相应数据操作
receNew = 1;
}
}else{
datarece=0;
receIndex=0;
i=0;
}
}
}
}
void Delay_1ms(uint t)
{
unsigned int i,j;
for(i=0; i<t; i++)
for(j=0; j<150; j++);
}
void Uart_byte(unsigned char adata)
{
TI=0;
SBUF=adata;
while(!TI);
TI=0;
}
void UartSeedDat(uchar *pucStr,uchar ulNum)
{
uchar i = 0;
for(i=0;i<6;i++){
Uart_byte(0xEE);
}
Uart_byte(0x00);
Uart_byte(0x01);
Uart_byte(0x07);
for(i=0;i<ulNum;i++){
Uart_byte(*pucStr++);
}
Uart_byte(0x00);
}
void KeyInter(void)
{
if(Key_Stop == 0){
Delay_1ms(5);
if(Key_Stop == 0){
CarSta = 0;
gSysDat[0] = CarSta;
gSysDat[1] = RecBuf[0];
gSysDat[2] = RecBuf[1];
gSysDat[3] = RecBuf[2];
gSysDat[4] = RecBuf[3];
gSysDat[5] = RecBuf[4];
UartSeedDat(gSysDat,6);
while(!Key_Stop);
}
}
if(Key_Forw == 0){
Delay_1ms(5);
if(Key_Forw == 0){
CarSta = 1;
gSysDat[0] = CarSta;
gSysDat[1] = RecBuf[0];
gSysDat[2] = RecBuf[1];
gSysDat[3] = RecBuf[2];
gSysDat[4] = RecBuf[3];
gSysDat[5] = RecBuf[4];
UartSeedDat(gSysDat,6);
while(!Key_Forw);
}
}
if(Key_BackOff == 0){
Delay_1ms(5);
if(Key_BackOff == 0){
CarSta = 2;
gSysDat[0] = CarSta;
gSysDat[1] = RecBuf[0];
gSysDat[2] = RecBuf[1];
gSysDat[3] = RecBuf[2];
gSysDat[4] = RecBuf[3];
gSysDat[5] = RecBuf[4];
UartSeedDat(gSysDat,6);
while(!Key_BackOff);
}
}
if(Key_Left == 0){
Delay_1ms(5);
if(Key_Left == 0){
CarSta = 3;
gSysDat[0] = CarSta;
gSysDat[1] = RecBuf[0];
gSysDat[2] = RecBuf[1];
gSysDat[3] = RecBuf[2];
gSysDat[4] = RecBuf[3];
gSysDat[5] = RecBuf[4];
UartSeedDat(gSysDat,6);
while(!Key_Left);
}
}
if(Key_Right == 0){
Delay_1ms(5);
if(Key_Right == 0){
CarSta = 4;
gSysDat[0] = CarSta;
gSysDat[1] = RecBuf[0];
gSysDat[2] = RecBuf[1];
gSysDat[3] = RecBuf[2];
gSysDat[4] = RecBuf[3];
gSysDat[5] = RecBuf[4];
UartSeedDat(gSysDat,6);
while(!Key_Right);
}
}
}
void Timing_Send(void)
{
if(Qued == 1){
Qued = 0;
gSysDat[0] = CarSta;
gSysDat[1] = RecBuf[0];
gSysDat[2] = RecBuf[1];
gSysDat[3] = RecBuf[2];
gSysDat[4] = RecBuf[3];
gSysDat[5] = RecBuf[4];
UartSeedDat(gSysDat,6);
}
}
//50Ms定时
void InitTimer0(void)
{
TH0 = 0x4C;
TL0 = 0x00;
EA = 1;
ET0 = 1;
TR0 = 1;
}
void main()
{
InitTimer0();
InitUART();
Delay_1ms(10);
while(1)
{
KeyInter();
Timing_Send();
}
}
void timer0()interrupt 1 using 2
{
TH0=0X4C; //50Ms定时
TL0=0X00;
if(jsnum++>=19){
jsnum = 0;
Qued = 1;
}
}
|