Header file: #ifndef __12864lcd_h__ #define __12864LCD_H__ #include "stm32f4xx.h" #include "stm32f4xx_gpio.h" #include "sys.h" #i Nclude "Delay.h" #include <stdlib.h>/*** control port GPIO definition ***/#define LCD_RST gpio_pin_0 #define LCD_PSB Gpio_pin_1 #define LCD_EN gpio_pin_8 #define LCD_RW gpio_pin_9 #define Lcd_rs GPIO_PIN_10//define control Port 1 #define SET_LCD_RS gpio_setbits (gpiod,lcd_rs) #define SET_LCD_RW GPIO_SETB Its (GPIOD,LCD_RW) #define Set_lcd_en gpio_setbits (gpiod,lcd_en) #define SET_LCD_PSB gpio_setbits (GPIOD,LCD _PSB) #define Set_lcd_rst gpio_setbits (Gpiod,lcd_rst)//define control port clear 0 #define CLEAR_LCD_RS gpio_resetbits (gpiod
, lcd_rs) #define CLEAR_LCD_RW gpio_resetbits (GPIOD,LCD_RW) #define Clear_lcd_en gpio_resetbits (gpiod,lcd_en) #define CLEAR_LCD_PSB gpio_resetbits (GPIOD,LCD_PSB) #define Clear_lcd_rst gpio_resetbits (Gpiod,lcd_rst)/****** * * Data port Gpio Definition *********/#Define LCD_DB0 gpio_pin_0 #define LCD_DB1 gpio_pin_1 #define LCD_DB2 Gpio_pin _2 #define LCD_DB3 gpio_pin_3 #define LCD_DB4 gpio_pin_4 #define LCD_DB5 GPIO _pin_5 #define LCD_DB6 gpio_pin_6 #define LCD_DB7 gpio_pin_7 #define Lcd_control_port Gpiod #define LCD_DATA_PORT Gpioe//gpio clock #define Lcd_control_port_clock RCC_AHB1PERIPH _gpiod #define Lcd_data_port_clock Rcc_ahb1periph_gpioe #define Lcd_control_pin Lcd_rst | lcd_psb| Lcd_en | LCD_RW | Lcd_rs #define Lcd_data_pin Lcd_db0 | lcd_db1| LCD_DB2 | LCD_DB3 | LCD_DB4 | LCD_DB5 | LCD_DB6 |
lcd_db7 void _nop_ (void);
void Lcd12864_port_init (void); void Lcd12864_writecmd (U8 cmd); Write command function void Lcd12864_writedata (U8 data); Write data function void lcd12864_init (void); Initialize 12864 void Locate16 (U8 X, U8 Y); void DisplayString (U8 x,u8 y,u8 *str);
The function void lcd12864_clear (void) that displays the string;
void Lcd_shiftleft (void);
void Lcd_shiftright (void);
#endif The following is the driver. C File: #include "12864LCD.h" void Lcd12864_port_init (void) {gpio_inittypedef gpio_portinit; /************ configuration control IO port **********/rcc_ahb1periphclockcmd (Lcd_control_port_clock, enable);//enable Gpiod clock Gpio_por Tinit.gpio_pin = lcd_control_pin;//control io Port gpio_portinit.gpio_mode = gpio_mode_out;//Normal output mode gpio_portinit.g Pio_otype = gpio_otype_pp;//Push-Pull output gpio_portinit.gpio_speed = Gpio_speed_100mhz;//100mhz GPIO_PortInit.GPIO_Pu Pd = gpio_pupd_up;//Gpio_init (lcd_control_port, &gpio_portinit);//Initialize control port GPIO/************ Configure Data port /Rcc_ahb1periphclockcmd (Lcd_data_port_clock, enable);//enable Gpioe clock Gpio_portinit.gpio_pin = Lcd_ data_pin;//Gpio_portinit.gpio_mode = gpio_mode_out;//normal output mode Gpio_portinit.gpio_otypE = gpio_otype_pp;//Push-Pull output gpio_portinit.gpio_speed = Gpio_speed_100mhz;//100mhz GPIO_PORTINIT.GPIO_PUPD = GPI o_pupd_up;//Gpio_init (Lcd_data_port, &gpio_portinit);//initialization Data port GPIO}//slightly delayed void _nop_ (void) {U8 i =
200;
while (i > 0) {i--; }/*******************************************************************//* Write instruction data to LCD * */*rs=l,rw=l,e= high pulse, d0-d7= command code.
*//*******************************************************************/void Lcd12864_writecmd (U8 cmd)//write command function { Clear_lcd_rs;
RS = 0. CLEAR_LCD_RW;
RW = 0. Set_lcd_en;
EN = 1.
_nop_ ();
_nop_ (); Lcd_data_port->odr= ((Lcd_data_port->odr & 0xff00) |cmd); Here,//Only the direct operation Register can//achieve, only change the output data register ODR's low 8 bits, other bits//constant purpose.
Because, only the lower 8 digits is the data pin,//other bit may be the control pin, cannot change.
_nop_ ();
_nop_ (); Clear_lcd_en;
EN = 0;
_nop_ ();
_nop_ (); }
/****************/* Write display data to LCD//*rs= H,rw=l,e= high pulse, d0-d7= data.
*//*******************************************************************/void Lcd12864_writedata (U8 data)//write function { Set_lcd_rs;
RS = 1. CLEAR_LCD_RW;
RW = 0. Set_lcd_en;
EN = 1.
_nop_ ();
_nop_ ();
Lcd_data_port->odr= ((Lcd_data_port->odr & 0xff00) |data);
_nop_ ();
_nop_ (); Clear_lcd_en;
EN = 0;
_nop_ ();
_nop_ (); }/*******************************************************************//* LCD12864 initialization *//*******************************************************************/void lcd12864_init (void)//initialization 12
864 {Clear_lcd_rst;
Delay_ms (2);
Set_lcd_rst;
Delay_ms (2); SET_LCD_PSB;
Make psb=1, set to parallel Data mode Delay_ms (2); Lcd12864_writecmd (0x30); Select the base instruction set, and, 8-bit data mode
Delay_ms (2); Lcd12864_writecmd (0x0c);
Open display, no cursor, no anti-white.
Delay_ms (2); Lcd12864_writecmd (0x01);
Clear the display and set the Ddram address counter AC to 00H.
Delay_ms (2); Lcd12864_writecmd (0x06);
setting, external read and write data, address register AC will automatically add 1 Delay_ms (2); Lcd12864_writecmd (0x80);
Set the Ddram address counter AC to 0.
Delay_ms (2); /********************************************************************************//* Set the display position, the following is the cursor positioning function, The first argument is row coordinates,/* The second is column coordinates, and the starting coordinates are 1 rows, 1 columns. Can only be moved with a width of 16 dots//********************************************************************************/void Pos
Ition (U8 x, U8 Y) {switch (x) {case 1:lcd12864_writecmd (0x80+y); Case 2:lcd12864_writecmd (0x90+y);
Break Case 3:lcd12864_writecmd (0x88+y);
Break Case 4:lcd12864_writecmd (0x98+y);
Break }}/*******************************************************************//* LCD12864 display string */
/***********************/void DisplayString (U8 x,u8 y,u8 *str)//display string function {if (x>4) {X = 1;}
if (y>4) {Y = 0;}
Position (X,y);
while (*str!= ' ") {lcd12864_writedata (*STR);
str++;
Delay_ms (5);
}//Clear screen, clear LCD12864 show void lcd12864_clear (void) {lcd12864_writecmd (0x01);}
The whole screen moves left one block for LCD12864 to do scrolling display void lcd_shiftleft (void) {lcd12864_writecmd (0x18);} The whole screen moves to the right one, for LCD12864 to do scrolling display void lcd_shiftright (void) {lcd12864_writecmd (0x1c);}