|
#include "sys.h"
#include "LCD_UartDriver.h"
#include "T5LOS8051.h"
#include "LCD_Can.h"
#include "LCD_GlobalVarDef.h"
#define Uart4_Tx_Lenth 14
#define Uart4_Rx_Lenth 14
//============================
// 模块内常量定义
//============================
#define C_SCI_DATA_LENGTH 14
struct Uartx_Define Uart4;
struct Uartx_Define Uart2;
//============================
// 静态局部变量定义
//============================
static uint8_t u8SciProcessStep; // 通信过程状态机状态
static uint8_t u8SciBytePoint = 0; // 发送和接收数据指针
static uint8_t u8SciReceEndTimer = 0; // 接收状态计时器
#if UART2_ENABLE
u8 xdata R_u2[UART2_RX_LENTH];
#else
u8 xdata R_u2[20];
#endif
u8 xdata R_OD2 = 0;
u16 xdata R_CN2 = 0;
u8 xdata T_O2 = 0;
bit Busy2 = 0;
#if UART3_ENABLE
u8 xdata R_u3[UART3_RX_LENTH];
#else
u8 xdata R_u3[20];
#endif
u8 xdata R_OD3 = 0;
u16 xdata R_CN3 = 0;
u8 xdata T_O3 = 0;
bit Busy3 = 0;
#if UART4_ENABLE
u8 xdata R_u4[UART4_RX_LENTH];
#else
u8 xdata R_u4[20];
#endif
u8 xdata R_OD4 = 0;
u16 xdata R_CN4 = 0;
u8 xdata T_O4 = 0;
bit Busy4 = 0;
#if UART5_ENABLE
u8 xdata R_u5[UART5_RX_LENTH];
#else
u8 xdata R_u5[20];
#endif
u8 xdata R_OD5 = 0;
u16 xdata R_CN5 = 0;
u8 xdata T_O5 = 0;
bit Busy5 = 0;
bit Response_flog = 0;
bit Auto_data_upload = 0;
bit Crc_check_flog = 0;
void uart_init()
{
#if UART2_ENABLE
Uart2_Init(BAUD_UART2);
#endif
#if UART3_ENABLE
Uart3_Init(BAUD_UART3);
#endif
#if UART4_ENABLE
Uart4_Init(BAUD_UART4);
#endif
#if UART5_ENABLE
Uart5_Init(BAUD_UART5);
#endif
}
void uart_send_byte(u8 Uart_number, u8 Dat)
{
if (Uart_number == 2)
{
while (Busy2)
;
Busy2 = 1;
SBUF2 = Dat;
}
else if (Uart_number == 3)
{
while (Busy3)
;
Busy3 = 1;
SBUF3 = Dat;
}
else if (Uart_number == 4) //原工程这里有问题
{
while (Uart4.Tx_Busy)
;
Uart4.Tx_Busy = 1;
SBUF4_TX = Dat;
}
else if (Uart_number == 5)
{
while (Busy5)
;
Busy5 = 1;
SBUF5_TX = Dat;
}
}
#if 1
void uart_send_str(u8 U_number, u8 *Str, u8 Len)
{
u8 i = 0;
if (U_number == 4)
TR4 = 1;
else if (U_number == 5)
TR5 = 1;
for (i = 0; i < Len; i++)
{
uart_send_byte(U_number, *Str++);
}
if (U_number == 4)
{
while (Uart4.Tx_Busy)
;
TR4 = 0;
}
else if (U_number == 5)
{
while (Busy5)
;
TR5 = 0;
}
}
#endif
void uart_data_send(u8 *Arr, u8 Uartnum, bit Outo_send, bit Crc_ck)
{
u8 i = 0;
u16 V1 = Read_Dgus(0x0f01);
Auto_data_upload = Outo_send;
if (Auto_data_upload)
{
Crc_check_flog = Crc_ck;
if (Crc_check_flog)
{
u16 Crc = 0;
Arr[2] = (((u8)V1) << 1) + 6;
Crc = crc16table(Arr + 3, Arr[2] - 2);
Arr[Arr[2] + 1] = Crc & 0x00FF;
Arr[Arr[2] + 2] = Crc >> 8;
uart_send_str(Uartnum, Arr, Arr[2] + 3);
Arr[2] = (((u8)V1) << 1) + 4;
}
else
{
uart_send_str(Uartnum, Arr, Arr[2] + 3);
}
}
}
void Uart4_Init(u32 baud_rate){
u16 i;
Uart4.id = 2;
Uart4.Tx_Read = 0;
Uart4.Tx_Write = 0;
Uart4.Tx_Busy = 0;
Uart4.Rx_Read = 0;
Uart4.Rx_Write = 0;
Uart4.Rx_Busy = 0;
Uart4.Rx_Flag = Uart_Rev_Pre;
for(i = 0; i < Uart4_Tx_Lenth; i++) Uart4.Tx_Buffer[i] = 0;
for(i = 0; i < Uart4_Rx_Lenth; i++) Uart4.Rx_Buffer[i] = 0;
i=FOSC/8/baud_rate;
SCON2T= 0x80 ;
SCON2R= 0x80;
ES2R = 1;
ES2T = 1;
BODE2_DIV_H = (u8)(i>>8);
BODE2_DIV_L = (u8)i;
}
void Uart4_Tx_write4buff(u8 dat){
Uart4.Tx_Buffer[Uart4.Tx_Write] = dat;
if(Uart4.Tx_Read > Uart4.Tx_Write){
while((Uart4.Tx_Write) + 1 == Uart4.Tx_Read);
}
++Uart4.Tx_Write;
if(Uart4.Tx_Write >= Uart4_Tx_Lenth){
while(Uart4.Tx_Read == 0);
Uart4.Tx_Write = 0;
}
if(Uart4.Tx_Busy == 0){
Uart4.Tx_Busy = 1;
SCON2T|=0x1 ;
}
}
void Uart4_RX_ISR_PC(void) interrupt 11
{
u8 res = 0;
EA = 0;
SCON2R&=0xFE;
if(!Uart4.Rx_Busy)
{
Uart4.Rx_Buffer[Uart4.Rx_Write] = SBUF0;
Uart4.Rx_Write++;
Uart4.Rx_Write %= Uart4_Rx_Lenth;
if(Uart4.Rx_Write == Uart4.Rx_Read)
Uart4.Rx_Busy = 1;
}
EA = 1;
}
void Uart4_TX_ISR_PC(void) interrupt 10
{
u8 res = 0;
EA = 0;
SCON2T&=0xFE ;
if(Uart4.Tx_Read != Uart4.Tx_Write){
SBUF2_TX = Uart4.Tx_Buffer[Uart4.Tx_Read];
Uart4.Tx_Read++;
Uart4.Tx_Read %= Uart4_Tx_Lenth;
}else{
Uart4.Tx_Busy = 0;
}
EA = 1;
}
void SciDriveInit(void)
{
u8SciProcessStep = C_SCI_LEISURE;
}
void SciDriverFun(void)
{
uint8_t t_u8Temp;
uint8_t i = 0;
g_u8SciSendTemp[0] = Uart4.Rx_Buffer[0];
g_u8SciSendTemp[1] = Uart4.Rx_Buffer[1];
g_u8SciSendTemp[2] = Uart4.Rx_Buffer[2];
g_u8SciSendTemp[3] = Uart4.Rx_Buffer[3];
g_u8SciSendTemp[4] = Uart4.Rx_Buffer[4];
g_u8SciSendTemp[5] = Uart4.Rx_Buffer[5];
g_u8SciSendTemp[6] = Uart4.Rx_Buffer[6];
g_u8SciSendTemp[7] = Uart4.Rx_Buffer[7];
g_u8SciSendTemp[8] = Uart4.Rx_Buffer[8];
g_u8SciSendTemp[9] = Uart4.Rx_Buffer[9];
g_u8SciSendTemp[10] = Uart4.Rx_Buffer[10];
g_u8SciSendTemp[11] = Uart4.Rx_Buffer[11];
uart_send_str(4,&g_u8SciSendTemp[0],C_SCI_SEND_BUFF_LENGTH);
}
|
|