PIC单片机C语言编程实例

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

PIC单片机C语言编程实例——液晶显示模块编程15.2.2MG-12232模块的编程下面以图15.1的接口电路为例。液晶显示区域分成E1边和E2边,下面只含E1边的程序(表15.1中E1=1,E2=0),E2边(表15.1中E1=0,E2=1)类推。在系统程序的初始化部分,应对程序中用到的寄存器和临时变量作说明,如:unsignedcharTRANS;unsignedcharPAGEADD;//存放页地址寄存器unsignedcharPAGENUM;//存放总页数寄存器unsignedcharCLMSUM;//存放总列数寄存器unsignedcharCLMADD;//存放列地址寄存器unsignedcharWRITE;//存放显示数据寄存器unsignedcharrow;//存放显示起始行寄存器unsignedchari,k;//通用寄存器//系统各口的输入输出状态初始化子程序voidINITIAL(){ADCON1=0X87;//设置PORTA口和PORTE口为数字I/O口TRISA3=0;TRISB0=0;TRISE=0X00;//设置液晶的4个控制脚为输出}//读液晶显示器状态子程序voidLCDSTA1(){while(1){TRISD=0XFF;//设置D口为输入RB0=1;//E1=1RA3=0;//E2=0RE0=1;//R/W=1RE1=0;//A0=0if(RD7==0)break;//为忙状态,则继续等待其为空闲}}//对液晶显示器发指令子程序(指令保存在TRANS寄存器中)voidTRANS1(){LCDSTA1();//判断液晶是否为忙TRISD=0X00;//置D口为输出RB0=1;//E1=1RA3=0;//E2=0RE0=0;//R/W=0RE1=0;//A0=0PORTD=TRANS;//需要写入的命令字送入数据线RB0=0;//E1=0写入指令RE0=1;//R/W=1}//对液晶显示器写数据子程序(数据保存在WRITE寄存器中)voidWRITE1(){TRANS=CLMADD;//设置列地址TRANS1();LCDSTA1();//查询液晶是否为空闲TRISD=0X00;//D口为输出RB0=1;//E1=1RA3=0;//E2=0RE0=0;//R/W=0RE1=1;//A0=1PORTD=WRITE;//需要写入的数据放入D口RB0=0;//E1=0,写入数据CLMADD++;//列地址加1RE0=1;//R/W=1}//开E1显示子程序voidDISP1(){while(1){TRANS=0XAF;TRANS1();//送出控制命令LCDSTA1();//判断液晶是否为空闲TRISD=0XFF;//设置D口为输入RB0=1;//E1=1RA3=0;//E2=0RE0=1;//R/W=1RE1=0;//A0=0if(RD5==0)break;//如果液晶没被关闭,则继续关}}//E1边清屏子程序voidCLEAR1(){PAGEADD=0xB8;//设置页地址代码for(PAGENUM=0X04;PAGENUM0;PAGENUM-){TRANS=PAGEADD;TRANS1();CLMADD=0x00;//设置起始列for(CLMSUM=0X50;CLMSUM0;CLMSUM-){LCDSTA1();//判断液晶是否为空闲WRITE=0X00;WRITE1();//写入00H以清屏}PAGEADD++;//页号增1}}//关E1显示子程序voidDISOFF1(){while(1){TRANS=0XAE;TRANS1();//发出控制命令LCDSTA1();//判断液晶是否为空闲TRISD=0XFF;//D口设置为输入RB0=1;//E1=1RA3=0;//E2=0RE0=1;//R/W=1RE1=0;//A0=0if(RD5==1)break;//如果液晶没被关闭,则继续关}}有了以上的通用子程序,就可以构造出各种显示程序,如字符。汉字。曲线等。执行这些程序前,必须对液晶进行初始化。初始化的顺序为:关显示→正常显示驱动设置→占空比设置→复位→ADC选择→清屏→开显示,程序如下://E1边初始化voidlcd1(){DISOFF1();//关显示E1TRANS=0XA4;//静态显示驱动TRANS1();//发出控制命令TRANS=0XA9;//占空比为1/32TRANS1();//发出控制命令TRANS=0XE2;//复位TRANS1();//发出控制命令TRANS=0XA0;//ADC选择正常输出TRANS1();//发出控制命令CLEAR1();//清屏LCDSTA1();//判断液晶是否为空闲DISP1();//开显示}15.4程序清单下面给出一个已经在模板上调试通过的程序。注意在调试该程序时,需把模板上的J9跳针短接。#includepic.h//该程序用于液晶显示功能的演示//运行程序后,液晶上显示电流有效值和电压有效值字样//系统总的初始化子程序unsignedcharTRANS;unsignedcharPAGEADD;//存放页地址寄存器unsignedcharPAGENUM;//存放总页数寄存器unsignedcharCLMSUM;//存放总列数寄存器unsignedcharCLMADD;//存放列地址寄存器unsignedcharWRITE;//存放显示数据寄存器unsignedcharrow;//存放显示起始行寄存器unsignedchari,k;//通用寄存器constchartable[192]={0X00,0XF8,0X48,0X48,0X48,0X48,0XFF,0X48,0X48,0X48,0X48,0XFC,0X08,0X00,0X00,0X00,0X00,0X07,0X02,0X02,0X02,0X02,0X3F,0X42,0X42,0X42,0X42,0X47,0X40,0X70,0X00,0X00,//电0X00,0X00,0XFE,0X02,0X82,0X82,0X82,0X82,0XFE,0X82,0X82,0X82,0XC3,0X82,0X00,0X00,0X40,0X30,0X0F,0X40,0X40,0X40,0X40,0X40,0X7F,0X40,0X42,0X44,0X4C,0X60,0X40,0X00,//压0X04,0X04,0X04,0X84,0XE4,0X3C,0X27,0X24,0X24,0X24,0X24,0XF4,0X24,0X06,0X04,0X00,0X4,0X2,0X1,0X0,0XFF,0X9,0X9,0X9,0X9,0X49,0X89,0X7F,0X0,0X0,0X0,0X0,//有0X88,0X48,0XB8,0X9,0XA,0X98,0X2C,0X48,0X20,0XD0,0X1F,0X10,0X10,0XF8,0X10,0X0,0X40,0X20,0X18,0X5,0X2,0XD,0X30,0X80,0X80,0X41,0X36,0X8,0X37,0XC0,0X40,0X0,//效0X80,0X40,0X20,0XF8,0X7,0X4,0XE4,0XA4,0XA4,0XBF,0XA4,0XA4,0XF6,0X24,0X0,0X0,0X0,0X0,0X0,0XFF,0X40,0X40,0X7F,0X4A,0X4A,0X4A,0X4A,0X4A,0X7F,0X40,0X40,0X0,//值0X10,0X22,0X64,0XC,0X80,0X44,0X44,0X64,0X55,0X4E,0X44,0X54,0X66,0XC4,0X0,0X0,0X4,0X4,0XFE,0X1,0X0,0X80,0X40,0X3F,0X0,0XFF,0X0,0X3F,0X40,0X40,0X70,0X0//流};//系统各口的输入输出状态初始化子程序voidINITIAL(){ADCON1=0X87;//设置PORTA口和PORTE口为数字I/O口TRISA3=0;TRISB0=0;TRISE=0X00;//设置液晶的4个控制脚为输出}//读液晶显示器状态子程序voidLCDSTA1(){while(1){TRISD=0XFF;//设置D口为输入RB0=1;//E1=1RA3=0;//E2=0RE0=1;//R/W=1RE1=0;//A0=0if(RD7==0)break;//为忙状态,则继续等待其为空闲}}//对液晶显示器发指令子程序(指令保存在TRANS寄存器中)voidTRANS1(){LCDSTA1();//判断液晶是否为忙TRISD=0X00;//D口为输出RB0=1;//E1=1RA3=0;//E2=0RE0=0;//R/W=0RE1=0;//A0=0PORTD=TRANS;//需要写入的命令字送入数据线RB0=0;//E1=0写入指令RE0=1;//R/W=1}//对液晶显示器写数据子程序(数据保存在WRITE寄存器中)voidWRITE1(){TRANS=CLMADD;//设置列地址TRANS1();LCDSTA1();//查询液晶是否为空闲TRISD=0X00;//D口为输出RB0=1;//E1=1RA3=0;//E2=0RE0=0;//R/W=0RE1=1;//A0=1PORTD=WRITE;//需要写入的数据放入D口RB0=0;//E1=0,写入数据CLMADD++;//列地址加1RE0=1;//R/W=1}//开E1显示子程序voidDISP1(){while(1){TRANS=0XAF;TRANS1();//送出控制命令LCDSTA1();//判断液晶是否为空闲TRISD=0XFF;//设置D口为输入RB0=1;//E1=1RA3=0;//E2=0RE0=1;//R/W=1RE1=0;//A0=0if(RD5==0)break;//如果液晶没被关闭,则继续关}}//E1边清屏子程序voidCLEAR1(){PAGEADD=0xB8;//设置页地址代码for(PAGENUM=0X04;PAGENUM0;PAGENUM-){TRANS=PAGEADD;TRANS1();CLMADD=0x00;//设置起始列for(CLMSUM=0X50;CLMSUM0;CLMSUM-){LCDSTA1();//判断液晶是否为空闲WRITE=0X00;WRITE1();//写入00H以清屏}PAGEADD++;//页号增1}}//关E1显示子程序voidDISOFF1(){while(1){TRANS=0XAE;TRANS1();//发出控制命令LCDSTA1();//判断液晶是否为空闲TRISD=0XFF;//D口设置为输入RB0=1;//E1=1RA3=0;//E2=0RE0=1;//R/W=1RE1=0;//A0=0if(RD5==1)break;//如果液晶没被关闭,则继续关}}//E1边初始化voidlcd1(){DISOFF1();//关显示E1TRANS=0XA4;//静态显示驱动TRANS1();//发出控制命令TRANS=0XA9;//占空比为1/32TRANS1();//发出控制命令TRANS=0XE2;//复位TRANS1();//发出控制命令TRANS=0XA0;//ADC选择正常输出TRANS1();//发出控制命令CLEAR1();//清屏LCDSTA1();//判断液晶是否为空闲DISP1();//开显示}//E2边的处理部分//读液晶显示器状态子程序voidLCDSTA2(){while(1){TRISD=0XFF;//设置D口为输入RB0=0;//E1=0RA3=1;//E2=1RE0=1;//R/W=1RE1=0;//A0=0if(RD7==0)break;//为忙状态,则继续等待其为空闲}}//对液晶显示器发指令子程序指令保存在TRANS寄存器中voidTRANS2(){LCDSTA2();//判断液晶是否为忙TRISD=0X00;//D口为输出RB0=0;

1 / 24
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功