标题:
基于LPC2131的OCMJ4X8C 液晶驱动程序
[打印本页]
作者:
wudawei
时间:
2015-1-1 17:32
标题:
基于LPC2131的OCMJ4X8C 液晶驱动程序
延时大就非常稳定,但是执行速度却下来了,延时小却不大稳定。最后终于把延时时间给调的合适而使其达到执行速度快而且稳定的效果。注意由于LPC2131是3.3V的电压,而OCMJ4X8C是5V供电,因此两者最好共地,在条件允许的情况下,可以给LPC2131和OCMJ4X8C的接口加一个3.3V转5V的电平转换电路,使其更加稳定,不加亦可。
//=====================================================================
// OCMJ4X8C 液晶驱动程序(串行)
//硬件连接: CS --P0^4;
// STD --P0^5;
// SCLK--P0^6;
// PSB --VSS;
// RST --VDD;
// VDD--逻辑电源(+5V)
// VSS--GND(0V)
//ocm4x8c(串).c
//writer:谷雨 2008年8月2日于EDA实验室
//说明: 修改端口的时候只需修改定义口以及函数void Lcd_IO_Inti (void)即可
//=====================================================================
#include "config.h"
#define comm 0
#define dat 1
#define x1 0x80
#define x2 0x88
#define y 0x80
//**************************修改硬件时要修改的部分********************************
/*定义CS控制*/
#define Lcd_CS 4 //串行口使能,高电平有效(作RS时,1为数据,0为指令)
#define Set_CS() IO0SET =1<<Lcd_CS
#define Clr_CS() IO0CLR =1<<Lcd_CS
/*定义STD控制*/
#define Lcd_STD 5 //串口数据(作R/W时,1为读,0为写)
#define Set_STD() IO0SET =1<<Lcd_STD
#define Clr_STD() IO0CLR =1<<Lcd_STD
/*定义SCLK控制*/
#define Lcd_SCLK 6 //串口时钟,高电平有效
#define Set_SCLK() IO0SET =1<<Lcd_SCLK
#define Clr_SCLK() IO0CLR =1<<Lcd_SCLK
//**************************函数定义********************************
static void Delay_1(uint32 time);
static void wr_lcd (uint8 dat_comm,uint8 content);
extern void Lcd_IO_Inti(void);
extern void Lcd_Inti(void);
extern void set_position(uint8 xx,uint8 yy);
extern void send_str(uint8 const *cc);
extern void send_disp (uint8 const *img);
extern void send_img1(uint8 const *img);
extern void lat_disp (uint8 data1,uint8 data2);
extern void con_disp (uint8 data1,uint8 data2,uint8 x0,uint8 y0,uint8 xl,uint8 yl);
extern void con_disp_line (uint8 line);
extern void clear (void);
extern void clear_line(uint8 line);
extern void clear_point(uint8 line,uint8 row);
//======================================================
// 函数名称 : Delay_1()
// 函数功能 : 1微秒延时
// 入口参数 : time 延时的毫秒数
// 出口参数 : 无
//======================================================
void Delay_1(uint32 time)
{
while(time--);
}
//=================================================================
// 函数名称 :void Lcd_IO_Inti (void)
// 函数功能 :实现lcd IO 口初始化
// 入口参数 :无
// 出口参数 :无
//=================================================================
void Lcd_IO_Inti(void)
{
PINSEL0&=(~0x3f00);
IO0DIR|= (1<<Lcd_CS) | (1<<Lcd_STD) | (1<<Lcd_SCLK); //设置Lcd_CS,Lcd_STD,Lcd_SCLK为输出
IO0CLR = (1<<Lcd_CS) | (1<<Lcd_STD) | (1<<Lcd_SCLK); //Lcd_CS,Lcd_STD,Lcd_SCLK置低消除影响
}
//=================================================================
// 函数名称 :void Lcd_Inti(void)
// 函数功能 :实现lcd初始化
// 入口参数 :无
// 出口参数 :无
//=================================================================
void Lcd_Inti(void)
{
wr_lcd (comm,0x30); //30---基本指令动作
wr_lcd (comm,0x01); //清屏,地址指针指向00H
Delay_1 (0xffff); //清屏需较长时间
wr_lcd (comm,0x06); //光标的移动方向
wr_lcd (comm,0x0c); //显示打开,光标关,反白关
}
//=================================================================
//函数名:void set_position(uint8 xx,uint8 yy)
//函数功能: 设定显示坐标
//入口参数:xx yy
// xx:0~3 yy:0~7
//出口参数:无
//=================================================================
void set_position(uint8 xx,uint8 yy) //坐标
{
uint8 line;
wr_lcd (comm,0x30); //30---基本指令动作
switch(xx)
{
case 0:line=0x00;break;
case 1:line=0x10;break;
case 2:line=0x08;break;
case 3:line=0x18;break;
default:break;
}
wr_lcd(comm,0x80+line+yy); //设定地址
}
//=================================================================
//函数名:void send_str(uint8 *cc)
//函数功能: 显示汉字或是字符
//入口参数:*cc
//出口参数:无
//注意:注意为全角字符,即每个字符占十六个字节
// 显示汉字或者字符时先设定显示坐标,最多显示8个汉字
//=================================================================
void send_str(uint8 const *cc)
{
uint8 i;
wr_lcd (comm,0x30);
for(i=0;(i<16)&&*(cc+i);i++)
wr_lcd(dat,*(cc+i));
}
//=================================================================
//函数名:void send_disp (uint8 const *img)
//函数功能: 显示图形
//入口参数:uchar code *img
//出口参数:无
//注意:显示图形时先写第一行数据,依次向后
//=================================================================
void send_disp (uint8 const *img)
{
uint8 i,j;
for(j=0;j<32;j++) //上半屏写入图形数据
{
for(i=0;i<8;i++)
{
wr_lcd (comm,0x34); //8位控制端口,选择扩充指令集
wr_lcd (comm,y+j); //选择图形区Y轴坐标
wr_lcd (comm,x1+i); //选择图形区X轴坐标
wr_lcd (comm,0x30); //选择基本指令集
wr_lcd (dat,img[j*16+i*2]); //写图形数据
wr_lcd (dat,img[j*16+i*2+1]);
}
}
for(j=32;j<64;j++) //下半屏写入图形数据
{
for(i=0;i<8;i++)
{
wr_lcd (comm,0x34);
wr_lcd (comm,y+j-32);
wr_lcd (comm,x2+i);
wr_lcd (comm,0x30);
wr_lcd (dat,img[j*16+i*2]);
wr_lcd (dat,img[j*16+i*2+1]);
}
}
wr_lcd (comm,0x36); //写入数据后选择图形显示
}
//=================================================================
//函数名:void send_img1(uint8 const *img)
//函数功能: 下半屏显示图形
//入口参数:uchar code *img
//出口参数:无
//=================================================================
void send_img1(uint8 const *img)
{
uint8 i,j;
for(j=0;j<32;j++)
{
for(i=0;i<8;i++)
{
wr_lcd (comm,0x34);
wr_lcd (comm,y+j);
wr_lcd (comm,x2+i);
wr_lcd (comm,0x30);
wr_lcd (dat,img[j*16+i*2]);
wr_lcd (dat,img[j*16+i*2+1]);
}
}
wr_lcd (comm,0x36);
}
//=================================================================
//函数名:void lat_disp (uint8 data1,uint8 data2)
//函数功能:显示点阵
//入口参数:uchar data1 奇数行显示的点阵
// uchar data2 偶数行显示的点阵
//出口参数:无
//=================================================================
void lat_disp (uint8 data1,uint8 data2)
{
uint8 i,j,k,x;
x=x1;
for(k=0;k<2;k++)
{
for(j=0;j<16;j++)
{
for(i=0;i<8;i++)
{
wr_lcd (comm,0x34); //8位控制端口,选择扩充指令集
wr_lcd (comm,y+j*2); //Y轴
wr_lcd (comm,x+i); //X轴
wr_lcd (comm,0x30); //选择基本指令集
wr_lcd (dat,data1); //写入数据
wr_lcd (dat,data1);
}
for(i=0;i<8;i++)
{
wr_lcd (comm,0x34);
wr_lcd (comm,y+j*2+1);
wr_lcd (comm,x+i);
wr_lcd (comm,0x30);
wr_lcd (dat,data2);
wr_lcd (dat,data2);
}
}
x=x2;
}
wr_lcd (comm,0x36); //写入数据后选择显示
}
//=================================================================
//函数名:void con_disp (uchar data1,uchar data2,uchar x0,uchar y0,uchar xl,uchar yl)
//函数功能:反白显示
//入口参数:data1,data2,x0,y0,x1,y1
// 当data1=0xff,data2=0xff时,在x0,y0处开始反白显示区域为16*xl*yl.
//出口参数:无
//=================================================================
void con_disp (uint8 data1,uint8 data2,uint8 x0,uint8 y0,uint8 xl,uint8 yl)
{
uint8 i,j;
for(j=0;j<yl;j++)
{
for(i=0;i<xl;i++)
{
wr_lcd (comm,0x34);
wr_lcd (comm,y0+j);
wr_lcd (comm,x0+i);
wr_lcd (comm,0x30);
wr_lcd (dat,data1);
wr_lcd (dat,data2);
}
}
wr_lcd (comm,0x36);
}
//=================================================================
//函数名:void con_disp_line (uint8 line)
//函数功能:选择某行反白显示
//入口参数:line
// line为1或者2,1选择1、3行反白显示,2选择2、4行反白显示
//出口参数:无
//=================================================================
void con_disp_line (uint8 line)
{
uint8 a;
wr_lcd (comm,0x34); //8位控制端口,选择扩充指令集
switch(line)
{
case 1:a=0x04;break;
case 2:a=0x05;break;
default:break;
}
wr_lcd (comm,a); //反白选择
Delay_1(0xffff);
wr_lcd (comm,0x36); //显示
}
//=================================================================
//函数名:void clear()
//函数功能:清屏
//入口参数:无
//出口参数:无
//=================================================================
void clear (void)
{
wr_lcd (comm,0x30); //选择基本指令集
wr_lcd (comm,0x01); //清屏
Delay_1(0xffff); //延时
}
//=================================================================
//函数名:void clear_line(uint8 line)
//函数功能:清除一行
//入口参数: line 0~3
//出口参数:无
//=================================================================
void clear_line(uint8 line) //清除一行(0~3行)
{
uint8 i;
set_position(line,0);
for(i=0;i<16;i++)
wr_lcd (dat,0x20);//填充空格
}
//=================================================================
//函数名:void clear_point(uint8 line,uint8 row)
//函数功能:清除一点
//入口参数: line 选择的行
// row 选择的列
//出口参数:无
//=================================================================
void clear_point(uint8 line,uint8 row)
{
set_position(line,row);
wr_lcd (dat,0x20);//填充空格
wr_lcd (dat,0x20);
}
//=================================================================
// 函数名称 :void wr_lcd (uint8 dat_comm,uint8 content)
// 函数功能 :向lcd中写数据或者指令
// 入口参数 :dat_comm 选择是写数据或者写指令位
// content dat_comm为1时写数据,否则写指令
// 出口参数 :无
//=================================================================
void wr_lcd (uint8 dat_comm,uint8 content)
{
uint8 i,j;
Clr_SCLK();
Set_CS();
Delay_1(1);
Set_STD();
for(i=0;i<5;i++) //写入5个1,作为启动位
{
Set_SCLK();
Delay_1(1);
Clr_SCLK();
Delay_1(1);
}
Clr_STD();
Set_SCLK();
Delay_1(1);
Clr_SCLK();
Delay_1(1);
if(dat_comm) //判断写数据还是指令
{
Set_STD(); //data
}
else
{
Clr_STD(); //command
}
Set_SCLK();
Delay_1(1);
Clr_SCLK();
Delay_1(1);
Clr_STD(); //写入1个0
Set_SCLK();
Delay_1(1);
Clr_SCLK();
Delay_1(1);
for(j=0;j<2;j++)
{
for(i=0;i<4;i++) //分别写入高四位和低四位
{
if(content&0x80)
{
Set_STD();
}
else
{
Clr_STD();
}
Set_SCLK();
Delay_1(1);
Clr_SCLK();
Delay_1(1);
content<<=1;
}
Clr_STD();
for(i=0;i<4;i++) //写入4个0
{
Set_SCLK();
Delay_1(1);
Clr_SCLK();
Delay_1(1);
}
}
Clr_CS();
Delay_1(1);
}
uint8 const tab5[]={
/*-- 宽度x高度=128x64 --*/
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x80,0x00,0x0F,0xFF,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x0C,0x43,0x01,0x80,0x00,0x7F,0xFF,0xF0,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x19,0xFF,0x07,0x00,0x07,0xFF,0xFF,0xFE,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x01,0x3F,0xFC,0x1E,0x00,0x1F,0xFF,0xFF,0xFF,0x80,0x00,0x00,0x00,
0x00,0x00,0x00,0x01,0xBF,0xFF,0xFC,0x00,0x7F,0xFC,0x00,0x7F,0xC0,0x00,0x00,0x00,
0x00,0x00,0x00,0x01,0xFF,0xFF,0xF0,0x00,0xFF,0xC0,0x00,0x0F,0xE0,0x00,0x00,0x00,
0x00,0x00,0x00,0x01,0xFF,0xFF,0xE0,0x03,0xFF,0xFF,0xFC,0x01,0xF0,0x00,0x00,0x00,
0x00,0x00,0x00,0x01,0xDF,0xFF,0xC0,0x07,0xFF,0xFF,0xFF,0x80,0xF0,0x00,0x00,0x00,
0x00,0x00,0x00,0x03,0xFF,0xFF,0x00,0x0F,0xFF,0xFF,0xFF,0xE0,0x38,0x00,0x00,0x00,
0x00,0x00,0x00,0x07,0xFF,0xF8,0x00,0x1F,0xFF,0xF0,0x03,0xF8,0x38,0x00,0x00,0x00,
0x00,0x00,0x00,0x07,0xFF,0xE0,0x00,0x3F,0xFF,0xFF,0xC0,0x7C,0x18,0x00,0x00,0x00,
0x00,0x00,0x00,0x04,0x7F,0xF0,0x00,0x3F,0xFF,0xFF,0xF8,0x1E,0x08,0x00,0x00,0x00,
0x00,0x00,0x00,0x01,0xFF,0xF8,0x00,0x7F,0xFF,0xFF,0xFE,0x0F,0x08,0x00,0x00,0x00,
0x00,0x00,0x00,0x0F,0xFF,0xFC,0x00,0xFF,0xFF,0xFF,0xFF,0x87,0x08,0x00,0x00,0x00,
0x00,0x00,0x00,0x1F,0xFF,0xFE,0x00,0xFF,0xFF,0xFF,0xFF,0x83,0x88,0x00,0x00,0x00,
0x00,0x00,0x00,0x3F,0xFF,0xFE,0x01,0xFF,0xFF,0xFF,0xFF,0xC3,0x88,0x00,0x00,0x00,
0x00,0x00,0x00,0x7F,0xFF,0xFE,0x01,0xFF,0xFF,0xFF,0xFF,0xE1,0x88,0x00,0x00,0x00,
0x00,0x00,0x00,0xFF,0xFF,0xFE,0x03,0xFF,0xFF,0xFF,0xFF,0xE1,0x88,0x00,0x00,0x00,
0x00,0x00,0x01,0xFF,0xFF,0xFE,0x03,0xFF,0xFF,0xFF,0xFF,0xF1,0x88,0x00,0x00,0x00,
0x00,0x00,0x01,0xFF,0xFF,0xFE,0x03,0xFF,0xFF,0xFF,0xFF,0xF3,0x08,0x00,0x00,0x00,
0x00,0x00,0x01,0xFF,0xFF,0xFF,0x07,0xFF,0xFF,0xFF,0xFF,0xF2,0x10,0x00,0x00,0x00,
0x00,0x00,0x01,0xFF,0xFF,0xFF,0x8F,0xFF,0xFF,0xFF,0xFF,0xF0,0x20,0x00,0x00,0x00,
0x00,0x00,0x01,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xF0,0x00,0x00,0x00,0x00,
0x00,0x00,0x01,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x70,0x00,0x00,0x00,0x00,
0x00,0x00,0x01,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFE,0xFF,0x70,0x00,0x00,0x00,0x00,
0x00,0x00,0x01,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFE,0xFF,0x70,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0x7F,0xFF,0x7F,0x20,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xBF,0xFF,0x7F,0x20,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x7F,0xFF,0xFF,0xFF,0xFF,0xDF,0xFF,0x7E,0x20,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x3F,0xFF,0xFF,0xFF,0xFF,0xDF,0xFF,0x3E,0x40,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x1F,0xFF,0xFF,0xFF,0xFF,0xCF,0xFF,0x3C,0x40,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x0F,0xFF,0xFF,0xFF,0xFF,0xCF,0xFE,0x38,0x40,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x07,0xFF,0xFF,0xFF,0xFF,0x8F,0xFE,0x38,0x40,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x01,0xFF,0xFF,0xFF,0xFF,0x8F,0xFE,0x30,0x40,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,0x1F,0x7C,0x20,0x40,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x7F,0xFF,0xFF,0xFE,0x1E,0x78,0x00,0x40,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x3F,0xFF,0xFF,0xFE,0x1E,0xF0,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x1F,0xFF,0xFF,0xDA,0x3C,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x07,0xFF,0xFF,0xF2,0x30,0x80,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x01,0xFF,0xFF,0xF1,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0xFE,0x7F,0xF0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x7C,0x3F,0xF0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x78,0x1F,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x72,0x07,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x70,0x00,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x60,0x00,0x78,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x60,0x00,0x60,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0xC0,0x03,0xF0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x01,0x40,0x04,0xD8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x01,0x00,0x01,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x02,0x0C,0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x02,0x40,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x0C,0x48,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x38,0x41,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x0F,0xE2,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
};
//=================================================================
//函数名: int main (void)
//函数功能: 主函数,用于测试,无实际意义
//入口参数: 无
//出口参数:无
//=================================================================
int main (void)
{
uint8 const tab[]={"中华人名共和国"};
Delay_1(0x1fffff);
Lcd_IO_Inti(); //初始化
Lcd_Inti();
while(1)
{
clear();
lat_disp (0x00,0x00);
Delay_1(0x1fffff);
set_position(0,0); //显示汉字
send_str(tab);
set_position(1,0);
send_str(tab);
set_position(2,0);
send_str(tab);
Delay_1(0x1fffff);
clear_point(0,1); //清除一点
Delay_1(0x1fffff);
con_disp (0xff,0xff,0x8c,0x80,2,16); //区域反白显示
Delay_1(0x6fffff);
con_disp_line(2); //行反白显示
Delay_1(0x6fffff);
clear_line(1); //清除一行
Delay_1(0x6fffff);
clear();
Delay_1(0x1fffff);
lat_disp (0xcc,0xcc); //显示点阵
Delay_1(0x1fffff);
clear();
lat_disp (0x00,0x00);
Delay_1(0x1fffff);
clear();
lat_disp (0xff,0x00); //显示点阵
Delay_1(0x1fffff);
clear();
send_disp(tab5); //显示图像
Delay_1(0x6fffff);
}
return 0;
}
复制代码
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1