2024-08-16 10:09:24 +08:00

246 lines
5.2 KiB
C

#include <uart.h>
#include <config.h>
bool UART_SEND_OK = FALSE;
bool UART_OK = FALSE;
static bool busy = FALSE;
static u8 UART_RST_NUM = 0;
static u8 old_data = 0;
static u8 UART_NUM = 0;
char wptr;
char rptr;
char buffer[16];
extern u8 hour;
extern u8 min;
extern u8 sec;
extern xdata u16 FILA_FAN_TIME;
extern u16 FAN_TEMP;
extern u8 PWM_DUT;
extern xdata u8 FILA_PTC_TIME;
extern xdata u16 FILA_FAN_TIME;
extern u8 ACC_FREQ;
extern u8 PTC_DEL_TIME;
void UartSend2(char dat);
void UartSendStr2(char *p);
void UartSendNumRN(u16 n);
void UartSendEcho();
void UartIsr() interrupt 4
{
char c = 0;
if (TI)
{
TI = 0;
busy = FALSE;
}
if (RI)
{
RI = 0;
c = SBUF;
if (c != old_data)
{
UART_RST_NUM = 0;
}
else
{
if (UART_RST_NUM < 100)
UART_RST_NUM++;
if (UART_RST_NUM > 50)
IAP_CONTR |= 0x60;
}
buffer[wptr++] = SBUF;
wptr &= 0x0f;
}
}
void Uart2Isr() interrupt 8
{
char c;
if (S2CON & 0x02)
{
S2CON &= ~0x02;
busy = FALSE;
}
if (S2CON & 0x01)
{
S2CON &= ~0x01;
c = S2BUF;
if (c == '<')
UART_NUM = 0;
if (c == '>')
UART_OK = TRUE;
if (UART_NUM < 16)
{
buffer[UART_NUM] = c;
UART_NUM++;
}
}
}
void UartInit()
{
SCON |= 0x50;
T2L = BRT;
T2H = BRT >> 8;
AUXR |= 0x15;
ES = 1;
}
void UartInit2()
{
S2CON |= 0x10;
T2L = BRT;
T2H = BRT >> 8;
AUXR |= 0x14;
IE2 = 0x01;
}
void UartSend(char dat)
{
while (busy);
busy = TRUE;
SBUF = dat;
}
void UartSend2(char dat)
{
while (busy);
busy = TRUE;
S2BUF = dat;
}
void UartSendStr2(char *p)
{
while (*p)
{
UartSend2(*p++);
}
}
#if 0
// void UartSendStr(char *p)
// {
// while (*p)
// {
// UartSend(*p++);
// }
// }
// void UartSendNum(u16 n)
// {
// UartSend('0' + n % 100000 / 10000);
// UartSend('0' + n % 10000 / 1000);
// UartSend('0' + n % 1000 / 100);
// UartSend('0' + n % 100 / 10);
// UartSend('0' + n % 10);
// }
// void UartSendNumRN(u16 n)
// {
// UartSend('0' + n % 100000 / 10000);
// UartSend('0' + n % 10000 / 1000);
// UartSend('0' + n % 1000 / 100);
// UartSend('0' + n % 100 / 10);
// UartSend('0' + n % 10);
// UartSend('\r');
// UartSend('\n');
// }
#endif
void UartSendEcho()//PWM
{
if(UART_SEND_OK==TRUE)
{
UartSend('T');
UartSend('>');
UartSend('0'+hour%100000/10000);
UartSend('0'+hour%10000/1000);
UartSend('0'+hour%1000/100);
UartSend('0'+hour%100/10);
UartSend('0'+hour%10);
UartSend(':');
UartSend('0'+min%100/10);
UartSend('0'+min%10);
UartSend(':');
UartSend('0'+sec%100/10);
UartSend('0'+sec%10);
UartSend('\r');UartSend('\n');
UartSend('T');UartSend('E');UartSend('M');UartSend('P');
UartSend(':');
UartSend('<');
UartSend('0'+FAN_TEMP%1000/100);
UartSend('0'+FAN_TEMP%100/10);
UartSend('.');
UartSend('0'+FAN_TEMP%10);
UartSend('C');
UartSend('>');
UartSend('\r');UartSend('\n');
UartSend('F');UartSend('A');UartSend('N');
UartSend(':');
UartSend('<');
UartSend('0'+PWM_DUT%1000/100);
UartSend('0'+PWM_DUT%100/10);
UartSend('0'+PWM_DUT%10);
UartSend('%');
UartSend('>');
UartSend('\r');UartSend('\n');
UartSend('A');UartSend('C');UartSend(':');
UartSend('<');
UartSend('0'+ACC_FREQ%1000/100);
UartSend('0'+ACC_FREQ%100/10);
UartSend('0'+ACC_FREQ%10);
UartSend('>');
UartSend('\r');UartSend('\n');
UartSend('P');UartSend('W');UartSend('R');UartSend(':');UartSend('<');if(IN_PWR==0)UartSend('1');else UartSend('0');UartSend('>');
UartSend('\r');UartSend('\n');
UartSend('P');UartSend('T');UartSend('C');UartSend(':');UartSend('<');if(IN_PTC==0)UartSend('1');else UartSend('0');UartSend('>');
UartSend('<');
if(PTC_RLY_1)UartSend('0');else UartSend('1');UartSend(',');
if(PTC_RLY_2)UartSend('0');else UartSend('1');UartSend(',');
if(PTC_RLY_3)UartSend('0');else UartSend('1');UartSend(',');
if(PTC_RLY_FAN)UartSend('0');else UartSend('1');UartSend('>');
UartSend('<');UartSend('0'+PTC_DEL_TIME%100/10);UartSend('0'+PTC_DEL_TIME%10);UartSend('>');
UartSend('\r');UartSend('\n');
UartSend('B');UartSend('E');UartSend('D');UartSend(':');UartSend('<');if(IN_BED==0)UartSend('1');else UartSend('0');UartSend('>');
UartSend('\r');UartSend('\n');
UartSend('A');UartSend('C');UartSend('C');UartSend(':');UartSend('<');if(PW_OUT_ACC)UartSend('1');else UartSend('0');UartSend('>');
UartSend('\r');UartSend('\n');
UartSend('F');UartSend('I');UartSend('L');UartSend('A');
UartSend('<');
UartSend('0'+FILA_PTC_TIME%1000/100);
UartSend('0'+FILA_PTC_TIME%100/10);
UartSend('0'+FILA_PTC_TIME%10);
UartSend('>');
UartSend('<');
UartSend('0'+FILA_FAN_TIME%100000/10000);
UartSend('0'+FILA_FAN_TIME%10000/1000);
UartSend('0'+FILA_FAN_TIME%1000/100);
UartSend('0'+FILA_FAN_TIME%100/10);
UartSend('0'+FILA_FAN_TIME%10);
UartSend('>');
UART_SEND_OK=FALSE;
}
}
void UART_CMD()//串口命令解析
{
if(UART_OK)
{
if(buffer[0]=='<' && buffer[1]=='A' && buffer[2]=='S' && buffer[3]=='K' && buffer[4]=='?'&& buffer[5]=='>')
{
if(PW_OUT_ACC==1)UartSendStr2("<ASK>");
else UartSendStr2("<OFF>");
}
if(buffer[0]=='<' && buffer[1]=='H' && buffer[12]=='>')
{
if(buffer[2]=='1')FILA_PTC_TIME=255;
FILA_FAN_TIME=20000;
}
UART_OK=FALSE;
}
UartSendEcho();//串口反馈 1S 一次
}