2020-03-10 07:18 AM
I'm using stm32f103ze to send some data over the serial peripheral, but it didn't work. The settings are correctly set but when I try to write to the data register it always remains zero. Furthermore, none of the Serial peripherals seem to work. What have I missed? Thanks.
Here is the code:
#include "stm32f10x.h" // Device header
#include "stdio.h"
#include "stdarg.h"
void USART1_Init(void);
void USART1_write(char ch);
void printMsg(char* buffer, char *msg, ...);
int main(void)
{
USART1_Init();
char buffer[100];
printMsg(buffer, "Today is %s", "awesome");
while(1)
{
}
}
void USART1_Init(void)
{
//Enable clock for USART1 and Port A
RCC->APB2ENR |= RCC_APB2ENR_USART1EN;
RCC->APB2ENR |= RCC_APB2ENR_IOPAEN;
/* Reset USART1*/
RCC->APB2RSTR |= RCC_APB2RSTR_USART1RST;
/* Clear reset */
RCC->APB2RSTR &= ~RCC_APB2RSTR_USART1RST;
// PA9 as alternate function push pull
GPIOA->CRH |= GPIO_CRH_MODE9 | GPIO_CRH_CNF9_1;
GPIOA->CRH &= ~GPIO_CRH_CNF9_0;
// Set you Baud rate
USART1->BRR = 0xEA6; // 9600bps @ 36MHz
USART1->CR1 |= USART_CR1_TE; // TE
USART1->CR1 |= USART_CR1_UE; // UE
}
void USART1_write(char ch)
{
while((USART1->SR & USART_SR_TXE) == RESET) {}
USART1->DR = ch;
}
void printMsg(char* buffer, char *msg, ...)
{
//char buffer[100];
va_list args;
va_start(args, msg);
vsprintf(buffer, msg, args);
for (int i = 0; i < buffer[i] != '\0'; i++)
{
USART1_write(buffer[i]);
}
}
Solved! Go to Solution.
2020-03-10 08:26 AM
It is a register, not a memory cell. The values read/written to USART->DR refer to TWO DIFFERENT internal registers. The write goes to a transmit holding register and the read from a receive holding register.
Don't peer into the register file in the debugger, it will clear bits in the status registers.
2020-03-10 08:26 AM
It is a register, not a memory cell. The values read/written to USART->DR refer to TWO DIFFERENT internal registers. The write goes to a transmit holding register and the read from a receive holding register.
Don't peer into the register file in the debugger, it will clear bits in the status registers.
2020-03-10 09:07 AM
+1
Also, check if clock is set as you expect, i.e. UART's clock is 36MHz.
Observe the Tx pin using oscilloscope/LA.
Try to wiggle it as a GPIO output to exclude hardware problem.
JW
2020-03-10 06:46 PM
I have checked the clock settings and everything is as I expected. The BRR, CR1 settings are set correctly. Even the TXE flag is cleared when I write to the DR(TDR) although no data is written in effect. I used a logic analyzer in USART1 TX (PA9) pin , but there is nothing shown.