深圳市英銳恩科技有限公司:臺灣麥肯單片機(Micon MDT單片機)亞太地區A級代理商
QLdsPIC3]LCD12864[C30+dsPIC30F6014A]
//實驗目的:熟悉12864LCD的使用
//12864LCD帶中文字庫
//編程讓12864LCD顯示公司LOGO和公司名字
//硬件設置:
//關斷所有撥碼開關。
#include //dsPIC30F6014標準頭文件
_FOSC(CSW_FSCM_OFF & XT_PLL4); //4倍頻晶振,Failsafe 時鐘關閉
_FWDT(WDT_OFF); //關閉看門狗定時器
_FBORPOR(PBOR_OFF & MCLR_EN); //掉電復位禁止,MCLR復位使能。
_FGS(CODE_PROT_OFF); //代碼保護禁止
#define di LATBbits.LATB4 //命令/數據選擇
#define rw LATBbits.LATB5 //讀/寫選擇
#define e LATBbits.LATB6 //使能
#define psb LATBbits.LATB2 //8位/4位并口選擇
#define rst LATBbits.LATB7 //復位
unsigned int __attribute__((address(0x900))) temp;
unsigned int __attribute__((address(0x902))) temp1;
const unsigned char TAB1[ ]={ //定義顯示圖片的數據表
0X7F,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF, 0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFE,
0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF, 0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X01,
0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF, 0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X01,
0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF, 0X00,0X81,0X00,0X10,0X40,0X00,0X02,0X41,
0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF, 0X00,0X81,0X00,0X10,0X40,0X00,0X02,0X41,
0XFF,0XFF,0XC0,0X00,0X00,0X7F,0XFF,0XFF, 0X07,0XE2,0X00,0XFE,0XC0,0X00,0X06,0X01,
0XFF,0XFF,0X00,0X00,0X00,0X1F,0XFF,0XFF, 0X01,0X87,0XE0,0X00,0X80,0X03,0XFF,0XF1,
0XFF,0XFC,0X00,0X00,0X00,0X07,0XFF,0XFF, 0X01,0X04,0X00,0X44,0XFC,0X02,0X04,0X01,
0XFF,0XF0,0X00,0X00,0X00,0X07,0XFF,0XFF, 0X01,0X08,0X00,0X48,0X04,0X02,0X04,0X41,
0XFF,0XF0,0X00,0X00,0X00,0X03,0XFF,0XFF, 0X0F,0XD8,0X00,0X08,0X04,0X03,0XF6,0X81,
0XFF,0XF0,0X00,0X00,0X00,0X01,0XFF,0XFF, 0X08,0X40,0X01,0XFE,0XFC,0X06,0X27,0X01,
0XFF,0XF0,0X00,0X00,0X00,0X01,0XFF,0XFF, 0X0F,0XCF,0X80,0X01,0X00,0X04,0X23,0X01,
0XFF,0XF0,0X3F,0XF0,0X3F,0XC1,0XFF,0XFF, 0X08,0XC1,0X00,0X01,0X00,0X04,0XE7,0X21,
0XFF,0XF0,0X7F,0XE0,0X7F,0XC1,0XFF,0XFF, 0X10,0X82,0X01,0XF9,0XF8,0X08,0X99,0XA1,
0XFF,0XF0,0X7F,0XE0,0X7F,0XC1,0XFF,0XFF, 0X1F,0X86,0X01,0X09,0X00,0X08,0X10,0XE1,
0XFF,0XF0,0XFF,0XC0,0XFF,0XC1,0XFF,0XFF, 0X06,0X04,0X01,0X09,0XF0,0X00,0X00,0X01,
0XFF,0XF0,0XFF,0XC1,0XFF,0XC1,0XFF,0XFF, 0X04,0X08,0X01,0XFA,0X00,0X07,0XFF,0X01,
0XFF,0XF0,0XFF,0X83,0XFF,0XC1,0XFF,0XFF, 0X7F,0XD0,0X03,0X12,0X00,0X06,0X89,0X01,
0XFF,0XF0,0XFF,0X03,0XFF,0XC1,0XFF,0XFF, 0X04,0X10,0X83,0X13,0XF0,0X08,0X91,0X01,
0XFF,0XF0,0XFE,0X07,0XFF,0XC1,0XFF,0XFF, 0X04,0X10,0X83,0XF2,0X00,0X08,0X91,0X01,
0XFF,0XF0,0XFC,0X0F,0XFF,0XC1,0XFF,0XFF, 0X04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0,0X01,0XFF,0XFF,0XFF,0XFF, 0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X01,
0XFF,0XFF,0XC0,0X00,0X3F,0XFF,0XFF,0XFF, 0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X01,
0XFF,0XFF,0X80,0X00,0X1F,0XFF,0XFF,0XCF, 0X3E,0X83,0XE7,0X3E,0X78,0X21,0X14,0XE1,
0XFF,0XFE,0X00,0X00,0X07,0XFF,0XFC,0X3F, 0X20,0X82,0X09,0X88,0X44,0X51,0X15,0X31,
0XFF,0XFC,0X7C,0X00,0X00,0XFF,0XC0,0X7F, 0X20,0X82,0X08,0X88,0X44,0X51,0X95,0X11,
0XFF,0XFF,0XFF,0XF0,0X00,0X00,0X01,0XFF, 0X20,0X82,0X08,0X08,0X44,0X89,0X95,0X01,
0XFF,0XFF,0XFF,0XFE,0X00,0X00,0X03,0XFF, 0X20,0X82,0X08,0X08,0X44,0X89,0X55,0X01,
0XFF,0XFF,0XFF,0XFF,0X80,0X00,0X07,0XFF, 0X3E,0X83,0XE8,0X08,0X44,0X89,0X55,0X01,
0XFF,0XFF,0XFF,0XFF,0XE0,0X00,0X1F,0XFF, 0X20,0X82,0X08,0X08,0X78,0X89,0X55,0X01,
0XFF,0XFF,0XFF,0XFF,0XFC,0X00,0XFF,0XFF, 0X20,0X82,0X08,0X08,0X48,0X89,0X55,0X01,
0XFF,0XFF,0XFF,0XFF,0XFE,0X03,0XFF,0XFF, 0X20,0X82,0X08,0X08,0X48,0X89,0X35,0X01,
0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF, 0X20,0X82,0X08,0X88,0X44,0X51,0X35,0X11,
0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF, 0X20,0X82,0X09,0X88,0X44,0X51,0X15,0X31,
0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF, 0X3E,0XF3,0XE7,0X08,0X44,0X21,0X14,0XE1,
0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF, 0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X01,
0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF, 0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X01,
0X7F,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF, 0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFE,
};
unsigned int lcd_x; //X地址
unsigned int lcd_y; //Y地址
// bit busy; //忙標志位
void init(); //系統初始函數
void lcd_init(); //LCD初始函數
void clear_p(); //清屏函數
void han_wr2a(); //寫函數
void wr_zb(); //寫準備函數
void qushu(); //查表函數
void send_d(unsigned char x); //寫一字節數據函數
void send_i(unsigned char x); //寫一字節命令函數
void chk_busy(); //查LCD忙函數
void delay();
//-------------------------------------------
int main(void)
{
init(); //系統初始化
lcd_init(); //LCD初始化
clear_p(); //清除顯示
qushu(); //寫數據到LCD顯示
while(1);
}
//-------------------------------------------
void init()
{
TRISB=0X0000; //設置B口為輸出
TRISD=0X0000; //設置D口為輸出
}
//-------------------------------------------
void lcd_init()
{
rst=0; //復位LCD
delay();
rst=1; //LCD正常工作
delay();
psb=1; //8位并行通信
send_i(0x30); //基本操作指令
send_i(0x01); //關顯示
send_i(0x06); //指定在寫入或讀取時,光標的移動方向
send_i(0x0c); //開顯示,關光標,不閃爍
}
//-------------------------------------------
void wr_zb()
{
send_i(0x34); //擴展操作指令
send_i(lcd_y); //設置Y地址
send_i(lcd_x); //設置X地址
send_i(0x30); //基本操作指令
}
//-------------------------------------------
void clear_p()
{
send_i(0x1); //清除顯示
send_i(0x34); //擴展操作指令
send_i(0x30); //基本操作指令
}
//------------------------------------------
void qushu()
{
unsigned char i,j,k; //定義臨時變量
temp=0; //查表偏移地址
lcd_x=0x0080; //上半屏X地址80H
for(i=0;i<2;i++) //共兩屏
{
lcd_y=0x0080; //每一行的Y地址80H(即頂格顯示)
for(j=0;j<32;j++) //每半屏共20H行
{
wr_zb(); //寫準備(設置X地址和Y地址)
for(k=0;k<16;k++) //每一行16字節(因為頂格寫)
{
temp1=TAB1[temp]; //查表獲取顯示數字
send_d(temp1); //送LCD顯示
temp++; //查表偏移地址+1
}
lcd_y++; //送下一行數據
send_i(0x0036); //擴展指令操作:圖形顯示開
send_i(0x0030); //基本指令操作
}
lcd_x=0X0088; //下半屏X地址
}
}
//-------------------------------------------
void send_d(unsigned char x)
{
chk_busy(); //判斷LCD是否忙
di=1; //總線上是數據
delay();
rw=0; //寫操作
delay();
PORTD=x; //數據送總線
e=1; //使能
delay();
e=0;
delay();
}
//--------------------------------------------
void send_i(unsigned char x)
{
chk_busy(); //判斷忙信號
di=0; //總線上是命令
delay();
rw=0; //寫操作
delay();
PORTD=x; //數據送總線
e=1; //使能
delay();
e=0;
delay();
}
//-------------------------------------------
void chk_busy()
{
// busy=1; //先置忙信號
TRISD=0Xffff; //更改總線方向為輸入
di=0; //總線上為命令
delay();
rw=1; //讀操作
delay();
while(PORTDbits.RD7)
{
delay();
delay();
e=1;
delay();
delay();
e=0;
}
e=0;
TRISD=0X0000; //總線還原為輸出
}
//-------------------------------------------
void delay()
{
int m;
for(m=0;m<10;m++)
{;}
}