问题都解决了,做以下结贴:
第一个问题:
USART1是由PCLK2提供时钟,故读取如下的值:
apbclock_SYSCLK = RCC_ClocksStatus.SYSCLK_Frequency;// 系统时钟
apbclock_PCLK2 = RCC_ClocksStatus.PCLK2_Frequency; //PCLK2值
Uart_Init(); // 使用的波特率是9600
baud_div = USART1->BRR; // 串口分频值
可以读出apbclock_SYSCLK = 0x7270E0000(120M)
apbclock_PCLK2 = 0x3938700(60M)
baud_div = 0x186A(6250)
计算baudrate = apbclock_PCLK2 / baud_div = 9600 一切都是如此完美
可,这一切都是假象,在stm32f2xx.h中有这么一句
#define HSE_VALUE ((uint32_t)25000000) /*!< Value of the External oscillator in Hz */
原来这里把外围晶振定义为了25MHZ,修改为12MHZ,
从新读取apbclock_SYSCLK = 0x57600000(57.6M)
apbclock_PCLK2 = 0x28800000(28.8M)
baud_div = 0xBB8(3000)
USART1使用的实际频率是28.8MHZ,28800000 / 6250 = 4608(实际的波特率) 原来这才是真相啊
之后修改了RCC寄出器一些寄存器的值,使系统时钟达到120MHZ就可以了
第二个问题:打开NVIC_Config();会“死”在中断中
如4楼所说,因为开启了串口发送中断,但是没有对TXE中断进行处理,感谢4楼的回答
datesheet有如下说明:
Bit 7TXE: Transmit data register empty
This bit is set by hardware when the content of the TDR register has been transferred into
the shift register. An interrupt is generated if the TXEIE bit =1 in the USART_CR1 register. It
is cleared by a write to the USART_DR register.
0: Data is not transferred to the shift register
1: Data is transferred to the shift register)
即当向USART_DR中写数据的时候才可以将标志位清楚。所以我将中断函数做了如写的修改
void USART1_IRQHandler(void)
{
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
UART5_RcvBuf[UART5_COUNT++] = USART_ReceiveData(USART1);
//USART1->SR &= ~0x20;
}
if(USART_GetITStatus(USART1, USART_IT_TXE) != RESET)
{
UART1_SendCount++;
/* Write one byte to the transmit data register */
USART_SendData(USART1, 0xAA);
if(UART1_SendCount >= 10)
{
/* Disable the USART1 Transmit interrupt */
USART_ITConfig(USART1, USART_IT_TXE, DISABLE);
}
}
}