[mw_shl_code=c,true]#include "pbdata.h"
void RCC_Configration(void);
void GPIO_Configration(void);
void NVIC_Configuration(void);
void USART_Configuration(void);
int main(void)
{
RCC_Configration();
GPIO_Configration();
USART_Configuration();
NVIC_Configuration();
while(1)
{
if((u8)(USART1->DR) == 0xFC)
{
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);//检测到标志位打开中断
}
}
}
void RCC_Configration(void)
{
SystemInit();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
}
void GPIO_Configration(void)
{
GPIO_InitTypeDef GPIO_Initstructure;
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);
GPIO_Initstructure.GPIO_Pin = GPIO_Pin_10;
GPIO_Initstructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA,&GPIO_Initstructure);
}
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void USART_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate=19200;
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);//先关闭中断,等收到 FC 标志时打开
USART_Cmd(USART1,ENABLE);
USART_ClearFlag(USART1,USART_FLAG_TC);
}
这是中断函数
void USART1_IRQHandler(void)
{
if(USART_GetITStatus(USART1,USART_IT_RXNE)!=RESET)
{
ReceiveData[i++] = USART_ReceiveData(USART1);
}
if(i >= 10)
{
i = 0;
USART_ITConfig(USART1,USART_IT_RXNE,DISABLE);//关中断
for(j=0; j<10; j++)
{
USART_SendData(USART1,ReceiveData[j]);
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
}
}
}
[/mw_shl_code]
大神帮忙看下,要实现的功能是:STM32接收数据,但要检测接收标志位,外部一次要发送10个字节数据,当检测到数据FC时开中断,然后在中断中开始接收数据,存放到依次存放到ReceiveData[10]这个数组中,然后在中断中发数据,发回电脑。本程序接收数据不稳定,不知咋回事? 想用DMA去做,怎样实现?还有就是外部数据一直循环发送,不会停止!! 先谢谢大神了!
|