0

[debugging window][1]I have tried to print a message using USART1, the code is compiling but nothing is printed on the serial monitor, i have tried the same configuration ( except baud rate and clk enabling) on usart2 and everything works, this is my code, please anyone has faced the same probleme with keil, ? on mbed , both usart1 and usart2 are working fine, this is my new code version 2[debugging window][2] :

this is my code after some modification i have added a screen to show the debugging window, i have deleted ORR, and cleared ODR register for PIN9,



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
  • USART1->BRR |= 0x1D4C; //9600 you are using a lot of ORR equals, this one and perhaps others you probably meant USART1->BRR = 0x1D4C; – old_timer Jul 03 '20 at 14:46
  • any OR equals either needs to be based on knowledge that those bits are zero based on reset or you need to zero them with an and then orr them which shouldnt be done as two separate accesses to the register but x = register; x&= something, x|= something; register = x; You are on the right path though – old_timer Jul 03 '20 at 14:48
  • have you debugged the timer code? are you monitoring the correct pins? what do you see on the oscilloscope? what if you remove the timer code and only send one character (cut the problem in half)? – old_timer Jul 03 '20 at 14:50
  • thank you for your response, concernig the ORR|= .. i have tried the same syntax for usart2 driver, and it works, the message is printed on my serial monitor, but usart1 as you can show in the code above doesn't work, i have checked also the baud rate at 72mhz (Usart1-->APB2 bus), the timer works fine i have tested it separately , .... really it frustrating :( – Saâd Idrissi Jul 03 '20 at 19:07
  • works or not is not what I was talking about, you should try to understand bitwise logic operations – old_timer Jul 03 '20 at 21:44
  • how are you monitoring PA9 and how were you monitoring PA2 (or an alternative) for usart2? – old_timer Jul 03 '20 at 21:50
  • actually going back to basic logic, with using only the OR function how do you expect to clear bits in the CRH register? the reset value for a port is 0b0100 and you want to make it 0b10xx where xx is non-zero, at best you are going to make it 0b11xx which is alternate function open drain and you then need a pull up somewhere, but why configure it wrong when you can configure it correctly as an alternate function push-pull? – old_timer Jul 03 '20 at 21:53
  • getting lucky with usart2 may have to do with how you are connected to an external uart on each of these pins. configure the registers correctly. you should have seen this problem on a scope. – old_timer Jul 03 '20 at 21:55
  • I have configured the pin PA9-->Tx as alternative function push pull GPIOA->CRH |= GPIO_CRH_MODE9_1 | GPIO_CRH_CNF9_1; GPIOA->CRH &=~ GPIO_CRH_MODE9_0 | GPIO_CRH_CNF9_0; before this i have tried to clear CRH register as follow : GPIOA->CRH &=~ GPIO_CRH_MODE9 | GPIO_CRH_CNF9; I think it's correct, when i measure the voltage at the PA9 output i have found 2.64v or somthing like this ! – Saâd Idrissi Jul 04 '20 at 20:03
  • magic defines like GPIO_CRH_MODE9_0 are useless. Post their value or show a disassembly of the related code so that we can see them. Post a minimal complete example that demonstrates the problem. – old_timer Jul 04 '20 at 20:16
  • Okay, i have reedit the code you can see it above, and add a screen of my debugg window – Saâd Idrissi Jul 04 '20 at 20:47
  • after debug, i figured out that the program is blocked into while loop – Saâd Idrissi Jul 04 '20 at 21:40

0 Answers0