Reputation: 29
This is my code for using usart with stm8l. The main problem I am facing is that no matter what value I assign to the USART_DR register it always takes the value 0xff without even changing as I increment my char variable. Only the USART_SR register toggles between the values 0xc0 and 0xf0 which I suppose is correct.
clockinit(void) //16MHz internal clock enable
{
CLK_SWR = 0x01;
CLK_ICKR = 0x01;
CLK_ECKR = 0x00;
CLK_DIVR = 0x00;
CLK_PCKENR1 = 0xFF;
CLK_PCKENR2 = 0xFF;
}
usartinit(void) //usart initialisation
{
char X;
PC_DDR = 0x10;
PC_CR1 = 0x10;
X = USART1_SR;
X = USART1_DR;
USART1_CR1 = 0x00;
USART1_CR2 = 0x0C;
USART1_CR3 = 0x0f;
USART1_CR4 = 0x03;
USART1_CR5 = 0x00;
USART1_GTR = 0x00;
USART1_PSCR = 0x00;
USART1_BRR2 = 0x0A;
USART1_BRR1 = 0x08;
}
void main()
{
char *z = "HELLO";
clockinit();
usartinit();
val = strlen(z);
while (*z)
{
USART1_DR = (unsigned char) *z;
while (USART1_SR_TXE == 0);
z++;
}
}
Upvotes: 2
Views: 460
Reputation: 18410
The behaviour you observe is as expected:
The USART_DR
register is indeed two registers. One is used when you write to the memory location and takes the next character to transmit. The other one is used when reading from the memory location and contains the last character received.
This architecture was chosen to permit simultaneously sending and receiving from the USART and that is the reason, why you do not read back from that location what you wrote to it.
Upvotes: 2