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.
202 lines
4.0 KiB
202 lines
4.0 KiB
/**
|
|
* @file uart.c
|
|
* @author 1722451300@qq.com
|
|
* @brief
|
|
* @version 0.1
|
|
* @date 2021-12-14
|
|
*
|
|
* @copyright Copyright (c) 2021
|
|
*
|
|
*/
|
|
#include <hic.h>
|
|
#include "t21_t8.h"
|
|
#include "io_config.h"
|
|
#include "uart.h"
|
|
#include "motor.h"
|
|
#include "lcd.h"
|
|
#if 0
|
|
#endif
|
|
#define IT_SR //中断发送接收,注释此宏定义则使用查询方式发送接收
|
|
#define CLRWDT() \
|
|
{ \
|
|
__Asm CWDT; \
|
|
} //宏定义清狗指令
|
|
|
|
void uart0_config(void)
|
|
{
|
|
/**
|
|
* @brief 串口初始化
|
|
* PAT1 = 0; //TX输出
|
|
* PAT2 = 1; //RX输入
|
|
*/
|
|
uart0_tx_io = 0; //TX输出
|
|
uart0_rx_io = 1; //RX输入
|
|
|
|
RX0LEN = 0; //8位数据接收格式
|
|
TX0LEN = 0; //8位数据发送格式
|
|
/*
|
|
BJT0EN = 0; //波特率去抖
|
|
BRGH0 = 0; //波特率低速模式:波特率=Fosc/(64*(BRR<7:0>+1))
|
|
BR0R = 0x33; //波特率=32MHz/(64*(51+1))≈9600bps = 9615.38
|
|
*/
|
|
BJT0EN = 1; //波特率去抖
|
|
BRGH0 = 1; //串口处于高速模式
|
|
BR0R = 17;
|
|
BR0FRA = 6;
|
|
|
|
#ifdef IT_SR
|
|
RX0IE = 1;
|
|
GIE = 1;
|
|
#endif
|
|
RX0EN = 1; //打开接收
|
|
TX0EN = 1; //打开发送
|
|
}
|
|
|
|
void uart1_config(void)
|
|
{
|
|
/**
|
|
* @brief 串口1的初始化
|
|
*
|
|
*/
|
|
uart1_tx_io = 0; //TX输出
|
|
uart1_rx_io = 1; //RX输入
|
|
|
|
RX1LEN = 0; //8位数据接收格式
|
|
TX1LEN = 0; //8位数据发送格式
|
|
/*
|
|
BJT1EN = 1; //波特率去抖
|
|
BRGH0 = 0; //波特率低速模式:波特率=Fosc/(64*(BRR<7:0>+1))
|
|
BR1R = 0x33; //波特率=32MHz/(64*(51+1))≈9600bps = 9615.38
|
|
*/
|
|
BJT1EN = 1; //波特率去抖
|
|
BRGH1 = 1; //串口处于高速模式
|
|
BR1R = 17;
|
|
BR1FRA = 6;
|
|
#ifdef IT_SR
|
|
RX1IE = 1;
|
|
GIE = 1;
|
|
#endif
|
|
RX1EN = 1; //打开接收
|
|
TX1EN = 1; //打开发送
|
|
}
|
|
|
|
void uart0_send_byte(char data)
|
|
{
|
|
/**
|
|
* @brief 串口0发送函数(发送一个字节)
|
|
* @param 发送的字符
|
|
*/
|
|
while (!TRMT0)
|
|
;
|
|
TX0B = data;
|
|
}
|
|
|
|
void uart0_send_buff(char *data)
|
|
{
|
|
/**
|
|
* @brief 串口0发送函数(发送字符串)
|
|
* @param 发送的字符串
|
|
*/
|
|
while (*data)
|
|
{
|
|
while (!TRMT0)
|
|
;
|
|
TX0B = *data;
|
|
data++;
|
|
}
|
|
}
|
|
|
|
void uart1_send_byte(char data)
|
|
{
|
|
/**
|
|
* @brief 串口1发送函数(发送一个字节)
|
|
* @param 发送的字符
|
|
*/
|
|
while (!TRMT1)
|
|
;
|
|
TX1B = data;
|
|
}
|
|
|
|
void uart1_send_buff(char *data)
|
|
{
|
|
/**
|
|
* @brief 串口1发送函数(发送字符串)
|
|
* @param 发送的字符串
|
|
*/
|
|
while (*data)
|
|
{
|
|
while (!TRMT1)
|
|
;
|
|
TX1B = *data;
|
|
data++;
|
|
}
|
|
}
|
|
|
|
void uart1_irq_interrupt(void)
|
|
{
|
|
/**
|
|
* @brief 串口1的中断服务函数
|
|
*
|
|
*/
|
|
if (RX1IE == 1)
|
|
{
|
|
//帧格式出错
|
|
if (FERR1 == 1) {
|
|
}
|
|
//接收溢出
|
|
if (OERR1 == 1) {
|
|
//重启串口
|
|
RX1EN = 0;
|
|
RX1EN = 1;
|
|
}
|
|
if ( RX1IF == 1)
|
|
{
|
|
uint8_t rx232data = RX1B;
|
|
recv_from_lcd_data(rx232data);
|
|
}
|
|
}
|
|
}
|
|
|
|
#ifdef IT_SR
|
|
void isr(void) interrupt
|
|
{
|
|
/**
|
|
* @brief 中断服务函数
|
|
*
|
|
*/
|
|
if (RX0IE == 1)
|
|
{
|
|
//帧格式出错
|
|
if (FERR0 == 1) {
|
|
}
|
|
//接收溢出
|
|
if (OERR0 == 1) {
|
|
//重启串口
|
|
RX0EN = 0;
|
|
RX0EN = 1;
|
|
}
|
|
|
|
if ( RX0IF == 1)//串口0(电机数据接收)
|
|
{
|
|
uint8_t rx485data = RX0B;
|
|
recv_from_motor_data(rx485data);
|
|
}
|
|
}
|
|
uart1_irq_interrupt();//屏幕数据接收
|
|
system_module_process_interrupt();//定时器中断
|
|
}
|
|
#endif
|
|
|
|
void urat0_text(void)
|
|
{
|
|
/**
|
|
* @brief 上电测试串口发送是否正常
|
|
*
|
|
*/
|
|
volatile unsigned char str[] = "urat0";
|
|
volatile unsigned char buf[] = "uart1";
|
|
|
|
//send_cmd_to_motor(str, 5); //发送一串字符测试
|
|
|
|
send_cmd_to_lcd(buf, 5); //发送一串字符测试
|
|
}
|