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.

218 lines
6.8 KiB

4 years ago
//////////////////////////////////////////////////////////////////////////
/// 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)
{
#if(MainFre_22M == D_sys_MainFre) //115200bps@22.1184MHz
SCON = 0x50; //8位数据,可变波特率
AUXR |= 0x40; //定时器1时钟为Fosc,即1T
AUXR &= 0xFE; //串口1选择定时器1为波特率发生器
TMOD &= 0x0F; //设定定时器1为16位自动重装方式
TL1 = 0xD0; //设定定时初值
TH1 = 0xFF; //设定定时初值
ET1 = 0; //禁止定时器1中断
TR1 = 1; //启动定时器1
#elif(MainFre_11M == D_sys_MainFre) //115200bps@22.1184MHz
#if(BRT_115200 == D_uart0_BRT && SBIT_1 == D_uart0_SBIT) //115200bps@11.0592MHz
SCON = 0x50; //8位数据,可变波特率
AUXR |= 0x01; //串口1选择定时器2为波特率发生器
AUXR &= 0xFB; //定时器2时钟为Fosc/12,即12T
T2L = 0xFE; //设定定时初值
T2H = 0xFF; //设定定时初值
AUXR |= 0x10; //启动定时器2
#elif(BRT_19200 == D_uart0_BRT && SBIT_1 == D_uart0_SBIT) //19200@11.0592MHz
xxxx
SCON = 0x50; //8位数据,可变波特率
AUXR |= 0x01; //串口1选择定时器2为波特率发生器
AUXR |= 0x04; //定时器2时钟为Fosc,即1T
T2L = 0x70; //设定定时初值
T2H = 0xFF; //设定定时初值
AUXR |= 0x10; //启动定时器2
#elif(BRT_19200 == D_uart0_BRT && SBIT_2 == D_uart0_SBIT) //19200@11.0592MHz
xxx
//使用第9位数据模拟1个停止位,可变波特率;SM2=0, TB8=1(第9bit总为1模拟1个停止位)
SCON = 0xD8;
AUXR |= 0x01; //串口1选择定时器2为波特率发生器
AUXR |= 0x04; //定时器2时钟为Fosc,即1T
T2L = 0x70; //设定定时初值
T2H = 0xFF; //设定定时初值
AUXR |= 0x10; //启动定时器2
#elif (BRT_9600 == D_uart0_BRT) //9600bps@11.0592MHz
xxx
SCON = 0x50; //8位数据,可变波特率
AUXR |= 0x01; //串口1选择定时器2为波特率发生器
AUXR &= 0xFB; //定时器2时钟为Fosc/12,即12T
T2L = 0xE8; //设定定时初值
T2H = 0xFF; //设定定时初值
AUXR |= 0x10; //启动定时器2
#endif
#endif//D_sys_MainFre)
}
void L0_uart0_buf_init(void)
{
ts_uart[uNum0].p = &ts_uart_send_shop;
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 = D_CMD_Filter1_ff;
//ts_uart[uNum0].t->head_1 = D_CMD_Filter1_fe;
ts_uart[uNum0].t->head = 0;
ts_uart[uNum0].t->ok = 0;
ts_uart[uNum0].tp_handler = L1_s2b_PH4;
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(); //清除接收中断标志
L0_timer1_start(0);
ts_uart[uNum0].t->reg = L0_uartN_get(uNum0);
ts_uart[uNum0].tp_handler(ts_uart[uNum0].t);
}
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();
}
#if 0
void timer1_isrHanddle (void) D_SERVE_TIMER1
{//
TF1 = 0;
//s_nos_tick.uart0_free = 1;
//if(s_uart0_rec.head == 1) //收到一条协议
if(s_uart0_rec.head == 1) //收到一条协议
{
s_uart0_rec.head = 0;
P10 ^= 1;
if(s_uart0_rec.ok == 0)
{
s_uart0_rec.ok = 1;
//Lc_buf_copy_uc();
}
}
}
#else
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))
{
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])
{
if(p->ok == 0)
{
p->ok = 1;
}
}
LED0 ^= 1;
}
L0_timer1_stop();
}
#endif