Docs

Micro-controllers, wireless transmission and database

Head

UART with STM8S103F3P6 using ST Visual develop IDE

Prerequisites

This project assumes you have already installed ST Visual develop IDE and Cosmic Compiler. You need to have previously done a basic blink sketch with STM8S103F3P6 using STVD. I have made a complete video from installing to LED blink program. You can watch it by clicking this link. https://www.youtube.com/watch?v=iFiBfx9vCqo

Wiring Diagram

Diagram

Libraries

FROM STM8S/A Standard peripheral library

from https://www.st.com/en/embedded-software/stsw-stm8069.html

stm8s.h

stm8s_clk.h

stm8s_gpio.h

stm8s_uart1.h

stm8s_clk.c

stm8s_gpio.c

stm8s_uart1.c

stm8s_conf.h

main.c code

#include "stm8s.h"

void delay_ms (int ms) // Milli second delay
{
  int a =0 ;
  int b=0;
  for (a=0; a<=ms; a++)
  {
    for (b=0; b<120; b++) 
      _asm("nop"); // Perform no operation 
  }
}

void UART1_Print_String(char *a)
{
    int i;
  for(i=0;a[i]!='\0';i++)
    {
      UART1_SendData8(a[i]);
        while( UART1_GetFlagStatus(UART1_FLAG_TXE) == RESET);
    }
}

// Can send from -99999 to 99999
void UART1_Print_Integer(signed long value)
{
    char ch[7] = {0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00};
  if(value < 0) // If negative number place "-" sign in front
  {
    ch[0] = 0x2D; 
    value = -value;
  }
  else
  {
    ch[0] = 0x20; // If positive number place space sign in front
  }
  if(value > 9999)
  {
    ch[1] = ((value / 10000) + 0x30); 
    ch[2] = (((value % 10000) / 1000) + 0x30); 
    ch[3] = (((value % 1000) / 100) + 0x30); 
    ch[4] = (((value % 100) / 10) + 0x30); 
    ch[5] = ((value % 10) + 0x30);
  }
  else if((value > 999) && (value <= 9999))
  {
    ch[1] = ((value / 1000) + 0x30); 
    ch[2] = (((value % 1000) / 100) + 0x30); 
    ch[3] = (((value % 100) / 10) + 0x30); 
    ch[4] = ((value % 10) + 0x30);
    ch[5] = 0x20;
  }
  else if((value > 99) && (value <= 999))
  {
    ch[1] = ((value / 100) + 0x30); 
    ch[2] = (((value % 100) / 10) + 0x30); 
    ch[3] = ((value % 10) + 0x30);
    ch[4] = 0x20;
    ch[5] = 0x20;
  }
  else if((value > 9) && (value <= 99))
  {
    ch[1] = ((value / 10) + 0x30); 
    ch[2] = ((value % 10) + 0x30);
    ch[3] = 0x20;
    ch[4] = 0x20;
    ch[5] = 0x20;
  }
  else
  {
    ch[1] = ((value % 10) + 0x30);
    ch[2] = 0x20;
    ch[3] = 0x20;
    ch[4] = 0x20;
    ch[5] = 0x20;
  }
  UART1_Print_String(ch);
}

main()
{
    unsigned char inChar;
    signed long countUART = -250;
    GPIO_Init(GPIOB, GPIO_PIN_5, GPIO_MODE_OUT_PP_LOW_SLOW);
  UART1_DeInit();
    UART1_Init(9600,UART1_WORDLENGTH_8D,UART1_STOPBITS_1,UART1_PARITY_NO,
               UART1_SYNCMODE_CLOCK_DISABLE,UART1_MODE_TXRX_ENABLE);
    UART1_Cmd(ENABLE);
    while (1)
    {
        if (UART1_GetFlagStatus(UART1_FLAG_RXNE) == SET) {
            inChar = UART1_ReceiveData8();
            if (inChar == 'N') GPIO_WriteHigh(GPIOB, GPIO_PIN_5);
            if (inChar == 'Y') GPIO_WriteLow(GPIOB, GPIO_PIN_5);
        }
        UART1_Print_String("The value is : ");
        UART1_Print_Integer(countUART);
        countUART++;
        UART1_Print_String("\n");
        delay_ms(300);
    }
}