1. 程式人生 > >stm32驅動LCD12864液晶屏驅動程式,親測可行,複製直接能用!

stm32驅動LCD12864液晶屏驅動程式,親測可行,複製直接能用!

廢話不多說,直接貼程式,你自己複製過去即可,免得我通過把工程上傳的方式,你下載還得需要c幣,這個現在很貴呀!!!

就兩個檔案,LCD12864.h和LCD12864.c,自己複製過去後,改一下引腳即可

(如果你連改引腳都不會,那你看來是初初學者,那你自己百度吧),微控制器主頻:72MHZ

LCD12864.h如下所示

#ifndef __LCD12864_H
#define __LCD12864_H	 
#include "sys.h"


#define LCD1602_RS PEout(0)	// PE0		go define之後我們會發現,這個巨集最後就是在這個io的地址這裡賦值
										// #define MEM_ADDR(addr)  *((volatile unsigned long  *)(addr)) 
#define LCD1602_RW PEout(1)	// PE1	
#define LCD1602_E PAout(3)	//  PA3	


void LCD12864IO_Init(void);

void LCD1602_DB_IO_outputMode(void);
void LCD1602_DB_IO_inputMode(void);

void write_LCD1602_DB(uint8_t data);
uint8_t read_LCD1602_DB(void);


void InitLcd1602(void);
void LcdShowStr(unsigned char row, unsigned char column, unsigned char *str);
void LcdWriteCmd(unsigned char cmd);

void LcdShowImage(uint8_t x, uint8_t y, uint8_t w, uint8_t h, uint8_t *img);
void LcdClearArea(uint8_t x, uint8_t y, uint8_t w, uint8_t h);

//#ifndef _HE_
extern uint8_t HE[];
extern uint8_t QQ[];
extern uint8_t myVX[];

//#endif

#endif

LCD12864.c如下所示:

#include "lcd12864.h"
#include "delay.h"
//#define _HE_

uint8_t HE[]=
{


0x20,0x00,0x70,0x00,0x1E,0x00,0x10,0x3E,0x10,0x22,0xFF,0x22,0x10,0x22,0x18,0x22,0x38,0x22,0x54,0x22,0x54,0x22,0x12,0x22,0x11,0x3E,0x10,0x22,0x10,0x00,0x10,0x00/*"和",0*/
/* (16 X 16 , 宋體 )*/
	
};

uint8_t QQ[]=
{


0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x0F,0xE0,0x3F,0xE0,0x7F,0xF0,0x7F,0xF8,0x7D,0xF0,0x3C,0xD0,0x2C,0x30,0x30,0x70,0x38,0xE0,0x1F,0x00,0x00,0x00,0x00/*"C:\Users\Administrator\Desktop\qqXiaoBing.bmp",0*/
/* (16 X 16 )*/



	
};

u8 myVX[]=
{
0x00,0x0D,0xFC,0xC8,0x0F,0x3F,0xB0,0x00,0x00,0x0D,0xFC,0xC8,0x0F,0x3F,0xB0,0x00,
0x3F,0xCC,0x7C,0x0F,0x93,0x26,0x33,0xFC,0x20,0xCD,0x80,0x31,0xF0,0x9F,0xB3,0x04,
0x20,0xCD,0x80,0x31,0xF0,0xDF,0xB3,0x04,0x20,0xCF,0xFF,0xC6,0x0C,0xD9,0xB3,0x04,
0x20,0xCF,0xFF,0xC6,0x0C,0x99,0xB3,0x04,0x20,0xCF,0x80,0xCE,0x1C,0xE1,0xB3,0x04,
0x20,0xCF,0xC0,0xCE,0x1C,0xE1,0x33,0x04,0x3F,0xCD,0xE0,0x30,0x00,0xC6,0x33,0xFC,
0x00,0x0D,0x9B,0x36,0x6C,0xD9,0xB0,0x00,0x00,0x0D,0x9B,0x36,0x6C,0xD9,0xB0,0x00,
0xFF,0xFD,0x9F,0xC9,0x83,0x9F,0xBF,0xFF,0xFF,0xFD,0x9F,0xC9,0x83,0x9F,0xBF,0xFF,
0xE0,0xCD,0xFC,0x07,0xF0,0x27,0xC0,0xE0,0x06,0x32,0x79,0xC6,0x33,0x39,0xBC,0x99,
0x06,0x32,0x7B,0xC6,0x13,0x39,0xBC,0x9B,0xF9,0x01,0x9E,0x0F,0x9C,0x18,0x33,0x64,
0xF9,0x01,0x9C,0x0F,0x9C,0x18,0x33,0x64,0x06,0xFC,0x63,0x08,0x73,0x18,0x33,0x1C,
0x06,0xFC,0x61,0x08,0x73,0x18,0x33,0x1C,0xD8,0x03,0x9F,0x01,0x8F,0xE7,0xF3,0xE3,
0xD8,0x03,0x9F,0x01,0x8F,0xE7,0xF3,0xE3,0xFE,0x31,0x98,0xC9,0xFC,0x87,0x8C,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FC,0x03,0x0E,0x6F,0x21,0xBF,0x7F,
0x00,0x0F,0xE3,0xFF,0xFF,0x81,0xB3,0x18,0x00,0x0F,0xE3,0xFF,0xFF,0xC1,0xB3,0x18,
0x3F,0xCE,0x67,0x37,0x83,0xF8,0x3F,0x7B,0x3F,0xCE,0x67,0x37,0x83,0xF8,0x3E,0x79,
0x20,0xCC,0x1B,0x36,0x13,0x18,0x00,0x60,0x20,0xCD,0xE7,0x07,0xEF,0x86,0x30,0x07,
0x20,0xCD,0xE7,0x07,0xEF,0xC6,0x30,0x07,0x20,0xCC,0x7C,0xCE,0x63,0xC6,0x3C,0x9C,
0x20,0xCC,0x7C,0xCE,0x63,0xC6,0x3C,0x9C,0x3F,0xCE,0x67,0x39,0xEC,0xDE,0x0C,0xFC,
0x3F,0xCF,0x67,0x3D,0xE4,0x9E,0x0D,0xFC,0x00,0x0F,0xE4,0x3E,0x63,0x19,0xFF,0x80,/*"C:\Users\Administrator\Desktop\我的微信二維碼.bmp",0*/
/* (64 X 64 )*/

}
;


void LCD12864IO_Init(void)
{

 GPIO_InitTypeDef  GPIO_InitStructure;
 	
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOG|RCC_APB2Periph_GPIOE|RCC_APB2Periph_GPIOA, ENABLE);	 
															//使能PG,PE,PA埠時鐘
 GPIO_InitStructure.GPIO_Pin = 0XFF;				 		//PG埠配置
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 		 	//推輓輸出
 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;		 	//IO口速度為50MHz
 GPIO_Init(GPIOG, &GPIO_InitStructure);					 	//根據設定引數初始化GPIOG.0
 GPIO_SetBits(GPIOG,GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7);						 //PG.0 輸出高

 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1;		//PE.0,PE.1,埠配置
 GPIO_Init(GPIOE, &GPIO_InitStructure);					 	//根據設定引數初始化GPIOE.0.1
 GPIO_SetBits(GPIOE,GPIO_Pin_0|GPIO_Pin_1); 				//PE.0.1 輸出高 

 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;				 	//PA3埠配置
 GPIO_Init(GPIOA, &GPIO_InitStructure);					 	//根據設定引數初始化GPIOPA3
 GPIO_SetBits(GPIOA,GPIO_Pin_3); 						 	//PA3 輸出高 
	
}
void LCD1602_DB_IO_outputMode(void)
{
 GPIO_InitTypeDef  GPIO_InitStructure;
 GPIO_InitStructure.GPIO_Pin = 0XFF;				 		//PG埠配置
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 		 	//推輓輸出
 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;		 	//IO口速度為50MHz
 GPIO_Init(GPIOG, &GPIO_InitStructure);					 	//根據設定引數初始化GPIOG.0
 GPIO_SetBits(GPIOG,GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7);						 //PG.0 輸出高
	
}

void LCD1602_DB_IO_inputMode(void)
{
 GPIO_InitTypeDef  GPIO_InitStructure;
 GPIO_InitStructure.GPIO_Pin = 0XFF;				 		//PG埠配置
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; 		 		//上拉輸入
 GPIO_Init(GPIOG, &GPIO_InitStructure);					 	//根據設定引數初始化GPIOG.0
 GPIO_SetBits(GPIOG,GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7);						 //PG.0 輸出高
	
}

void write_LCD1602_DB(uint8_t data)
{
	GPIO_Write(GPIOG,(uint16_t)data);
}

uint8_t read_LCD1602_DB(void)
{
	uint8_t value;
	value=(uint8_t)GPIO_ReadInputData(GPIOG);
	return value;
}

/* 等待液晶準備好 */
void LcdWaitReady()
{
    unsigned char sta;
		delay_us(1);

    LCD1602_RS = 0;
		delay_us(1);

    LCD1602_RW = 1;
		delay_us(1);

    do {
        LCD1602_E = 1;
		delay_us(1);

		LCD1602_DB_IO_inputMode();
		sta = read_LCD1602_DB();
		LCD1602_DB_IO_outputMode();	
		delay_us(1);

        LCD1602_E = 0;
		delay_us(1);

    } while (sta & 0x80); 	//bit7等於1表示液晶正忙,重複檢測直到其等於0為止
}
/* 向LCD1602液晶寫入一位元組命令,cmd-待寫入命令值 */
void LcdWriteCmd(unsigned char cmd)
{
		delay_us(1);

    LcdWaitReady();
		delay_us(1);

    LCD1602_RS = 0;
		delay_us(1);

    LCD1602_RW = 0;
		delay_us(1);

    write_LCD1602_DB(cmd);
		delay_us(1);

    LCD1602_E  = 1;
		delay_us(1);

    LCD1602_E  = 0;
		delay_us(1);

}

/* 向LCD1602液晶寫入一位元組資料,dat-待寫入資料值 */
void LcdWriteDat(unsigned char dat)
{
		delay_us(1);

    LcdWaitReady();
		delay_us(1);

    LCD1602_RS = 1;
		delay_us(1);

    LCD1602_RW = 0;
		delay_us(1);

    write_LCD1602_DB(dat);
		delay_us(1);

    LCD1602_E  = 1;
		delay_us(1);

    LCD1602_E  = 0;
		delay_us(1);

}
/* 設定顯示RAM起始地址,亦即游標位置,(x,y)-對應螢幕上的字元座標 */
void LcdSetCursor(unsigned char x, unsigned char y)
{
    unsigned char addr;
    
	if (y >= 2)	   						//說明我們想顯示在右半屏
    {
        y -= 2;
        x += 8;							//那麼x就得往右挪動8個漢字了
    }
    addr = y*16 + x;					//現在這個是以一個漢字為單位,這裡所有所說的漢字為單位的意思是16*16
										//由起始DDRAM地址連續寫入字串				  
										//addr的值是所有的漢字後面的地址,也是漢字為單位,即ddram也是以漢字為單位的
    LcdWriteCmd(0x30); 					//啟動DDRAM操作
    LcdWriteCmd(0x80|addr);				//設定ram地址
}
/* 在液晶上顯示字串,(x,y)-對應螢幕上的起始座標,str-字串指標 */
void LcdShowStr(unsigned char row, unsigned char column, unsigned char *str)
{
	unsigned char n=0,a;
	unsigned char *fore_str;
    LcdSetCursor(column, row);   		//設定起始地址
    while (*str != '\0')  				//連續寫入字串資料,直到檢測到結束符
    {
		
		if((n+1)%2==0)					//奇數地址
		{
			fore_str=str-1;
			if(*fore_str>0x80)
			{
				LcdWriteDat(*str++);
			}
			else if(*str>0x80)
				LcdWriteDat(' ');
			else if(*str<=0x80)
				LcdWriteDat(*str++);
		}			
		else
			LcdWriteDat(*str++);  		//先取str指向的資料,然後str自加1
		n++;

		if((n+2*column)%16==0)
		{
			a=(n+2*column)/16;
			if(row==0)
			{
			if(a==1)
				LcdWriteCmd(0x90);
			if(a==2)
				LcdWriteCmd(0x88);
			if(a==3)
				LcdWriteCmd(0x98);
			if(a==4)
				LcdWriteCmd(0x80);
			}

			if(row==1)
			{
			if(a==1)
				LcdWriteCmd(0x88);
			if(a==2)
				LcdWriteCmd(0x98);
			if(a==3)
				LcdWriteCmd(0x80);
			if(a==4)
				LcdWriteCmd(0x90);
			}	

			if(row==2)
			{
			if(a==1)
				LcdWriteCmd(0x98);
			if(a==2)
				LcdWriteCmd(0x80);
			if(a==3)
				LcdWriteCmd(0x90);
			if(a==4)
				LcdWriteCmd(0x88);
			}	

			if(row==3)
			{
			if(a==1)
				LcdWriteCmd(0x80);
			if(a==2)
				LcdWriteCmd(0x90);
			if(a==3)
				LcdWriteCmd(0x88);
			if(a==4)
				LcdWriteCmd(0x98);
			}	


	
		}
    }
}/* 初始化1602液晶 */
void InitLcd1602()
{
    LcdWriteCmd(0x38);  //16*2顯示,5*7點陣,8位資料介面
    LcdWriteCmd(0x0C);  //顯示器開,游標關閉
    LcdWriteCmd(0x06);  //文字不動,地址自動+1
    LcdWriteCmd(0x01);  //清屏
}




/*
* 函式名:LcdShowImage
* 描  述:在顯示屏上顯示一幅影象
* 輸  入:img - 待顯示影象指標
*         x - 螢幕顯示橫座標(以畫素為單位)
*         y - 螢幕顯示縱座標(以畫素為單位)
*         w - 影象寬度(以畫素為單位)
*         h - 影象高度(以畫素為單位)
* 輸  出:無
* 備  注:x與w必須是16的整數倍,因CGRAM最小定址單位為2位元組;y與h可為0-63的任意值。
*/

//經過我的不斷測試,y和h的值也是可以取到64,而且顯示漢字,也得取到16而不是15,那麼來說,是多少畫素高度,這裡就填多少
void LcdShowImage(uint8_t x, uint8_t y, uint8_t w, uint8_t h, uint8_t *img)
{
    uint16_t i;
    uint8_t xi,yi;
    uint8_t xt,yt;

    x >>= 4;
    w >>= 3;
    i = 0;
    LcdWriteCmd(0x36); //啟動CGRAM操作
	
    for (yi=0; yi<h; yi++)
    {
        yt = y+yi;
        xt = x;
        if (yt >= 32)
        {
            yt -= 32;
            xt += 8;
        }
        LcdWriteCmd(0x80|yt);
        LcdWriteCmd(0x80|xt);
        for (xi=0; xi<w; xi++)
        {
            LcdWriteDat(img[i++]);
        }
    }
}

/*
* 函式名:LcdClearArea
* 描  述:清除螢幕上的一塊圖形顯示區域
* 輸  入:x - 區域起始橫座標(以畫素為單位)
*         y - 區域起始縱座標(以畫素為單位)
*         w - 區域寬度(以畫素為單位)
*         h - 區域高度(以畫素為單位)
* 輸  出:無
* 備  注:x與w必須是16的整數倍,因CGRAM最小定址單位為2位元組;y與h可為0-63的任意值。
*/


//記住,繪圖模式時候螢幕預設有亂點顯示的資料的,需要我們先呼叫這個函式後面才能正常顯示

void LcdClearArea(uint8_t x, uint8_t y, uint8_t w, uint8_t h)
{
    uint8_t xi,yi;
    uint8_t xt,yt;

    x >>= 4;
    w >>= 3;
    LcdWriteCmd(0x36); 			//啟動CGRAM操作
    for (yi=0; yi<h; yi++)
    {
        yt = y+yi;
        xt = x;
        if (yt >= 32)
        {
            yt -= 32;
            xt += 8;
        }
        LcdWriteCmd(0x80|yt);
        LcdWriteCmd(0x80|xt);
        for (xi=0; xi<w; xi++)
        {
            LcdWriteDat(0x00); 	//寫入0x00以清除顯示
        }
    }
}

 主函式main()中這樣呼叫即可:

void main()
{
	LCD12864IO_Init(void);
	// 為什麼驅動函式會出現1602的字樣,因為這個LCD12864的驅動程式我是從1602液晶屏移植過來的,
	// 只是忘記改了名字而已,就這樣吧
	InitLcd1602(void);
	LcdShowStr(1,1,"hello 你好");
	while(1);
}

程式完全o98k,拿去用吧!