[STM8] Realization of serial communication (LED on and off control)

Scenario description:
I found an STM8 development board and planned to play with it. Realize LED on and off control

1. Development platform and environment

Chip model: STM8S03F
development board Picture:
insert image description here
Development environment:
use IAR software. Environment construction, please Baidu.
insert image description here

2. Baud rate configuration

official:

official
in:

  • f is the clock source frequency
    . The main clock is divided by CLK_CKD IVR. I use the HSI internal clock 16M here, and CLK_CKDIVR=00 does not divide the frequency, that is, f=16M.
  • UATRT_DVI serial port frequency division.
    That is, the value we want to calculate.
    Take 115200 as an example: UATRT_DVI = 16m/115200 = 138.89. The hex result is approximately: 0x008B
Calculate the value of a register

UATRT_DVI[15:0] consists of BRR2 and BRR1.
BRR1[7:0] corresponds to UATRT_DVI [11:4]
BRR2[3:0] corresponds to UATRT_DVI [3:0]
BRR2[7:4] corresponds to UATRT_DVI [15:12]
Therefore, the middle of 0x008B is for BRR1, and the two sides are for BRR1 BRR2. As shown below.
insert image description here

Note:
1. BRR1 = 0x00 means disabled, so the value given to BRR1 should be greater than 0.
2. First assign value to BRR1, then assign value to BRR2
3. Pay attention to the error. Errors can affect data validity.

3. Control register configuration

Register Function Table
insert image description here

  • TIEN: start sending interrupt enable (generally not used)
  • TCIEN: Transmit complete interrupt enable
  • RIEN: Receive Interrupt Enable
  • ILIEN:IDLE Interrupt Enable
  • TEN: send enable
  • REN: Receive Enable
  • RWU: Receive wakeup
  • SBK: Send break frame.

Turn on the send and receive functions, and enable the receive interrupt configuration as follows

    UART1_CR2=0x00;// 先清0
    
    UART1_CR2_TEN = 1;     //允许发送
    UART1_CR2_REN = 1;     //允许接收
    UART1_CR2_RIEN = 1;    //接收中断使能

The above code can also be written as:UART1_CR2=0x00;// 0010 1100

4. Key code

Clock initialization

The internal clock is used here, 16M

#define SYS_CLOCK 16
void CLOCK_Config(unsigned char SYS_CLK)
{
    
    
   //时钟配置为内部RC,16M
   CLK_CKDIVR &=0xe7;
  
   switch(SYS_CLK)
   {
    
    
      case 2: CLK_CKDIVR|=((1<<4)|(1<<3)); break;
      case 4: CLK_CKDIVR|=(1<<4); break;
      case 8: CLK_CKDIVR|=(1<<3); break;
   }
}
Serial port initialization:

baud rate 115200

void  UART1_init(void)
{
    
    
    UART1_CR1=0x00;
    UART1_CR2=0x00;
    UART1_CR3=0x00;
    UART1_BRR2 = 0x0B;     //  16m/9600 = 1666.67(0x0683有误差 ) 取中间和俩边   BRR2 = 0x03,BRR1=0x68
    UART1_BRR1 = 0x08;     //115200波特率
    UART1_CR2_TEN = 1;     //允许发送
    UART1_CR2_REN = 1;     //允许接收
    UART1_CR2_RIEN = 1;    //接收中断使能
}
Send a byte:
void  UART1_SendByte(unsigned char dat)
{
    
    
  	UART1_SR &= 0xBF; // 清除清除发送完成TC标志位
	UART1_DR = dat;
	while((UART1_SR & 0x40)==0x00); // SR状态寄存器,第6位为0表示完成 
	UART1_SR &= 0xBF; // 清除清除发送完成TC标志位
}

receive interrupt

To achieve receive 1, turn on LED1
to receive 2, turn on LED2 and
turn off LEDs.

#pragma   vector = UART1_R_RXNE_vector   //接受中断向量

//接受中断函数
__interrupt void UART1_RX_IRQHandler(void)
  {
    
    
    unsigned char rx_data;
    
    if(UART1_SR & 0x20) // 是否接收完成
    {
    
    
	     UART1_SR_RXNE = 0;// 清除接收标识
         rx_data = UART1_DR; // 读取数据
         
         // 用户处理
         if(rx_data == '1')
         {
    
    
           LED1_ON();
           LED2_OFF();
         }
         else if(rx_data == '2')
         {
    
    
           LED2_ON();
           LED1_OFF();
         }
         else
         {
    
    
           LED1_OFF();
           LED2_OFF();
         }
    }
        
  }
main function
void main(  )
{
    
    

  asm("sim");//全局中断
  CLOCK_Config(SYS_CLOCK);
  UART1_init();   
  timer_Init();
  LED_init();
 asm("rim");//开全局中断 
 
  while(1);     
   
}

Guess you like

Origin blog.csdn.net/qq_44078824/article/details/123689074