在线时间0 小时
UID315428
ST金币0
蝴蝶豆0
注册时间2011-8-29
新手上路
- 最后登录
- 1970-1-1
|
楼主 |
发表于 2011-9-23 16:07:05
|
显示全部楼层
a0a.1 0b0c
回复:USART接受数据时丢包
串口接收GPS信号
串口的初始化,选用的是USART2
void wvGPS_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
USART_ClockInitTypeDef USART_InitClockStructure;
NVIC_InitStructure.NVIC_IRQChannel = EVAL_COM1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
RCC_AHB1PeriphClockCmd(EVAL_COM1_TX_GPIO_CLK|EVAL_COM1_RX_GPIO_CLK, ENABLE);
RCC_APB1PeriphClockCmd(EVAL_COM1_CLK, ENABLE);
GPIO_PinAFConfig(EVAL_COM1_TX_GPIO_PORT,EVAL_COM1_TX_SOURCE,EVAL_COM1_TX_AF);
GPIO_PinAFConfig(EVAL_COM1_RX_GPIO_PORT,EVAL_COM1_RX_SOURCE,EVAL_COM1_RX_AF);
//TX
GPIO_InitStructure.GPIO_Pin = EVAL_COM1_TX_PIN;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_Init(EVAL_COM1_TX_GPIO_PORT, &GPIO_InitStructure);
//RX
GPIO_InitStructure.GPIO_Pin = EVAL_COM1_RX_PIN;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_Init(EVAL_COM1_RX_GPIO_PORT, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 4800;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_Mode = USART_Mode_Rx|USART_Mode_Tx;
USART_InitStructure.USART_HardwareFlowContro= USART_HardwareFlowControl_None;
USART_InitClockStructure.USART_CPOL = USART_CPOL_Low;
USART_InitClockStructure.USART_CPHA = USART_CPHA_2Edge;
USART_InitClockStructure.USART_LastBit = USART_LastBit_Disable;
USART_InitClockStructure.USART_Clock = USART_Clock_Disable;
USART_ClockInit(USART2, &USART_InitClockStructure);
USART_Init(EVAL_COM1, &USART_InitStructure);
USART_Cmd(EVAL_COM1, ENABLE); //enable USART2
USART_ITConfig(EVAL_COM1, USART_IT_RXNE, ENABLE); //开USART2的接收中断
}
接收数据程序
void wvGPS_usart(void)
{
char res1;
static int8u NEMA_count = 0; //接收GPS数据的条数
static int8u USART_RX_STA = 0; //接收状态标记
if (USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == SET)//接收到数据 RXNE 位,USART->SRZ中
{
res1 = USART_ReceiveData(USART2);
if ('$' == res1)
{
GPS_NEMA[NEMA_count].isupdated = 1; //将上面一条语句打上更新标志
NEMA_count++;
if(NEMA_count > (NEMA_NUM_MAX-1))
{
NEMA_count = 0;
}
GPS_NEMA[NEMA_count].isupdated = 0; //将本条语句打上未更新标志
GPS_NEMA[NEMA_count].buffer[0] = '$';
USART_RX_STA = 1;
}
else
{
if (USART_RX_STA < NEMA_CHAR_MAX)
{
GPS_NEMA[NEMA_count].buffer[USART_RX_STA++] = res1;
}
if (USART_RX_STA > NEMA_CHAR_MAX-1) //
USART_RX_STA=0;
}
}
}
中断程序
void USART2_IRQHandler(void)
{
wvGPS_usart();
}
有时会把字符“*”接收为“0x2A”有时候就是0,别的字符没发现错误
回复第 2 楼 于2011-09-23 06:49:50发表:
能不能把代码贴出来看看。可能是程序读取数据时候的问题。
|
|