刚学
单片机, 弄了2天没有搞定,希望大家帮帮忙。谢谢!
void USART_Config_USART1()
{
GPIO_InitTypeDef GPIO_InitStructure9;
GPIO_InitTypeDef GPIO_InitStructure10;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
// Enable GPIO clock
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
// Enable USART clock
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
// Configure USART Tx as alternate func
tion
GPIO_InitStructure9.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure9.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure9.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure9.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure9.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure9);
// Configure USART Rx as alternate function
GPIO_InitStructure10.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure10.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure10.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure10);
// Config AF pin
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);
// Initialize USART
USART_InitStructure.USART_BaudRate = 9600;
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);
// Enable USART interrupt
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
//USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
// Enable USART
USART_Cmd(USART1, ENABLE);
// Initialize USART interrupt on NVIC
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void USART1_IRQHANDLER(void)
{
if(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) != RESET)
{
USART_GetFlagStatus(USART1, USART_FLAG_TC);
// Read one byte from the receive data register
USART_SendData(USART1, USART_ReceiveData(USART1));
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
{
}
}
}
// Enable USART interrupt
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
//USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
// Enable USART
USART_Cmd(USART1, ENABLE);
#if defined(INNO_DEBUG)
USART1_SendDataStrings("USART1 CMD enabled! ");
#endif
// Initialize USART interrupt on NVIC
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0xF;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
问题: 使能中断不能在使能NVIC 表格之前
一周热门 更多>