最近在使用STM32F030-DISCOVERY开发板,使用UART1普通发送,中断接收。
用串口转USB工具在PC监控。
发现我PC端并没有向芯片发送数据,芯片却进入了中断,查询中断状态寄存器,并不是溢出等中断,就是接收中断标志,很奇怪,而且每次显示接收到的都是0xC000,就好像是初始化后移位寄存器里就有数据了。但是不应该啊,为什么呢
void USART_IROHandler(void)
{
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
USART_ClearITPendingBit(USART2,USART_IT_RXNE);
RevBuf = UART_Recive();
RevIsOn = 1;
USART_SendData(USART1, (uint16_t)(USART1->ISR));
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET){} ; //µÈ´ýÊý¾Ý·¢ËÍ
USART_SendData(USART1, (uint16_t)((USART1->ISR)>>8));
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET){} ; //µÈ´ýÊý¾Ý·¢ËÍ
}
}
void Usart1_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
USART_DeInit(USART1); //¸´Î»Usart1
RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);//ʹÄÜUSART1
GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_1);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_1);
USART_InitStructure.USART_BaudRate = dUsart1BaudRate; //²¨ÌØÂÊ
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//Êý¾Ýλ8λ
USART_InitStructure.USART_StopBits = USART_StopBits_1;//ֹͣλ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, ENABLE);//½ÓÊÕÖжÏʹÄÜ
USART_ITConfig(USART1, USART_IT_RXNE, DISABLE);
USART_Cmd(USART1, ENABLE);
USART_ClearITPendingBit(USART2,USART_IT_RXNE);
GPIO_InitStructure.GPIO_Pin = (GPIO_Pin_9);//TX1
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//¸´ÓÃÊä³ö
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//Êä³öÉèΪÍÆÍìģʽ
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;;//
GPIO_Init( GPIOA, &GPIO_InitStructure );
GPIO_InitStructure.GPIO_Pin = (GPIO_Pin_10);//RX1
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//¸´ÓÃ
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;;
GPIO_Init( GPIOA, &GPIO_InitStructure );
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
int main ( void )
{
SystemInit();
LED_Init();
Usart1_Init();
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//½ÓÊÕÖжÏʹÄÜ
USART_ClearITPendingBit(USART2,USART_IT_RXNE);
LED_Open( GPIO_Pin_9 );
while(1)
{
if ( RevIsOn )
{
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET){} ; //µÈ´ýÊý¾Ý·¢ËÍ
USART1_Puts("received");
USART_SendData(USART1, RevBuf);
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
LED_reverse( GPIO_Pin_9 );
RevIsOn = 0;
}
}
}
|