Using the UART on the STM8S

If you have been reading my recent posts you will have noticed that I have had some problems setting up the UART on the STM8S. IN fact I spent several days getting this working. This post gives a couple of code snippets which I have used to get around this problem. You may need to refer to the data sheet on the processor for more detail on how this code works.

Setting Up the UART

This code makes a fundamental, namely that you have configured the chip and your circuit to run at 16 MHz. I did this by setting the chip to use the internal oscillator as it’s clock source and using a prescalar of 1. If you have this set up then the following method will allow you access to the UART running at 115200 baud, 8 data bits, 1 stop bit and no parity.

Firstly, we need to make sure we are using the right include files:

#include "stm8s.h"
#include "stm8s_uart1.h"

Next we need to set up the port:

void SetupSerialPort()
    //  Clear the Idle Line Detected bit in the status register by a read
    //  to the UART1_SR register followed by a Read to the UART1_DR register.
    (void) UART1->SR;
    (void) UART1->DR;

    //  Setup the port.
    UART1->CR1 = (u8) UART1_WORDLENGTH_8D | (u8) UART1_PARITY_NO;   // Word length = 8, no parity.
    UART1->CR3 = (u8) UART1_STOPBITS_1;                             // 1 stop bit.

    UART1->BRR1 &= (uint8_t) (~UART1_BRR1_DIVM);
    UART1->BRR2 &= (uint8_t) (~UART1_BRR2_DIVM);
    UART1->BRR2 &= (uint8_t) (~UART1_BRR2_DIVF);
    //  Set the clock prescaler for 11520 baud.  This assumes a 16 MHz clock speed.
    UART1->BRR2 = 0x0b;
    UART1->BRR1 = 0x08;
    //  Disable the Transmitter and Receiver before seting the LBCL, CPOL and CPHA bits
    UART1->CR2 &= (u8) ~(UART1_CR2_TEN | UART1_CR2_REN);
    //  Clear the Clock Polarity, lock Phase, Last Bit Clock pulse
    UART1->CR3 &= (u8)~(UART1_CR3_CPOL | UART1_CR3_CPHA | UART1_CR3_LBCL);
    //  Set the Clock Polarity, lock Phase, Last Bit Clock pulse
                       (u8) (UART1_CR3_CPOL | UART1_CR3_CPHA | UART1_CR3_LBCL));
    //  Set the Tx and Rx state
    UART1->CR2 |= (u8) ((u8) UART1_CR2_TEN | (u8) UART1_CR2_REN);
    UART1->CR3 &= (u8) (~UART1_CR3_CKEN);

Having set up the UART we need a method to write data to the port. The following method will send simple text strings to the UART:

//  Send a message to the debug port (UART1).
void Printf(char *message)
    char *ch = message;
    while (*ch)
        UART1->DR = (u8) (*ch);
        while ((UART1->SR & (u8) UART1_FLAG_TXE) == RESET);

Using the UART

The use of this code can be illustrated by the following program:

void main()
    Printf("Hello from my program");

Tags: , ,

Friday, June 8th, 2012 at 12:26 pm • Electronics, Software Development, STM8RSS 2.0 feed Both comments and pings are currently closed.

2 Responses to “Using the UART on the STM8S”

  1. Mike Hole says:


    I have added the code you have above but it does not compile. The error(s) I keep getting from the compiler is the following:

    Error[Pe020]: identifier “UART1” is undefined C:Embedded Devstm8s-discovery_devLibrariesSTM8S_StdPeriph_Driversrcstm8s_uart1.c 709

    Any ideas what I have missed?



    • Mark says:

      Do you have a preprocessor macro defined to tell the system which chip you are using? If you look at this post:

      Setting up a new project for STM8S

      You will see that you can set up the environment variable as part of the project options. I think the Discovery board you are using is the 105 so you will need to define something like STM8S105.