stm8s development (Part 3) UART usage: serial communication

Publisher:幸福的人生Latest update time:2020-02-24 Source: eefocusKeywords:stm8s  UART Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Serial communication is one of the most basic and important functions of microcontroller learning. Serial communication can be used indirectly as a debugging interface to achieve communication between the microcontroller and the computer. Of course, it can communicate with some modules (such as Bluetooth, WiFi), and can also be used as a tool for communication with other microcontrollers.


Main features of STM8S's universal asynchronous receiver and transmitter (UART):


● Full-duplex, asynchronous communication


● Programmable data word length (8 or 9 bits) 


● Configurable stop bits - supports 1 or 2 stop bits


Generally, when we use serial communication, we are mainly concerned with several parameters: baud rate, stop bit, and parity bit.


The second is how to send and receive data. Here we introduce the use of blocking method to send data and interrupt method to receive data.


The following code is to initialize the serial port, the parameters are: 115200 1 stop bit no parity bit


void Init_UART1(void)

{     

  CLK_PCKENR1 |= 0X04; //Enable USART1 clock 

  

  UART1_CR1=0x00;

  UART1_CR2=0x00;

  UART1_CR3=0x00;

  // BRR2 must be written first

  // For example, when the baud rate is 115200, the division factor = 16000000/115200 = 139

  // The corresponding hexadecimal number is 008B, BBR1=08, BBR2=0B

  

  UART1_BRR2=0x0B;

  UART1_BRR1=0x08;

  

  UART1_CR2=0x2c; // Allow receiving, sending, and enable receiving interrupt

}


The baud rate setting needs to be calculated. The default main frequency of STM8S is 16M, and the number written to the register is 139. Note that BRR2 needs to be written first and then BRR1, and the format is special:

BRR1 should be sandwiched between BRR2! (You need to understand the need to modify the baud rate)


The programming manual describes it as follows:

The blocking send function (sending one byte) is as follows:


void SendChar(u8 dat)

{

  while((UART1_SR & 0x80)==0x00);

  UART1_DR = dat;

}

Just load the data into the UART1_DR register and it will be OK.



The terminal receives data function as follows:


//Send the received data again

#pragma vector = UART1_R_OR_vector // 0x19

__interrupt void UART1_R_OR_IRQHandler(void)

{

  u8 res;

  res = UART1_DR;

  

  return;

}


To use interrupts, you need to declare the interrupt function entry and specify the interrupt table number (the serial port 1 receiving interrupt vector is 0x19):

#pragma vector = UART1_R_OR_vector // 0x19

__interrupt void UART1_R_OR_IRQHandler(void)



As soon as the serial port receives a byte of data, it will immediately enter this function and store the data in the UART1_DR register.

Note: To use the interrupt function, you need to add an interrupt enable statement in the main function:

_RIM;

Keywords:stm8s  UART Reference address:stm8s development (Part 3) UART usage: serial communication

Previous article:Usage of UART in stm8s (four types of UART interrupts)
Next article:STM8L151 series uses timer timing, global ticker jiffes

Latest Microcontroller Articles
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号