📄 lcdconfig.c
字号:
/**************************************************************** LcdConfig.c* LCD液晶显示程序* LCD液晶型号:6963控制器* File Name: LcdConfig.c* Description: Lcd显示Logo,Time,Date等信息的应用程序* Author: 唐子超* Date : 2007.1.25***************************************************************/#include <stdio.h>#include <stdlib.h>#include <string.h>#include <fcntl.h>#include <time.h>#include <math.h>#include "Lcdzimo.h"#include "LcdConfig.h"#define DRV "/dev/lcd"unsigned int rgcon=0;int fd;int mySleep(unsigned long sleepSecond, unsigned long nMinSec){ struct timeval t_timeval; t_timeval.tv_sec = sleepSecond; t_timeval.tv_usec = nMinSec; select( 0, NULL, NULL, NULL, &t_timeval ); return 0;}void Delay(unsigned short int MS){ unsigned char us,usn; // MS*=10; while (MS!=0) //for 12M { usn = 2; while (usn!=0) { us=0xf5; while (us!=0) { us--; }; usn--; } MS--; }}void RS(unsigned char flag){ int count; if (flag==1) { rgcon=rgcon|(0x1<<11); if ((count=write(fd,&rgcon,4))!=4) { perror("write error"); exit(1); } } else { rgcon&=0xfffff7ff; if ((count=write(fd,&rgcon,4))!=4) { perror("write error"); exit(1); } }}void CS(unsigned char flag){ int count; if (flag==1) { rgcon=rgcon|(0x1<<10); if ((count=write(fd,&rgcon,4))!=4) { perror("write error"); exit(1); } } else { rgcon&=0xfffffbff; if ((count=write(fd,&rgcon,4))!=4) { perror("write error"); exit(1); } }}void RD(unsigned char flag){ int count; if (flag==1) { rgcon=rgcon|(0x1<<9); if ((count=write(fd,&rgcon,4))!=4) { perror("write error"); exit(1); } } else { rgcon&=0xfffffdff; if ((count=write(fd,&rgcon,4))!=4) { perror("write error"); exit(1); } }}void WR(unsigned char flag){ int count; if (flag==1) { rgcon=rgcon|(0x1<<8); if ((count=write(fd,&rgcon,4))!=4) { perror("write error"); exit(1); } } else { rgcon&=0xfffffeff; if ((count=write(fd,&rgcon,4))!=4) { perror("write error"); exit(1); } }}unsigned char WritePort(unsigned char da){ unsigned char i; int count; for (i=0;i<8;i++) { if (((da>>i)&0x01)!=0) { switch (i) { case 0: { rgcon =rgcon|(0x1 << 0); goto ty; } case 1: { rgcon =rgcon|(0x1 << 1); goto ty; } case 2: { rgcon =rgcon|(0x1 << 2); goto ty; } case 3: { rgcon =rgcon|(0x1 << 3); goto ty; } case 4: { rgcon =rgcon|(0x1 << 4); goto ty; } case 5: { rgcon =rgcon|(0x1 << 5); goto ty; } case 6: { rgcon =rgcon|(0x1 << 6); goto ty; } case 7: { rgcon =rgcon|(0x1 << 7); goto ty; } default:break; } } else { switch (i) { case 0: { rgcon = rgcon&0xfffffffe;//PB0=0 1111 1111 1111 1111 1111 1111 1110 goto ty; } case 1: { rgcon = rgcon&0xfffffffd;//PB1=0 1111 1111 1111 1111 1111 1111 1101 goto ty; } case 2: { rgcon = rgcon&0xfffffffb;//PB2=0 1111 1111 1111 1111 1111 1111 1011 goto ty; } case 3: { rgcon = rgcon&0xfffffff7;//PB3=0 1111 1111 1111 1111 1111 1111 0111 goto ty; } case 4: { rgcon = rgcon&0xffffffef;//PB4=0 1111 1111 1111 1111 1111 1110 1111 goto ty; } case 5: { rgcon = rgcon&0xffffffdf;//PB5=0 1111 1111 1111 1111 1111 1101 1111 goto ty; } case 6: { rgcon = rgcon&0xffffffbf;//PB6=0 1111 1111 1111 1111 1111 1011 1111 goto ty; } case 7: { rgcon = rgcon&0xffffff7f;//PB7=0 1111 1111 1111 1111 1111 0111 1111 goto ty; } default:break; } } ty:i=i; } if ((count=write(fd,(char *)&rgcon,4))!=4) { perror("write1 error"); exit(1); } return 1;}unsigned char ReadPort(void){ int count; unsigned short int result; unsigned char cResult; if ((count=read(fd,(char*)&result,2))<0) { printf("read error \n"); } else { // printf("*lcd:Data=%d\n",result); //0XFFFE 1111 1111 1111 1110 } cResult=(unsigned char)(result&0x00ff); // printf("cResult=%d,result =%d\n",cResult,result); return cResult;}void check_status(unsigned char state){ unsigned char back_st; while (1) { CS(0); RS(1); WR(1); //Delay(1); RD(0); // Delay(1); back_st=ReadPort(); // Delay(1); RD(1); if (back_st & state) break; } CS(1); WR(1); RD(1);}void write_data(unsigned char da){ check_status(0x03); CS(0); RS(0); RD(1); WR(0); WritePort(da); WR(1); CS(1);}void write_code(unsigned char cm){ check_status(0x03); CS(0); RS(1); RD(1); WR(0); WritePort(cm); WR(1); CS(1);}void out_command0(unsigned char cm){ write_code(cm);}void out_command1(unsigned char da, unsigned char cm){ write_data(da); write_code(cm);}void out_command2(unsigned char d1, unsigned char d2, unsigned char cm){ write_data(d1); write_data(d2); write_code(cm);}void init_lcd(){ out_command2(0x00,0x00,0x42); out_command2(30,0x00,0x43); out_command2(0x00,0x00,0x40); out_command2(30,0x00,0x41); out_command0(0x80); out_command0(0x9c);}void clear_device(void){ unsigned short int i; out_command2(0x00,0x00,0x24); out_command0(0xb0); for (i=0; i<128*30; i++) { check_status(0x08); write_data(0x00); } out_command0(0xb2);}void black_device(void){ unsigned short int i; //Port4_In_Out(1); out_command2(0x00,0x00,0x24); out_command0(0xb0); for (i=0; i<128*30; i++) { check_status(0x08); write_data(0xff); } out_command0(0xb2);}unsigned short int LocateXY(unsigned char x,unsigned char y){ union { unsigned short int m; unsigned char hz1[2]; }s1; s1.hz1[1]=x; s1.hz1[0]=y; return(s1.m-x);}//IO测试程序void testio(void){ if ((fd=open(DRV,O_RDWR))==-1) { perror("open error"); exit(1); } WritePort(0xff); printf("0xff"); getchar(); RS(1); printf("RS1"); getchar(); RS(0); printf("RS0"); getchar(); RD(1); printf("rd1"); getchar(); RD(0); printf("rd0"); getchar(); WR(1); printf("wr1"); getchar(); WR(0); printf("wr0"); getchar(); RD(1); printf("rd1"); getchar(); RD(0); printf("rd0"); getchar(); close(fd);}//显示汉字void dis_hanzi(unsigned char x,unsigned char y,unsigned char word[32]){ unsigned char i,j; unsigned short int add1,k; union { unsigned short int m; unsigned char hz1[2]; }s1; union { unsigned short int n; unsigned char hz2[2]; }s2; add1=LocateXY(x,y); for (i=0;i<16;i++) { k=i*30; s1.m=add1+k; out_command2(s1.hz1[0],s1.hz1[1],0x24); //out_command1(*p,0xc4); j=i*2; out_command1(word[j],0xc4); //p++; s2.n=s1.m+1; out_command2(s2.hz2[0],s2.hz2[1],0x24); //out_command1(*p,0xc4); j=i*2+1; out_command1(word[j],0xc4); //p++; }}//显示图片void dis_bmp(unsigned char x,unsigned char y,unsigned char length,unsigned char height,unsigned char word[]){ unsigned char i,j,h; unsigned short int add1,k; union { unsigned short int m; unsigned char hz1[2]; }s1; add1=LocateXY(x,y); for (i=0;i<height;i++) { k=i*30; for (h=0;h<length/8;h++) { s1.m=add1+k+h; out_command2(s1.hz1[0],s1.hz1[1],0x24); j=i*(length/8)+h; out_command1(word[j],0xc4); } }}//显示欢迎画面void fnWelcome(void){ dis_bmp(10,1,32,32,e1); dis_bmp(10,5,32,32,e2); dis_bmp(10,9,32,32,e3); dis_bmp(10,13,32,32,e4); dis_bmp(10,17,32,32,e5); dis_bmp(10,21,32,32,e6); dis_bmp(10,25,32,32,e7);}//从系统获取时间,然后显示void* dis_Time(void){ time_t crr_time; struct tm *ptm; int Tflag=0; dis_hanzi(4,4,szm); while (1) { time(&crr_time); ptm=localtime(&crr_time); switch ((ptm->tm_min)/10) { case 0: dis_hanzi(4,5,sz0); break; case 1: dis_hanzi(4,5,sz1); break; case 2: dis_hanzi(4,5,sz2); break; case 3: dis_hanzi(4,5,sz3); break; case 4: dis_hanzi(4,5,sz4); break; case 5: dis_hanzi(4,5,sz5); break; default: break; } switch ((ptm->tm_min)%10) { case 0: dis_hanzi(4,6,sz0); break; case 1: dis_hanzi(4,6,sz1); break; case 2: dis_hanzi(4,6,sz2); break; case 3: dis_hanzi(4,6,sz3); break; case 4: dis_hanzi(4,6,sz4); break; case 5: dis_hanzi(4,6,sz5); break; case 6: dis_hanzi(4,6,sz6); break; case 7: dis_hanzi(4,6,sz7); break; case 8: dis_hanzi(4,6,sz8); break; case 9: dis_hanzi(4,6,sz9); break; default: break; } switch ((ptm->tm_hour)/10) { case 0: dis_hanzi(4,1,sz0); break; case 1: dis_hanzi(4,1,sz1); break; case 2: dis_hanzi(4,1,sz2); break; default: break; } switch ((ptm->tm_hour)%10) { case 0: dis_hanzi(4,2,sz0); break; case 1: dis_hanzi(4,2,sz1); break; case 2: dis_hanzi(4,2,sz2); break; case 3: dis_hanzi(4,2,sz3); break; case 4: dis_hanzi(4,2,sz4); break; case 5: dis_hanzi(4,2,sz5); break; case 6: dis_hanzi(4,2,sz6); break; case 7: dis_hanzi(4,2,sz7); break; case 8: dis_hanzi(4,2,sz8); break; case 9: dis_hanzi(4,2,sz9); break; default: break; } // if (Tflag==1) // if ((ptm->tm_hour!=23) && (ptm->tm_min!=59)) // { // mySleep(60,0); // continue; // } ptm->tm_year+=1900; ptm->tm_mon+=1; // Tflag=1; switch ((ptm->tm_year)/1000) { case 0: dis_hanzi(2,0,sz0); break; case 1: dis_hanzi(2,0,sz1); break; case 2: dis_hanzi(2,0,sz2); break; case 3: dis_hanzi(2,0,sz3); break; case 4: dis_hanzi(2,0,sz4); break; case 5: dis_hanzi(2,0,sz5); break; case 6: dis_hanzi(2,0,sz6); break; case 7: dis_hanzi(2,0,sz7); break; case 8: dis_hanzi(2,0,sz8); break; case 9: dis_hanzi(2,0,sz9); break; default: break; } switch ((ptm->tm_year)/100%10) { case 0: dis_hanzi(2,1,sz0); break; case 1: dis_hanzi(2,1,sz1); break; case 2: dis_hanzi(2,1,sz2); break; case 3: dis_hanzi(2,1,sz3); break; case 4: dis_hanzi(2,1,sz4); break; case 5: dis_hanzi(2,1,sz5); break; case 6: dis_hanzi(2,1,sz6); break; case 7: dis_hanzi(2,1,sz7); break; case 8: dis_hanzi(2,1,sz8); break; case 9: dis_hanzi(2,1,sz9); break; default: break; } switch ((ptm->tm_year)/10%100) { case 0: dis_hanzi(2,2,sz0); break; case 1: dis_hanzi(2,2,sz1); break; case 2: dis_hanzi(2,2,sz2); break; case 3: dis_hanzi(2,2,sz3); break; case 4: dis_hanzi(2,2,sz4); break; case 5: dis_hanzi(2,2,sz5); break; case 6: dis_hanzi(2,2,sz6); break; case 7: dis_hanzi(2,2,sz7); break; case 8: dis_hanzi(2,2,sz8); break; case 9: dis_hanzi(2,2,sz9); break; default: break; } switch ((ptm->tm_year)%1000) { case 0: dis_hanzi(2,3,sz0); break; case 1: dis_hanzi(2,3,sz1); break; case 2: dis_hanzi(2,3,sz2); break; case 3: dis_hanzi(2,3,sz3); break; case 4: dis_hanzi(2,3,sz4); break; case 5: dis_hanzi(2,3,sz5); break; case 6: dis_hanzi(2,3,sz6); break; case 7: dis_hanzi(2,3,sz7); break; case 8: dis_hanzi(2,3,sz8); break; case 9: dis_hanzi(2,3,sz9); break; default: break; } //end year //dis_hanzi(2,4,szg); switch ((ptm->tm_mon)/10) { case 0: dis_hanzi(2,5,sz0); break; case 1: dis_hanzi(2,5,sz1); break; default: break; } switch ((ptm->tm_mon)%10) { case 0: dis_hanzi(2,6,sz0); break; case 1: dis_hanzi(2,6,sz1); break; case 2: dis_hanzi(2,6,sz2); break; case 3: dis_hanzi(2,6,sz3); break; case 4: dis_hanzi(2,6,sz4); break; case 5: dis_hanzi(2,6,sz5); break; case 6: dis_hanzi(2,6,sz6); break; case 7: dis_hanzi(2,6,sz7); break; case 8: dis_hanzi(2,6,sz8); break; case 9: dis_hanzi(2,6,sz9); break; default: break; }//end month // dis_hanzi(2,7,szg); switch ((ptm->tm_mday)/10) { case 0: dis_hanzi(2,8,sz0); break; case 1: dis_hanzi(2,8,sz1); break; case 2: dis_hanzi(2,8,sz2); break; case 3: dis_hanzi(2,8,sz3); break; default: break; } switch ((ptm->tm_mday)%10) { case 0: dis_hanzi(2,9,sz0); break; case 1: dis_hanzi(2,9,sz1); break; case 2: dis_hanzi(2,9,sz2); break; case 3: dis_hanzi(2,9,sz3); break; case 4: dis_hanzi(2,9,sz4); break; case 5: dis_hanzi(2,9,sz5); break; case 6: dis_hanzi(2,9,sz6); break; case 7: dis_hanzi(2,9,sz7); break; case 8: dis_hanzi(2,9,sz8); break; case 9: dis_hanzi(2,9,sz9); break; default: break; }//end day mySleep(60,0); }}void FirstP(void){ void *tret; pthread_t disT; dis_bmp(2,18,32,32,tx); dis_bmp(2,22,32,32,xh1); dis_bmp(2,26,32,32,xh2); mySleep(0,50000); dis_hanzi(4,10,hzzhong); dis_hanzi(4,12,hzguo); dis_hanzi(4,14,hzlian); dis_hanzi(4,16,hztong);//china unicom mySleep(0,50000); if (pthread_create(&disT,NULL,dis_Time,NULL)) { perror("Cant creat pthread!"); exit(1); } if ((pthread_join(disT,tret))!=0) perror("Can't join");}int fnCloseLcd(void){ clear_device(); close(fd); return 0;}int fnClearLcd(void){ clear_device(); return 0;}//显示主界面int fnDispMainLogo(void){ if ((fd=open(DRV,O_RDWR))==-1) { perror("open error"); exit(1); } init_lcd(); mySleep(0,50000); printf("INIT OK\n"); clear_device(); mySleep(0,50000); printf("clear OK\n"); black_device(); mySleep(0,50000); printf("black OK\n"); clear_device(); mySleep(0,50000); printf("clear OK\n"); fnWelcome(); mySleep(1,0); clear_device(); mySleep(1,0); FirstP(); mySleep(0,50000); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -