新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設計應用 > 12232點陣屏驅動程序

12232點陣屏驅動程序

作者: 時間:2016-11-18 來源:網絡 收藏
#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讀

本文引用地址:http://m.butianyuan.cn/article/201611/315570.htm

#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 //動態(tài)驅動
#define adc_select0xA0//clockwise
#define clk32 0xA9//刷新時鐘設置1/32
#define clk16 0xA8//刷新時鐘設置1/16
#define reset 0xE2//軟件復位

////新的驅動著重在簡化代碼,這樣外部可調用的函數為:系統(tǒng)初始化,清屏,判忙標志////
////ASCII和漢字混合輸出函數,RAM buffer數據顯示輸出(一般用于輸出數字)//////////
#include
#include


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

uchar dot_buffer[32];//點陣緩存區(qū)
uchar disp_buffer[4]; //ram數據顯示緩存區(qū)

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)
//函數說明:發(fā)指令instruction到主窗口(內函數,私有,用戶不能直接調用)
////////////////////////////////////////////////////////////////////////////////
void send_mi(uchar instruction)
{
clr_E2(); //關S
set_E1(); //開M
wait_ready();
clr_A0(); //指令
clr_WR(); //寫觸發(fā)
NOP();
lcd_data = instruction; //指令碼
NOP();
clr_E1(); //關M
}

////////////////////////////////////////////////////////////////////////////////
//調用方式:void OutMD(uchar i)
//函數說明:發(fā)數據data到主窗口(內函數,私有,用戶不能直接調用)
////////////////////////////////////////////////////////////////////////////////
void send_md(uchar c)
{
clr_E2(); //關S
set_E1(); //開M
wait_ready();
set_A0(); //數據
clr_WR(); //寫觸發(fā)
NOP();
lcd_data = c; //數據
NOP();
clr_E1(); //關M
}

////////////////////////////////////////////////////////////////////////////////
//調用方式:void send_si(uchar instruction)
//函數說明:發(fā)指令instruction到從窗口(內函數,私有,用戶不能直接調用)
////////////////////////////////////////////////////////////////////////////////
void send_si(uchar instruction)
{
clr_E1(); //關M
set_E2(); //開S
wait_ready();
clr_A0(); //指令
clr_WR(); //寫觸發(fā)
NOP();
lcd_data=instruction; //指令碼
NOP();
clr_E2(); //關S
}

////////////////////////////////////////////////////////////////////////////////
//調用方式:void send_sd(uchar data)
//函數說明:發(fā)數據data到從窗口(內函數,私有,用戶不直接調用)
////////////////////////////////////////////////////////////////////////////////
void send_sd(uchar c)
{
clr_E1();
set_E2();
wait_ready();
set_A0(); //數據
clr_WR(); //寫觸發(fā)
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狀態(tài),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);//動態(tài)驅動
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)
//函數說明:在右頁(從窗口)當前地址畫一個字節(jié)(8點)
////////////////////////////////////////////////////////////////////////////////
void putchar_l(uchar c)
{
send_md(c);
}

////////////////////////////////////////////////////////////////////////////////
//調用方式:void putchar_r(uchar c)
//函數說明:在左頁(主窗口)當前地址畫一個字節(jié)(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個字節(jié)
{
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個的十進制數字//修改緩沖區(qū)的大小可以擴展顯示
////////////////////////////////////////////////////////////////////////////////
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字符與漢字內碼的區(qū)別在于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 { //查找定位當前漢字的點陣區(qū)
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狀態(tài)時為輸入)
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);
}



評論


技術專區(qū)

關閉