0

[调试窗口][1]我尝试使用 USART1 打印一条消息,代码正在编译,但串行监视器上没有打印任何内容,我在 usart2 上尝试了相同的配置(除了波特率和 clk 启用),一切正常,这是我的代码,请问有人遇到过同样的keil问题吗?在 mbed 上,usart1 和 usart2 都工作正常,这是我的新代码版本 2[调试窗口][2]:

这是我的代码经过一些修改我添加了一个屏幕来显示调试窗口,我删除了 ORR,并清除了 PIN9 的 ODR 寄存器,



void USART1_Init(void);
void USART_write(int ch);
void DelayTimerUs1(int n);
//void DelayMS (int delay);

int main (void){
    
    USART1_Init();
    
    while(1){
        
        USART_write('B');
     
        USART_write('R');
        
        USART_write('A');
        
        USART_write('H');
        //DelayMS (1);
        DelayTimerUs1(10000);
    }
    
}
void USART1_Init(void){
    
    // Clear 
    USART1->CR1 &=~USART_CR1_UE;
  USART1->CR1 &=~USART_CR1_M;
    USART1->CR2 &=~USART_CR2_STOP;
    USART1->CR1 &=~USART_CR1_PCE;
    USART1->BRR = 0x1D4C;
    USART1->CR1 = (USART_CR1_TE | USART_CR1_RE);
    USART1->CR1 = USART_CR1_UE;
    RCC->APB2ENR = RCC_APB2ENR_IOPAEN;  /
    GPIOA->ODR &=~ GPIO_ODR_ODR9;
    
    RCC->APB2ENR |= RCC_APB2ENR_AFIOEN;
    
    GPIOA->CRH =( GPIO_CRH_MODE9_1  | GPIO_CRH_CNF9_1);
    GPIOA->CRH &=~ GPIO_CRH_MODE9_0  | GPIO_CRH_CNF9_0;
    
   RCC->APB2ENR = RCC_APB2ENR_USART1EN;

}
    
void USART_write( int ch){

    while(!(USART1->SR & USART_SR_TXE)){}  // we check if the transmit buffer is empty before sending the data
        
        USART1->DR |= (ch & 0xFF );   
    }
    void DelayTimerUs1(int n){

    RCC->APB1ENR |= RCC_APB1ENR_TIM2EN;
    TIM2->PSC = 7200;
    TIM2->ARR = n;
    TIM2->CNT = 0;
    TIM2->CR1 = TIM_CR1_CEN;
    
    for( int i =0; i<n; i++){
        
          while (!(TIM2->SR & (1<<0))) {}
        }
    TIM2->SR &=~ (1<<0);
        
    TIM2->CR1 &=~ TIM_CR1_CEN;
    }
    #include "stm32f10x.h"                  // Device header


void USART1_Init(void);
void USART_write(int ch);
void DelayTimerUs1(int n);
//void DelayMS (int delay);

int main (void){
    
    USART1_Init();
    
    while(1){
        
        USART_write('B');
        USART_write('R');
        USART_write('A');
        USART_write('H');
        DelayTimerUs1(10000);
    }   
}
void USART1_Init(void){

    USART1->CR1 &=~USART_CR1_UE;
  USART1->CR1 &=~USART_CR1_M;
    USART1->CR2 &=~USART_CR2_STOP;
    USART1->CR1 &=~USART_CR1_PCE;
    USART1->BRR |= 0x1D4C;
    USART1->CR1 |= (USART_CR1_TE | USART_CR1_RE);
    USART1->CR1 |= USART_CR1_UE;
    RCC->APB2ENR |= RCC_APB2ENR_IOPAEN;  // enable clock for USART2TX which is connected to PA2 P180 Ref manual
        
    GPIOA->ODR &=~ GPIO_ODR_ODR9;
    
    RCC->APB2ENR |= RCC_APB2ENR_AFIOEN;
    
    GPIOA->CRH |= GPIO_CRH_MODE9_1  | GPIO_CRH_CNF9_1;
    GPIOA->CRH &=~ GPIO_CRH_MODE9_0  | GPIO_CRH_CNF9_0;
    

    RCC->APB2ENR |= RCC_APB2ENR_USART1EN;
    
  //USART1->BRR |= 0x1D4C; 
    // 72Mhz -->USART2
     //0x1D4C; // 9600--->USART1
    //0x138A
    //0xEA6

}
    
void USART_write( int ch){

    while(!(USART1->SR & USART_SR_TXE)){}  // we check if the transmit buffer is empty before sending the data
        
        USART1->DR |= (ch & 0xFF );    // contains the received or transmitted data 
                                    //we put the data which we will send in DR register of the microcontroller
    }

    void DelayTimerUs1(int n){

    RCC->APB1ENR |= RCC_APB1ENR_TIM2EN;
    TIM2->PSC = 7200;
    TIM2->ARR = n;
    TIM2->CNT = 0;
    TIM2->CR1 = TIM_CR1_CEN;
    
    for( int i =0; i<n; i++){
        
          while (!(TIM2->SR & (1<<0))) {}
        }
    TIM2->SR &=~ (1<<0);
        
    TIM2->CR1 &=~ TIM_CR1_CEN;
    }
    ```
[As you can see in the screen the debug window, i don't know exactly what happens][2]

}```


  [1]: https://i.stack.imgur.com/n1QGx.png
  [2]: https://i.stack.imgur.com/wbxnC.png
4

0 回答 0