USART with stm8l

Thread Starter

ABHI1892

Joined Jun 23, 2017
6
This is my code for using usart with stm8l. However I'm facing an issue that whatever value I assign to the USART_DR register it always takes the value 0xff even if it is a char value or a int value. I'm using the inbuilt debugger of the STVD for debugging. Is my approach wrong?
C:
# include "stm8l.h"
# include "stm8s.h"

unsigned int i = 0;
unsigned int val = 0;

strlen(const char *str)
{
const char *s;

for (s = str; *s; ++s);
return (s - str);
}

void clockinit(void) //16MHz internal clock enable
{
CLK_SWR = 0x01;
CLK_ICKR = 0x00;
CLK_ECKR = 0x00;
CLK_DIVR = 0x00;
CLK_PCKENR1 = 0xFF;
CLK_PCKENR2 = 0xFF;
}

void usartinit(void)
{
char X;
PC_DDR = 0x10;
PC_CR1 = 0x10;
PE_DDR = 0xff;
PE_CR1 = 0xff;
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);

for (i = 0;i<val;i++)
{
USART1_DR = *z;

while (USART1_SR_TXE == 1)
z++;
}
}
Mod edit: please use code tags
 

Attachments

Last edited by a moderator:

MrChips

Joined Oct 2, 2009
30,712
Your problem could be with the statement
char *z = "HELLO";

not being intialized.

Break it down into steps:

Code:
char sz[20];
char *z;
strcpy(sz, "HELLO");
z = &sz[0];
or

Code:
char sz[] = "HELLO";
char *z = &sz[0];
 
Top