文章详情

清达光电1602SPI串口液晶屏样例程序

日期:2024-04-27 00:34
浏览次数:1325
摘要:清达光电1602SPI串口液晶屏供电电压有5V和3.3V,显示屏有黄绿模式和蓝模式。

    清达光电1602SPI串口液晶屏为16*2字符型,接口为SPI接口,供电电压有5V/3.3V,显示模式有蓝模式/黄绿模式,下面为这款1602SPI串口液晶屏样例程序(基于普通的51单片机,IO口模拟时序的):


#include<reg51.h>

#include<string.h>

#include<stdio.h>

#include <intrins.h>

#define uchar unsigned char

#define uint unsigned int

sbit SID=P3^4;

sbit SCK=P3^3;

sbit CS=P3^1;

sbit RS=P3^0;

#define clear 0x01 /* Clear Display */

#define home 0x02 /* Return Home */

#define enmode 0x06 /* Enter Mode Set */

#define off 0x08 /* Display on/off Set */

#define on 0x0e /* Display on/off Set */

#define shift 0x14 /*Cursor or Display Shift*/

#define reset 0x30 /* reset */

#define function 0x38 /* Function Set */

#define cgram 0x40 /* Set CGRAM

Address */

#define ddram1 0x80 /* Set DDRAM Line1

Address */

#define ddram2 0xC0 /* Set DDRAM Line2

Address */

#define time 1000 /* 1000 延时

1.03895500 s */

uchar code string1[]={"Jia Xian Ke Ji"};

uchar code string2[]={"www.tsingtek.com"};

uchar code string3[]={"0123456789"};

uchar code

string4[]={"abcdefghijklmnopqrstuvwxyz"};

uchar code string5[]={"Thank You !"};

uchar code

string6[]={'2','0','1','2',0x06,'1','0',0x07,'\0'};

/********* 自定义字符库 *********/

uchar code CGF[8][8]={

0x1f,0x00,0x1f,0x00,0x1f,0x00,0x1f,0x00,

/* 隔行 00H */

0x00,0x1f,0x00,0x1f,0x00,0x1f,0x00,0x1f,

/* 01H */

0x15,0x15,0x15,0x15,0x15,0x15,0x15,0x15,

/* 隔列 02H */

0x0a,0x0a,0x0a,0x0a,0x0a,0x0a,0x0a,0x0a,

/* 03H */

0x15,0x0a,0x15,0x0a,0x15,0x0a,0x15,0x0a,

/* 隔点 04H */

0x0a,0x15,0x0a,0x15,0x0a,0x15,0x0a,0x15,

/* 05H */

0x08,0x0f,0x12,0x0f,0x0a,0x1f,0x02,0x02,

/* "年" 06H */

0x0f,0x09,0x0f,0x09,0x0f,0x09,0x11,0x00,

/* "月" 07H */

};

/********* 时间延时函数 *********/

void delay(uint t) /* 1.02500 ms */

{ uint i,j;

for(i=t;i>0;i--)

for(j=125;j>0;j--);

}

void write_com(uchar Command_byte)

{

uint i;

RS=0;//write command

CS=0;

SCK=1;

for(i=0;i<8;i++)

{

if((Command_byte&0x80)==0x80)

SDI=1;

else

SDI=0;

SCK=0;

if(i==0)

{

delay(1);

}

Command_byte<<=1;

SCK=1;

}

CS=1;

delay(1);

}

/////////////////////////////////////////

void write_data(uchar Data8_byte)

{

uint i;

RS=1;//write data

CS=0;

SCK=1;

for(i=0;i<8;i++)

{

if((Data8_byte&0x80)==0x80)

SDI=1;

else

SDI=0;

SCK=0;

if(i==0)

{

delay(1);

}

Data8_byte<<=

SCK=1;

}

CS=1;

RS=0;

delay(1);

}

/********* 建立自定义字符函数

*********/

void CGFUNTION(uchar array[8][8])

{ uchar i,j;

write_com(cgram); /* 写“ 指令”

CGRAM 地址 */

for(i=0;i<8;i++)

for(j=0;j<8;j++)

write_data(array[i][j]); /* 写“数据”

*/

}

/********* 字符全屏显示函数 *********/

void char_fill_disp(uchar dat)

{ uchar i,j,temp=0;

for(i=0;i<2;i++)

for(j=0;j<16;j++)

{ if(i==1)

temp=j|ddram2;

/* DDRAM **行地址 */

else

temp=j|ddram1;

/* DDRAM **行地址 */

write_com(temp);

/* 写“指令” */

write_data(dat); /* 写“数据”*/

}

delay(time); /* 延时 */

}

/********* 字符串显示函数 *********/

/*********** 可选行显示 **********/

void string_disp(uchar x, uchar y, uchar *str)

{ uchar i,j,temp=0;

for(i=x;i<2;i++)

for(j=y;j<16;j++)

if(*str!='\0') /*字符串结束标志 */

{ if(i)

temp=j|ddram2; /* DDRAM

**行地址 */

else

temp=j|ddram1; /* DDRAM

**行地址 */

write_com(temp); /* 写“指

令” */

write_data(*str++); /* 写“数据”

*/

}

else

{ if(i)

temp=j|ddram2;

/* DDRAM **行地址 */

else

temp=j|ddram1;

/* DDRAM **行地址 */

write_com(temp);

/* 写“指令” */

write_data(0xfe);

/* 写“数据” */

}

delay(time); /* 延时 */

}

/********* 设置参数函数 *********/

void set(void)

{ delay(15); /* 延时 15.20800 ms */

write_com(0x38); //Function set

N=1(2line),F=0(5*8 dot)

delay(5);

write_com(0x0C); //display on

delay(5);

write_com(0x01); //clear display

delay(10);

write_com(0x06); //entry set

delay(5);

}

/********* 主函数 *********/

void main() /* 子程序里

有延时1s */

{ set(); /* 设置参数 */

write_com(clear); /* 清屏 */

{ char_fill_disp(0xff); /* 全屏 */

write_com(clear); /* 清屏 */

string_disp(0,1,string1);

/* **行显示字符串string1 */

string_disp(1,0,string2);

/* **行显示字符串string2 */

write_com(clear); /* 清屏 */

char_fill_disp(0x31); /* 显示“1”*/

CGFUNTION(CGF);

/* 建立自定义字符 */

write_com(clear); /* 清屏 */

char_fill_disp(0x00);

/* 显示“隔行” */

char_fill_disp(0x02);

/* 显示“隔列” */

char_fill_disp(0x04);

/* 显示“隔点” */

char_fill_disp(0xff); /* 全屏 */

write_com(clear); /* 清屏 */

string_disp(0,3,string3);

/* **行显示字符串string3 */

string_disp(1,0,string4);

/* **行显示字符串string4 */

write_com(clear); /* 清屏 */

string_disp(0,2,string5);

/* **行显示字符串string5:*/

string_disp(1,8,string6); /* **行显

示字符串string6: 2012 年10 月*/

delay(time);

}

}