STM32 drives LCD12864 display

Publisher:QianfengLatest update time:2018-06-07 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

When we make an electronic product, we often need to realize the function of human-computer interaction. In addition to outputting to the host computer for display through the computer, the display is also a very good way for human-computer interaction, which can be used in some occasions where computers cannot be used. One of the LCD12864 displays has the advantages of low price and simple operation. Today, I will bring you a routine program for STM32 to drive 12864, using SPI serial communication, and only three data lines are needed to complete the communication. Without further ado, let's get to the point.

Wiring: RS----PB15
RW----PB14
EN----PB13
PSB---GND

1. Initialize IO ports and display screen

void Lcd_Init(void)   

{   

    GPIO_InitTypeDef  GPIO_InitStructure;  

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB, ENABLE); //Enable port clock  

      

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;            //端口配置  

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //Push-pull output  

    GPIO_InitStructure.GPIO_Speed ​​= GPIO_Speed_50MHz; //IO port speed is 50MHz  

    GPIO_Init(GPIOA, &GPIO_InitStructure); //Init GPIO according to the set parameters  

    GPIO_SetBits(GPIOA,GPIO_Pin_0);              //输出高  

      

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;//端口配置  

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;                //推挽输出  

    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;           //IO口速度为50MHz  

    GPIO_Init(GPIOB, &GPIO_InitStructure); //Initialize GPIO according to the set parameters  

    GPIO_SetBits(GPIOB,GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15);     //输出高  

      

    LCD_PSB_0; //Keep pulling low and drive with serial port (can be directly grounded)  

    LCD_RS_1; //CS is always pulled high to enable LCD (can be directly connected to VCC)  

  

    Lcd_Write_Cmd(0x30);   

    delay_us(10000);  

    Lcd_Write_Cmd(0x01);  

    delay_us(10000);  

    Lcd_Write_Cmd(0x02);  

    delay_us(10000);  

    Lcd_Write_Cmd(0x06);  

    delay_us(10000);  

    Lcd_Write_Cmd(0x0c);  

    delay_us(10000);  

    Lcd_Write_Cmd(0x34); //Graphics on (not displayed)  

    delay_us(10000);  

    Lcd_Fill(0x00); //Clear the RAM in the screen  

    delay_us(10000);  

    Lcd_Write_Cmd(0x36); //Graphics on (display on)  

    delay_us(10000);  

      

}  

2. Write byte function to LCD12864


void SendByte(float Dbyte)    

{  

     flying i;  

     for(i=8;i>0;i--)  

     {  

       if (Dbyte&0x80)  LCD_SID_1;  

       else  LCD_SID_0;   

       LCD_SCLK_1; //If there is a problem with the display, you need to add a delay after this  

       delay_us(1); //72MHZ is too fast, so there is no need to add 48MHZ of M0  

[cpp] view plain copy

  LCD_SCLK_0;  

  Dbyte<<=1;  

}  

3. Write command and data functions


void Lcd_Write_Cmd(uchar Cbyte )  

{  

      

     SendByte(0xf8);              //11111,RW(0),RS(0),0  

     SendByte(0xf0&Cbyte);          

     SendByte(0xf0&Cbyte<<4);     

      

}  

  

void Lcd_Write_Data(uchar Dbyte )  

{      

     SendByte(0xfa);              //11111,RW(0),RS(1),0  

     SendByte(0xf0&Dbyte);          

     SendByte(0xf0&Dbyte<<4);    

}  

4. Set the display start address function


void LCD_Set_XY(uchar x,uchar y)   

{  

    unsigned char k = 0;     

    switch(x)  

    {  

     case 1:   

            k = 0x80 + y;  

                break;    

        case 2:   

            k = 0x90 + y;  

                break;      

    case 3:   

            k = 0x88 + y;  

                break;     

        case 4:   

            k = 0x98 + y;  

                break;    

    default:  

            k = 0x80 + y;  

    }  

    Lcd_Write_Cmd(k);  

}  

5. Display Chinese character function


void hanzi_Disp(unsigned char x,unsigned char y,unsigned char *s)  

{   

    Lcd_Write_Cmd(0x30); //Enter standard mode  

    LCD_Set_XY(x,y);  

    while (*s)    

    {  

        Lcd_Write_Data(*s);  

        s++;  

    }  

    Lcd_Write_Cmd(0x36); //Return to graphics mode  

}  

6. Write another screen clearing function

[cpp] view plain copy

void Lcd_Fill(uchar color) //Fill the screen RAM  

{    

  fly x,y,i;  

  Lcd_Write_Cmd(0x34);    

   for(i=0;i<9;i=i+8)     

   for(y=0;y<0x20;y++)  

   {       

   for(x=0;x<8;x++)  

       {   

        Lcd_Write_Cmd(y+0x80);         

        Lcd_Write_Cmd(x+0x80+i);        

        Lcd_Write_Data(color);     

        Lcd_Write_Data(color);     

       }  

   }   

  Lcd_Write_Cmd(0x36);  

}  

The above are some function definitions. Finally, we click on these functions in the main function to display the content we want.

Finally, let me add a little trick to display variables. Use the sprintf function to store the variable in a string first, and then we can call the display string function.


Keywords:STM32 Reference address:STM32 drives LCD12864 display

Previous article:Solutions for all interface types of STM32 parallel bus
Next article:Realization of Parallel Communication between ARM9 and FPGA

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号