您的当前位置:首页正文

51单片机LCD12864绘图

2022-08-27 来源:汇智旅游网
精品--

LCD12864绘图方法如下:

一、绘图方法:

1、使用扩展指令集并关闭绘图显示 2、输入y轴坐标

3、输入x轴坐标 4、写入数据D15-D8 5、写入数据D7-D0 6、开绘图显示 7、返回普通指令 对应程序如下: LCD_write_comm(0x34);//使用扩充指令集 LCD_delay(); LCD_write_comm(0x80+y);//y坐标起始地址 LCD_delay(); LCD_write_comm(0x80+x);//x坐标起始地址 LCD_delay(); LCD_write_date(0x55);//写入数据D15-D8 LCD_delay(); LCD_write_date(0x55);//写入数据D7-D0 LCD_delay(); LCD_write_comm(0x36);//显示RAM LCD_delay(); LCD_write_comm(0x30);//退出扩展指令集 LCD_delay();

--精品

精品--

程序执行效果则是第一行的第1-16个像素点会描出0x5555的二进制像素点,则后面的整个屏幕则是花屏,如果只想看到这0x5555,这几个像素点,则必须要给LCD的RAM重新赋值,也可以说是清屏,程序如下:

void LCD12864_RAM_Cls() { u8 x,y;//xy坐标 LCD_write_comm(0x34);//使用扩充指令集 LCD_delay(); for(y=0;y<32;y++) { LCD_write_comm(0x80+y);//y坐标起始地址 LCD_delay(); LCD_write_comm(0x80);//x坐标起始地址 LCD_delay(); for(x=0;x<16;x++) { LCD_write_date(0x00); LCD_delay(); LCD_write_date(0x00); LCD_delay(); } } LCD_write_comm(0x36);//显示RAM LCD_delay(); LCD_write_comm(0x30);//退出扩展指令集 LCD_delay(); }

二、坐标与显示

X轴坐标设定初始值后会自动加1,取值范围0-15 Y轴需要人为加1,取值范围为0-31

整个屏幕分为上下屏,上屏对应坐标为x(0-7)y(0-31),下屏对应坐标为x(8-15)y(0-31) 三、LCD12864图片显示代码 1、Main.c

#include\"common.h\" #include\"delay.h\" #include\"12864.h\"

unsigned char code Picture[]= // 图片数据表 {

//此处添加图片取模16进制码共1024Byte,取模方式:从左到右从上到下 0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x14,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01, 0x80,0x06,0x30,0x01,0xE0,0x00,0x00,0x2A,0x00,0x00,0xD8,0x00,0x00,0x00,0x00,0x01, 0x80,0x0F,0x7B,0x63,0xE0,0x00,0x00,0x22,0x00,0x01,0x24,0x00,0x00,0x00,0x00,0x01,

--精品

精品--

0x80,0x0F,0xFB,0x63,0x07,0x34,0x00,0x14,0x00,0x01,0x04,0x00,0x00,0x00,0x00,0x01, 0x80,0x0D,0xDB,0x63,0x01,0xBC,0x00,0x08,0x00,0x00,0x88,0x00,0x00,0x00,0x00,0x01, 0x80,0x0C,0x1B,0x63,0x07,0xB0,0x00,0x00,0x00,0x00,0x50,0x00,0x00,0x00,0x00,0x01, 0x80,0x0C,0x1B,0x63,0xED,0xB0,0xDB,0x00,0x01,0x00,0x20,0x00,0x00,0x00,0x00,0x01, 0x80,0x0C,0x19,0xE1,0xE7,0xB0,0xDB,0x00,0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x01, 0x80,0x0C,0x19,0xE1,0xE7,0xB0,0xDB,0x00,0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x01, 0x80,0x00,0x00,0x60,0x00,0x00,0x00,0x00,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x01, 0x80,0x00,0x03,0xC0,0x00,0x00,0x00,0x00,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x01, 0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x05,0x00,0x00,0x00,0x00,0x00,0x01, 0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x10,0x0A,0x80,0x00,0x00,0x00,0x00,0x01, 0x80,0x00,0x00,0x0A,0x00,0x00,0xFF,0xF8,0xE0,0x05,0x00,0x00,0x00,0x00,0x00,0x01, 0x80,0x06,0xC0,0x15,0x00,0x1F,0xFF,0xFF,0xC0,0x02,0x00,0x00,0x00,0x00,0x00,0x01, 0x80,0x09,0x20,0x11,0x00,0xFB,0xFF,0xE1,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x01, 0x80,0x08,0x20,0x0A,0x03,0x9F,0x00,0x9E,0x3E,0x00,0x00,0x00,0x00,0x00,0x00,0x01, 0x80,0x04,0x40,0x04,0x0E,0x70,0x00,0x81,0xC7,0x80,0x01,0x40,0x00,0x00,0x00,0x01, 0x80,0x02,0x80,0x00,0x1D,0x80,0x00,0xE0,0x61,0xE0,0x02,0xA0,0x00,0x00,0x00,0x01, 0xC0,0x01,0x00,0x00,0x77,0x9F,0xFC,0xF0,0x18,0xF8,0x02,0x20,0x00,0x00,0x00,0x01, 0xC0,0x00,0x00,0x01,0xDF,0x00,0x00,0xF3,0x0C,0x3C,0x01,0x40,0x00,0x00,0x00,0x01, 0xC0,0x00,0x00,0x03,0x9E,0x00,0x00,0xF8,0x06,0x1E,0x00,0x80,0x00,0x00,0x00,0x01, 0x80,0x00,0x00,0x07,0x0E,0x30,0x01,0xFC,0x7F,0x07,0x00,0x00,0x00,0x00,0x00,0x01, 0x80,0x00,0x00,0x07,0x0E,0x30,0x01,0xFC,0x7F,0x07,0x00,0x00,0x00,0x00,0x00,0x01, 0x80,0x00,0x00,0x7E,0xFF,0xFF,0xFF,0xFF,0xFF,0x83,0xC0,0x00,0x00,0x00,0x00,0x01, 0x80,0x00,0x0F,0xFD,0xFF,0xFF,0xFF,0xFF,0xFC,0x01,0xF0,0x00,0x00,0x00,0x00,0x01, 0x80,0x00,0x3F,0x81,0xFF,0x00,0x00,0x00,0x00,0x1F,0xF0,0x00,0x00,0x00,0x00,0x01, 0x80,0x00,0xFE,0xF0,0x00,0x00,0x01,0x00,0x00,0xC0,0x1C,0x00,0x00,0x00,0x00,0x01, 0x80,0x03,0xC0,0x07,0x80,0x00,0x03,0xE0,0x07,0x00,0x1C,0x00,0x00,0x00,0x00,0x01, 0x80,0x03,0x80,0x03,0x60,0x00,0x03,0xE0,0x0C,0x00,0x0E,0x00,0x00,0x00,0x00,0x01, 0x80,0x07,0x00,0x00,0xC0,0x00,0x00,0x20,0x18,0x00,0x07,0x00,0x00,0x00,0x00,0x01, 0x80,0x09,0x01,0x80,0x60,0x00,0x00,0x20,0x73,0x9F,0x03,0x80,0x00,0x00,0x00,0x01, 0x80,0x1E,0x0F,0xF2,0x20,0x00,0x00,0x20,0x67,0xFF,0xC1,0xC0,0x00,0x00,0x00,0x01, 0x80,0x1E,0x1F,0xF9,0x30,0x00,0x00,0x20,0xEF,0xFF,0xE0,0xE0,0x00,0x00,0x00,0x01, 0x80,0x38,0x3F,0xFC,0x90,0x00,0x00,0x20,0xCB,0xFF,0xF9,0xF0,0x00,0x00,0x00,0x01, 0x80,0x7F,0x7F,0xFE,0x10,0x00,0x00,0x20,0xDB,0xFF,0xFF,0xF0,0x00,0x00,0x00,0x01, 0x80,0x6E,0xF9,0xBF,0x10,0x00,0x00,0x20,0xF7,0xED,0xFF,0xF0,0x00,0x00,0x00,0x01, 0x80,0x7C,0xFC,0x3F,0x10,0x00,0x00,0x20,0x67,0xE1,0xFD,0xE0,0x00,0x00,0x00,0x01, 0x80,0x7C,0xFC,0x3F,0x10,0x00,0x00,0x20,0x67,0xE1,0xFD,0xE0,0x00,0x00,0x00,0x01, 0x80,0x38,0xFC,0x3F,0x18,0x00,0x00,0x20,0x87,0xE0,0xFD,0xC0,0x00,0x00,0x00,0x01, 0x80,0x38,0xF1,0x8F,0x18,0x00,0x1F,0xE7,0x87,0x9C,0x7D,0xC0,0x00,0x00,0x00,0x01, 0x80,0x1C,0xFE,0x1F,0x81,0xFF,0xFF,0xC0,0x0F,0xE0,0xFF,0x80,0x00,0x00,0x00,0x01, 0x80,0x0C,0xFC,0x3F,0x80,0x00,0x00,0x00,0x0F,0xE1,0xF8,0x00,0x00,0x00,0x00,0x01, 0x80,0x00,0xF9,0xBF,0xFF,0xFF,0xFF,0xFF,0xFF,0xEC,0xFE,0x00,0x00,0x00,0x00,0x01, 0x80,0x03,0xF9,0xBF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xC0,0x00,0x00,0x00,0x01, 0x80,0x0F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xC0,0x00,0x00,0x00,0x01, 0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,

--精品

精品--

0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03, 0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03, 0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03, 0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03, 0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01, 0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01, 0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01, 0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01, 0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01, 0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01, 0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01, 0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01, 0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01, 0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01, 0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01, 0xE0,0x00,0x00,0x00,0x60,0x00,0x00,0x01,0xC0,0x02,0x80,0x00,0x00,0x00,0x1C,0x01,

0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF };

void main() { LCD12864_init();//LCD初始化 //LCD12864_RAM_Cls(); LCD_Picture_show(Picture); while(1); }

2、common.h

#ifndef __COMMON_H #define __COMMON_H

#include #include \"intrins.h\"

#define u8 unsigned char//无符号字符型 #define u16 unsigned int//无符号整型

#endif

3、delay.h

#ifndef __DELAY_H #define __DELAY_H

#include \"common.h\"

--精品

精品--

#define MAIN_Fosc 24000000L //定义主时钟

void delay_ms(u16 ms);

void LCD_delay();//LCD延时函数

#endif

4、delay.c

#include \"delay.h\"

//-------毫秒延时函数--------------- void delay_ms(u16 ms) {

u16 i; do{

i = MAIN_Fosc / 13000;

while(--i) ; //14T per loop }while(--ms); }

//------LCD延时函数----------------- void LCD_delay(void) {

_nop_();_nop_();_nop_(); _nop_();_nop_();_nop_(); }

5、12864.h

#ifndef __12864_H #define __12864_h

#include \"common.h\"

sbit CS =P1^1;//片选 sbit SID=P1^2;//数据口 sbit CLK=P1^3;//同步时钟

void LCD_write_spi(u8 date);//LCD SPI数据写入 void LCD_write_comm(u8 comm);//LCD写指令 void LCD_write_date(u8 date);//LCD写数据 void LCD12864_init();//LCD初始化 void LCD12864_RAM_Cls();//RAM清屏 void LCD_print(u8 *date);//LCD字符串显示 void LCD_Picture_show(u8 *date);

--精品

精品--

#endif

6、12864.c

#include\"common.h\" #include\"delay.h\" #include\"12864.h\"

//------LCD SPI数据写入-------------------- void LCD_write_spi(u8 date) { u8 i; for(i=0;i<8;i++) { CS=1; if(date&0x80){SID=1;} else{SID=0;} date <<= 1; CLK=1; LCD_delay(); CLK=0; } }

//------LCD写指令-------------------- void LCD_write_comm(u8 comm) { u8 date = 0xf8;//串行写入指令到LCD(数据手册) }

u8 comm_H = comm&0xf0; u8 comm_L = comm<<4; CS =0; CLK=0; SID=0;

LCD_delay();

LCD_write_spi(date);

LCD_write_spi(comm_H); LCD_write_spi(comm_L); CS=0;

//------LCD写数据-----------------------

--精品

精品--

void LCD_write_date(u8 date) { u8 date2 = 0xfa;//串行写入指令到LCD(数据手册) }

u8 date_H = date&0xf0; u8 date_L = date<<4; CS =0; CLK=0; SID=0;

LCD_delay();

LCD_write_spi(date2); LCD_write_spi(date_H); LCD_write_spi(date_L); CS=0;

//-----LCD初始化-------------------------- void LCD12864_init() { LCD_write_comm(0x01);//清屏幕 delay_ms(1); LCD_write_comm(0x0c);//开显示,游标显示关闭 LCD_delay(); LCD_write_comm(0x81);//设定坐标 LCD_delay(); }

//-----写RAM清屏-------------------------- void LCD12864_RAM_Cls() { u8 x,y;//xy坐标 LCD_write_comm(0x34);//使用扩充指令集 LCD_delay(); for(y=0;y<32;y++) { LCD_write_comm(0x80+y);//y坐标起始地址 LCD_delay(); LCD_write_comm(0x80);//x坐标起始地址 LCD_delay(); for(x=0;x<16;x++) { LCD_write_date(0x00); LCD_delay();

--精品

精品--

}

LCD_write_date(0x00); LCD_delay(); } }

LCD_write_comm(0x36);//显示RAM LCD_delay();

LCD_write_comm(0x30);//退出扩展指令集 LCD_delay();

//----LCD字符串显示----------------------- void LCD_print(u8 *date) { u8 j=0; while(date[j]!='\\0') { LCD_write_date(date[j]); j++; LCD_delay(); } }

//----图片显示----------------------------- void LCD_Picture_show(u8 *date) { u8 x,y;//xy坐标 LCD_write_comm(0x34);//使用扩充指令集 LCD_delay(); for(y=0;y<32;y++) { LCD_write_comm(0x80+y);//y坐标起始地址 LCD_delay(); LCD_write_comm(0x80);//x坐标起始地址 LCD_delay(); for(x=0;x<8;x++)//写上半屏幕 { LCD_write_date(date[16*y+x*2]); LCD_delay(); LCD_write_date(date[16*y+x*2+1]); LCD_delay(); } for(x=0;x<8;x++)//写下半屏幕 { LCD_write_date(date[16*(y+32)+x*2]); LCD_delay();

--精品

精品--

LCD_write_date(date[16*(y+32)+x*2+1]); LCD_delay(); } } LCD_write_comm(0x36);//显示RAM LCD_delay(); LCD_write_comm(0x30);//退出扩展指令集 LCD_delay(); }

四、程序执行效果

--精品

因篇幅问题不能全部显示,请点此查看更多更全内容