#include
#define uchar unsigned char
#define uint unsigned int
sbit rs = P3^5;
sbit rw = P3^6;
sbit en = P3^4;
sbit duan= P2^7;
sbit wela= P2^6;
sbit key1 = P3^0;
sbit key2 = P3^1;
sbit key3 = P3^2;
sbit key4 = P3^3;
uchar num,key1num,count,hour,min,sec;
uchar code cg_1602[]={ 0x08,0x0f,0x12,0x0f,0x0a,0x1f,0x02,0x02,
0x0f,0x09,0x0f,0x09,0x0f,0x09,0x11,0x00,
0x0f,0x09,0x09,0x0f,0x09,0x09,0x0f,0x00}; /*年月日*/
void delay(uint xms)
{
uint i,j;
for(i=xms;i>0;i--)
for(j=114;j>0;j--);
}
void write_com(uchar com)
{
rs=0;
rw=0;
P0=com;
delay(5);
en=1;
delay(5);
en=0;
}
void write_data(uchar date)
{
rs=1;
rw=0;
P0=date;
delay(5);
en=1;
delay(5);
en=0;
}
void write_by_xy(uchar x,uchar y)
{
uchar address;
if(y==0)
address=0x80+x;
else
address=0xc0+x;
write_com(address);
}
void write_string(uchar x,uchar y,uchar *s)
{
write_by_xy(x,y);
while(*s)
{
P0=*s;
write_data(*s);
s++;
}
}
void write_sfm(uchar add,uchar date)
{
uchar hour,ge;
hour=date/10;
ge=date%10;
write_com(0x80+0x40+add);
write_data(0x30+hour);
write_data(0x30+ge);
}
void write_cg()
{
write_com(0x40);
for(num=0;num<24;num++)
{
write_data(cg_1602[num]);
}
write_com(0x84);//年
{
write_data(0);
}
write_com(0x87);//月
{
write_data(1);
}
write_com(0x8a);//日
{
write_data(2);
}
write_string(2,1,":");
write_string(5,1,":");
write_string(9,1,"see xj");
}
void init_1602()//初始化
{
duan=0;wela=0;rw=0;
hour=05;min=55;sec=55;key1num=0;count=0;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
write_string(0,0,"2017");
write_string(5,0,"06");
write_string(8,0,"23");
write_sfm(0,hour);
write_sfm(3,min);
write_sfm(6,sec);
write_cg();
EA=1;
ET0=1;
TMOD=0x01;//设置定时器0工作模式1
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
TR0=1;
/*
EA=1;
ET1=1;
TMOD=0x10//设置定时器1模式2
TH1=(65536-50000)/256;
TL1
=(65536-50000)%256;
TR1=1;
*/
}
void keyscan()
{
// key4=0;
loop:
if(key4==0)
{
key1num=0;
write_com(0x0c);
TR0=1;
}
if(key1==0)
{
if(key4==0) goto loop;
delay(5);
if(key1==0)
{
if(key4==0) goto loop;
key1num++;
while(!key1);
if(key1num==1)
{
if(key4==0) goto loop;
TR0=0;
write_com(0x80+0x40+7);
write_com(0x0f);
}
if(key1num==2)
{
if(key4==0) goto loop;
write_com(0x80+0x40+4);
}
if(key1num==3)
{
if(key4==0) goto loop;
write_com(0x80+0x40+1);
}
}
}
if(key1num!=0)
{
if(key2==0)
{
delay(5);
if(key2==0)
{
while(!key2);
if(key1num==1)
{
sec++;
if(sec==60)
sec=0;
write_sfm(6,sec);
write_com(0x80+0x40+7);
}
if(key1num==2)
{
min++;
if(min==60)
min=0;
write_sfm(3,min);
write_com(0x80+0x40+4);
}
if(key1num==3)
{
hour++;
if(hour==24)
hour=0;
write_sfm(0,hour);
write_com(0x80+0x40+1);
} if(key4==0) goto loop;
} if(key4==0) goto loop;
} if(key4==0) goto loop;
if(key3==0)
{
delay(5);
if(key3==0)
{
while(!key3);
if(key1num==1)
{
sec--;
if(sec==-1)
sec=59;
write_sfm(6,sec);
write_com(0x80+0x40+7);
}
if(key1num==2)
{
min--;
if(min==-1)
min=59;
write_sfm(3,min);
write_com(0x80+0x40+4);
}
if(key1num==3)
{
hour--;
if(hour==-1)
hour=23;
write_sfm(0,hour);
write_com(0x80+0x40+1);
} if(key4==0) goto loop;
} if(key4==0) goto loop;
} if(key4==0) goto loop;
} if(key4==0) goto loop;
}
void main()
{
duan=0;wela=0;rw=0;
init_1602();
while(1)
{
keyscan();
}
}
void timer0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
count++;
if(count==20)
{
count=0;
sec++;
if(sec==60)
{
sec=0;
min++;
if(min==60)
{
min=0;
hour++;
if(hour==24)
{
hour=0;
}
write_sfm(0,hour);
}
write_sfm(3,min);
}
write_sfm(6,sec);
}
}
/*void timer1() interrupt 3
{
TH1=(65536-50000)/256;
TL1=(65536-50000)%256;
count++;
if(count==20)
{
count=0;
sec++;
if(sec==60)
{
sec=0;
min++;
if(min==60)
{
min=0;
hour++;
if(hour==24)
{
hour=0;
}
write_sfm(0,hour);
}
write_sfm(3,min);
}
write_sfm(6,sec);
}
} */
/* if(key 4==0)
{
key1num=0;
write_com(0x0c);
TR0=1;
}
*/
/*郭天祥的视屏讲了如何点亮任何一个LED以及流水灯,如何控制蜂鸣器的响以及电磁继电器的控制还有就是中断系统,A/D,D/A的采集它主要讲了如何采集信息将一些其他物理信息与电流电压的俄关系,之进行的额一种转化,让后就是计算机的串行通信总线以及I^C总线的概念 ,最后就是两个小问题了,一个是键盘的控制,另一个就是1602显示器的控制
下面说一说主要几个可能在后面没看见他的用处,不过在学习STM的时候他就又韩庚多的用处了
首先介绍一下两个英语单词,一个是digitad 数字量,anoiog 模拟量,d/a 转化是我们一般选的是直通工作方式,即 当DAc0832芯片的片选信号,些信号,及传送控制信号的引脚接+5v时,DAC0832芯片就处于直通工作方式,数字量一旦输入就直接进入DAC寄存器,进行D/A转化,这个用相关的软件进行让后你体验一下就可以 你或许会体会到一中感觉就是在建立一种新的平台的额;注意 寄存器的额控制 在此很简单局势声明一下位变量就行,让后你只要看一下时序图就行,控制一下低高电压即可,再次你用软件编程给相应的寄存器赋一个初值0或1就可。
薄弱点还有很多;还有8*8矩阵 电磁继电器 温度 。。。等实验没学会,数模之间转化没有学透彻。*/