sop板
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.
 
 
 
 

160 lines
5.0 KiB

//////////////////////////////////////////////////////////////////////////
/// 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 "../msp/time.h"
//#define _USE_485
static volatile Ts_uart_send_buf idata ts_uart_send_shop;
TP_Handler_X s_uart0_rec;
TS_PH4_modbus s_uart0_ack;
void L0_uart0_init(void)
{
U16 val = D_BRT_COUNT(12,D_sys_MainFre,D_uart0_BRT);
SCON = 0x50; //8位数据,可变波特率
AUXR |= 0x01; //串口1选择定时器2为波特率发生器
AUXR &= 0xFB; //定时器2时钟为Fosc/12,即12T
//T2L = 0xFE; //设定定时初值
//T2H = 0xFF; //设定定时初值
T2L = val;
T2H = val >> 8;
AUXR |= 0x10; //启动定时器2
}
void L0_uart0_buf_init(void)
{
ts_uart[uNum0].p = &ts_uart_send_shop;
ts_uart[uNum0].p->max = ts_uart[uNum0].p->now = 0;
ts_uart[uNum0].p->ok = D_ready;
ts_uart[uNum0].t = &s_uart0_rec;
ts_uart[uNum0].t->head = 0;
ts_uart[uNum0].t->ok = 0;
//ts_uart[uNum0].tp_handler = L1_s2b_PH4;
ts_uart[uNum0].tp_handler = NULL; //使用函数指针导致错误
ts_uart[uNum0].ack = (U8*)&s_uart0_ack;
L0_uart0_init();
D_uart0_ES_INT(1); //打开串口中断
#if (D_UART0_485_TYPE != TYPE_485_NONE)
D_UART0_485_RX() //默认处于接收状态
#endif
}
void L0_uart0_sendArray(U8 * buf, U16 len)
{
#if (D_UART0_485_TYPE != TYPE_485_NONE)
D_UART0_485_TX() //切换到输出状态
#endif
L0_uartN_sendArray(uNum0,buf,len);
}
/*************************************************
UART 中断
*************************************************/
void INTERRUPT_UART(void) D_SERVE_UART
{
//NOP(); NOP(); NOP();
if(L0_uart0_IntRI()) //如果是U0接收中断
{
L0_uart0_IntRIClear(); //清除接收中断标志
ts_uart[uNum0].t->reg = L0_uartN_get(uNum0);
//ts_uart[uNum0].tp_handler(ts_uart[uNum0].t);
L1_s2b_PH4(ts_uart[uNum0].t);
L0_timer1_restart();
}
if(L0_uart0_IntTI()) //如果是U0发送中断
{
L0_uart0_IntTIClear(); //清除发送中断标志
if(ts_uart[uNum0].p->max != ts_uart[uNum0].p->now)
{
L0_uartN_set(uNum0,ts_uart[uNum0].p->p[ts_uart[uNum0].p->now]);
ts_uart[uNum0].p->now ++;
}
else
{
ts_uart[uNum0].p->ok = D_ready;
ts_uart[uNum0].p->max = 0;
ts_uart[uNum0].p->now = 0;//可以发送下一个数据
#if (D_UART0_485_TYPE != TYPE_485_NONE)
D_UART0_485_RX() //切换到接收状态
#endif
}
}
//NOP(); NOP(); NOP();
}
void timer1_isrHanddle(void) D_SERVE_TIMER1
{
struct _tp_handler_x *p = &s_uart0_rec;
if((p->head == 1) && (p->num >= D_s_modbus_min))
{
//LED1 ^= 1;
p->head = 0;
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;
}
}
L0_timer1_stop();
}