用的青风的例程,USART1是没问题的,但是改成USART2就不行,调了很长时间还是找不到原因。
先贴出USART1的例程吧,接受我用的是中断,发送没用中断。
int main(void)
{
unsigned char i = 3;
USART1_Configuration();
while (1)
{
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
UART1_send_byte(i ++);
}
}
extern void USART1_Configuration(void)//′??ú3?ê??ˉoˉêy
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_SetPriority(USART1_IRQn, 0); /* (4) */
NVIC_EnableIRQ(USART1_IRQn); /* (5) */
RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE );
GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_1);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_1);
/*
* USART1_TX -> PA9 , USART1_RX -> PA10
*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9|GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 115200;//éè??′??ú2¨ì??ê
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//éè??êy?Y??
USART_InitStructure.USART_StopBits = USART_StopBits_1;//éè??í£?1??
USART_InitStructure.USART_Parity = USART_Parity_No;//éè??D§?é??
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//éè??á÷????
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;//éè??1¤×÷?£ê?
USART_Init(USART1, &USART_InitStructure); //????è??á11ì?
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
//USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
USART_Cmd(USART1, ENABLE);//ê1?ü′??ú1
}
extern void UART1_send_byte(uint8_t byte)
{
while(!((USART1->ISR)&(1<<7)));
USART1->TDR=byte;
}
extern volatile uint8_t USART_Data = 0;
extern void USART1_IRQHandler(void)
{
if((USART1->ISR & USART_ISR_TC) == USART_ISR_TC)
{
USART1->ICR |= USART_ICR_TCCF;/* clear transfer complete flag */
}
else if((USART1->ISR & USART_ISR_RXNE) == USART_ISR_RXNE)
{
USART_Data = USART1->RDR;/* receive data, clear flag */
}
else
{
NVIC_DisableIRQ(USART1_IRQn);/* Disable USART1_IRQn */
}
}
上面的代码是可以的,我用仿真器和逻辑分析仪查过,都是没问题的。
但是对着改USART2就是不行,用逻辑分析仪看,TX都没有输出信号。试过做回环测试,发不出数据,自然也不会收到数据,也就进不了接受中断。
int main(void)
{
unsigned char i = 3;
USART2_Configuration();
while (1)
{
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
UART2_send_byte(i ++);
}
}
extern void USART2_Configuration(void)//′??ú3?ê??ˉoˉêy
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_SetPriority(USART2_IRQn, 0); /* (4) */
NVIC_EnableIRQ(USART2_IRQn); /* (5) */
RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE );
GPIO_PinAFConfig(GPIOA,GPIO_PinSource2,GPIO_AF_1);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_1);
/*
* USART2_TX -> PA2 , USART2_RX -> PA3
*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 115200;//éè??′??ú2¨ì??ê
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//éè??êy?Y??
USART_InitStructure.USART_StopBits = USART_StopBits_1;//éè??í£?1??
USART_InitStructure.USART_Parity = USART_Parity_No;//éè??D§?é??
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//éè??á÷????
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;//éè??1¤×÷?£ê?
USART_Init(USART2, &USART_InitStructure); //????è??á11ì?
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
//USART_ITConfig(USART2, USART_IT_TXE, ENABLE);
USART_Cmd(USART2, ENABLE);//ê1?ü′??ú1
}
extern void UART2_send_byte(uint8_t byte)
{
while(!((USART2->ISR)&(1<<7)));
USART2->TDR=byte;
}
extern volatile uint8_t USART_Data = 0;
extern void USART2_IRQHandler(void)
{
if((USART2->ISR & USART_ISR_TC) == USART_ISR_TC)
{
USART2->ICR |= USART_ICR_TCCF;/* clear transfer complete flag */
}
else if((USART2->ISR & USART_ISR_RXNE) == USART_ISR_RXNE)
{
USART_Data = USART2->RDR;/* receive data, clear flag */
}
else
{
NVIC_DisableIRQ(USART2_IRQn);/* Disable USART1_IRQn */
}
} |