您的位置 首页 IC

12232点阵屏驱动程序

#includereg51.h>//功能引脚定义//#defineA0BIT(PC6)//数据1/命令0选择sbitA0=P1^4;//#defineWRBIT(PC5)//读1/写0sbit_

#include
//功用引脚界说
//#define A0 BIT(PC6)//数据1/指令0挑选
sbit A0 = P1^4;
//#define WR BIT(PC5)//读1/写0
sbit _WR = P1^3;
//#define E1 BIT(PC4)//片选1(Master)
sbit E1 = P1^1;
//#define E2 BIT(PC7)//片选2(slave)
sbit E2 = P1^2;
//#define lcd_data PORTA //数据
#define lcd_data P0

//常用操作宏界说
#define set_E1() (E1=1) //1片选M
#define set_E2() (E2=1) //1片选S
#define set_A0() (A0=1) //1数据
#define set_WR() (_WR=1) //1读

#define clr_E1() (E1=0) //0
#define clr_E2() (E2=0) //0
#define clr_A0() (A0=0) //0指令
#define clr_WR() (_WR=0) //0写

//液晶显现操控指令表
#define disp_off0xAE//显现封闭
#define disp_on 0xAF//显现翻开
#define disp_start_line 0xC0//显现开端地址(后5位-表明0-31行)
#define page_addr_set0xB8//页地址设置(0~3)
#define col_addr_set0x00//列地址设置(0~61)
#define status_busy0x80//0=ready
#define mode_write 0xEE //写形式
#define dynamic_driver 0xA4 //动态驱动
#define adc_select0xA0//clockwise
#define clk32 0xA9//改写时钟设置1/32
#define clk16 0xA8//改写时钟设置1/16
#define reset 0xE2//软件复位

////新的驱动着重在简化代码,这样外部可调用的函数为:体系初始化,清屏,判忙标志////
////ASCII和汉字混合输出函数,RAM buffer数据显现输出(一般用于输出数字)//////////
#include
#include

#define uchar unsigned char
#define uint unsigned int
#define NOP() _nop_();

uchar dot_buffer[32];//点阵缓存区
uchar disp_buffer[4]; //ram数据显现缓存区

void lcd_init(void);//LCD初始化
void lcd_clr(void); //LCD清屏
void wait_ready(void);//等候ready
void draw_bmp(uchar col,uchar layer,uchar width,uchar *bmp);
//点阵码显现输出
void disp_one_ascii(uchar col,uchar layer,uchar ascii_code,uchar mode);
//单个ascci码输出(ascii_code为ascii编码)
void disp_ram_data(uchar col,uchar layer,uchar n,uchar mode);
//ram数据(数字)显现输出
void dprintf(uchar col,uchar layer,uchar *buf,uchar mode);
//通用混合字串显现

typedef struct data_gb16//汉字字模数据结构
{
uchar index[2];
uchar zimo[32];
};

struct data_gb16 code hz16[] =
{
“延”,0x00,0x84,0xC4,0xA4,0x94,0x8C,0x00,0xE4,
0x04,0x04,0xFC,0x42,0x63,0x42,0x00,0x00,
0x80,0x44,0x28,0x10,0x2C,0x43,0x40,0x4F,
0x48,0x48,0x4F,0x48,0x4C,0x68,0x20,0x00,
“时”,0x00,0xFC,0x84,0x84,0x84,0xFE,0x14,0x10,
0x90,0x10,0x10,0x10,0xFF,0x10,0x10,0x00,
0x00,0x3F,0x10,0x10,0x10,0x3F,0x00,0x00,
0x00,0x23,0x40,0x80,0x7F,0x00,0x00,0x00
};

uchar code ascii[] = {
//前面0x20(32个)ASCII码为操控指令 //本程序中不必
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x38,0xFC,0xFC,0x38,0x00,0x00,//!
0x00,0x00,0x00,0x0D,0x0D,0x00,0x00,0x00,
0x00,0x0E,0x1E,0x00,0x00,0x1E,0x0E,0x00,//”
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x20,0xF8,0xF8,0x20,0xF8,0xF8,0x20,0x00,//#
0x02,0x0F,0x0F,0x02,0x0F,0x0F,0x02,0x00,
0x38,0x7C,0x44,0x47,0x47,0xCC,0x98,0x00,//$
0x03,0x06,0x04,0x1C,0x1C,0x07,0x03,0x00,
0x30,0x30,0x00,0x80,0xC0,0x60,0x30,0x00,//%
0x0C,0x06,0x03,0x01,0x00,0x0C,0x0C,0x00,
0x80,0xD8,0x7C,0xE4,0xBC,0xD8,0x40,0x00,//&
0x07,0x0F,0x08,0x08,0x07,0x0F,0x08,0x00,
0x00,0x10,0x1E,0x0E,0x00,0x00,0x00,0x00,//
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0xF0,0xF8,0x0C,0x04,0x00,0x00,//(
0x00,0x00,0x03,0x07,0x0C,0x08,0x00,0x00,
0x00,0x00,0x04,0x0C,0xF8,0xF0,0x00,0x00,//)
0x00,0x00,0x08,0x0C,0x07,0x03,0x00,0x00,
0x80,0xA0,0xE0,0xC0,0xC0,0xE0,0xA0,0x80,//*
0x00,0x02,0x03,0x01,0x01,0x03,0x02,0x00,
0x00,0x80,0x80,0xE0,0xE0,0x80,0x80,0x00,//+
0x00,0x00,0x00,0x03,0x03,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//,
0x00,0x00,0x10,0x1E,0x0E,0x00,0x00,0x00,
0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x00,//-
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//.
0x00,0x00,0x00,0x0C,0x0C,0x00,0x00,0x00,
0x00,0x00,0x00,0x80,0xC0,0x60,0x30,0x00,///
0x0C,0x06,0x03,0x01,0x00,0x00,0x00,0x00,
0xF8,0xFC,0x04,0xC4,0x24,0xFC,0xF8,0x00,//0
0x07,0x0F,0x09,0x08,0x08,0x0F,0x07,0x00,
0x00,0x10,0x18,0xFC,0xFC,0x00,0x00,0x00,//1
0x00,0x08,0x08,0x0F,0x0F,0x08,0x08,0x00,
0x08,0x0C,0x84,0xC4,0x64,0x3C,0x18,0x00,//2
0x0E,0x0F,0x09,0x08,0x08,0x0C,0x0C,0x00,
0x08,0x0C,0x44,0x44,0x44,0xFC,0xB8,0x00,//3
0x04,0x0C,0x08,0x08,0x08,0x0F,0x07,0x00,
0xC0,0xE0,0xB0,0x98,0xFC,0xFC,0x80,0x00,//4
0x00,0x00,0x00,0x08,0x0F,0x0F,0x08,0x00,
0x7C,0x7C,0x44,0x44,0xC4,0xC4,0x84,0x00,//5
0x04,0x0C,0x08,0x08,0x08,0x0F,0x07,0x00,
0xF0,0xF8,0x4C,0x44,0x44,0xC0,0x80,0x00,//6
0x07,0x0F,0x08,0x08,0x08,0x0F,0x07,0x00,
0x0C,0x0C,0x04,0x84,0xC4,0x7C,0x3C,0x00,//7
0x00,0x00,0x0F,0x0F,0x00,0x00,0x00,0x00,
0xB8,0xFC,0x44,0x44,0x44,0xFC,0xB8,0x00,//8
0x07,0x0F,0x08,0x08,0x08,0x0F,0x07,0x00,
0x38,0x7C,0x44,0x44,0x44,0xFC,0xF8,0x00,//9
0x00,0x08,0x08,0x08,0x0C,0x07,0x03,0x00,
0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,//:
0x00,0x00,0x00,0x06,0x06,0x00,0x00,0x00,
0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,//;
0x00,0x00,0x08,0x0E,0x06,0x00,0x00,0x00,
0x00,0x80,0xC0,0x60,0x30,0x18,0x08,0x00,//<
0x00,0x00,0x01,0x03,0x06,0x0C,0x08,0x00,
0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x00,//=
0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x00,
0x00,0x08,0x18,0x30,0x60,0xC0,0x80,0x00,//>
0x00,0x08,0x0C,0x06,0x03,0x01,0x00,0x00,
0x18,0x1C,0x04,0xC4,0xE4,0x3C,0x18,0x00,//?
0x00,0x00,0x00,0x0D,0x0D,0x00,0x00,0x00,
0xF0,0xF8,0x08,0xC8,0xC8,0xF8,0xF0,0x00,//@
0x07,0x0F,0x08,0x0B,0x0B,0x0B,0x01,0x00,
0xE0,0xF0,0x98,0x8C,0x98,0xF0,0xE0,0x00,//A
0x0F,0x0F,0x00,0x00,0x00,0x0F,0x0F,0x00,
0x04,0xFC,0xFC,0x44,0x44,0xFC,0xB8,0x00,//B
0x08,0x0F,0x0F,0x08,0x08,0x0F,0x07,0x00,
0xF0,0xF8,0x0C,0x04,0x04,0x0C,0x18,0x00,//C
0x03,0x07,0x0C,0x08,0x08,0x0C,0x06,0x00,
0x04,0xFC,0xFC,0x04,0x0C,0xF8,0xF0,0x00,//D
0x08,0x0F,0x0F,0x08,0x0C,0x07,0x03,0x00,
0x04,0xFC,0xFC,0x44,0xE4,0x0C,0x1C,0x00,//E
0x08,0x0F,0x0F,0x08,0x08,0x0C,0x0E,0x00,
0x04,0xFC,0xFC,0x44,0xE4,0x0C,0x1C,0x00,//F
0x08,0x0F,0x0F,0x08,0x00,0x00,0x00,0x00,
0xF0,0xF8,0x0C,0x84,0x84,0x8C,0x98,0x00,//G
0x03,0x07,0x0C,0x08,0x08,0x07,0x0F,0x00,
0xFC,0xFC,0x40,0x40,0x40,0xFC,0xFC,0x00,//H
0x0F,0x0F,0x00,0x00,0x00,0x0F,0x0F,0x00,
0x00,0x00,0x04,0xFC,0xFC,0x04,0x00,0x00,//I
0x00,0x00,0x08,0x0F,0x0F,0x08,0x00,0x00,
0x00,0x00,0x00,0x04,0xFC,0xFC,0x04,0x00,//J
0x07,0x0F,0x08,0x08,0x0F,0x07,0x00,0x00,
0x04,0xFC,0xFC,0xC0,0xF0,0x3C,0x0C,0x00,//K
0x08,0x0F,0x0F,0x00,0x01,0x0F,0x0E,0x00,
0x04,0xFC,0xFC,0x04,0x00,0x00,0x00,0x00,//L
0x08,0x0F,0x0F,0x08,0x08,0x0C,0x0E,0x00,
0xFC,0xFC,0x38,0x70,0x38,0xFC,0xFC,0x00,//M
0x0F,0x0F,0x00,0x00,0x00,0x0F,0x0F,0x00,
0xFC,0xFC,0x38,0x70,0xE0,0xFC,0xFC,0x00,//N
0x0F,0x0F,0x00,0x00,0x00,0x0F,0x0F,0x00,
0xF0,0xF8,0x0C,0x04,0x0C,0xF8,0xF0,0x00,//O
0x03,0x07,0x0C,0x08,0x0C,0x07,0x03,0x00,
0x04,0xFC,0xFC,0x44,0x44,0x7C,0x38,0x00,//P
0x08,0x0F,0x0F,0x08,0x00,0x00,0x00,0x00,
0xF8,0xFC,0x04,0x04,0x04,0xFC,0xF8,0x00,//Q
0x07,0x0F,0x08,0x0E,0x3C,0x3F,0x27,0x00,
0x04,0xFC,0xFC,0x44,0xC4,0xFC,0x38,0x00,//R
0x08,0x0F,0x0F,0x00,0x00,0x0F,0x0F,0x00,
0x18,0x3C,0x64,0x44,0xC4,0x9C,0x18,0x00,//S
0x06,0x0E,0x08,0x08,0x08,0x0F,0x07,0x00,
0x00,0x1C,0x0C,0xFC,0xFC,0x0C,0x1C,0x00,//T
0x00,0x00,0x08,0x0F,0x0F,0x08,0x00,0x00,
0xFC,0xFC,0x00,0x00,0x00,0xFC,0xFC,0x00,//U
0x07,0x0F,0x08,0x08,0x08,0x0F,0x07,0x00,
0xFC,0xFC,0x00,0x00,0x00,0xFC,0xFC,0x00,//V
0x01,0x03,0x06,0x0C,0x06,0x03,0x01,0x00,
0xFC,0xFC,0x00,0x80,0x00,0xFC,0xFC,0x00,//W
0x03,0x0F,0x0E,0x03,0x0E,0x0F,0x03,0x00,
0x0C,0x3C,0xF0,0xC0,0xF0,0x3C,0x0C,0x00,//X
0x0C,0x0F,0x03,0x00,0x03,0x0F,0x0C,0x00,
0x00,0x3C,0x7C,0xC0,0xC0,0x7C,0x3C,0x00,//Y
0x00,0x00,0x08,0x0F,0x0F,0x08,0x00,0x00,
0x1C,0x0C,0x84,0xC4,0x64,0x3C,0x1C,0x00,//Z
0x0E,0x0F,0x09,0x08,0x08,0x0C,0x0E,0x00,
0x80,0x80,0x80,0x80,0xe0,0xC0,0x80,0x00,//->0x5b(自界说显现字符)
0x01,0x01,0x01,0x01,0x07,0x03,0x01,0x00
//0x5c开端能够参加其他的自界说字符
};

////////////////////////////////////////////////////////////////////////////////
//调用方法:void send_mi(uchar instuction)
//函数阐明:发指令instruction到主窗口(内函数,私有,用户不能直接调用)
////////////////////////////////////////////////////////////////////////////////
void send_mi(uchar instruction)
{
clr_E2(); //关S
set_E1(); //开M
wait_ready();
clr_A0(); //指令
clr_WR(); //写触发
NOP();
lcd_data = instruction; //指令码
NOP();
clr_E1(); //关M
}

////////////////////////////////////////////////////////////////////////////////
//调用方法:void OutMD(uchar i)
//函数阐明:发数据data到主窗口(内函数,私有,用户不能直接调用)
////////////////////////////////////////////////////////////////////////////////
void send_md(uchar c)
{
clr_E2(); //关S
set_E1(); //开M
wait_ready();
set_A0(); //数据
clr_WR(); //写触发
NOP();
lcd_data = c; //数据
NOP();
clr_E1(); //关M
}

////////////////////////////////////////////////////////////////////////////////
//调用方法:void send_si(uchar instruction)
//函数阐明:发指令instruction到从窗口(内函数,私有,用户不能直接调用)
////////////////////////////////////////////////////////////////////////////////
void send_si(uchar instruction)
{
clr_E1(); //关M
set_E2(); //开S
wait_ready();
clr_A0(); //指令
clr_WR(); //写触发
NOP();
lcd_data=instruction; //指令码
NOP();
clr_E2(); //关S
}

////////////////////////////////////////////////////////////////////////////////
//调用方法:void send_sd(uchar data)
//函数阐明:发数据data到从窗口(内函数,私有,用户不直接调用)
////////////////////////////////////////////////////////////////////////////////
void send_sd(uchar c)
{
clr_E1();
set_E2();
wait_ready();
set_A0(); //数据
clr_WR(); //写触发
lcd_data = c;
NOP();
clr_E2();
}

////////////////////////////////////////////////////////////////////////////////
//等候ready:等候LCD内部操作完结
////////////////////////////////////////////////////////////////////////////////
void wait_ready(void)
{
// DDRA = 0x00; //PA口输入
clr_A0(); //指令
set_WR(); //读
NOP();
while(lcd_data & status_busy); //读入LCD状况,1=忙,一向等候LCD内部操作完结
//DDRA = 0xff; //从头置PA口输出
}

////////////////////////////////////////////////////////////////////////////////
//调用方法:void lcd_init(void)
//函数阐明:122x32LCD初始化,开机后仅调用一次
////////////////////////////////////////////////////////////////////////////////
void lcd_init(void)
{
send_mi(reset);//复位m-left,s-right
send_si(reset);

send_mi(disp_off);//封闭显现
send_si(disp_off);

send_mi(dynamic_driver);//动态驱动
send_si(dynamic_driver);

send_mi(clk32); //1/32占空比
send_si(clk32);

send_mi(adc_select);//clockwise
send_si(adc_select);

send_mi(mode_write);//写形式
send_si(mode_write);

send_mi(col_addr_set);
send_mi(disp_start_line);//归回零列,设定显现开端行首
send_si(col_addr_set);
send_si(disp_start_line);

send_mi(disp_on); //开显现
send_si(disp_on);
}

////////////////////////////////////////////////////////////////////////////////
//调用方法:void lcd_clr(void)
//函数阐明:清屏
////////////////////////////////////////////////////////////////////////////////
void lcd_clr(void)
{
uchar i, page;
for (page=0;page<4;page++)
{
send_mi(page_addr_set|page);
send_si(page_addr_set|page);
send_mi(0); //主窗口设置为0列
send_si(0); //从窗口设置为0列
for (i=0;i<62;i++)
{
send_md(0x00);
send_sd(0x00);
}
}
}

////////////////////////////////////////////////////////////////////////////////
//调用方法:void set_page(uchar page)
//函数阐明:一起设置主(右)从(左)显现页为0-3页
////////////////////////////////////////////////////////////////////////////////
void set_page(uchar page)
{
send_mi(page_addr_set|page);
send_si(page_addr_set|page);
}

////////////////////////////////////////////////////////////////////////////////
//调用方法:void SetAddress(uchar address)
//函数阐明:一起设置主(右)从(左)列地址为0-61列
////////////////////////////////////////////////////////////////////////////////
void set_address(uchar address)
{
send_mi(address&0x7F); //&0x7F,考虑到避免越限
send_si(address&0x7F);
}

////////////////////////////////////////////////////////////////////////////////
//调用方法:void putchar_l(uchar c)
//函数阐明:在右页(从窗口)当时地址画一个字节(8点)
////////////////////////////////////////////////////////////////////////////////
void putchar_l(uchar c)
{
send_md(c);
}

////////////////////////////////////////////////////////////////////////////////
//调用方法:void putchar_r(uchar c)
//函数阐明:在左页(主窗口)当时地址画一个字节(8点)
////////////////////////////////////////////////////////////////////////////////
void putchar_r(uchar c)
{
send_sd(c);
}

////////////////////////////////////////////////////////////////////////////////
//调用方法:void draw_bmp(uchar col,uchar layer,uchar width,uchar *bmp)
//函数阐明:画一个图,横坐标是col,layer表明上基层,width是图形的宽,高固定16
// bmp是图形指针
// 运用zimo21软件,选用纵向取模下高位得到bmp数据。
// col 图型的开端方位0~121
// layer 图形的方位(Y坐标)0-下半部分 非0-上半部分
// width 图形宽度8,16可选
// bmp 图形数据指针
////////////////////////////////////////////////////////////////////////////////
void draw_bmp(uchar col,uchar layer,uchar width,uchar *bmp)
{
uchar x;
uchar address; //address表明显存的物理地址
uchar p=0;
uchar page=0;
uchar window=0; //page表明上下两页,window表明左右窗口(0左,1右)
if (layer) page=2; //左-主窗口,右-从窗口

for (x=col; x {
if (x>121)return; //避免显现乱码
if (x>60)//左右窗口定位
{
window=1; //右-从窗口
address=x%61;
}
else
address=x; //主窗口输出

set_page(page); //上层数据输出
set_address(address);

if (window)
putchar_r(bmp[p]);
else
putchar_l(bmp[p]);

set_page(page+1); //基层数据输出
set_address(address); //列坚持不变

if (window)
putchar_r(bmp[p+width]);
else
putchar_l(bmp[p+width]);

p++;
}
}

////////////////////////////////////////////////////////////////////////////////
//函数:void disp_one_ascii(uchar col,uchar layer,uchar ascii_code)
//阐明:显现单个ASCII码col-列;layer-上下行:1-上,0-下;ascii_code:所要显现德ASCCII码
////////////////////////////////////////////////////////////////////////////////
void disp_one_ascii(uchar col,uchar layer,uchar ascii_code,uchar mode)
{
uchar i;
for(i=0;i<16;i++) //ASCII码显现占用16个字节
{
if(mode)dot_buffer[i]=~ascii[(ascii_code-0x20)*16 + i];
else dot_buffer[i]= ascii[(ascii_code-0x20)*16 + i];
}
draw_bmp(col,layer,8,dot_buffer);
}

////////////////////////////////////////////////////////////////////////////////
//函数:void disp_ram_data(uchar col,uchar layer,uchar n,uchar mode)
//阐明:显现小于4个的十进制数字//修正缓冲区的巨细能够扩展显现
////////////////////////////////////////////////////////////////////////////////
void disp_ram_data(uchar col,uchar layer,uchar n,uchar mode)
{
while(n–)
{
if(mode)disp_one_ascii(col,layer,disp_buffer[n]+0x30,1);
else disp_one_ascii(col,layer,disp_buffer[n]+0x30,0);
col += 8;
}
}

////////////////////////////////////////////////////////////////////////////////
//函数:void dprintf(uchar col,uchar layer,uchar *ptr,uchar mode)
//阐明:ASCII(8*16) 和 汉字(16*16)显现函数
////////////////////////////////////////////////////////////////////////////////
void dprintf(uchar col,uchar layer,uchar *ptr,uchar mode)
{
uchar c1,c2;
uchar i,j,k;
uchar ulen;
//uchar ucol,ulayer,umode;
uchar ucol,ulayer;
ulen = 0;
ucol = col;
ulayer = layer;

while (ptr[ulen]!= 0)ulen++; //勘探字串长度
i=0;
while(i{
c1 = ptr[i];
c2 = ptr[i+1];
//ASCII字符与汉字内码的差异在于128做分界;大于128的为汉字码
if(c1 <=128) //ASCII
{
if(mode)disp_one_ascii(ucol,ulayer,c1,1);
else disp_one_ascii(ucol,ulayer,c1,0);
ucol+=8;
i++; //ASCII码的处理
}
else //中文
{
for(j=0;j { //查找定位当时汉字的点阵区
if((c1 == hz16[j].index[0]) && (c2 == hz16[j].index[1]))
break;
}
for(k=0;k<32;k++)
{ if(mode)dot_buffer[k]=~hz16[j].zimo[k];
else dot_buffer[k]= hz16[j].zimo[k];
}
draw_bmp(ucol,ulayer,16,dot_buffer);
ucol+=16;
i+=2; //中文的处理
}
}
}

////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
void main(void)
{
uchar i;
uint j;
// DDRC=0xF0; //C口高四位为输出操控端口
// DDRA=0xFF; //A口为数据线(一般为输出,仅在读LCD状况时为输入)
for(i=0;i<10;i++)for(j=1;j;j++); //开机延时
lcd_init();
lcd_clr();
dprintf(10,1,”I AM CHINESE!”,0); //阐明/在up 行从第10列开端显现/正常显现
disp_ram_data(10,0,2,1); //阐明/在dowm行从第10列开端显现/反白显现
while(1);
}

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/bandaoti/266096.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部