/************************************************************************************ * Copyright (c) 2007,E精灵小店 * 小店地址: * http://auction1.taobao.com/auction/goods/goods_on_sale.htm * All rights reserved. * * 文件名称: main.c * 文件标识: none * 适用器件: STC89C51RC * * 摘 要: 按键控制液晶的翻页 * * 当前版本:1.0 * 作 者:李芳 * 完成日期:2007年12月10日 * *注:程序在我小店的Easy51板上测试通过 *************************************************************************************/ #include "reg51.h"
sbit CS =P1^2; sbit SID=P1^3; sbit SCLK=P1^4; sbit B_light=P2^6; //背光控制
sbit Mode=P2^0; //按键控制 sbit Up=P2^1; sbit Down=P2^2; //sbit BUZZ =P2^7; unsigned int time,time1; unsigned char mean=0; bit flag=0; void CtrOut(); void key_press();
void delay(unsigned int j) { unsigned char i; do{ for(i=0;i<100;i++); }while(j--); } void send_command(unsigned char command_data) { unsigned char i; unsigned char i_data,temp_data1,temp_data2;
i_data=0xf8; delay(10); CS=1; SCLK=0; for(i=0;i<8;i++) { SID=(bit)(i_data&0x80); SCLK=0; SCLK=1; i_data=i_data<<1; } i_data=command_data; i_data&=0xf0; for(i=0;i<8;i++) { SID=(bit)(i_data&0x80); SCLK=0; SCLK=1; i_data=i_data<<1; } i_data=command_data; temp_data1=i_data&0xf0; temp_data2=i_data&0x0f; temp_data1>>=4; temp_data2<<=4; i_data=temp_data1|temp_data2; i_data&=0xf0; for(i=0;i<8;i++) { SID=(bit)(i_data&0x80); SCLK=0; SCLK=1; i_data=i_data<<1; } CS=0; }
void send_data(unsigned char command_data) { unsigned char i; unsigned char i_data,temp_data1,temp_data2;
i_data=0xfa; delay(10); CS=1; for(i=0;i<8;i++) { SID=(bit)(i_data&0x80); SCLK=0; SCLK=1; i_data=i_data<<1; } i_data=command_data; i_data&=0xf0; for(i=0;i<8;i++) { SID=(bit)(i_data&0x80); SCLK=0; SCLK=1; i_data=i_data<<1; } i_data=command_data; temp_data1=i_data&0xf0; temp_data2=i_data&0x0f; temp_data1>>=4; temp_data2<<=4; i_data=temp_data1|temp_data2; i_data&=0xf0; for(i=0;i<8;i++) { SID=(bit)(i_data&0x80); SCLK=0; SCLK=1; i_data=i_data<<1; } CS=0; }
void InitLCD() /*液晶初始化*/ { send_command(0x30); //功能设置:一次送8位数据,基本指令集 send_command(0x06); //点设定:显示字符/光标从左到右移位,DDRAM地址加1 send_command(0x0c); //显示设定:开显示,显示光标,当前显示位反白闪动 send_command(0x04); //显示设定:开显示,显示光标,当前显示位反白闪动 send_command(0x01); //清DDRAM send_command(0x02); //DDRAM地址归位 send_command(0x80); //把显示地址设为0X80,即为第一行的首位 } /* flagnew 是刷新标志位,1则刷新,0不刷新, x,y为起始座标 x(0<=x<=3),y(0<=y<=7),x为行座标,y为列座标; how为要显示汉字的个数; str是要显示汉字的地址 */ void DispHanzi(unsigned char x,unsigned char y,unsigned char how,unsigned char *stri) { unsigned char hi=0; //汉字显示 if(x==0) send_command(0x80+y); else if(x==1) send_command(0x90+y); else if(x==2) send_command(0x88+y); else if(x==3) send_command(0x98+y); for(hi=0;hi<how;hi++) { send_data(*(stri+hi*2)); send_data(*(stri+hi*2+1)); } }
void DispZimu(unsigned char x,unsigned char y,unsigned char how,unsigned char *stri)//字母数字都可以显示 { unsigned char hi=0; //字母显示 if(x==0) send_command(0x80+y); else if(x==1) send_command(0x90+y); else if(x==2) send_command(0x88+y); else if(x==3) send_command(0x98+y); for(hi=0;hi<how;hi++) { //send_data(0xA3);//有这条语句占一个位置的字,如果没有只占半个 send_data(*(stri+hi)); } }
void CtrOut()//输出 { // unsigned char i; if(mean==0)//模式为自动切换状态循环显示并且通过指示灯来对应 { DispHanzi(0,0,8,"E精灵小店学习板"); DispZimu(1,1,14,"U-easytech.com"); } else if(mean==1) { DispHanzi(0,0,8,"重庆市E精灵小店"); DispHanzi(2,0,7,"提供全面的程序"); // DispHanzi(0,0,8,"重庆市E精灵小店"); DispZimu(1,1,14,"U-easytech.com"); } else if(mean==2) { DispHanzi(0,0,8,"单片机学习的工具"); DispZimu(1,1,14,"U-easytech.com"); } }
void Key_press() //按键处理 { if(Mode==0||Up==0||Down==0) { send_command(0x01); if(Mode==0) { mean=1;} else if(Up==0) { if(mean==0) {mean=2;} else if(mean==1) {mean=0; } else if(mean==2) {mean=1;} } else if(Down==0) { if(mean==0) { mean=1;} else if(mean==1) {mean=2;} else if(mean==2) {mean=0; }
} } flag=1; } void main() { InitLCD(); B_light=0; TMOD=0x00; TH0=0xee; TL0=0x00; TR0=1; ET0=1; EA=1; mean=0; while(1) { if(flag) { CtrOut(); } } }
void intime() interrupt 1 { TH0=0xee; TL0=0x00; if(time++>=20) {time=0; flag=1; } Key_press(); }
相关链接:http://auction1.taobao.com/auction/goods/goods_on_sale.htm |