欧美极品高清xxxxhd,国产日产欧美最新,无码AV国产东京热AV无码,国产精品人与动性XXX,国产传媒亚洲综合一区二区,四库影院永久国产精品,毛片免费免费高清视频,福利所导航夜趣136
標(biāo)題:
關(guān)于STM32串口1IDLE模式兼DMA接收數(shù)據(jù)會(huì)丟失數(shù)據(jù)的問(wèn)題
[打印本頁(yè)]
作者:
Sanada
時(shí)間:
2018-11-24 19:55
標(biāo)題:
關(guān)于STM32串口1IDLE模式兼DMA接收數(shù)據(jù)會(huì)丟失數(shù)據(jù)的問(wèn)題
本人想用STM32的DMA和串口1 的ILDE模式接收不定長(zhǎng)數(shù)據(jù)幀,遇到一個(gè)奇怪的現(xiàn)象,當(dāng)串口助手V2.0(正點(diǎn)原子的)手動(dòng)發(fā)送所需要的數(shù)據(jù)包時(shí),誤包率很高,而改為60ms自動(dòng)發(fā)送就幾乎沒(méi)有出現(xiàn)誤包的問(wèn)題,所謂的誤包問(wèn)題要不出現(xiàn)功能碼出錯(cuò),要不幀長(zhǎng)度出現(xiàn)錯(cuò)誤了。
void Configuration_GPIO()
{
GPIO_InitTypeDef GPIO_InitStructure;
// USART1_TX:PA9
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// USART1_RX:PA10
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
復(fù)制代碼
#include "Configuration_DMA.h"
u8 RecBuffer[RecLength] = {0};
u8 Usart1_Rec_Cnt = 0;
void Configuration_DMA_USART()
{
DMA_InitTypeDef DMA_InitStructure;
/* 該DMA用于USART快速傳輸一幀數(shù)據(jù),單字節(jié)時(shí)不需要 */
DMA_DeInit(DMA1_Channel5);
DMA_InitStructure.DMA_PeripheralBaseAddr = (u32) &USART1->DR;
DMA_InitStructure.DMA_MemoryBaseAddr = (u32) &RecBuffer;//這是初始化時(shí)候配置,可以根據(jù)程序運(yùn)行重新設(shè)置。
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize = RecLength;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel5, &DMA_InitStructure);
DMA_Cmd(DMA1_Channel5, ENABLE);
}
復(fù)制代碼
void Control_NVIC_USART1(FunctionalState NewStatus)
{
NVIC_InitTypeDef NVIC_InitStructure;
// 設(shè)置NVIC中斷分組4:4位搶占優(yōu)先級(jí),0位響應(yīng)優(yōu)先級(jí)
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd = NewStatus;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_Init(&NVIC_InitStructure);
}
復(fù)制代碼
void Configuration_USART(u32 baudrate)
{
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = baudrate;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure);
USART_ITConfig(USART1, USART_IT_RXNE, DISABLE); // 不使能接收中斷
USART_ITConfig(USART1, USART_IT_TC, DISABLE); // 不使能發(fā)送中斷
USART_ITConfig(USART1, USART_IT_IDLE, ENABLE); // 使能空閑中斷
USART_ClearFlag(USART1, USART_FLAG_TC);
USART_DMACmd(USART1, USART_DMAReq_Rx, ENABLE);
USART_Cmd(USART1, ENABLE);
// 防止首字節(jié)丟失
while(USART_GetFlagStatus(USART1,USART_FLAG_TC) == RESET);
}
void USART1_SendByte(uint8_t c)
{
USART_SendData(USART1, (uint8_t)c);
while(USART_GetFlagStatus(USART1,USART_FLAG_TC) == RESET);
}
void USART1_SendNByte(u8 *pData,u8 Length)
{
while(Length--)
{
USART1_SendByte(*pData);
pData++;
}
}
void USART1_IRQHandler(void)
{
u8 i;
if(USART_GetITStatus(USART1, USART_IT_IDLE) != RESET)
{
// 關(guān)閉USART1 TX DMA1 所指示的通道
DMA_Cmd(DMA1_Channel5, DISABLE);
// 讀取數(shù)據(jù) 注意:這句必須要,否則不能夠清除中斷標(biāo)志位。我也不知道為啥!
USART_ReceiveData(USART1);
// 算出接本幀數(shù)據(jù)長(zhǎng)度
Usart1_Rec_Cnt = RecLength - DMA_GetCurrDataCounter(DMA1_Channel5);
if(Usart1_Rec_Cnt != 8)
Usart1_Rec_Cnt = 0;
/*------------------------串口測(cè)試段--------------------------------*/
for(i = 0;i < Usart1_Rec_Cnt;i++)
RecBuffer1[i] = RecBuffer[i];
//***********幀數(shù)據(jù)處理函數(shù)************//
// printf ("The lenght:%d\r\n",Usart1_Rec_Cnt);
// printf ("The data:\r\n");
// USART1_SendNByte(RecBuffer, Usart1_Rec_Cnt);
// printf ("\r\nOver! \r\n");
//*************************************//
// 清除中斷標(biāo)志
USART_ClearITPendingBit(USART1, USART_IT_IDLE);
// DMA通道的DMA緩存的大小
DMA_SetCurrDataCounter(DMA1_Channel5, RecLength);
// 使能USART1 TX DMA1 所指示的通道
DMA_Cmd(DMA1_Channel5, ENABLE);
}
}
復(fù)制代碼
以上是我的GPIO。DMA、USART和中斷的配置,請(qǐng)遇到過(guò)類似問(wèn)題的大佬能夠伸出援手,在下感激不盡!
作者:
Dradece
時(shí)間:
2019-2-15 20:39
樓主解決了嗎,我也遇到過(guò)了這問(wèn)題
作者:
pkoko
時(shí)間:
2019-12-25 20:42
遇到類似問(wèn)題,用串口助手自動(dòng)發(fā)送,一樣丟包
歡迎光臨 (http://m.raoushi.com/bbs/)
Powered by Discuz! X3.1