此程序是在51单片机上实现的,能够实现的功能是现实字母、数字、汉字。汉字是自己写的库是用PCtoLCD2002软件自作的,由于1602的内存有限只自制了部分汉字,如果有需要,大家自己可以根据需要制成想要实现效果的文字。能够实现多种效果如闪烁,滚屏等效果。
代码如下:
#include <reg51.h> #include <intrins.h> sbit BF=P0^7; //?|?μ±ê????£???BF???¨ò???P0.7òy??£?í¨1y2é?ˉê?2áμ?3? sbit RS = P2^6;//êy?Y?üá???????£???RS???¨ò???P2.6 sbit RW = P2^5;//?áD′??????£???RW?¨ò??aP2.5 sbit E = P2^7;//ê1?üD?o???£???E?¨ò??aP2.7 unsigned char code dis1[] = {" WELCOME TO "};//??ê?μ?×?·? unsigned char code dis2[] = {" AN HUIDA XUE "}; unsigned char code dis3[] = {" ZHI DAOLAO SHI "}; unsigned char code dis4[] = {"OU YANGjuan juan"}; unsigned char code dis5[] = {" ZHANG TIAN GUO "}; unsigned char code dis6[] = {" 2015210475 "}; unsigned char codetable[]={"0123456789"};//??ê?è??úμ?êy×é unsigned char code hanzi[]={0x02,0x04,0x0f,0x12,0x0f,0x0a,0x1f,0x12,0x0f,0x09,0x0f,0x09,0x0f,0x09, 0x09,0x11,0x1f,0x11,0x11,0x1f,0x11,0x11,0x1f,0x00,0x00,0x0e,0x04,0xff,0x04,0x0a,0x11,0x00,0x00, 0x0e,0x0a,0x0a,0x1f,0x0a,0x0a,0x00,0x00,0x00,0x80,0x1d,0x0b,0x0e,0x00,0x00};//?ê??è?μ?×??£?? void delay(int ms) { // ?óê±×ó3ìDò?óê±è??éms int i; while(ms--) { for(i = 0; i< 250; i++) { _nop_(); _nop_(); _nop_(); _nop_(); } } } /***************************************************** oˉêy1|?ü£o?D??òo?§?£?é?|?μ×′ì? ·μ???μ£oresult£?result=1±íê??|?μ£?result=0±íê?2??| ***************************************************/ unsigned char BusyTest(void) { bit result; RS=0; //?ù?Yê?2á£?RS?aμíμ???£?RW?a??μ???£??éò??á RW=1; E=1; //E=12??êDíD′£??á£??a???μ??×÷×?±? _nop_(); //??2ù×÷ò????ú?÷?ü?ú _nop_(); _nop_(); _nop_(); //??2ù×÷?ú?÷?ü?ú??μ¥???ú·′ó|ê±?? result=BF; //???|?μ±ê?????3?μ??result E=0; //2úéú???μ??£?D′è???á? return result; //·μ???á1??a1???1?áD′£??a0?éò??áD′ } void lcd_wcmd(unsigned char cmd) // D′??á??ò??êy?Yμ?LCD { while(BusyTest()==1); //?á1??a2??ü1???DD?áD′2ù×÷ RS = 0; //D′??á? RW = 0; E = 0; _nop_(); _nop_(); P0 = cmd; //????á?D′μ?P0?ú _nop_(); _nop_(); _nop_(); _nop_(); E = 1; //ê1?ü???a??μ????a???μ??×?o?×?±? _nop_(); _nop_(); _nop_(); _nop_(); E = 0; //2úéú???μ??????á?D′è?LCD } void lcd_pos(unsigned char pos) //éè?¨LCD??ê????? { lcd_wcmd(pos | 0x80); //LCD??ê?μ??????a80H+μ??·?÷òaμúò?DD′ó80H?aê? } void lcd_wdat(unsigned char dat) //D′è?×?·?êy?Y??ê?μ?LCD { while(BusyTest()==1); //LCD?|?μ?ì2a?a1ê±2??ü1???DD?áD′?a0?éò??áD′ RS = 1; //D′êy?Y RW = 0; E = 0; P0 = dat; //??êy?Y?íè?P0?ú _nop_(); _nop_(); _nop_(); _nop_(); E = 1; //ê1?ü?????a??μ????a???μ??×?×?±? _nop_(); _nop_(); _nop_(); _nop_(); E = 0; //ê1?ü?????aμíμ?????êy?YD′è?μ?LCD } /***************************************************** oˉêy1|?ü£o??×??¨ò?μ?×?·?μ?±à??êy×éD′è?μ?CGRAM?D D′á?áù??oo×?μ?×?·?±à??·?±e?a?ê??è?ìì?aD? ***************************************************/ void write_hanzi() { unsigned char i=0,kk=0; unsigned char temp=0x40; //2ù×÷CGRAMμ?2ù×÷?? for(i=0;i<47;i++) //????×?·?óé8??×??ú×é3éóD6??×?·?Dèòa?-?·48′? { lcd_wcmd(temp+i); //2ù×÷μ?CGRAMμ??üá???+D′è?CGRAMμ?μ??· lcd_wdat(hanzi[kk]) ; //D′è?êy?Y kk++; } } void lcd_init() //LCDμ?3?ê??ˉ£?éè????ê?μ??£ê? { lcd_wcmd(0x38); //16*2??DD??ê?,5*7μ??ó,8??êy?Y delay(1); //?ó3ù1ms lcd_wcmd(0x0c); //?a??ê?,1?1a±ê delay(1); lcd_wcmd(0x06); //ò??ˉ1a±ê£?μ±??ê?ê±1a±ê×??ˉ?ó1 delay(1); lcd_wcmd(0x01); //??3yLCD??ê??úèY,1a±ê?′??μ?μ??·00H???? delay(1); } void flash() //??????ê?μ?éá?? { delay(300); //??????í£á?ê±??300ms lcd_wcmd(0x08); //1?±???ê? delay(100); //?óê±ò???ê±?? lcd_wcmd(0x0c); //?a??ê? delay(100); lcd_wcmd(0x08); //1?±???ê? delay(100); //?ó3ù lcd_wcmd(0x0c); //?a??ê? delay(100); } void main() { unsigned char i,j; lcd_init(); //LCDμ?3?ê??ˉ delay(10); //?ó3ù10ms while(1) { lcd_wcmd(0x06); //?òóòò??ˉ1a±ê lcd_pos(0x10); //òaêμ?????áóòò?±?D?éè????ê??????aμúò?DD17μ??????aê?ò2?′′ó10H?aê? i = 0; while(dis1[ i ] != '\0') { lcd_wdat(dis1[ i ]); //??ê?"WLCOME TO" i++; delay(30); //?????????×?·?????μ???ê??ù?è } lcd_pos(0x50); //éè???aμú?tDD??ê?μú17??????′ó50H?aê? i = 0; while(dis2[ i ] != '\0') { lcd_wdat(dis2[ i ]); //??ê?×?·?" AN HUI LI GONG " i++; delay(30); //?ó3ù??????ê?μ??ù?è } for(j=0;j<16;j++) //?ò×óò??ˉ16??′ó10Hó?50H?aê? { lcd_wcmd(0x18); //×?·?μ?ò??ˉ?üá??? delay(80); //????×?·?μ?ò??ˉê±?? } flash(); //éá??á?′? delay(800); //????í£?ùê±?? lcd_wcmd(0x01); //??3yLCD??ê??úèY delay(1); lcd_wcmd(0x06); //1a±ê?òóòò??ˉ lcd_pos(0); //éè????ê?μ?×?·??aμúò?DDμúò???×?·? i = 0; while(dis3[ i ] != '\0') { //??ê?×?·?" ZHI DAO LAO SHI " lcd_wdat(dis3[ i ]); i++; delay(30); //????á?×?·?????μ???ê??ù?è } lcd_pos(0x40); //éè????ê????? ?aμú?tDDμúò??? i = 0; while(dis4[ i ] != '\0') { lcd_wdat(dis4[ i ]); //??ê?×?·?"OU YANG MING SAN" i++; delay(30); //????á?×?·?????μ???ê??ù?è } flash(); delay(800); //éè??í£?ùê±?? lcd_wcmd(0x01); //??3yLCDμ???ê??úèY delay(1); lcd_wcmd(0x06); //1a±ê?òóòò??ˉ lcd_pos(0); //éè????ê??????aμúò?DDμúò??? i = 0; while(dis5[ i ] != '\0') { //??ê?×?·?" DENG CONG LONG " lcd_wdat(dis5[ i ]); i++; delay(30); //??????ê??ù?è } lcd_pos(0x40); //éè????ê??????aμú?tDDμúò??? i = 0; while(dis6[ i ] != '\0') { lcd_wdat(dis6[ i ]); //??ê?×?·?" 2015200461 " i++; } flash(); delay(800); write_hanzi(); //μ÷ó?D′oo×?×ó3ìDò?÷òa????D′è?CGRAM?D lcd_wcmd(0x01); //??3y??ê??úèY lcd_wcmd(0x80); //??ê?D′è?μ?CGRAM?D×??¨ò?×?·?μ??úèY ??á??? lcd_pos(0x00); lcd_wdat(table[2]); //μúò?DDμúò?????????ê?2 delay(30); lcd_pos(0x01); //μúò?DDμú?t????????0 lcd_wdat(table[0]); delay(30); lcd_pos(0x02); //μúò?DDμúèy????????1 lcd_wdat(table[1]); delay(30); lcd_pos(0x03); lcd_wdat(table[6]); //μúò?DDμú4????????6 delay(30); lcd_pos(0x05); lcd_wdat(0x00); //μúò?DDμú5????????ê?×??¨ò?×?·??a?ê?ò×??¨ò????a00H delay(30); lcd_pos(0x07); //μúò?DDμú7????????ê?1 lcd_wdat(table[1]); delay(30); lcd_pos(0x09); //μúò?DDμú9????????ê?×??¨ò?×?·??a???ò×??¨ò????a01H lcd_wdat(0x01); delay(30); lcd_pos(0x0a); //μúò?DDμú10????????ê?1 lcd_wdat(table[1]); delay(30); lcd_pos(0x0c); //μúò?DDμú12????????ê?×??¨ò?×?·?è??ò×??¨ò????a02H lcd_wdat(0x02); delay(30); lcd_pos(0x43); //μú?tDDμúèy????????ê?×??¨ò?×?·?ìì?ò×??¨ò????a03H lcd_wdat(0x03); delay(30); lcd_pos(0x46); //μú?tDDμúèáù????????ê?×??¨ò?×?·?ìì?ò×??¨ò????a03H lcd_wdat(0x03); delay(30); lcd_pos(0x49); //μú?tDDμú9????????ê?×??¨ò?×?·??a?ò×??¨ò????a04H lcd_wdat(0x04); lcd_pos(0x4c); //μú?tDDμú12????????ê?×??¨ò?×?·?D??ò×??¨ò????a05H lcd_wdat(0x05); delay(30); flash(); delay(800); lcd_wcmd(0x01); //??3yLCD??ê?μ??úèY delay(200); //??????×a??ê±?? } }
|