2021-09-01 05:57 AM
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"
2021-09-01 06:02 AM
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.
2021-09-01 06:17 AM
@Community member
Thanks, could you explain a little bit more about what's wrong with the delay?
2021-09-01 06:43 AM
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.