cancel
Showing results for 
Search instead for 
Did you mean: 

Keil uvision

SIDRI.1
Associate III

hi everyone, i would like to know if anyone has faced the same probleme : so i'm working with keil uvison5, i have build usart2 (PA2-->Tx / PA3-->Rx), everything works, and the message is printed correctly in the serial monitor, 

but when i'm trying to use USART1 (PA9-->Tx /PA10--Rx), also USART3, the code is compiling but nothing is printed in serial monitor, 

but in mbed, everything works, i have faced

the same probleme with I2C, i have build a driver for my LCD, the code seems okay, but nothing happens.

i think that it could be related with the compiler keil itself ??

I'm not fun of mbed, but it's still my last choice if it doesn't work

#include "stm32f10x.h"                  // Device header
#include "stdio.h"
 
void USART1_Init(void);
void USART_write(int ch);
void DelayTimerUs1(int n);
 
int main (void){
	
	USART1_Init();
	
	while(1){
		
		USART_write('S');
                USART_write('A');
		USART_write('A');
		USART_write('D');
		
DelayTimerUs1(10000);
	}
void USART1_Init(void){
 
	RCC->APB2ENR |= RCC_APB2ENR_USART1EN;
        RCC->APB2ENR |= RCC_APB2ENR_IOPAEN;  	// enable clock for USART1_TX which is connected to PA9 P180 Ref manual
  //RCC->APB2ENR |= RCC_APB2ENR_AFIOEN;
 
	
	// configure PA9 as alternate function to be used for UART_Tx 
	GPIOA->CRH |= GPIO_CRH_MODE9_0 | GPIO_CRH_MODE9_1;
	GPIOA->CRH |= GPIO_CRH_CNF9_1  | GPIO_CRH_CNF9_0 ;
	
   USART1->BRR |= 0x0EA6;  //9600
	
  USART1->CR1 |= USART_CR1_RE | USART_CR1_TE | USART_CR1_UE;
}
	
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){
	// 1 Mhz ---> 1Us
	//ARR = n (n=1 --> 1us) (n=2 --> 2us) ....
// n = 30000 for 30ms delay
	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;
	}
	

4 REPLIES 4
TDK
Guru

USART1 is on APB2. Your BRR value suggests your system clock is 36 MHz, but I bet it's 72 MHz and BRR should be doubled.

> i think that it could be related with the compiler keil itself ??

Based on what information?

If you feel a post has answered your question, please click "Accept as Solution".
SIDRI.1
Associate III

Thank you for your response,

Following the block diagram the microcontroller, USART1 is on APB2 bus -->72MHz, USART2 and USART3 are on APB1 bus -->36Mhz.

the reason why i'm suspecting Keil : USART2 is working perfectly, but usart3 which is on the same bus, so logically use the same BRR, doesn't work, i have activate the clock for USART3 and the other small modifications, but the problem is still here, nothing is printed.

0693W000001s0qyQAA.png

#include "stm32f10x.h"                  // Device header
#include "stdio.h"
 
void USART1_Init(void);
void USART_write(int ch);
void DelayTimerUs1(int n);
 
 
int main (void){
	
	USART1_Init();
	
	while(1){
		
		USART_write('S');
     
		USART_write('A');
		
		USART_write('A');
		
		USART_write('D');
		
		DelayTimerUs1(10000);
	}
	
}
void USART1_Init(void){
   RCC->APB1ENR |= RCC_APB1ENR_USART3EN;
  
	RCC->APB2ENR |= RCC_APB2ENR_IOPBEN; // PB3-->Tx (USART3)
 
 
	// configure PB3 as alternate function USART3-->Tx
	GPIOB->CRL |= GPIO_CRL_MODE3_0 | GPIO_CRL_MODE3_1;
	GPIOB->CRL |= GPIO_CRL_CNF3_1  | GPIO_CRL_CNF3_0 ;
	
  USART3->BRR |= 0x0EA6;
	 //0x1D4C; // 9600
	//0x138A
	//0xEA6
	
 
 USART3->CR1 |= USART_CR1_RE | USART_CR1_TE | USART_CR1_UE;
}
	
void USART_write( int ch){
 
	while(!(USART3->SR & USART_SR_TXE)){}  // we check if the transmit buffer is empty before sending the data
		
		USART3->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){
	// 1 Mhz ---> 1Us
	//ARR = n (n=1 --> 1us) (n=2 --> 2us) ....
// n = 30000 for 30ms delay
	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;
	}
	

TDK
Guru

The last code you posted is different than the code in the OP.

You were asking about USART1 vs USART2 and now you're asking about USART3 vs USART2. Why?

Did you solve the original problem?

If you feel a post has answered your question, please click "Accept as Solution".
SIDRI.1
Associate III

i'm asking about why USART2 is working, and USART3 doesn't work, USART1 also doesn't work, i have posted the second code to show you that even if i have make the necessary configuration for USART3, the problem is still here!! is there any additional configuration ? or should i move to another compiler, because on mbed it works fine, but i'm not fun of mbed