Raspberry Pi drives LCD12864 display

LCD12864 display screen suitable for ST7920 controller, using wiringPi GPIO library, written in C language.


The code is at the end of the article. The

hardware
connection adopts the 8-bit parallel drive mode of LCD12864, and the connection with Raspberry Pi GPIO has been defined at the beginning of the program. The

driving
principle is used WiringPi controls GPIO, and sends commands to LCD according to the communication protocol and the data to be displayed can be displayed.
It is mainly composed of hexadecimal conversion, character encoding conversion, and sending data.

code
/*
	run: sudo ./12864 [char]
	Compile: gcc 12864.c -o 12864 -L lib -l wiringPi (wiringPi must be installed)
	by:WuSiYu
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <wiringPi.h>
#include <iconv.h>
 
#define LCD_RS 4 //Display control line
#define LCD_RW 5
#define LCD_EN 1
 
#define D1 30 //Display data line
#define D2 21
#define D3 22
#define D4 23
#define D5 24
#define D6 25
#define D7 26
#define D8 27
 
char u2g_out[255];
 
/*===================================================================
Function: code conversion
Input: UTF8
Output: GB2312
====================================================================*/
int code_convert(char *from_charset,char *to_charset,char *inbuf,int inlen,char *outbuf,int outlen){
	iconv_t cd;
	int rc;
	char **pin = &inbuf;
	char **pout = &outbuf;
 
	cd = iconv_open(to_charset,from_charset);
	if (cd==0) return -1;
	memset(outbuf,0,outlen);
	if (iconv(cd,pin,&inlen,pout,&outlen)==-1) return -1;
	iconv_close(cd);
	return 0;
}
 
int u2g(char *inbuf,int inlen,char *outbuf,int outlen){
	return code_convert("utf-8","gb2312",inbuf,inlen,outbuf,outlen);
}
 
/*===================================================================
Function: bus write
Input: hexadecimal data
output: none
====================================================================*/
void bus_write(unsigned char data){
	int t[10];
	int f=0,i=0,d=data;
 
	// base conversion
	for(i=0;i<8;i++){
		t[i]=data%2;
		data = data / 2;
	}
 
	// output
	digitalWrite(D1,t[0]);
	digitalWrite(D2,t[1]);
	digitalWrite(D3,t[2]);
	digitalWrite(D4,t[3]);
	digitalWrite(D5,t[4]);
	digitalWrite(D6,t[5]);
	digitalWrite(D7,t[6]);
	digitalWrite(D8,t[7]);
}
/*===================================================================
Function: Check LCD busy status                                                    
Input: none
Output: When lcd_busy is 1, busy, waiting. When lcd-busy is 0, it is idle, and instructions and data can be written.      
====================================================================*/
void chk_busy(){//Check the busy bit
	digitalWrite(LCD_RS,0);
	digitalWrite(LCD_RW,1);
	digitalWrite(LCD_EN,1);
	bus_write(0xff);
	pinMode(D8, INPUT);
	while(digitalRead(D8));
	pinMode(D8, OUTPUT);
	digitalWrite(LCD_EN,0);
}
/*====================================================================
Function: write command
Input: 8-bit data
output: none
=====================================================================*/
void WriteCmd_LCD12864(unsigned char cmdcode){
	chk_busy();
	digitalWrite(LCD_RS,0);
	digitalWrite(LCD_RW,0);
	digitalWrite(LCD_EN,1);
	delay(5);
	bus_write(cmdcode);
	digitalWrite(LCD_EN,0);
	delay(5);
}
/*====================================================================
Function: write data
Input: 8-bit data
output: none
=====================================================================*/
void WriteData_LCD12864(unsigned char Dispdata){
	chk_busy();
	digitalWrite(LCD_RS,1);
	digitalWrite(LCD_RW,0);
	digitalWrite(LCD_EN,1);
	delay(5);
	bus_write(Dispdata);
	digitalWrite(LCD_EN,0);
	delay(5);
}
/*==========================================================================
Function: send string
Input: address, string
output: none
===========================================================================*/
void WriteWord_LCD12864(unsigned char a,unsigned char *d){//Send a string to the specified position of the LCD, the length is within 64 characters.
	unsigned char *s;
	u2g(d,strlen(d),u2g_out,255);
	s=u2g_out;
	WriteCmd_LCD12864(a);
	while(*s>0){
		WriteData_LCD12864(*s);
		s++;
	}
}
/*==========================================================================
Function: send string 2
input: string
output: none
===========================================================================*/
void WriteWord_LCD12864_2(unsigned char *d){//Send a screen string to LCD, the length is within 64 characters.
	int i=0;
	unsigned char *s;
	u2g(d,strlen(d),u2g_out,255);
	s=u2g_out;
	WriteCmd_LCD12864(0x80);
	while(*s>0){
		WriteData_LCD12864(*s);
		s++;
		i++;
		if(i==16){
			WriteCmd_LCD12864(0x90);
		}
		if(i==32){
			WriteCmd_LCD12864(0x88);
		}
		if(i==48){
			WriteCmd_LCD12864(0x98);
		}
	}
}
/*==========================================================================
Function: Initialize LCD
Input: none
output: none
===========================================================================*/
void Init_LCD12864(void){ //Initialize the LCD screen
	pinMode(D1, OUTPUT); //Set GPIO
	pinMode(D2, OUTPUT);
	pinMode(D3, OUTPUT);
	pinMode(D4, OUTPUT);
	pinMode(D5, OUTPUT);
	pinMode(D6, OUTPUT);
	pinMode(D7, OUTPUT);
	pinMode(D8, OUTPUT);
 
	pinMode(LCD_RS, OUTPUT);
	pinMode(LCD_RW, OUTPUT);
	pinMode(LCD_EN, OUTPUT);
 
	WriteCmd_LCD12864(0x38); //Select 8bit data stream
	delay(20);
	WriteCmd_LCD12864(0x01); //Clear the display and set the address pointer to 00H
	delay(20);
	WriteCmd_LCD12864(0x0c); //Open display (no cursor, no reverse white)
	delay(20);
}
 
int main (int args, char *argv[]){
	wiringPiSetup();
	Init_LCD12864();
 
	WriteCmd_LCD12864(0x01);
	WriteWord_LCD12864(0x80,"Hello LCD12864");
	if(argv[1]){
		WriteCmd_LCD12864(0x01);
		WriteCmd_LCD12864(0x80);
		WriteWord_LCD12864_2(argv[1]);
	}
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326050951&siteId=291194637