STM8L101F3P6 serial port query send

Publisher:CrystalBreezeLatest update time:2020-03-28 Source: eefocusKeywords:STM8L101F3P6 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Let me say a few words first. I have never summarized the serial port application of STM8. I will summarize it recently. I developed it with the STM8 application library. My understanding of the basic serial port application is still inadequate. I thought that the serial port of STM8 would also have several methods such as query sending, interrupt sending, query receiving, and interrupt receiving~! But in fact, I still don’t understand the interrupt sending mode very well, and it is still different from the serial port work of 51 microcontroller.


STM8 provides sending interrupt and sending completion interrupt. My own understanding is that it enters interrupt when sending and interrupts when sending a byte. This is different from the serial port interrupt I understand in my brain. I will continue to figure it out later.


Generally speaking, the serial port of STM8 is mostly sent by query and received by interrupt. Before using the serial port, you should pay attention to the following points:


1. Configure the GPIO pin status of TX and RX


2. Initialize the serial port. Here you need to turn on the clock of the serial port device, configure the serial port working mode, and turn off the USART_IT_TXE and USART_IT_TC interrupts, and turn on the USART_IT_RXNE interrupt. Then you can use the serial port normally.


3. To send a byte, call the library function USART_SendData8(), but remember to query the flag bit after calling it. Write USART_SendByte() as follows


void USART_SendByte(uint8_t data)

{

USART_SendData8((unsigned char)data);

  /* Loop until the end of transmission */

  while (USART_GetFlagStatus(USART_FLAG_TXE) == RESET);

}


Pay attention to querying the USART_FLAG_TXE flag. When this flag is 0, wait until it is 1, the transmission is completed, and the function ends. The USART_FLAG_TXE flag is the "transmit data register empty flag".

-------------------------------------------------------------

The above is what I want to say. This log posts the program to query and send a byte, as follows:


/******************************Copyright (c)***********************************/

/* */

/* Lao Li's Electronic Work */

/* */

/*------------------------------File Info-------------------------------------*/

/* File name: main.c */

/* Last modified Date:  2014-06-19 */

/* Last Version: 1.0 */

/* Descriptions: STM8L103F3P6, internal clock, 16MHz, serial port query send */

/* */

/* Hardware connections: */

/* TX----PC3 */

/* RX----PC2 */

/* */

/*----------------------------------------------------------------------------*/

/* Created by: Li Xiang */

/* Created date: 2014-06-19 */

/* Version: 1.0 */

/* Descriptions: None */

/* */

/******************************************************************************/

 

/* Includes ------------------------------------------------------------------*/

#include "stm8l10x.h"

#include "stm8l10x_usart.h"

/* Private typedef -----------------------------------------------------------*/

/* Private define ------------------------------------------------------------*/

#define POWER_BD GPIO_Pin_0

#define POWER_BT GPIO_Pin_1

#define MSEL GPIO_Pin_2

#define NRESET GPIO_Pin_3

#define BD_NRESET GPIO_Pin_4

#define RESETB GPIO_Pin_5

#define SCL2 GPIO_Pin_6

#define SDA2 GPIO_Pin_7

 

#define SDA GPIO_Pin_0

#define SCL GPIO_Pin_1

#define SCREEN_CTRL GPIO_Pin_4

 

#define POWER_WIFI GPIO_Pin_0

/* Private macro -------------------------------------------------------------*/

/* Private variables ---------------------------------------------------------*/

/* Private function prototypes -----------------------------------------------*/

static void BoardInit(void);

static void CLK_Init(void);

static void GPIO_Init_my(void);

static void USART_Config(void);

void USART_SendByte(uint8_t data);

void USART_SendString(uint8_t* Data,uint16_t len);

void Delay_ms(uint32_t nCount);

/* Private functions ---------------------------------------------------------*/

 

/******************************************************************************/

/* Function name: main */

/* Descriptions: Main function */

/* input parameters: None */

/* output parameters:   无 */

/* Returned value: None */

/******************************************************************************/

void main(void)

{

BoardInit();

 

while (1){

USART_SendString("Hello world!rn",14);

Delay_ms(1000);

USART_SendByte('H');Delay_ms(200);

USART_SendByte('e');Delay_ms(200);

USART_SendByte('l');Delay_ms(200);

USART_SendByte('l');Delay_ms(200);

USART_SendByte('o');Delay_ms(200);

USART_SendByte(' ');Delay_ms(200);

USART_SendByte('w');Delay_ms(200);

USART_SendByte('o');Delay_ms(200);

USART_SendByte('r');Delay_ms(200);

USART_SendByte('l');Delay_ms(200);

USART_SendByte('d');Delay_ms(200);

USART_SendByte('!');Delay_ms(200);

USART_SendString("rn",2);Delay_ms(200);

Delay_ms(1000);

}

}

 

/******************************************************************************/

/* Function name: BoardInit */

/* Descriptions: Main function */

/* input parameters: None */

/* output parameters:   无 */

/* Returned value: None */

/******************************************************************************/

static void BoardInit(void)

{

CLK_Init();

GPIO_Init_my();

USART_Config();

}

 

/******************************************************************************/

/* Function name: CLK_Init */

/* Descriptions: Clock initialization function */

/* input parameters: None */

/* output parameters:   无 */

/* Returned value: None */

/******************************************************************************/

static void CLK_Init(void)

{    

CLK_DeInit();

  CLK_MasterPrescalerConfig(CLK_MasterPrescaler_HSIDiv1);

}

 

/******************************************************************************/

/* Function name: GPIO_Init_my */

/* Descriptions: IO initialization function */

/* input parameters: None */

/* output parameters:   无 */

/* Returned value: None */

/******************************************************************************/

static void GPIO_Init_my(void) 

{

GPIO_Init(GPIOA,GPIO_Pin_2|GPIO_Pin_3,GPIO_Mode_Out_PP_Low_Slow); //悬空未用

GPIO_Init(GPIOB,POWER_BD,GPIO_Mode_Out_PP_Low_Slow); //Default power off

GPIO_Init(GPIOB,POWER_BT,GPIO_Mode_Out_PP_Low_Slow); // Cancel unused

GPIO_Init(GPIOB,MSEL,GPIO_Mode_Out_PP_Low_Slow); //Cancel unused, Wifi mode selection

GPIO_Init(GPIOB,NRESET,GPIO_Mode_Out_PP_Low_Slow); //Cancel unused, reset Wifi

GPIO_Init(GPIOB,BD_NRESET,GPIO_Mode_Out_PP_Low_Slow); //Beidou reset signal, default reset state

GPIO_Init(GPIOB,RESETB,GPIO_Mode_Out_PP_Low_Slow); //Cancel unused, reset Bluetooth

GPIO_Init(GPIOB,SDA2|SCL2,GPIO_Mode_Out_OD_HiZ_Slow); //Battery power usage

GPIO_Init(GPIOC,SDA|SCL,GPIO_Mode_Out_OD_HiZ_Slow); //Temperature sensor

GPIO_Init(GPIOC,GPIO_Pin_2,GPIO_Mode_In_PU_No_IT); //Serial port receiving

GPIO_Init(GPIOC,GPIO_Pin_3,GPIO_Mode_Out_PP_High_Slow); //串口发送

GPIO_Init(GPIOD,GPIO_Pin_All,GPIO_Mode_Out_PP_Low_Slow); //Cancel unused, Wifi power supply

}

 

/******************************************************************************/

/* Function name: USART_Config */

/* Descriptions: Serial port initialization function */

/* input parameters: None */

/* output parameters:   无 */

/* Returned value: None */

/******************************************************************************/

static void USART_Config(void)

{

CLK_PeripheralClockConfig(CLK_Peripheral_USART, ENABLE);

USART_DeInit();

USART_Init((uint32_t)9600, USART_WordLength_8D, USART_StopBits_1,

USART_Parity_No, (USART_Mode_TypeDef)(USART_Mode_Rx | USART_Mode_Tx));

USART_Cmd(ENABLE);

}

 

/******************************************************************************/

/* Function name: UART1_SendByte */

/* Descriptions: Send single byte */

/* input parameters: data: data to be sent */

/* output parameters:   无 */

/* Returned value: None */

/******************************************************************************/

void USART_SendByte(uint8_t data)

{

    USART_SendData8((unsigned char)data);

  /* Loop until the end of transmission */

  while (USART_GetFlagStatus(USART_FLAG_TXE) == RESET);

}

 

/******************************************************************************/

/* Function name: UART1_SendString */

/* Descriptions: Send string */

/* input parameters: None */

/* output parameters:   无 */

/* Returned value: None */

/******************************************************************************/

void USART_SendString(uint8_t* Data,uint16_t len)

{

  uint16_t i=0;

  for(;i    USART_SendByte(Data[i]);

  

}

/******************************************************************************/

/* Function name: Delay_ms */

/* Descriptions: Delay function */

/* input parameters: delay time */

/* output parameters:   无 */

/* Returned value: None */

/******************************************************************************/

void Delay_ms(uint32_t nCount)

{

uint16_t i=0,j=0;;

for(i=0;i for(j=0;j<1100;j++){

;

}

}

}

 

#ifdef  USE_FULL_ASSERT

 

/**

  * @brief  Reports the name of the source file and the source line number

  *   where the assert_param error has occurred.

  * @param file: pointer to the source file name

  * @param line: assert_param error line source number

[1] [2]
Keywords:STM8L101F3P6 Reference address:STM8L101F3P6 serial port query send

Previous article:STM8 MCU STVD environment to view the instruction running time
Next article:STVD reported the problem of can't open file crtsi0.sm8

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号