STM8 serial communication UART driver

Article directory

Software used: IAR FOR STM8

Programming method: firmware library

Hardware support: STM8S105K4T6 [minimum system] board

Note: In the IAR project configuration: in General Options->Library [Configuration-] >Library select Full; in General Options->Library Options->Printf formatter select Large, Math functions select Default; In addition, the serial port cannot print decimals, need further Data processing

This is the code I referenced when I was designing the course, and the record is as follows:

1. [uart].h

#ifndef __UART_H
#define __UART_H

#include "stm8s.h"
#include "stdio.h"

#ifdef _RAISONANCE_
#define PUTCHAR_PROTOTYPE int putchar (char c)
#define GETCHAR_PROTOTYPE int getchar (void)
#elif defined (_COSMIC_)
#define PUTCHAR_PROTOTYPE char putchar (char c)
#define GETCHAR_PROTOTYPE char getchar (void)
#else /* _IAR_ */
#define PUTCHAR_PROTOTYPE int putchar (int c)
#define GETCHAR_PROTOTYPE int getchar (void)
#endif /* _RAISONANCE_ */

void UART2_Config(void);
void UART2_SendByte(u8 data);
void UART2_SendStr(const unsigned char *p_str);
void Delay(uint32_t nCount);

#endif /* __UART_H */

2. uart.c

#include "UART.h"

void UART2_Config(void)
{
    CLK_PeripheralClockConfig(CLK_PERIPHERAL_UART2, ENABLE);
    UART2_DeInit(); 

    /*
     * Configure UART2 as:
     * baud rate = 115200
     * data bits = 8
     * 1 stop bit
     * no check digit
     * Enable receive and transmit
     * Enable receive interrupt
     */

    UART2_Init((u32)115200, UART2_WORDLENGTH_8D, UART2_STOPBITS_1, UART2_PARITY_NO , UART2_SYNCMODE_CLOCK_DISABLE , UART2_MODE_TXRX_ENABLE);
    UART2_ITConfig(UART2_IT_RXNE_OR, ENABLE);
    UART2_Cmd(ENABLE);
}

void Delay(uint32_t nCount)
{
  /* Decrement nCount value */
  while (nCount != 0)
  {   
    nCount--;
  }
}

void UART2_SendByte(u8 data)
{
    UART2_SendData8((unsigned char)data);

    /* Wait for the transfer to end*/ 
    while (UART2_GetFlagStatus(UART2_FLAG_TXE) == RESET);
}

void UART2_SendStr(const unsigned char *p_str)
{
//  u8 i = 0;
  while(*p_str != '
#include "UART.h"
void UART2_Config(void)
{
CLK_PeripheralClockConfig(CLK_PERIPHERAL_UART2, ENABLE);
UART2_DeInit(); 
/*
* Configure UART2 as:
* baud rate = 115200
* data bits = 8
* 1 stop bit
* no check digit
* Enable receive and transmit
* Enable receive interrupt
*/
UART2_Init((u32)115200, UART2_WORDLENGTH_8D, UART2_STOPBITS_1, UART2_PARITY_NO , UART2_SYNCMODE_CLOCK_DISABLE , UART2_MODE_TXRX_ENABLE);
UART2_ITConfig(UART2_IT_RXNE_OR, ENABLE);
UART2_Cmd(ENABLE);
}
void Delay(uint32_t nCount)
{
/* Decrement nCount value */
while (nCount != 0)
{   
nCount--;
}
}
void UART2_SendByte(u8 data)
{
UART2_SendData8((unsigned char)data);
/* Wait for the transfer to end*/ 
while (UART2_GetFlagStatus(UART2_FLAG_TXE) == RESET);
}
void UART2_SendStr(const unsigned char *p_str)
{
//  u8 i = 0;
while(*p_str != '\0')
{
UART2_SendByte(*p_str);
//   Delay(1000);
p_str++;
}
}
/**********************************printf*********************************************/
/**
* @brief Retargets the C library printf function to the UART.
* @param c Character to send
* @retval char Character sent
*/
PUTCHAR_PROTOTYPE
{
/* Write a character to the UART2 */
UART2_SendData8(c);
/* Loop until the end of transmission */
while (UART2_GetFlagStatus(UART2_FLAG_TXE) == RESET);
return (c);
}
/**
* @brief Retargets the C library scanf function to the USART.
* @param None
* @retval char Character to Read
*/
GETCHAR_PROTOTYPE
{
#ifdef _COSMIC_
char c = 0;
#else
int c = 0;
#endif
/* Loop until the Read data register flag is SET */
while (UART2_GetFlagStatus(UART2_FLAG_RXNE) == RESET);
c = UART2_ReceiveData8();
return (c);
}
') { UART2_SendByte(*p_str); // Delay(1000); p_str++; } } /**********************************printf*********************************************/ /** * @brief Retargets the C library printf function to the UART. * @param c Character to send * @retval char Character sent */ PUTCHAR_PROTOTYPE { /* Write a character to the UART2 */ UART2_SendData8(c); /* Loop until the end of transmission */ while (UART2_GetFlagStatus(UART2_FLAG_TXE) == RESET); return (c); } /** * @brief Retargets the C library scanf function to the USART. * @param None * @retval char Character to Read */ GETCHAR_PROTOTYPE { #ifdef _COSMIC_ char c = 0; #else int c = 0; #endif /* Loop until the Read data register flag is SET */ while (UART2_GetFlagStatus(UART2_FLAG_RXNE) == RESET); c = UART2_ReceiveData8(); return (c); }

Leave a Comment

Your email address will not be published. Required fields are marked *