📄 uart1.c
字号:
/***************************************************************
*77LE516的串口0!!!程序
*为GPS定位程序使用。此应用中只是接收GPS发送过来的数据
*在一体机中使用串口0!!!!
***************************************************************/
#include "w77e532.h"
#include "uart.h"
#include "mcuInit.h"
#include "string.h"
/*全双工串口数据结构*/
typedef struct
{
unsigned char rxState; /*接收状态标志*/
unsigned char rxTimer; /*接收超时控制*/
unsigned int rxLen; /*接收数据长度*/
unsigned char *rxPtr; /*当前接收指针*/
}GPS_UART_INFO;
GPS_UART_INFO uart1;
#define UART1_BUFFER_LENGTH 612
unsigned char uart1RxBuffer[UART1_BUFFER_LENGTH];
/*串口1采用定时器1作为波特率发生器*/
void uart1_init(void)
{
uart1.rxState = 0;
uart1.rxLen = 0;
uart1.rxTimer = 0;
uart1.rxPtr = uart1RxBuffer;
memset(uart1RxBuffer, 0, sizeof(uart1RxBuffer) );
/*initialize serial port*/
#ifdef UART_PCON
PCON |= 0x80;
#endif
T2CON = 0x30; /**/
T2MOD = 0;
RCAP2L = T2BAUD_9600;
RCAP2H = T2BAUD_9600 >> 8;
TR2 = 1; /*波特率发生器工作*/
/*初始化串口0*/
SCON = SCON_INIT;
// REN1 = 1; /*允许接收数据*///old主机
ES0 = 1;
REN = 1;
}
void uart1_timeout(void) using 1
{
if( uart1.rxState == 1)
{
uart1.rxTimer ++;
if( uart1.rxTimer > 20)
{/*至少25ms未收到数据,认为接收到一帧数据*/
uart1.rxState = PACKET_RECEIVE_OK;
// REN1 = 0; /*禁止接收,准备处理一帧数据*/////old主机
REN = 0;
}
}
else if( uart1.rxState != PACKET_RECEIVE_OK )
{
uart1.rxState = 0;
uart1.rxLen = 0;
// REN1 = 1;//old主机
REN = 1;
}
}
void enable_rcv_gps(void)
{
uart1.rxState = 0;
uart1.rxLen = 0;
uart1.rxPtr = uart1RxBuffer;
// REN1 = 1;//old主机
REN = 1;
}
/********************************************************
*查看是否收到一个GPS数据包
*return 0:ERROR, \
*other: 接收到的GPS数据包长度
*********************************************************/
int get_gps_packet(char **retPtr)
{
*retPtr = uart1RxBuffer;
if(uart1.rxState == PACKET_RECEIVE_OK )
{
uart1.rxState = 0;
return uart1.rxLen;
}
else
{
return 0;
}
}
/*串口接收中断函数*/
void ISR1uart(void) interrupt 4//7//old主机
{
if(RI)//RI1)//old主机
{
// RI1 = 0;//old主机
RI = 0;
if( uart1.rxLen == 0 )
{
uart1.rxPtr = uart1RxBuffer;
}
if( uart1.rxLen < UART1_BUFFER_LENGTH )
{
*uart1.rxPtr ++ = SBUF;//SBUF1;//old主机
uart1.rxLen ++;
uart1.rxTimer = 0;
uart1.rxState = 1;
}
}
// if(TI1)//old主机
if (TI)
{
// TI1 = 0;
TI = 0;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -