Skip to main content
Abhishek Kulkarni
Associate II
June 19, 2017
Question

USART with stm8l

  • June 19, 2017
  • 8 replies
  • 3671 views
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)

    This topic has been closed for replies.

    8 replies

    Szymon PANECKI
    Senior III
    June 21, 2017
    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

    Abhishek Kulkarni
    Associate II
    June 21, 2017
    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