////////////////////////////////////////////////////////////////////////// /// COPYRIGHT NOTICE /// Copyright (c) 2015, 传控科技 /// All rights reserved. /// /// @file main.c /// @brief main app /// ///(本文件实现的功能的详述) /// /// @version 1.1 CCsens technology /// @author CC /// @date 20150102 /// /// /// 修订说明:最初版本 /// Modified by: /// Modified date: /// Version: /// Descriptions: // 20160413 CC-ACC-VH02 // 连接至 J22 RXD0 TXD0 //P5_DIR &= ~BITN1; //p5.1输出TXD //P5_DIR |= BITN0; //p5.0输入RXD //P5_SEL0 &= ~(BITN0 +BITN1); //设置P5.0 P5.1为UART0 RXD TXD //P5_SEL1 |= BITN0 +BITN1; /***************************************************************************** update by cc @201700110 针对多串口 和 单一串口 有区别 每个串口是独立的还是分开的有讲究 程序是复杂的还是软件应用简单是 个需要平衡的事情. clib/clib.c: 公用的函数 和硬件无关 放置串行模式(串口等其他通讯总线类的输出)输出的函数, 一些覆盖模式输出的(lcd等固屏输出的)的也可使用 void Lc_print(void (*L0pf_send_uc)(char ww), char *dat,...) ----------------------------------------------------------------------------------------- uartcom/Uprotocol2app 协议到应用 为了适应不同的通讯协议需要不同的uart口来对应 和应用相关 typedef struct _ts_lcm_pro_; 应用协议包的定义? LCM的协议------------ L3_UARTcom0_exp_protocol 解析应用协议 ----------------------------------------------------------------------------------------- uartcom/urec2protocol: 接收到的数据放入到指向特定协议的缓存中,和协议的格式有关 一般分为 标头式或者标尾式 公用的串口通讯定义 struct _s_uart_rec_ 的公共协议包(关键的结构体)的声明------struct _s_uart_rec_ void L1_uart_2buf(struct _s_uart_rec_ *p)串行数据保存到指向特定协议的缓冲中 -------------------------------------------------------------------------------------------- msp/uartx.c 底层代码 和cpu相关 缓存发送也放在里面 L0_UART0_Init UART0_IRQHandler L0_Usend_uc------UserDef ----------------------------------------------------------------------------------------- ********************************************************************************/ #include "uart0.h" #include "uart2.h" #include "uart3.h" #include "uart4.h" #include struct _s_protocol_ s_at0; struct _s_uart0_send_buf_ s_uart0_send_shop; struct _s_uart0_send_buf_ s_uart0_send_depot; #define FOSC 11059200UL #define BRT (256 - FOSC / 115200 / 32) ///定时器1 模式2 做串口波特率 ///#define Debug_with_keil_sim #ifdef Debug_with_keil_sim void L0_uart0_init(void) { SCON = 0x50; /* SCON: mode 1, 8-bit UART, enable rcvr */ TMOD |= 0x20; /* TMOD: timer 1, mode 2, 8-bit reload */ TH1 = 221; /* TH1: reload value for 1200 baud @ 16MHz */ TR1 = 1; /* TR1: timer 1 run */ TI = 1; /* TI: set TI to send first char of UART */ EA = 1; AUXR = 0x40; // TI = 0; RI = 0; D_uart0_ES_INT(1); //打开串口中断 } #else void L0_uart0_init(void) {// 19200 ///定时器1 模式2 做串口波特率 SCON = 0x50; TMOD = 0x20; TL1 = BRT; TH1 = BRT; TR1 = 1; AUXR = 0x40; TI = 0; RI = 0; D_uart0_ES_INT(1); //打开串口中断 } #endif #if 0 void L0_uart0_init(void) {// 19200 //------------------------------------------------ SCON = 0x50; //8位数据,可变波特率 AUXR |= 0x40; //定时器1时钟为Fosc,即1T AUXR &= 0xFE; //串口1选择定时器1为波特率发生器 TMOD &= 0x0F; //设定定时器1为16位自动重装方式 // TL1 = 0x70; //设定定时初值 // TH1 = 0xFF; //设定定时初值 //115200 TL1 = 0xE8; //设定定时初值 TH1 = 0xFF; //设定定时初值 ET1 = 0; //禁止定时器1中断 TR1 = 1; //启动定时器1 TI = 0; D_uart0_ES_INT(1); //打开串口中断 void UartInit(void) //115200bps@11.0592MHz { SCON = 0x50; //8位数据,可变波特率 AUXR |= 0x40; //定时器1时钟为Fosc,即1T AUXR &= 0xFE; //串口1选择定时器1为波特率发生器 TMOD &= 0x0F; //设定定时器1为16位自动重装方式 TL1 = 0xE8; //设定定时初值 TH1 = 0xFF; //设定定时初值 ET1 = 0; //禁止定时器1中断 TR1 = 1; //启动定时器1 } #endif void L1_uart0_buf_init(void) { s_uart0_send_depot.p = s_uart0_send_depot.buf; s_uart0_send_shop.now = 0; L0_uart0_init(); } void L0_uart0_uc(U8 ww) { s_uart0_send_depot.max = 1; s_uart0_send_depot.buf[0] = ww; s_uart0_send_depot.p = s_uart0_send_depot.buf; L0_uart0_sendbuf(); } void L0_uart0_us(U16 ww) { L0_uart0_uc(ww >> 8 & 0xFF); L0_uart0_uc(ww >> 0 & 0xFF); } void L0_uart0_0d0a(void) { s_uart0_send_depot.max = 2; s_uart0_send_depot.buf[0] = 0x0d; s_uart0_send_depot.buf[1] = 0x0a; s_uart0_send_depot.p = s_uart0_send_depot.buf; L0_uart0_sendbuf(); } // L0_uart0_uc(cguHex2Char[D_uc_low(s->sec)][0]); void L0_uart0_uchex(U8 ww) { s_uart0_send_depot.max = 2; s_uart0_send_depot.buf[0] = cguHex2Char[D_uc_low(ww)][1]; s_uart0_send_depot.buf[1] = cguHex2Char[D_uc_high(ww)][1]; s_uart0_send_depot.p = s_uart0_send_depot.buf; L0_uart0_sendbuf(); } void L0_uart0_ulhex(vU32 ww) { U_U32 ultemp; ultemp.dWord = ww; L0_uart0_uchex(ultemp.BYTE4.byte0); L0_uart0_uchex(ultemp.BYTE4.byte1); L0_uart0_uchex(ultemp.BYTE4.byte2); L0_uart0_uchex(ultemp.BYTE4.byte3); } void L0_uart0_sendstr(U8 *str) { //L0_uart0_sendbuf(str,strlen(str)); s_uart0_send_depot.max = strlen(str); s_uart0_send_depot.p = str; L0_uart0_sendbuf(); } void L0_uart0_sendArray(U8 *buf,U8 len) { //L0_uart0_sendbuf(str,strlen(str)); s_uart0_send_depot.max = len; s_uart0_send_depot.p = buf; L0_uart0_sendbuf(); } // 发送buf数组 中的0----(num-1) void L0_uart0_sendbuf(void) { register unsigned char n = 0; if(s_uart0_send_depot.max>= D_send_buf_max) { s_uart0_send_depot.max = D_send_buf_max - 1; } D_uart0_ES_INT(0); if(0 == s_uart0_send_shop.now) {/// 上次的已经发送完毕了,或者第一次开始 /// (s_uart0_send_shop.now == s_uart0_send_shop.max = 0 buf中为空) for(n = 0;n < s_uart0_send_depot.max; n++) { s_uart0_send_shop.buf[n] = s_uart0_send_depot.p[n]; } s_uart0_send_shop.max = s_uart0_send_depot.max; L0_uart0_IntTIClear(); s_uart0_send_shop.now = 1; ///D_uart0_ES_INT(1); L0_uart0_set(s_uart0_send_shop.buf[0]); }else {// 需要插入 /// [0]--?--[pool.now]--?--[pool.max-1]....................................... /// [0]--?--[pool.now]--?--[pool.max]+[bath.0]--?---[bath.max-1]............. for(n = 0;n < s_uart0_send_depot.max; n++) { s_uart0_send_shop.buf[s_uart0_send_shop.max] = s_uart0_send_depot.p[n]; s_uart0_send_shop.max ++; if(s_uart0_send_shop.max >= D_send_buf_max) { s_uart0_send_shop.max = D_send_buf_max - 1; } } } D_uart0_ES_INT(1); } /************************************************* UART 中断 *************************************************/ #define D_SERVE_UART interrupt 4 void INTERRUPT_UART(void) D_SERVE_UART// using 2 { D_uart0_ES_INT(0); //------------------------------------------------ if(L0_uart0_IntRI()) //如果是U0接收中断 {L0_uart0_IntRIClear(); //清除接收中断标志 //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>用户程序 添加协议>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> /// output: 对应的结构体变量中携带 缓存buf和协议ok的标志 //s_uart0_rec.reg = L0_uart0_get(); //s_uart0_rec.ok = 1; Lp0_uart0_fun(L0_uart0_get()); } //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< //------------------------------------------------------------ else { if(L0_uart0_IntTI()) //如果是U0发送中断 {L0_uart0_IntTIClear(); //清除发送中断标志 if(s_uart0_send_shop.max != s_uart0_send_shop.now) { L0_uart0_set(s_uart0_send_shop.buf[s_uart0_send_shop.now]); s_uart0_send_shop.now ++; }else { s_uart0_send_shop.max = 0; s_uart0_send_shop.now = 0;//可以发送下一个数据 } } } D_uart0_ES_INT(1); }