You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

291 lines
7.3 KiB

1 year ago
#include "msp_uart1.h"
///#include "msp_time2.h"
#include "c_crc.h"
#include "c_delay.h"
//#include "tpc_debug.h"
#include "debug_drv.h"
#include "tpc_road.h"
//#define _USE_485
///static volatile TS_Uart_Send_ ts_uart_send_shop;
///>>>>>>>>>>>>>>>>和cpu相关
#if(MainFre_5M == D_sys_MainFre)
#elif(MainFre_22M == D_sys_MainFre)
#if(BRT_921600 == D_UART1_BRT)
void L0_uart1_init(void) /// void UartInit(void) //921600bps@22.1184MHz
{
SCON = 0x50; //8位数据,可变波特率
AUXR |= 0x40; //定时器时钟1T模式
AUXR &= 0xFE; //串口1选择定时器1为波特率发生器
TMOD &= 0x0F; //设置定时器模式
TL1 = 0xFA; //设置定时初始值
TH1 = 0xFF; //设置定时初始值
ET1 = 0; //禁止定时器中断
TR1 = 1; //定时器1开始计时
}
#elif(BRT_460800 == D_UART1_BRT)
#elif(BRT_115200 == D_UART1_BRT)
void L0_uart1_init(void) /// void UartInit(void) //115200bps@22.1184MHz
{
SCON = 0x50; //8位数据,可变波特率
AUXR |= 0x40; //定时器时钟1T模式
AUXR &= 0xFE; //串口1选择定时器1为波特率发生器
TMOD &= 0x0F; //设置定时器模式
TL1 = 0xD0; //设置定时初始值
TH1 = 0xFF; //设置定时初始值
ET1 = 0; //禁止定时器中断
TR1 = 1; //定时器1开始计时
}
#warning "MainFre_22M BRT_115200"
#elif(BRT_19200 == D_UART1_BRT)
#elif(BRT_9600== D_UART1_BRT)
void L0_uart1_init(void) //9600bps@22.1184MHz
{
SCON = 0x50; //8位数据,可变波特率
AUXR |= 0x40; //定时器时钟1T模式
AUXR &= 0xFE; //串口1选择定时器1为波特率发生器
TMOD &= 0x0F; //设置定时器模式
TL1 = 0xC0; //设置定时初始值
TH1 = 0xFD; //设置定时初始值
ET1 = 0; //禁止定时器中断
TR1 = 1; //定时器1开始计时
}
1 year ago
#else///9600
#endif//)
#elif(MainFre_27M == D_sys_MainFre)
#elif(MainFre_44M == D_sys_MainFre)
#if(BRT_115200 == D_UART1_BRT)
#elif(BRT_460800 == D_UART1_BRT)
void L0_uart1_init(void) /// void UartInit(void) //460800bps@44.2368MHz
{
SCON = 0x50; //8位数据,可变波特率
AUXR &= 0xBF; //定时器时钟12T模式
AUXR &= 0xFE; //串口1选择定时器1为波特率发生器
TMOD &= 0x0F; //设置定时器模式
TL1 = 0xFE; //设置定时初始值
TH1 = 0xFF; //设置定时初始值
ET1 = 0; //禁止定时器%d中断
TR1 = 1; //定时器1开始计时
}
#elif(BRT_921600 == D_UART1_BRT)
void L0_uart1_init(void) /// void UartInit(void) //921600bps@44.2368MHz
{
SCON = 0x50; //8位数据,可变波特率
AUXR &= 0xBF; //定时器时钟12T模式
AUXR &= 0xFE; //串口1选择定时器1为波特率发生器
TMOD &= 0x0F; //设置定时器模式
TL1 = 0xFF; //设置定时初始值
TH1 = 0xFF; //设置定时初始值
ET1 = 0; //禁止定时器%d中断
TR1 = 1; //定时器1开始计时
}
#info "0000"
#elif(BRT_19200 == D_UART1_BRT)
#elif(BRT_9600== D_UART1_BRT)
#else///9600
#endif//D_sys_MainFre)
#else ///MainFre_11M
#if(BRT_115200 == D_UART1_BRT)
void L0_uart1_init(void) ///void UartInit(void) //115200bps@11.0592MHz
{
SCON = 0x50; //8位数据,可变波特率
AUXR |= 0x01; //串口1选择定时器2为波特率发生器
AUXR &= 0xFB; //定时器时钟12T模式
T2L = 0xFE; //设置定时初始值
T2H = 0xFF; //设置定时初始值
AUXR |= 0x10; //定时器2开始计时
}
#elif(BRT_19200 == D_UART1_BRT)
#elif(BRT_9600== D_UART1_BRT)
#else///9600
#endif//D_sys_MainFre)
#endif//D_sys_MainFre)
///>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>和cpu相关
void L1_uart1_open(void)
{//
L0_uart1_buf_init();//串口初始化//
EA = 1;
L0_uart1_str("\r\nuart1 open");
}
void L0_uart1_buf_init(void)
{
L0_uartN_sbuf(D_UART1);
D_s2b_road1_init();
D_stdIO_P3(BITN1);
D_stdIO_P3(BITN0);////PIN19 TXD1 D+
L0_uart1_init();
L0_uart1_IntRIClear();
L0_uart1_IntTIClear();
D_UART1_RS_EN(1);
D_uart1_ES_INT(1); //打开串口中断
#if (D_UART1_485_TYPE != TYPE_485_NONE)
D_UART1_485_RX(); //默认处于接收状态
#endif
}
/*
void L0_uart1_Array(U8 * buf, U16 len)
{
#if (D_UART1_485_TYPE != TYPE_485_NONE)
D_UART1_485_TX(); //切换到输出状态
#endif
L0_uartN_Array(D_UART1,buf,len);
}
*/
////U8 overtime_t; //超时溢出的计数器
////U8 overtime_flag; ///0:没有超时溢出 1:超时溢出
////U8 over_flag; ////串口接收结束的状态 0:超时溢出 1:超出max接收
///D_LED4_REV();
void L2_uart1_overtime_callback(void)
{////必须清除当前的idle
if(0 == ts_Urec[D_UART1].idle)
{
if(ts_Urec[D_UART1].overtime_t >= 2)
{
ts_Urec[D_UART1].idle = 1;//总线空闲
ts_Urec[D_UART1].head = 0;
//// printf(" ts_s2b_debug.fifo = %d ",(int)ts_s2b_debug.fifo);
if(ts_Urec[D_UART1].fifo > 0)
{ts_Urec[D_UART1].fifo = 0;
ts_Urec[D_UART1].ok = 1;/// 接收到的数据结束 : 总线空闲+buf非空
ts_Urec[D_UART1].sp = ts_Urecbuf.r1;
//// L1_uartD_sendArray((U8 *)ts_debug_rec.filter, 5);
/*******
L1_uartD_sendArrayhex((U8 *)&ts_debug_rec.filter, 5);
ts_debug_rec.filter = 0x31;
ts_debug_rec.R1 = 0x32;
ts_debug_rec.R2 = 0x33;
ts_debug_rec.R3 = 0x34;
ts_debug_rec.ocr = 0x35;
L1_uartD_sendArrayhex((U8 *)&ts_debug_rec.filter, 5);
*********/
}
}
ts_Urec[D_UART1].overtime_t ++;
}
}
/*************************************************
UART
*************************************************/
void INTERRUPT_UART(void) D_SERVE_UART
{
if(L0_uart1_IntRI()) //如果是U0接收中断
{
L0_uart1_IntRIClear(); //清除接收中断标志
/// D_LED1_REV();
///>>>>>>>>>>>>>接收协议 可以更改为其他协议>>>>>>>>>>>>>>>>>>>>>>>>
ts_Urec[D_UART1].reg = L0_uart1_get();
ts_Urec[D_UART1].idle = 0;
ts_Urec[D_UART1].overtime_t = 0;///需要和tick 定时器中的配合 作为接收超时判定
/// L1_s2b_PH5_debug(&ts_s2b_debug);
/// L1_s2b_PH5_debug(TS_s2b_debug);
L1_s2b_road(D_UART1);
///<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
}
if(L0_uart1_IntTI()) //如果是U0发送中断
{
L0_uart1_IntTIClear(); //清除发送中断标志
if(ts_Usend[D_UART1].max != ts_Usend[D_UART1].now)
{
///L0_uartN_set(uNum1,ts_uart[uNum1].p->p[ts_uart[uNum1].p->now]);
L0_uart1_set(ts_Usend[D_UART1].sbuf[ts_Usend[D_UART1].now]);
ts_Usend[D_UART1].now ++;//发送本包的下一个数据
}
else
{
ts_Usend[D_UART1].ok = D_ready;
ts_Usend[D_UART1].max = 0;
ts_Usend[D_UART1].now = 0;//可以发送下一包数据
#if (D_UART1_485_TYPE != TYPE_485_NONE)
D_UART1_485_RX() //切换到接收状态
///gfgfgfh
#endif
}
}
//NOP(); NOP(); NOP();
}
///L2_callback_uart1_free(&s_uart1_rec);
#if 0
void L2_callback_uart1_free(Ts_uart_rev_ *p)
{
///Ts_uart_rev_ *p = &s_uart1_rec;
#define D_s_modbus_min 4 //modbus协议的最小长度
/// D_P24_REV();
////while(9) D_P24_REV();
if((p->head == 1) && (p->num >= D_s_modbus_min))
{
//LED1 ^= 1;
p->head = 0;
#ifdef D_use_crc
crc16qq(p->crc,p->buf,p->num - 2); ////fixme 20220311 ///crc16qq(p->crc,p->buf,p->num - 2); //校验CRC
#endif
// crc16(p->crc,p->buf,p->num - 2); //校验CRC
if(p->crc[0] == p->buf[p->num-2] && p->crc[1] == p->buf[p->num-1])
{
p->ok = 1;
/// LED0 ^= 1;
}
p->debugok = 1;////s_uart1_rec.debugok
}
L1_uart1_isFree_over();
//// D_motor1_FI_rev();
}
#endif