This post was last edited by Fu pj on 2019-10-30 16:02
This content is original by EEWORLD forum user Fu pj . If you want to reprint or use it for commercial purposes, you must obtain the author's consent and indicate the source.
I just started learning stm32, and then wanted to make a LCD1602 to display the temperature of DHT11, and then I got stuck on displaying characters. Through my own exploration, I finally wrote the code and put it here for people like me to have some reference. There is no problem after debugging.
The development board uses minisTM32 v3.41 from Zhengdian Atom.
You can ignore the commented out code because I am still debugging the dht11 program.
This program is written in IAR. If there is an error, please check if there is any problem with the configuration.
LCD1602.H header file code:
#ifndef __LCD1602_H_
#define __LCD1602_H_
#define RW_H GPIO_SetBits(GPIOA,GPIO_Pin_4)
#define RW_L GPIO_ResetBits(GPIOA,GPIO_Pin_4)
#define RS_H GPIO_SetBits(GPIOA,GPIO_Pin_5)
#define RS_L GPIO_ResetBits(GPIOA,GPIO_Pin_5)
#define EN_H GPIO_SetBits(GPIOA,GPIO_Pin_6)
#define EN_L GPIO_ResetBits(GPIOA,GPIO_Pin_6)
#define GPIO_WriteHigh(GPIOx,a) GPIOx->BSRR=(((uint8_t)(uint8_t)~(a))<<24)|(((uint32_t)(uint8_t)(a))<<8)
#define GPIO_WriteLow(GPIOx,a) GPIOx->BSRR=(((uint32_t)(uint8_t)~(a))<<16)|((uint32_t)(uint8_t)(a))
#define DATAOUT(x) GPIO_Write(GPIOB, x)
#include "stm32f10x.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_rcc.h"
//void lcd1602_gpio_ENinput_init();
void lcd1602_gpio_ENoutput_init();
//void lcd1602_gpio_RSinput_init();
void lcd1602_gpio_RSoutput_init();
//void lcd1602_gpio_RWinput_init();
void lcd1602_gpio_RWoutput_init();
void lcd1602_write_cmd(u16 cmd);
void lcd1602_write_data(u16 data);
void lcd1602_gpio_shujuoutput_init();
void lcd1602_init();
void lcd1602_char(unsigned char *word);
//void lcd1602_gpio_shujuinput_init();
#endif
LCD1602.C code:
#include "lcd1602.h"
#include "systick.h"
void lcd1602_gpio_RWoutput_init() //Set RW to output mode
{
GPIO_InitTypeDef GPIO_InitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE );//Initialize clock, enable
/*initialize gpio*/
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStruct);
}
/*void lcd1602_gpio_RWinput_init() //Set RW to input mode
{
GPIO_InitTypeDef GPIO_InitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE );//Initialize clock, enable
/*Initialize gpio*/
/* GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStruct);
}*/
void lcd1602_gpio_RSoutput_init() //Set RS to output mode
{
GPIO_InitTypeDef GPIO_InitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE );//Initialize clock, enable
/*initialize gpio*/
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStruct);
}
/*void lcd1602_gpio_RSinput_init() //Set RS to input mode
{
GPIO_InitTypeDef GPIO_InitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE );//Initialize clock, enable
/*Initialize gpio*/
/* GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStruct);
}*/
void lcd1602_gpio_ENoutput_init() //Set EN to output mode
{
GPIO_InitTypeDef GPIO_InitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE );//Initialize clock, enable
/*initialize gpio*/
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStruct);
}
/*void lcd1602_gpio_ENinput_init() //Set EN to input mode
{
GPIO_InitTypeDef GPIO_InitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE );//Initialize clock, enable
/*Initialize gpio*/
/* GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStruct);
}*/
void lcd1602_gpio_shujuoutput_init() //Set 8 data ports to output mode
{
GPIO_InitTypeDef GPIO_InitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE );//Initialize clock, enable
/*initialize gpio*/
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
//GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14
|
GPIO_Pin_15
;
/*void lcd1602_gpio_shujuinput_init() //Set 8 data ports to input mode
{
GPIO_InitTypeDef GPIO_InitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE );//Initialize clock, enable
/*Initialize gpio*/
/* GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStruct);
}*/
void lcd1602_write_cmd(u16 cmd) //写指令函数
{
RS_L;
RW_L;
GPIO_Write(GPIOB, cmd);
GPIO_WriteHigh(GPIOB,cmd);
EN_H;
DelayUs(5000);
EN_L;
}
void lcd1602_write_data(u16 data) //write data function
{
RS_H;
RW_L;
GPIO_WriteHigh(GPIOB,data);
EN_H;
DelayUs(5000);
EN_L;
}
void lcd1602_init() //1602配置初始化
{
lcd1602_write_cmd(0x34);
lcd1602_write_cmd(0x0c);
lcd1602_write_cmd(0x06);
lcd1602_write_cmd(0x01);
}
void lcd1602_char(unsigned char *word) //1602 write string function
{
while(*word)
{
lcd1602_write_data(*word);
word++;
}
}
All the delays in this program are delayed by tick timers.
Tick timer systick.h header file code:
#ifndef _SYSTICK_H_
#define _SYSTICK_H_
#include "stm32f10x.h"
void SysTick_CLKSourceConfig(u32 SysTick_CLKSource);
void systick_init();
void DelayUs(__IO uint32_t timer);
#endif
Tick timer systick.c code:
#include "systick.h"
__IO uint32_t TimerDelay; //Define a variable that can query the status repeatedly
void systick_init() //Configure SysTick clock timing 1ms
{
if( SysTick_Config(72)==1) //Configure Systick reload value
{
while(1);
}
}
void TimerDelay_Decrement() //Generate overflow function
{
if(TimerDelay!=0)
{
TimerDelay--;
}
}
void SysTick_Handler() //Systick interrupt vector function
{
TimerDelay_Decrement();
}
void DelayUs(__IO uint32_t timer) //delay setting function
{
TimerDelay=timer;
while(TimerDelay!=0);
}
Main program code:
#include "stm32f10x.h"
#include "lcd1602.h"
#include "systick.h"
#include "dht11.h"
//u8 temp_h_init ,temp_l_init ,damp_h_init ,damp_l_init ,check ,temp_h ,temp_l ,damp_h ,damp_l ,T_L ,T_H1 ,T_H2 ,D_L ,D_H1 ,D_H2;
void main()
{
SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);//设定时钟为SysTick,72M
systick_init();
lcd1602_gpio_RWoutput_init();
lcd1602_gpio_ENoutput_init();
lcd1602_gpio_RSoutput_init();
lcd1602_gpio_shujuoutput_init();
lcd1602_init();
while(1)
{
// dhtll_init();
// dht11_readbit();
lcd1602_write_cmd(0x80);
lcd1602_char("hello stm32");
//lcd1602_write_data(T_H1+'0');
//lcd1602_write_data(T_H2+'0');
}
}
You can ignore the commented out code.
If you have any questions, please leave a message.