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.
Previous article:Solutions for all interface types of STM32 parallel bus
Next article:Realization of Parallel Communication between ARM9 and FPGA
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- Vietnam's chip packaging and testing business is growing, and supply-side fragmentation is splitting the market
- Vietnam's chip packaging and testing business is growing, and supply-side fragmentation is splitting the market
- Three steps to govern hybrid multicloud environments
- Three steps to govern hybrid multicloud environments
- Microchip Accelerates Real-Time Edge AI Deployment with NVIDIA Holoscan Platform
- Microchip Accelerates Real-Time Edge AI Deployment with NVIDIA Holoscan Platform
- Melexis launches ultra-low power automotive contactless micro-power switch chip
- Melexis launches ultra-low power automotive contactless micro-power switch chip
- ST sensor driver package, official C-Driver-MEMS
- Fujian Gulei police cracked a "million-chip" case
- How to measure analog voltage using the ADC of MSP430?
- Problems with the primary absorption clamp circuit in the flyback circuit
- How to use MSO5 series oscilloscope to capture abnormal signals?
- Analysis of interrupt processing of S3C2410 && WinCE6.0
- What is the impedance of a power module?
- I didn’t expect that Musk’s SpaceX manned spacecraft programming used C++ and Python
- ESP32-S2-Saola-1 运行 circuitpython(2)
- Sinlinx A33 realizes LED driver