|
/*1602LCD演示程序*/ /*單片機型號:AT89S51*/ /*晶振:11.0592MHZ*/ /*作者:http://www.51hei.com*/ /*編譯環(huán)境:Keil 7.50A*/ /****包含頭文件*****/ #include <reg51.h> #include <intrins.h> /******************端口定義可根據(jù)你的電路自行修改*******/ sbit rs=P3^5; sbit rw=P2^7; sbit cs=P3^4; sbit ep=P3^4;/*****顯示數(shù)據(jù)表*******/ unsigned char code dis1[] = {"xu han jun"}; unsigned char code dis2[] = {"best wishes"}; /******************** 函數(shù)功能:LCD延時子程序 入口參數(shù):ms 出口參數(shù): ********************/ void delay(unsigned char ms) { unsigned char i; while(ms--) { for(i = 0; i<250; i++) { _nop_(); _nop_(); _nop_(); _nop_(); } } } /******************** 函數(shù)功能:測試LCD忙碌狀態(tài) 入口參數(shù): 出口參數(shù):result **********************/ bit lcd_bz() { bit result; rs = 0; rw = 1; ep = 1; _nop_(); _nop_(); _nop_(); _nop_(); result = (bit)(P0 & 0x80); ep = 0; return result; } /***************** 函數(shù)功能:寫指令數(shù)據(jù)到LCD子程序 入口參數(shù):cmd 出口參數(shù): ***************/ void lcd_wcmd(unsigned char cmd) { while(lcd_bz());//判斷LCD是否忙碌 rs = 0; rw = 0; ep = 0; _nop_(); _nop_(); P0 = cmd; _nop_(); _nop_(); _nop_(); _nop_(); ep = 1; _nop_(); _nop_(); _nop_(); _nop_(); ep = 0; } /************************* 函數(shù)功能:設(shè)定顯示位置子程序 入口參數(shù):pos 出口參數(shù): *************************/ void lcd_pos(unsigned char pos) { lcd_wcmd(pos | 0x80); } /*********************** 函數(shù)功能:寫入顯示數(shù)據(jù)到LCD子程序 入口參數(shù):dat 出口參數(shù): ************************/ void lcd_wdat(unsigned char dat) { while(lcd_bz());//判斷LCD是否忙碌 rs = 1; rw = 0; ep = 0; P0 = dat; _nop_(); _nop_(); _nop_(); _nop_(); ep = 1; _nop_(); _nop_(); _nop_(); _nop_(); ep = 0; } /********************** 函數(shù)功能:LCD初始化子程序 入口參數(shù): 出口參數(shù): ***********************/ void lcd_init() { lcd_wcmd(0x38); delay(1); lcd_wcmd(0x0c); delay(1); lcd_wcmd(0x01); delay(1); lcd_wcmd(0x04); delay(1); } /***************** 函數(shù)功能:主程序 入口參數(shù): 出口參數(shù): ********************/ void main(void) { unsigned char i; lcd_init();// 初始化LCD delay(10); lcd_pos(0x0E);//設(shè)置顯示位置 i = 0; while(dis1[i] != '\0') { lcd_wdat(dis1[i]);//顯示字符 i++; } lcd_pos(0x4E);// 設(shè)置顯示位置 i = 0; while(dis2[i] != '\0') { lcd_wdat(dis2[i]);// 顯示字符 i++; } while(1); } |