////////////////////////////////////////////////////////////////////////// /// 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 "uart4.h" //#define D_txd4_low() BITN_0(X,BITNx) struct _s_uart4_send_buf_ s_uart4_send_shop; struct _s_uart4_send_buf_ s_uart4_send_depot; #define FOSC 11059200L //系统频率 #define BAUD4 115200 //串口波特率 void L0_uart4_init(void) { #if 0 S4CON = 0x10; //8位可变波特率 T2L = (65536 - (FOSC/4/BAUD)); //设置波特率重装值 T2H = (65536 - (FOSC/4/BAUD))>>8; AUXR = 0x14; //T2为1T模式, 并启动定时器2 #else S4CON = 0x50; //8位可变波特率 T4L = (65536 - (FOSC/4/BAUD4)); //设置波特率重装值 T4H = (65536 - (FOSC/4/BAUD4))>>8; T4T3M |= 0x20; //定时器4为1T模式 T4T3M |= 0x80; //定时器4开始计时 #endif //设置txd4为强推挽 BITN_1(P5M0,BITN3); BITN_0(P5M1,BITN3);//P53 //设置txd4为高阻态,调试gm35模块 //D_HighR_P5(BITN3); D_uart4_ES_INT_OPEN(); //打开串口中断 EA = 1; } void L1_uart4_buf_init(void) { s_uart4_send_depot.p = s_uart4_send_depot.buf; s_uart4_send_shop.now = 0; //D_txd4_low(); L0_uart4_init(); } // 发送buf数组 中的0----(num-1) void L0_uart4_sendbuf(void) { register unsigned char n = 0; if(s_uart4_send_depot.max >= D_send4_buf_max) { s_uart4_send_depot.max = 3; } D_uart4_ES_INT_CLOSE(); if(0 == s_uart4_send_shop.now) {/// 上次的已经发送完毕了,或者第一次开始 /// (s_uart4_send_shop.now == s_uart4_send_shop.max = 0 buf中为空) for(n = 0;n < s_uart4_send_depot.max; n++) { s_uart4_send_shop.buf[n] = s_uart4_send_depot.p[n]; } s_uart4_send_shop.max = s_uart4_send_depot.max; //L0_uart4_IntTIClear(); s_uart4_send_shop.now = 1; L0_uart4_set(s_uart4_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_uart4_send_depot.max; n++) { s_uart4_send_shop.buf[s_uart4_send_shop.max] = s_uart4_send_depot.p[n]; s_uart4_send_shop.max ++; if(s_uart4_send_shop.max >= D_send4_buf_max) { s_uart4_send_shop.max = D_send4_buf_max; } } } D_uart4_ES_INT_OPEN(); } /************************************************* UART 中断 *************************************************/ #define D_SERVE_uart4 interrupt 18 void INTERRUPT_uart4(void) D_SERVE_uart4// using 2 { D_uart4_ES_INT_CLOSE(); //------------------------------------------------ if(L0_uart4_IntRI()) //如果是U0接收中断 { L0_uart4_IntRIClear(); //清除接收中断标志 //s_uart4_rec.reg = L0_uart4_get(); //s_uart4_rec.ok = 1; Lp0_uart4_fun(L0_uart4_get()); } //------------------------------------------------ else { if(L0_uart4_IntTI()) //如果是U0发送中断 {L0_uart4_IntTIClear(); //清除发送中断标志 if(s_uart4_send_shop.max != s_uart4_send_shop.now) { L0_uart4_set(s_uart4_send_shop.buf[s_uart4_send_shop.now]); s_uart4_send_shop.now ++; }else { s_uart4_send_shop.max = 0; s_uart4_send_shop.now = 0;//可以发送下组个数据 } } } D_uart4_ES_INT_OPEN(); } void L0_uart4_uc(U8 ww) { s_uart4_send_depot.max = 1; s_uart4_send_depot.buf[0] = ww; s_uart4_send_depot.p = s_uart4_send_depot.buf; L0_uart4_sendbuf(); } #if 1 void L0_uart4_0d0a(void) { s_uart4_send_depot.max = 2; s_uart4_send_depot.buf[0] = 0x0d; s_uart4_send_depot.buf[1] = 0x0a; s_uart4_send_depot.p = s_uart4_send_depot.buf; L0_uart4_sendbuf(); } // L0_uart4_uc(cguHex2Char[D_uc_low(s->sec)][0]); void L0_uart4_uchex(U8 ww) //1A ==> 0x1 and 0xa { s_uart4_send_depot.max = 2; s_uart4_send_depot.buf[0] = cguHex2Char[D_uc_low(ww)][1]; s_uart4_send_depot.buf[1] = cguHex2Char[D_uc_high(ww)][1]; s_uart4_send_depot.p = s_uart4_send_depot.buf; L0_uart4_sendbuf(); } void L0_uart4_ulhex(vU32 ww) { U_U32 ultemp; ultemp.dWord = ww; L0_uart4_uchex(ultemp.BYTE4.byte0); L0_uart4_uchex(ultemp.BYTE4.byte1); L0_uart4_uchex(ultemp.BYTE4.byte2); L0_uart4_uchex(ultemp.BYTE4.byte3); } U8 L0_strlen(U8 *str) { U8 count = 0; while(*str++) count++; return count; } void L0_uart4_sendstr(U8 *str) { //L0_uart4_sendbuf(str,strlen(str)); s_uart4_send_depot.max = L0_strlen(str); s_uart4_send_depot.p = str; L0_uart4_sendbuf(); } void L0_uart4_sendArray(U8 *str,U8 len) { //L0_uart4_sendbuf(str,strlen(str)); s_uart4_send_depot.max = len; s_uart4_send_depot.p = str; L0_uart4_sendbuf(); } #endif