STM8 four-line driver LCD1602

Article directory

Software used: IAR FOR STM8

Programming method: firmware library

Hardware support: STM8S105K4T6 [minimum system] board

This is the code I found out when I was doing the course design, and it is recorded as follows:

1. LCD1602.h

#ifndef __LCD1602_H
#define __LCD1602_H

/************LCD1602 four-line driver***********/

#include "stm8s.h"
#include "delay.h"

/***********Definition***********/
#define LINE0   0x80
#define LINE1   0xC0

#define Rx_PORT (GPIOB)
#define RS_PIN (GPIO_PIN_4) /* PB4 */
#define EN_PIN (GPIO_PIN_5) /* PB5 */
#define Rx_FOUR_PINS (RS_PIN | EN_PIN)
#define Rx_ALL_PINS (RS_PIN | RW_PIN | EN_PIN) /* NOT USE */

#define Dx_PORT (GPIOC)
#define D0_PIN (GPIO_PIN_0) /* PC0 */ /* NOT USE */
#define D1_PIN (GPIO_PIN_1) /* PC1 */ /* NOT USE */
#define D2_PIN (GPIO_PIN_2) /* PC2 */ /* NOT USE */
#define D3_PIN (GPIO_PIN_3) /* PC3 */ /* NOT USE */
#define D4_PIN (GPIO_PIN_4) /* PC4 */
#define D5_PIN (GPIO_PIN_5) /* PC5 */
#define D6_PIN (GPIO_PIN_6) /* PC6 */
#define D7_PIN (GPIO_PIN_7) /* PC7 */
#define Dx_FOUR_PINS (D4_PIN | D5_PIN | D6_PIN | D7_PIN)
#define Dx_ALL_PINS (D0_PIN | D1_PIN | D2_PIN | D3_PIN | D4_PIN | \
                     D5_PIN | D6_PIN | D7_PIN) /* NOT USE */

#define RS_LOW  GPIO_WriteLow (Rx_PORT, RS_PIN)
#define RS_HIGH GPIO_WriteHigh(Rx_PORT, RS_PIN)
#define EN_LOW  GPIO_WriteLow (Rx_PORT, EN_PIN)
#define EN_HIGH GPIO_WriteHigh(Rx_PORT, EN_PIN)
#define Dx_CLR  GPIO_Write(Dx_PORT, 0x00)

/***********Function***********/
static void LCD1602_Cmd(uint8_t cmd);
static void LCD1602_Data(uint8_t data);
void LCD1602_Init(void);
static void LCD1602_SetCursor(uint8_t x, uint8_t y);
void LCD1602_PrintStr(uint8_t x, uint8_t y, uint8_t *str);

#endif /* __LCD1602_H */

2. LCD1602.c

#include "lcd1602.h"

/******Send command******/ 
static  void  LCD1602_Cmd ( uint8_t cmd)
 { 
   //Delay_us(30);
  EN_LOW;
  RS_LOW; /* RS=0, write command */

  GPIO_Write(Dx_PORT, cmd);      /* Receive high nibble commands*/
  EN_HIGH; 
  Delay_ms(15);
  EN_LOW;

  GPIO_Write(Dx_PORT, cmd<< 4 ); /* Receive low-order four-bit commands*/
  EN_HIGH;
  Delay_ms(15);
  EN_LOW;
}

/******Send data******/ 
static  void  LCD1602_Data ( uint8_t data)
 {  
   //Delay_us(30);
  EN_LOW;
  RS_HIGH; /* RS=1, write data */

  GPIO_Write(Dx_PORT, data);      /* Receive high four-bit data*/
  EN_HIGH;
  Delay_ms(15);
  EN_LOW;

  GPIO_Write(Dx_PORT, data<< 4 ); /* Receive low 4-bit data*/
  EN_HIGH;
  Delay_ms(15);
  EN_LOW;
}

/******LCD initialization******/ 
void  LCD1602_Init ( void )
 {
  GPIO_Init(Dx_PORT, (GPIO_Pin_TypeDef)Dx_FOUR_PINS, GPIO_MODE_OUT_PP_HIGH_FAST);
  GPIO_Init(Rx_PORT, (GPIO_Pin_TypeDef)Rx_FOUR_PINS, GPIO_MODE_OUT_PP_HIGH_FAST);

  /*LCD1602_Cmd(0x20); 
  LCD1602_Cmd(0x32); 
  Delay_ms(5);
  LCD1602_Cmd(0x28); // Finally send 0x28, enter 4-wire mode, set 16*2 display, 115*7 dot matrix, 4-bit data interface
  Delay_ms(5);
  LCD1602_Cmd(0x28);
  Delay_ms(5);
  EN_HIGH;
  LCD1602_Cmd(0x28);
  EN_LOW;*/

  LCD1602_Cmd(0x20);
  Delay_ms(20);
  LCD1602_Cmd(0x28);
  Delay_ms(20);
  LCD1602_Cmd( 0x01 ); /* Clear the screen*/ 
  LCD1602_Cmd( 0x06 ); /* Move the cursor to the right when writing data, and the display does not move when writing new data*/ 
  LCD1602_Cmd( 0x0C ); /* Open the display, there is a cursor, the cursor blinks */ 
  //LCD1602_Cmd(0x80);
}

/* 0x80 and 0xC0 are the start addresses of the two lines, respectively, add the serial number of the character to the location of the line
   Sending the address as a command to LCD1602 will make the next character output at the specified position*/ 
/******Send address******/ 
static  void  LCD1602_SetCursor ( uint8_t x, uint8_t y)  // x:coordinate of the column y: row coordinate
 {
  LCD1602_Cmd(x + (y ? LINE1:LINE0));
}

/******Send data continuously******/ 
void  LCD1602_PrintStr ( uint8_t x, uint8_t y, uint8_t *str)
 {
  LCD1602_SetCursor(x, y);
  while(*str != '
#include "lcd1602.h"
/******Send command******/ 
static  void  LCD1602_Cmd ( uint8_t cmd)
{ 
//Delay_us(30);
EN_LOW;
RS_LOW; /* RS=0, write command */
GPIO_Write(Dx_PORT, cmd);      /* Receive high nibble commands*/
EN_HIGH; 
Delay_ms(15);
EN_LOW;
GPIO_Write(Dx_PORT, cmd<< 4 ); /* Receive low-order four-bit commands*/
EN_HIGH;
Delay_ms(15);
EN_LOW;
}
/******Send data******/ 
static  void  LCD1602_Data ( uint8_t data)
{  
//Delay_us(30);
EN_LOW;
RS_HIGH; /* RS=1, write data */
GPIO_Write(Dx_PORT, data);      /* Receive high four-bit data*/
EN_HIGH;
Delay_ms(15);
EN_LOW;
GPIO_Write(Dx_PORT, data<< 4 ); /* Receive low 4-bit data*/
EN_HIGH;
Delay_ms(15);
EN_LOW;
}
/******LCD initialization******/ 
void  LCD1602_Init ( void )
{
GPIO_Init(Dx_PORT, (GPIO_Pin_TypeDef)Dx_FOUR_PINS, GPIO_MODE_OUT_PP_HIGH_FAST);
GPIO_Init(Rx_PORT, (GPIO_Pin_TypeDef)Rx_FOUR_PINS, GPIO_MODE_OUT_PP_HIGH_FAST);
/*LCD1602_Cmd(0x20); 
LCD1602_Cmd(0x32); 
Delay_ms(5);
LCD1602_Cmd(0x28); // Finally send 0x28, enter 4-wire mode, set 16*2 display, 115*7 dot matrix, 4-bit data interface
Delay_ms(5);
LCD1602_Cmd(0x28);
Delay_ms(5);
EN_HIGH;
LCD1602_Cmd(0x28);
EN_LOW;*/
LCD1602_Cmd(0x20);
Delay_ms(20);
LCD1602_Cmd(0x28);
Delay_ms(20);
LCD1602_Cmd( 0x01 ); /* Clear the screen*/ 
LCD1602_Cmd( 0x06 ); /* Move the cursor to the right when writing data, and the display does not move when writing new data*/ 
LCD1602_Cmd( 0x0C ); /* Open the display, there is a cursor, the cursor blinks */ 
//LCD1602_Cmd(0x80);
}
/* 0x80 and 0xC0 are the start addresses of the two lines, respectively, add the serial number of the character to the location of the line
Sending the address as a command to LCD1602 will make the next character output at the specified position*/ 
/******Send address******/ 
static  void  LCD1602_SetCursor ( uint8_t x, uint8_t y)  // x:coordinate of the column y: row coordinate
{
LCD1602_Cmd(x + (y ? LINE1:LINE0));
}
/******Send data continuously******/ 
void  LCD1602_PrintStr ( uint8_t x, uint8_t y, uint8_t *str)
{
LCD1602_SetCursor(x, y);
while(*str != '\0')
{
LCD1602_Data(*str++);
}
}
') { LCD1602_Data(*str++); } }

3. delay.h

#ifndef __DELAY_H
#define __DELAY_H 

#include "stm8s.h"

void Delay_ms(uint16_t nCount);
void Delay_us(uint16_t nCount);

#endif /* __DELAY_H */

4. delay.c

#include "delay.h"

//ms level delay 
void  Delay_ms ( uint16_t nCount)
 {
   while (nCount--)
  {
    Delay_us(1000);
  }
}

//us level delay 
void  Delay_us ( uint16_t nCount)
 {
  nCount *= 2;
  while(--nCount);
}

5. main.c

#include "stm8s.h"
#include "lcd1602.h"

void SystemInit_CLK(void);

void main(void)
{
  SystemInit_CLK();
  LCD1602_Heat();
  LCD1602_PrintStr( 1 , 0 , "Hello World!" ); // row 0, column 1

  while (1)
  {
    //LCD1602_PrintStr(0, 0, "Hello!");
    //Delay_us(0xFFF);
  }
}

void SystemInit_CLK(void)
{
  CLK_DeInit();
  CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1); // 16MHz
  CLK_SYSCLKConfig(CLK_PRESCALER_HSIDIV1);
  CLK_HSICmd(ENABLE);
}

#ifdef USE_FULL_ASSERT

void assert_failed(uint8_t* file, uint32_t line)
{ 
  while (1)
  {
  }
}
#endif

/****END OF FILE****/

Leave a Comment

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