cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F107 not sending data to PC. How can I know what's wrong?

Mr.Resse
Associate

Here is the code:

#include "stm32f10x.h"                  // Device header
void USART2_ini(void);
void USART_write(int ch);
void delayMS(int delay);
int main(void)
{
	USART2_ini();
	while(1)
	{
		USART_write('H');
		USART_write('I');
		delayMS(100);
	}
}
void USART2_ini(void)
{
	RCC->APB1ENR |= (1<<17);   
	RCC->APB2ENR |= (1<<2);
	RCC->APB2ENR |= 1; 				
	AFIO -> MAPR |= (1<<4);
	GPIOA -> CRL |= 0x00000B00;
	USART2 -> BRR = 0x0341; //Baud rate of 9600 @8MHz
	USART2 -> CR1 |= 0x0008;
	USART2 -> CR1 |= 0x2000; //Enabling USART itself!
}
void USART_write(int ch)
{
	while(!(USART2 -> SR & 0x0080))
		USART2 -> DR = (ch & 0xFF);
}
void delayMS(int delay)
{
	int i;
	for(;delay>0;delay--)
	{
		for(i=0;i<3195;i++);
	}
}
 
 
 

I am new to all of this stuff and also am new to this site and progress of asking questions.

The code is written based on "Bare-Metal"

3 REPLIES 3

There's the reference manual.

Working code examples.

A Debugger to look at registers involved.

Scope to confirm output and timings.

Delay looks miles from right, and apt to be optimized away.​

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Mr.Resse
Associate

@Community member​ 

Thanks, could you explain a little bit more about what's wrong with the delay?

Likely needs the variables to be volatile so it's not folded away.

Toggle a GPIO to tune.

Perhaps better to watch time advance via free running TIM, or DWT's CYCCNT, as these will be less dependent on address alignments, and more immune from interrupts.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..