#include <AT89X52.H>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
//ADC0832端口
sbit ADCS = P1^1;
sbit ADCLK = P1^2;
sbit ADDI = P1^3;
sbit ADDO = P1^3;
//液晶屏端口
sbit RST = P2^0; //复位端
sbit E = P2^1; //使能端
sbit RW = P2^2; //使能端
sbit RS = P2^3; //模式端
sbit CS1 = P2^4;
sbit CS2 = P2^5;
sbit K4=P1^0;
sbit K5=P1^1;
sbit K6=P1^2;
sbit K7=P1^3;
//时钟芯片端口
sbit RST2 = P3^5; // 复位线引脚
sbit CLK = P3^6; // 时钟线引脚
#define Disp_On 0x3f //显示开指令
#define Disp_Off 0x3e //显示关指令
#define Col_Add 0x40 //定位到第0列指令
#define Page_Add 0xb8 //定位到第0页指令
#define Start_Line 0xc0 //定位从DDROM中的第0行开始往屏幕上显示
/*---------------定义时间日期显示缓存 ----------------------*/
uchar Curtime[7];
uchar day,sec,min,hour,date,month,year,hms,n,fla_cou;
uchar code shuzi[10]={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39};
uchar xdata dsw[20]; //存放sprintf转换字符
uchar xdata value[149]; //点阵字符储存
void select_item(unsigned char);
idata unsigned char user_choosen=0;
idata unsigned char max_item=0;
idata unsigned char temp_choosen;
idata unsigned char shuaxin=0;
idata struct menu_item main1_menu[4]; //每个菜单项为一个元素.整个菜单地址即为数组名.
data struct menu_item measure_menu[2];
idata struct menu_item store_menu[2];
uchar menu_led,keyval,nusekey;
/**********初始化CPU************/
void init_cpu()
{
EA=1;
TR0=1;
TR1=1;
TMOD=0x11;
TH1=0x3c;
TL1=0xb0;
}
/*-------------------------时钟初始化子函数----------------------------*/
void Init_Clock(void)
{
//K1=1;
//K2=1;
//K3=1;
K4=1;
K5=1;
K6=1;
K7=1;
//KT=0;
//n=0;
TMOD=0x01; //定时器T0工作模式1
TH0=0x3c; //50ms
}
void delay(unsigned int t) //延时
{
unsigned int i,j;
for(i=0;i<t;i++)
for(j=0;j<10;j++);
}
/*-------------------------写命令到LCD------------------------------*/
void write_command(unsigned char cmdcode)
{
RS = 0;
RW = 0;
P0 = cmdcode;
delay(0);
E = 1;
delay(0);
E = 0;
}
/*-----指定位置显示汉字16*16-----------------------*/
void hz_disp16(unsigned char pag,unsigned char col, unsigned char mod,unsigned char code *hzk)
{
uchar i;
pag=0;
col=0;
mod=0;
i=*hzk;
}
/*------------------初始化LCD屏--------------------------*/
void init_lcd()
{
RST=0;
delay(100);
RST=1;
delay(100);
CS2=1;
CS1=1;
delay(100);
write_command(Disp_Off);
write_command(Page_Add+0);
write_command(Start_Line+0);
write_command(Col_Add+0);
write_command(Disp_On);
}
/*-------------------------写数据到LCD----------------------------*/
void write_data(unsigned char Dispdata)
{
Dispdata=0;
}
/*------------------------清显示屏--------------------------------*/
void Clr_Scr()
{
unsigned char j ,k;
//清左半屏
CS2=1;CS1=0;
write_command(Page_Add+0);
write_command(Col_Add+0);
for(k=0;k<4;k++) //清左上半屏
{
// write_command(Page_Add+k);
for(j=0;j<64;j++)
write_data(0x00);
}
for(k=0;k<4;k++) //清左下半屏
{
write_command(Page_Add+4);
for(j=0;j<64;j++)
write_data(0x00);
}
//清右半屏
CS2=0;CS1=1;
write_command(Page_Add+0);
write_command(Col_Add+0);
for(k=0;k<4;k++) //清右上半屏
{
for(j=0;j<64;j++)
write_data(0x00);
}
for(k=0;k<4;k++) //清右下半屏
{
write_command(Page_Add+4);
for(j=0;j<64;j++)
write_data(0x00);
}
}
/*------------------显示12864图片------------------------------*/
void Disp_Img(unsigned char code *img)
{
uchar i;
i=*img;
}
void GetTime()
{}
/*-------------------------- 刷新显示子函数--------------------------*/
void Refresh(void)
{
GetTime();
CS1=1;CS2=0;
if(K6)
{
//sz_disp16(5,40,1,shuzi[sec%10]); //秒位
//sz_disp16(5,32,1,shuzi[sec/10]); //十秒位
}
else
{
hz_disp16(5,32,1,"kongbai2"); //秒位 十秒位
}
if(K5)
{
//sz_disp16(5,8,1,shuzi[min&0x0f]); //分钟位
//sz_disp16(5,0,1,shuzi[min>>4]); //十分位
}
else
{
hz_disp16(5,0,1,"kongbai2");
}
}
//附录三 ADC0832程序
uint Adc0832(unsigned char channel) //AD转换,返回结果
{
uchar i=0,j,ndat=0;
uint dat=0;
if(channel==0)channel=2;
if(channel==1)channel=3;
ADDI=1;
_nop_();
_nop_();
ADCS=0; //拉低CS端
_nop_();
_nop_();
ADCLK=1; //拉高CLK端
_nop_();
_nop_();
ADCLK=0; //拉低CLK端,形成下降沿1
_nop_();
_nop_();
ADCLK=1; //拉高CLK端
ADDI=channel&0x1;
_nop_();
_nop_();
ADCLK=0; //拉低CLK端,形成下降沿2
_nop_();
_nop_();
ADCLK=1; //拉高CLK端
ADDI=(channel>>1)&0x1;
_nop_();
_nop_();
ADCLK=0; //拉低CLK端,形成下降沿3
ADDI=1; //控制命令结束
_nop_();
_nop_();
dat=0;
for(i=0;i<8;i++)
{
dat|=ADDO; //收数据
ADCLK=1;
_nop_();
_nop_();
ADCLK=0; //形成一次时钟脉冲
_nop_();
_nop_();
dat<<=1;
if(i==7)dat|=ADDO;
}
for(i=0;i<8;i++)
{
j=0;
j=j|ADDO; //收数据
ADCLK=1;
_nop_();
_nop_();
ADCLK=0; //形成一次时钟脉冲
_nop_();
_nop_();
j=j<<7;
ndat=ndat|j;
if(i<7)ndat>>=1;
}
ADCS=1; //拉低CS端
ADCLK=0; //拉低CLK端
ADDO=1; //拉高数据端,回到初始状态
dat<<=8;
dat|=ndat;
return(dat); //return ad k
}
/*------------------------时钟固定汉字显示-----------------------*/
void ClockMsg(void)
{
Clr_Scr(); // 先清屏
CS2=1;CS1=0; // 左屏
hz_disp16(1,32,1,"xing"); //星-期-2-0-年-时
CS2=0;CS1=1; //右屏
hz_disp16(3,0,1,"yue"); //月-日-分-秒
}
void main_Menu()
{
uchar k,ltemp;
float l;
Clr_Scr(); //先清屏
CS2=1;
CS1=0;
hz_disp16(0,32,1,"jiaquan"); //甲//醛//测//量//数//据//存//储
delay(2000);
do
{
k=Adc0832(0); //AD转换结果
l=k/255.0*100.0;
ltemp=floor(l);
dsw[0]=ltemp/10;
dsw[1]=ltemp%10;
//sprintf(dsw,"%2.3f",l);
//dsw[0]=1;
switch(dsw[0])
{
case 0:
hz_disp16(2,32,1,"sz0");
break;
case 1:
hz_disp16(2,32,1,"sz9");
}
switch(dsw[1])
{
case 0:
hz_disp16(2,40,1,"sz0");
break;
case 1:
hz_disp16(2,40,1,"sz9");
}
//sz_disp16(2,40,1,sz0);
//*tra(dsw);
CS2=1;CS1=0;
//sz_disp16(2,56,1,value[10]);
}
while(P1_3);
}
void led_menu_show()
{
uchar n;
//max_item=menu_led->menu_count;
if (max_item>=4) //菜单项为3则表示为主菜单.
{
for(n=0;n<4;n++)
{
//draw_bmp(n*2,20,96,0,menu_led[n].display);
}
select_item(user_choosen); //标记出当前菜单项.
}
else
{
switch(temp_choosen)
{ case 0:
//draw_bmp(0,20,96,0,measurearray); //"开始测量"数组//
break;
default:
break;
}
for(n=0;n<max_item;n++)
{
//draw_bmp((n+1)*2,20,32,0,menu_led[n].display);
}
select_item(user_choosen+1);
}
}
void main_menu_initial() //LCD主菜单初始化.//
{
//main1_menu[0].menu_count=4; //有4个菜单项.//
//main1_menu[0].display=measurearray; //定义一个"开始测量"数组//
//main1_menu[0].subs=NULL;
//main1_menu[0].children_menus=measure_menu; //当前菜单子菜单的指针
//main1_menu[0].parent_menus=NULL; //还有"数据存储"、"时间设置"……//
}
void measure_menu_initial() //"开始测量"菜单设置//
{
//measure_menu[0].menu_count=2;
//measure_menu[0].display=qr; //开始测量函数, 确认.
//measure_menu[0].subs=start_measure_function; //开始测量函数
//measure_menu[0].children_menus=NULL;
//measure_menu[0].parent_menus=main1_menu;
//measure_menu[1].menu_count=2;
//measure_menu[1].display=qx; //开始测量函数, 取消.
//measure_menu[1].subs=NULL;
//measure_menu[1].children_menus=NULL;
//measure_menu[1].parent_menus=main1_menu;
}
void store_menu_initial(){}
void time_menu_initial(){}
void led_menu_pro()
{
//max_item=menu_led->menu_count;
switch(keyval)
{
case 0: break;
case 1: //向上键.
if(user_choosen==0)
{
user_choosen=max_item;
}
shuaxin=1;
user_choosen--;
break;
}
//"向上""向下""确认""取消"键//
if(shuaxin) //是否需要刷新LCD标志位.
{
Clr_Scr();
shuaxin=0;
}
led_menu_show();
}
//附录四 按键程序
#define nullkey 0 //定义//
uchar get_key() //读键//
{
uchar pass,kcodebuf;
static uchar lastkcode,keytime=0; //定义为静态变量,外部不可见,但需要常驻内存.
static uchar havekey=0;
static uchar keylock=0;
kcodebuf=P1 & 0xff;
keytime++;
if (kcodebuf!=lastkcode)
{
keytime=0;
}
if (kcodebuf==0xff)
{
havekey=0;
keylock=0;
keytime=0;
pass=0;
}
else
pass=nusekey;
lastkcode=kcodebuf;
return(pass);
}
/*----------------主函数-------------------*/
main()
{
init_cpu();
Init_Clock();
init_lcd();
Disp_Img(0x11);
delay(2000);
ClockMsg();
Refresh();
delay(2500);
Clr_Scr();
main_Menu();
Clr_Scr();
main_menu_initial();
measure_menu_initial();
store_menu_initial();
time_menu_initial();
//communication_menu_initial();
while(1)
{
keyval=get_key();
led_menu_pro();
}
}
温馨提示:内容为网友见解,仅供参考