51单片机四线spi驱动UC1601液晶屏

#include 

/*
	老王1元液晶屏
	丝印:HEM12864-12,驱动IC:UC1601
	接线:1-CS;2-RST;3-DC;4-RW;5-SCK;8-SDA;13-VDD;14-BL-;15-BL+;16-GND
	51单片机四线spi驱动UC1601液晶屏
	焊盘1(BM0)\焊盘4(BM1)接地,BM[1:0]:"LL":S8(四线spi)

*/


#define uchar unsigned char
#define uint unsigned int


#define Disp_On 0xaf // Display Enable
#define Disp_Off 0xae // Display Disable
#define Page_Add 0xb0 // Page Address Start
//#define Row_Add 0x40 // Scroll Line Start


sbit CD = P2^5; // Control or Data Select
sbit CS0 = P2^7; // Chip select
sbit RST = P2^6; // System reset
sbit SCK=P2^3;
sbit SDA=P2^2;


uchar row,col; // 全局变量

unsigned char code peom[]={
0x02,0xE2,0x22,0x22,0x22,0xFE,0x22,0x22,
0x22,0xFE,0x22,0x22,0x22,0xE2,0x02,0x00,
0x00,0xFF,0x00,0x08,0x06,0x01,0x16,0x08,
0x06,0x01,0x02,0x4C,0x80,0x7F,0x00,0x00,/*"两",0*/

0x80,0x80,0x40,0x20,0x10,0x08,0x04,0xC3,
0x04,0x08,0x10,0x20,0x40,0x80,0x80,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,/*"个",1*/

0x10,0x10,0x12,0xD2,0x52,0x5F,0x52,0xF2,
0x52,0x5F,0x52,0xD2,0x12,0x10,0x10,0x00,
0x00,0x00,0x00,0x9F,0x52,0x32,0x12,0x1F,
0x12,0x32,0x52,0x9F,0x00,0x00,0x00,0x00,/*"黄",2*/

0xF2,0x12,0x12,0xF2,0x02,0xF2,0x12,0x12,
0xF2,0x00,0xFC,0x16,0x25,0x84,0xFC,0x00,
0xFF,0x01,0x82,0xFF,0x00,0xFF,0x01,0x82,
0xFF,0x10,0x13,0x12,0x52,0x82,0x7E,0x00,/*"鹂",3*/

0x00,0xFC,0x04,0x04,0xFC,0x00,0x00,0xFC,
0x04,0x16,0x65,0x04,0x84,0xFC,0x00,0x00,
0x00,0x0F,0x04,0x04,0x0F,0x00,0x10,0x13,
0x12,0x12,0x12,0x52,0x92,0x42,0x3E,0x00,/*"鸣",4*/

0x00,0x22,0x26,0x1A,0x12,0x02,0x7E,0x80,
0x22,0x26,0x1A,0x12,0x02,0x7E,0x00,0x00,
0x20,0x21,0x29,0x25,0x23,0x25,0x29,0xF1,
0x29,0x25,0x23,0x25,0x29,0x21,0x20,0x00,/*"翠",5*/

0x10,0x10,0xD0,0xFF,0x90,0x10,0xFC,0x04,
0x02,0xF8,0x00,0xFC,0x04,0x04,0xFC,0x00,
0x04,0x03,0x00,0xFF,0x00,0x01,0x8F,0x44,
0x32,0x0F,0x00,0xFF,0x04,0x08,0x07,0x00,/*"柳",6*/

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x58,0x38,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,/*",",7*/

0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,
0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,/*"一",8*/

0x00,0x10,0x88,0xC4,0x33,0x00,0x40,0x42,
0x42,0x42,0xC2,0x42,0x42,0x42,0x40,0x00,
0x02,0x01,0x00,0xFF,0x00,0x00,0x00,0x00,
0x40,0x80,0x7F,0x00,0x00,0x00,0x00,0x00,/*"行",9*/

0x00,0x00,0xF8,0x08,0x08,0x0C,0x0A,0x09,
0x08,0x08,0x08,0x08,0xF8,0x00,0x00,0x00,
0x00,0x00,0xFF,0x41,0x41,0x41,0x41,0x41,
0x41,0x41,0x41,0x41,0xFF,0x00,0x00,0x00,/*"白",10*/

0x00,0x80,0xEE,0x8A,0xFA,0xAA,0xAE,0x14,
0x12,0xF7,0xAA,0xAA,0xF6,0x12,0x10,0x00,
0x00,0x40,0x40,0x5E,0x52,0x53,0x56,0x5A,
0x52,0x5A,0x1E,0x50,0x90,0x70,0x00,0x00,/*"鹭",11*/

0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0x40,
0x40,0x40,0x40,0x40,0x40,0x00,0x00,0x00,
0x40,0x40,0x40,0x40,0x40,0x40,0x7F,0x40,
0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x00,/*"上",12*/

0x40,0x44,0x54,0x54,0x54,0x54,0x54,0x7F,
0x54,0x54,0x54,0x54,0x54,0x44,0x40,0x00,
0x00,0x00,0x00,0xFF,0x15,0x15,0x15,0x15,
0x15,0x55,0x95,0x7F,0x00,0x00,0x00,0x00,/*"青",13*/

0x40,0x40,0x42,0x42,0x42,0x42,0x42,0xFE,
0x42,0x42,0x42,0x42,0x42,0x40,0x40,0x00,
0x80,0x80,0x40,0x20,0x10,0x0C,0x03,0x00,
0x03,0x0C,0x10,0x20,0x40,0x80,0x80,0x00,/*"天",14*/

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x18,0x24,0x24,0x18,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,/*"。",15*/

0x10,0x0C,0xC4,0x54,0x4C,0x64,0xD5,0x46,
0x44,0x44,0x4C,0x54,0xC4,0x14,0x0C,0x00,
0x00,0x00,0xFF,0x40,0x44,0x62,0x55,0x49,
0x55,0x43,0x40,0x40,0xFF,0x00,0x00,0x00,/*"窗",0*/

0x40,0x40,0x20,0x20,0x90,0x88,0x94,0xE3,
0x84,0x88,0x90,0x20,0x20,0x40,0x40,0x00,
0x00,0x00,0x00,0xFC,0x44,0x44,0x44,0x44,
0x46,0x45,0x44,0xFC,0x00,0x00,0x00,0x00,/*"含",1*/

0x02,0x02,0xE2,0x22,0x22,0xFE,0x22,0x22,
0x22,0xFE,0x22,0x22,0xE2,0x02,0x02,0x00,
0x00,0x00,0xFF,0x48,0x44,0x43,0x40,0x40,
0x40,0x43,0x44,0x44,0xFF,0x00,0x00,0x00,/*"西",2*/

0x00,0xF0,0x00,0xFF,0x00,0xF0,0x40,0x20,
0x10,0x0C,0x23,0xCC,0x10,0x20,0x40,0x00,
0x00,0x1F,0x10,0x0F,0x08,0x1F,0x00,0x01,
0x09,0x11,0x21,0xD1,0x0D,0x03,0x00,0x00,/*"岭",3*/

0x80,0x80,0x84,0x84,0x84,0x84,0x84,0xFC,
0x82,0x82,0x82,0x83,0x82,0x80,0x80,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,/*"千",4*/

0x24,0x24,0xA4,0xFE,0x23,0x22,0x80,0x70,
0x00,0xFF,0x00,0x80,0x40,0x30,0x00,0x00,
0x08,0x06,0x01,0xFF,0x01,0x86,0x40,0x30,
0x0E,0x01,0x06,0x18,0x20,0x40,0x80,0x00,/*"秋",5*/

0x10,0x0C,0x05,0x55,0x55,0x55,0x05,0x7F,
0x05,0x55,0x55,0x55,0x05,0x14,0x0C,0x00,
0x00,0x00,0x41,0x49,0x49,0x49,0x49,0x49,
0x49,0x49,0x49,0x49,0xFF,0x00,0x00,0x00,/*"雪",6*/

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x58,0x38,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,/*",",7*/

0x00,0xF8,0x01,0x06,0x00,0x00,0x02,0x02,
0x02,0x02,0x02,0x02,0x02,0xFE,0x00,0x00,
0x00,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x40,0x80,0x7F,0x00,0x00,/*"门",8*/

0x10,0x60,0x02,0x0C,0xC0,0x00,0xF8,0x08,
0x0C,0x0B,0x08,0x08,0x08,0xF8,0x00,0x00,
0x04,0x04,0x7C,0x03,0x00,0x00,0xFF,0x41,
0x41,0x41,0x41,0x41,0x41,0xFF,0x00,0x00,/*"泊",9*/

0x00,0x08,0x88,0x48,0x28,0x18,0x0F,0xE8,
0x08,0x08,0x08,0x08,0x08,0x08,0x00,0x00,
0x00,0x20,0x11,0x09,0x05,0x41,0x81,0x7F,
0x01,0x01,0x05,0x09,0x11,0x20,0x00,0x00,/*"东",10*/

0x00,0x00,0x80,0x9E,0x92,0x92,0x92,0x92,
0x92,0x92,0x92,0x9E,0x80,0x00,0x00,0x00,
0x84,0x84,0x44,0x44,0x24,0x14,0x0C,0x07,
0x0C,0x14,0x24,0x44,0x44,0x84,0x84,0x00,/*"吴",11*/

0x04,0x04,0x04,0x04,0x04,0xFC,0x44,0x44,
0x44,0x44,0x44,0xC4,0x04,0x04,0x04,0x00,
0x80,0x40,0x20,0x18,0x06,0x01,0x00,0x00,
0x40,0x80,0x40,0x3F,0x00,0x00,0x00,0x00,/*"万",12*/

0x00,0x00,0xFE,0x92,0x92,0x92,0x92,0xFE,
0x92,0x92,0x92,0x92,0xFE,0x00,0x00,0x00,
0x40,0x40,0x44,0x44,0x44,0x44,0x44,0x7F,
0x44,0x44,0x44,0x44,0x44,0x40,0x40,0x00,/*"里",13*/

0x80,0x80,0xFC,0x96,0xE5,0x84,0xFC,0x80,
0x40,0x3E,0x02,0x02,0x7E,0x40,0x40,0x00,
0x80,0x60,0x1F,0x02,0x4C,0x80,0x7F,0x00,
0xFE,0x42,0x42,0x42,0x42,0xFE,0x00,0x00,/*"船",14*/

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x18,0x24,0x24,0x18,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,/*"。",15*/



};
/*------------------延时子程序1----------------------------*/

void delay(uint i) 
{
	while(i--);
}

/*------------------延时子程序2----------------------------*/

void delay1s(uint t)
{
	uint i,j;
	for(i=0;i>=1;
	}	
	
	SCK=0;
	delay(10);
	SDA=1;
}
/*------------------写命令到LCD-----------------------------*/

void write_com(uchar cmdcode)
{
	CD = 0;
	CS0 = 0;
	write_byte(cmdcode);
	CS0 = 1;
}

/*-------------------写数据到LCD----------------------------*/

void write_data(uchar Dispdata)
{
	CD = 1;
	CS0 = 0;
	write_byte(Dispdata);
	CS0 = 1;
}

/*-------------------清屏-----------------------------------*/
void Clr_Scr()
{
	uchar j,k;
	for(k=0;k<8;k++)
	{
		write_com(Page_Add+k);
		write_com(0x10);
		write_com(0x00);
		for(j=0;j<132;j++)
			write_data(0x00);
	}
}

/*------------------初始化LCD屏-----------------------------*/
void Init_lcd()
{
	delay(6000);
	RST=0;
	delay(6000);
	RST=1;
	delay(6000);
	write_com(0xe2); // Set System Reset
	delay(6000);
	write_com(0x2f); // Set Power Control loading 15-24nF

	write_com(0xa6); // Set 正常 Display
	write_com(0xc4); // Set Mapping Control
	write_com(0xa0); // Set Frame Rate
	write_com(0x89); // Set RAM Address Control 
	write_com(0xe8); // Set Bias Ratio : 1/6
	write_com(0x81);
	write_com(0xaf); // Set Vbias : 7.99V 0xff
	write_com(0xf1);
	write_com(0x3f); // Set COM End (63) 3f
	write_com(0xf2);
	write_com(0x00); // Set Partial Display Start (0)  0x20
	write_com(0xf3);
	write_com(0x3f); // Set Partial Display End (63) 3f
	write_com(0x85); // Set Partial Display Control
	write_com(Disp_On); // Set Display Enable
}


//-------LCD设置坐标---------------------------------------

void LCD_SetPos(uint page, uint column)
{
    column = column - 1; //我们平常所说的第 1 列,在 LCD 驱动 IC 里是第 0 列。所以在这里减去 1.
    page = page - 1;
		write_com(0xb0 + page);
		write_com(column & 0x0f);
    column >>= 4;
    column=column|0x10;
		write_com(column);                //取高4位行地址
}


//显示 16x16 点阵图像、汉字、生僻字或 16x16 
void display_graphic_16x16(uint page, uint column,uchar fs,signed char *dp)
{
		uchar i;//;
    LCD_SetPos(page, column);                //发送地址,第一页
    for (i = 0; i < 16; i++)                                //第一页的数据16字节,列号自动累加
    {
        if(fs==0)  write_data( *dp); //写数据到LCD,如果要反色显示,就在*P前用~取反。
				if(fs==1)  write_data(~*dp); //反色
        dp++;
    }
    LCD_SetPos(page + 1, column);                //发送地址,第二页
    for (i = 0; i < 16; i++)                                //第二页的数据16字节
    {
			if(fs==0)  write_data( *dp); //写数据到LCD,如果要反色显示,就在*P前用~取反。
			if(fs==1)  write_data(~*dp); //反色
      dp++;
    }
}


void main()
{
	int i;
	Init_lcd();
	Clr_Scr();
	while(1) 
	{
		Clr_Scr();

		for(i=0;i<32;i++)
		{
			display_graphic_16x16(1+(int)(i/8)*2, i*16%128+1, 0, peom+i*32);
		}
		delay1s(50);

		for(i=0;i<1024;i++)
		{
			display_graphic_16x16((1+(int)(i/8)%8*2)%8, i*16%128+1, 0, peom+(i%32)*32);
			write_com(0x40|i%64); // Set Scroll Line
            delay1s(1);
		}

		delay1s(50);

	}
}

51单片机四线spi驱动UC1601液晶屏_第1张图片

东拼西凑弄了段代码,把屏幕驱动了。

1、4线SPI比3线多了CD引脚控制,CD=0写命令,CD=1写数据。数据命令部分都8bit,所以被称作S8吧。

2、3线SPI用第一bit的高低来控制CD,每次发9bit。所以称作S9吧。

3、各种类型IC的SPI驱动都是写字节,写命令,写数据,就是LCD初始化部分不同,这部分不查资料(抄别人代码)自己搞不定。

4、显示汉字的函数display_graphic_16x16是另一个TM9665屏用过的,拷过来还可以用。

51单片机四线spi驱动UC1601液晶屏_第2张图片

 焊盘修改如上

你可能感兴趣的:(51单片机)