您好,我正在使用 stm32f103c8,我正在尝试使用 usart2,但它不起作用。当我使用与 USART2 相同配置的 USART1 时,它对我很有效。但是 USART2 向 PC 终端发送垃圾 有人可以帮我解决 USART2 中的问题吗?
这是 USART2 的配置:
int main(void){
RCC_VidInit(); //INTIALIZE EXTERNAL CRYSTAL OSCILATOR = 8 MHZ
RCC_VidEnablePeripheralClock( APB2_BUS , 2 ); //ENABLE CLOCK OF GPIOA
CLR_BIT( GPIOA->CRL , 8 ); SET_BIT( GPIOA->CRL , 9 ); //SPEED OF PA2 = 2MHZ
CLR_BIT( GPIOA->CRL , 10 ); SET_BIT( GPIOA->CRL , 11); //PA2 OUTPUT PUSHPULL AF
CLR_BIT( GPIOA->CRL , 12 ); CLR_BIT( GPIOA->CRL , 13 ); //PA3 INPUT
SET_BIT( GPIOA->CRL , 14 ); CLR_BIT( GPIOA->CRL , 15); //PA3 FLOATING INPUT
RCC_VidEnablePeripheralClock( APB1_BUS , 17 ); //ENABLE CLOCK OF USART2
SET_BIT( USART2->CR1 , 13 ); SET_BIT( USART2->CR1 , 3 ); //ENABLE UE & TE
USART2 -> BRR = 0x341 ; //BAUDRATE 9600 OF CRYSTLE 8MHZ BY Eq = (8000 000 / 16 * 9600)
while(1){
if( ( GET_BIT( USART2 -> SR , 6 ) ) == 1 ){ //CHECH ABOUT TC FLAG
USART2 -> DR = '1' ; /*PUT DATA ON DATA REG*/ for(int i = 0 ; i <= 1000000 ; i++ ); /*JUST BASIC DELAY*/ }}
return 0; }
这是适合我的 USART1 配置
int main(void){
RCC_VidInit(); //INTIALIZE EXTERNAL CRYSTAL OSCILATOR = 8 MHZ
RCC_VidEnablePeripheralClock( APB2_BUS , 2 ); //ENABLE CLOCK OF GPIOA
CLR_BIT( GPIOA->CRH , 4 ); SET_BIT( GPIOA->CRH , 5 ); //SPEED OF PA9 = 2MHZ
CLR_BIT( GPIOA->CRH , 6 ); SET_BIT( GPIOA->CRH , 7 ); //PA9 OUTPUT PUSHPULL AF
CLR_BIT( GPIOA->CRH , 8 ); CLR_BIT( GPIOA->CRH , 9 ); //PA10 INPUT
SET_BIT( GPIOA->CRH , 10 ); CLR_BIT( GPIOA->CRH , 11 ); //PA10 FLOATING INPUT
RCC_VidEnablePeripheralClock( APB2_BUS , 14 ); //ENABLE CLOCK OF USART1
SET_BIT( USART1->CR1 , 13 ); SET_BIT( USART1->CR1 , 3 ); //ENABLE UE & TE
USART1 -> BRR = 0x341 ; //BAUDRATE 9600 OF CRYSTLE 8MHZ BY Eq = (8000 000 / 16 * 9600)
while(1){
if( ( GET_BIT( USART1 -> SR , 6 ) ) == 1 ){ //CHECH ABOUT TC FLAG
USART1 -> DR = '1' ; /*PUT DATA ON DATA REG*/ for(int i = 0 ; i <= 1000000 ; i++ ); /*JUST BASIC DELAY*/ }}
return 0; }