cancel
Showing results for 
Search instead for 
Did you mean: 

USART with stm8l

Abhishek Kulkarni
Associate II
Posted on June 19, 2017 at 20:12

I am trying to interface USART1 with the stm8l. However I am getting a problem that whatever the value I assign to the USART1_DR , it always takes the value of 0xff no matte what. my code is as follows:

(I am working with a 16 MHz internal clock and have already done the initialisation)

USART1_CR1 = 0x00;

USART1_CR1 = 0x0C;

USART1_CR3 = 0x0F;

USART1_BRR2 = 0x0A;

USART1_BRR1 = 0x08;

USART1_DR = 'HELLO';  (still shows the value 0xff in the DR register)

8 REPLIES 8
Szymon PANECKI
Senior III
Posted on June 21, 2017 at 10:29

Hello Abhishek,

I didn't make a full review of your code, but at least one line looks wrong to me. USART1_DR register has only one byte, so it is not possible to write to it five bytes in one time, like you did. In your case if you try to write 'HELLO' to this register, I would expect you will send just the last byte ('O', so hexadecimal 0x4F).

Proper way to send five bytes is to send them byte by byte and you need to make sure, that you start sending next byte after previous one is already sent, so for example like this:

USART1->DR = 'H';

while(!(USART1->SR & USART_SR_TC));

USART1->DR = 'E';

while(!(USART1->SR & USART_SR_TC));

USART1->DR = 'L';

while(!(USART1->SR & USART_SR_TC));

USART1->DR = 'L';

while(!(USART1->SR & USART_SR_TC));

USART1->DR = 'O';

while(!(USART1->SR & USART_SR_TC));

If you would like to evalaute complete application, you can use the minimum code for UART configuration, which I attach below + the code for data transmission. I checked it on my side and it is working fine. Pin PC3 is used here as a UART_TX line.

#include 'stm8l15x.h'

main()

{

  CLK->PCKENR1 = 0x20; //enable clock for USART1

  GPIOC->DDR = 0x08; //PC3 as OUT_PP for USART_TX

  GPIOC->CR1 = 0x08;

  GPIOC->CR2 = 0x08;

  USART1->BRR2 = 0x01; //UART configuration

  USART1->BRR1 = 0x01;

  USART1->CR2 = 0x0C;

  while (1)

  {

    USART1->DR = 'H';

    while(!(USART1->SR & USART_SR_TC));

    USART1->DR = 'E';

    while(!(USART1->SR & USART_SR_TC));

    USART1->DR = 'L';

    while(!(USART1->SR & USART_SR_TC));

    USART1->DR = 'L';

    while(!(USART1->SR & USART_SR_TC)); 

    USART1->DR = 'O';

    while(!(USART1->SR & USART_SR_TC));

  }

}

Best regards

Szymon

Posted on June 21, 2017 at 11:35

Hello Szymon,

I tried compiling your code but unfortunately the problem still persists

that the USART_DR register takes only 0xff value whatever the character.

I'm attaching a screenshot. Also I didn't understand the purpose of the

line while(!(USART1->SR & USART_SR_TC)) . As in what's the difference

between the condition I had written and yours.

Regards,

Abhishek

On Wed, Jun 21, 2017 at 1:59 PM, Szymon Panecki <

________________

Attachments :

ss.PNG : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006Hy0K&d=%2Fa%2F0X0000000b9y%2FUCiQQaYHE9l7HuGa8Yfn20QiRxgJpDk53bnJETA2Ul8&asPdf=false
Posted on June 21, 2017 at 11:47

What about the clock configuration(for setting the baud rate) for the above code ?

Posted on June 21, 2017 at 12:23

HelloAbhishek,

Regarding

while(!(USART1->SR & USART_SR_TC)) : this line checks if USART transfer is complete. It is recommended to use it, because with this condition you are sure, that previous byte was sent and you can write new data to register, so you will not face the situation that new data overwrites old one before it is sent by USART.

Regarding clock configuration in my application: as you can see I don't configure the clock domain, which means that MCU uses default settings: system clock is equal toHSI / 8 = 16 MHz / 8 = 2 MHz.

Please find attached a STVD project with my code. Could you plese try it? I tested it on STM8L Discovery kit (STM8L152C6T6).

Regards

Szymon

________________

Attachments :

stm8l_test.zip : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006HyK2&d=%2Fa%2F0X0000000b9x%2FO5SIcrs.041GvtMq9jwoV7y8uLXxE9G5kpECbHXzoV8&asPdf=false

STVD_screen.png : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006HybH&d=%2Fa%2F0X0000000b9w%2FFFOHfgr9lR7riDHK5yFtmdRE71BJe9ySrym4N9EbkaQ&asPdf=false
Posted on June 21, 2017 at 12:32

Okay that's the reason but I'm still getting the wrong value in the

USART_DR register

On Wed, Jun 21, 2017 at 3:55 PM, Szymon Panecki <

Posted on June 22, 2017 at 10:10

0690X00000607S1QAI.png

This is debugging of your own code. Why is it working for you and not for me?

Posted on June 22, 2017 at 15:10

You can't inspect the DR like that, it doesn't act like a memory cell. Unless you loop-back the output externally, the RECEIVE register is not going to reflect what you put in the TRANSMIT register.

Wait for the register to be empty, and send a 'U' character, do this in a loop, attach a scope probe to the TX pin and review the signalling, check bit timing, etc.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
Posted on June 23, 2017 at 06:57

Hello Clive,  I'm a bit of an amateur so couldn't really follow the last part? Could you please elaborate?