功能:从电脑端发送数据给32,再由32返回至PC端
疑问:调试了很多次,发现不开启中断才能正常收发数据,请问例程里为什么要打开中断
#include "led.h"
#include "delay.h"
#include "sys.h"
#include "key.h"
#include "usart.h"
//ALIENTEK Mini STM32??·?°?·????ú??3
//???????é
//????????:www.openedv.com
void Uart_init(u32 bound)
{
//?¨???á????±???
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
//??????????IO?±??
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO, ENABLE);
//RCC->APB2ENR|=1<<2; // ????GPIOA?±??
//RCC->APB2ENR|=1<<14;// ????????1?±??
//GPIO????
GPIO_InitStructure.GPIO_Pin =GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode =GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
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);
//Usart1 NVIC ????
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ?¨??????
NVIC_Init(&NVIC_InitStructure); //?ù??NVIC_InitStruct?????¨???????????????èNVIC?????÷USART1
//USART ???????è??
USART_InitStructure.USART_BaudRate=bound;
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_Cmd(USART1, ENABLE);
}
int main(void)
{
u8 res;
SystemInit();//
delay_init(72);
NVIC_Configuration();
Uart_init(9600);
while(1)
{
if(USART_GetFlagStatus(USART1,USART_FLAG_RXNE)==1)
{
res=USART1->DR;
USART_SendData(USART1,res);
while( USART_GetFlagStatus(USART1,USART_FLAG_TC)!= SET);
}
}
}
|