#include<STC90C5xAD.H>
#define yh 0x80
#define er 0x80+0x40
int count;
sbit du=P2^6;
sbit we=P2^7;
//DS1302时钟芯片与C51之间的引脚连接定义
sbit IO=P1^0;
sbit SCLK=P1^1;
sbit RST=P1^2;
sbit ACC0=ACC^0;
sbit ACC7=ACC^7;
//校时按键与C51的引脚连接定义
sbit key1=P3^4; //设置键
sbit key2=P3^5; //加键
sbit key3=P3^6; //减键
sbit buzzer=P2^4; //蜂鸣器,通过三极管8550驱动,端口低电平响
/***********************固定时间字符设置*****************/
uchar code tab1[]={"2013- - "}; //年显示的固定字符
uchar code tab2[]={" : : "}; //时间显示的固定字符
sbit rs = P2^2; //1602使能端
sbit rw = P2^1;
sbit en = P2^0;
sbit led=P1^1;
unsigned int scale; //占空比控制变量
#define uchar unsigned char;
#define uint unsigned int;
uchar a,miao,shi,fen,ri,yue,nian,week,flag,key1n,temp;
#define RS_CLR RS=0
#define RS_SET RS=1
#define RW_CLR RW=0
#define RW_SET RW=1
#define EN_CLR EN=0
#define EN_SET EN=1
uchar da1=0,da2=0,da3=0;
double Data,c;
char b[5]="";
uchar ADC_Chanul_Turn=0;
void delay(uint xms) //延时函数,有参函数
{
uint x,y;
for(x=xms;x>0;x--)
for(y=110;y>0;y--);
}
/********液晶写入指令函数与写入数据函数,以后可调用**************/
write_1602com(uchar com) //液晶写入指令函数
{
rs=0; //数据/指令选择置为指令
rw=0; //读写选择置为写
P0=com; //送入数据
delay(1);
en=1; //拉高使能端,为制造有效的下降沿做准备
delay(1);
en=0; //en由高变低,产生下降沿,液晶执行命令
}
write_1602dat(uchar dat) //液晶写入数据函数
{
rs=1; //数据/指令选择置为数据
rw=0; //读写选择置为写
P0=dat; //送入数据
delay(1);
en=1; //en置高电平,为制造下降沿做准备
delay(1);
en=0; //en由高变低,产生下降沿,液晶执行命令
}
lcd_init() //液晶初始化函数
{
write_1602com(0x38); //设置液晶工作模式,意思:16*2行显示,5*7点阵,8位数据
write_1602com(0x0c); //开显示不显示光标
write_1602com(0x06); //整屏不移动,光标自动右移
write_1602com(0x01); //清显示
write_1602com(yh+1); //日历显示固定符号从第一行第1个位置之后开始显示
for(a=0;a<14;a++)
{
write_1602dat(tab1[a]); //向液晶屏写日历显示的固定符号部分
//delay(3);
}
write_1602com(er+2); //时间显示固定符号写入位置,从第2个位置后开始显示
for(a=0;a<8;a++)
{
write_1602dat(tab2[a]); //写显示时间固定符号,两个冒号
//delay(3);
}
}
/***************DS1302有关子函数********************/
void write_byte(uchar dat) //写一个字节
{
ACC=dat;
RST=1;
for(a=8;a>0;a--)
{
IO=ACC0;
SCLK=0;
SCLK=1;
ACC=ACC>>1;
}
}
uchar read_byte() //读一个字节
{
RST=1;
for(a=8;a>0;a--)
{
ACC7=IO;
SCLK=1;
SCLK=0;
ACC=ACC>>1;
}
return (ACC);
}
//----------------------------------------
void write_1302(uchar add,uchar dat) //向1302芯片写函数,指定写入地址,数据
{
RST=0;
SCLK=0;
RST=1;
write_byte(add);
write_byte(dat);
SCLK=1;
RST=0;
}
uchar read_1302(uchar add) //从1302读数据函数,指定读取数据来源地址
{
uchar temp;
RST=0;
SCLK=0;
RST=1;
write_byte(add);
temp=read_byte();
SCLK=1;
RST=0;
return(temp);
}
uchar BCD_Decimal(uchar bcd) //BCD码转十进制函数,输入BCD,返回十进制
{
uchar Decimal;
Decimal=bcd>>4;
return(Decimal=Decimal*10+(bcd&=0x0F));
}
//--------------------------------------
void ds1302_init() //1302芯片初始化子函数(2010-01-07,12:00:00,week4)
{
RST=0;
SCLK=0;
write_1302(0x8e,0x00); //允许写,禁止写保护
write_1302(0x80,0x00); //向DS1302内写秒寄存器80H写入初始秒数据00
write_1302(0x82,0x00); //向DS1302内写分寄存器82H写入初始分数据00
write_1302(0x84,0x12); //向DS1302内写小时寄存器84H写入初始小时数据12
write_1302(0x8a,0x04); //向DS1302内写周寄存器8aH写入初始周数据4
write_1302(0x86,0x07); //向DS1302内写日期寄存器86H写入初始日期数据07
write_1302(0x88,0x01); //向DS1302内写月份寄存器88H写入初始月份数据01
write_1302(0x8c,0x10); //向DS1302内写年份寄存器8cH写入初始年份数据10
write_1302(0x8e,0x80); //打开写保护
}
//时分秒显示子函数
void write_sfm(uchar add,uchar dat) //向LCD写时分秒,有显示位置加、现示数据,两个参数
{
uchar gw,sw;
gw=dat%10; //取得个位数字
sw=dat/10; //取得十位数字
write_1602com(er+add); //er是头文件规定的值0x80+0x40
write_1602dat(0x30+sw); //数字+30得到该数字的LCD1602显示码
write_1602dat(0x30+gw); //数字+30得到该数字的LCD1602显示码
}
//年月日显示子函数
void write_nyr(uchar add,uchar dat) //向LCD写年月日,有显示位置加数、显示数据,两个参数
{
uchar gw,sw;
gw=dat%10; //取得个位数字
sw=dat/10; //取得十位数字
write_1602com(yh+add); //设定显示位置为第一个位置+add
write_1602dat(0x30+sw); //数字+30得到该数字的LCD1602显示码
write_1602dat(0x30+gw); //数字+30得到该数字的LCD1602显示码
}
void write_week(uchar week) //写星期函数
{
write_1602com(yh+0x0c); //星期字符的显示位置
switch(week)
{
case 1:write_1602dat('M'); //星期数为1时,显示
write_1602dat('O');
write_1602dat('N');
break;
case 2:write_1602dat('T'); //星期数据为2时显示
write_1602dat('U');
write_1602dat('E');
break;
case 3:write_1602dat('W'); //星期数据为3时显示
write_1602dat('E');
write_1602dat('D');
break;
case 4:write_1602dat('T'); //星期数据为4是显示
write_1602dat('H');
write_1602dat('U');
break;
case 5:write_1602dat('F'); //星期数据为5时显示
write_1602dat('R');
write_1602dat('I');
break;
case 6:write_1602dat('S'); //星期数据为6时显示
write_1602dat('T');
write_1602dat('A');
break;
case 7:write_1602dat('S'); //星期数据为7时显示
write_1602dat('U');
write_1602dat('N');
break;
}
}
//****************键盘扫描有关函数**********************//
void keyscan()
{
if(key1==0) //key1为功能键(设置键)
{
delay(9); //延时,用于消抖动
if(key1==0) //延时后再次确认按键按下
{
buzzer=0; //蜂鸣器短响一次
delay(20);
buzzer=1;
while(!key1);
key1n++;
if(key1n==9)
key1n=1; //设置按键共有秒、分、时、星期、日、月、年、返回,8个功能循环
switch(key1n)
{
case 1: TR0=0; //关闭定时器
//TR1=0;
write_1602com(er+0x09); //设置按键按动一次,秒位置显示光标
write_1602com(0x0f); //设置光标为闪烁
temp=(miao)/10*16+(miao)%10; //秒数据写入DS1302
write_1302(0x8e,0x00);
write_1302(0x80,0x80|temp);//miao
write_1302(0x8e,0x80);
break;
case 2: write_1602com(er+6); //按2次fen位置显示光标
//write_1602com(0x0f);
break;
case 3: write_1602com(er+3); //按动3次,shi
//write_1602com(0x0f);
break;
case 4: write_1602com(yh+0x0e); //按动4次,week
//write_1602com(0x0f);
break;
case 5: write_1602com(yh+0x0a); //按动5次,ri
//write_1602com(0x0f);
break;
case 6: write_1602com(yh+0x07); //按动6次,yue
//write_1602com(0x0f);
break;
case 7: write_1602com(yh+0x04); //按动7次,nian
//write_1602com(0x0f);
break;
case 8:
write_1602com(0x0c); //按动到第8次,设置光标不闪烁
TR0=1; //打开定时器
temp=(miao)/10*16+(miao)%10;
write_1302(0x8e,0x00);
write_1302(0x80,0x00|temp); //miao数据写入DS1302
write_1302(0x8e,0x80);
break;
}
}
}
//------------------------------加键key2---------------------------- //
if(key1n!=0) //当key1按下以下。再按以下键才有效(按键次数不等于零)
{
if(key2==0) //上调键
{
delay(10);
if(key2==0)
{
buzzer=0; //蜂鸣器短响一次
delay(20);
buzzer=1;
while(!key2);
switch(key1n)
{
case 1:miao++; //设置键按动1次,调秒
if(miao==60)
miao=0;
write_sfm(0x08,miao);
temp=(miao)/10*16+(miao)%10;
write_1302(0x8e,0x00);
write_1302(0x80,temp);
write_1302(0x8e,0x80);
write_1602com(er+0x09);
//write_1602com(0x0b);
break;
case 2:fen++;
if(fen==60)
fen=0;
write_sfm(0x05,fen);
temp=(fen)/10*16+(fen)%10;
write_1302(0x8e,0x00);
write_1302(0x82,temp);
write_1302(0x8e,0x80);
write_1602com(er+6);
break;
case 3:shi++;
if(shi==24)
shi=0;
write_sfm(2,shi);
temp=(shi)/10*16+(shi)%10;
write_1302(0x8e,0x00);
write_1302(0x84,temp);
write_1302(0x8e,0x80);
write_1602com(er+3);
break;
case 4:week++;
if(week==8)
week=1;
write_1602com(yh+0x0C) ;
write_week(week);
temp=(week)/10*16+(week)%10;
write_1302(0x8e,0x00);
write_1302(0x8a,temp);
write_1302(0x8e,0x80);
write_1602com(yh+0x0e);
break;
case 5:ri++;
if(ri==32)
ri=1;
write_nyr(9,ri);
temp=(ri)/10*16+(ri)%10;
write_1302(0x8e,0x00);
write_1302(0x86,temp);
write_1302(0x8e,0x80);
write_1602com(yh+10);
break;
case 6:yue++;
if(yue==13)
yue=1;
write_nyr(6,yue);
temp=(yue)/10*16+(yue)%10;
write_1302(0x8e,0x00);
write_1302(0x88,temp);
write_1302(0x8e,0x80);
write_1602com(yh+7);
break;
case 7:nian++;
if(nian==100)
nian=0;
write_nyr(3,nian);
temp=(nian)/10*16+(nian)%10;
write_1302(0x8e,0x00);
write_1302(0x8c,temp);
write_1302(0x8e,0x80);
write_1602com(yh+4);
break;
}
}
}
//------------------减键key3,各句功能参照'加键'注释---------------//
if(key3==0)
{
delay(10); //调延时,消抖动
if(key3==0)
{
buzzer=0; //蜂鸣器短响一次
delay(20);
buzzer=1;
while(!key3);
switch(key1n)
{
case 1:miao--;
if(miao==-1)
miao=59;
write_sfm(0x08,miao);
temp=(miao)/10*16+(miao)%10;
write_1302(0x8e,0x00);
write_1302(0x80,temp);
write_1302(0x8e,0x80);
write_1602com(er+0x09);
//write_1602com(0x0b);
break;
case 2:fen--;
if(fen==-1)
fen=59;
write_sfm(5,fen);
temp=(fen)/10*16+(fen)%10;
write_1302(0x8e,0x00);
write_1302(0x82,temp);
write_1302(0x8e,0x80);
write_1602com(er+6);
break;
case 3:shi--;
if(shi==-1)
shi=23;
write_sfm(2,shi);
temp=(shi)/10*16+(shi)%10;
write_1302(0x8e,0x00);
write_1302(0x84,temp);
write_1302(0x8e,0x80);
write_1602com(er+3);
break;
case 4:week--;
if(week==0)
week=7;
write_1602com(yh+0x0C);
write_week(week);
temp=(week)/10*16+(week)%10;
write_1302(0x8e,0x00);
write_1302(0x8a,temp);
write_1302(0x8e,0x80);
write_1602com(yh+0x0e);
break;
case 5:ri--;
if(ri==0)
ri=31;
write_nyr(9,ri);
temp=(ri)/10*16+(ri)%10; //十进制转换成DS1302要求的DCB码
write_1302(0x8e,0x00); //允许写,禁止写保护
write_1302(0x86,temp); //向DS1302内写日期寄存器86H写入调整后的日期数据BCD码
write_1302(0x8e,0x80); //打开写保护
write_1602com(yh+10); //因为设置液晶的模式是写入数据后,指针自动加一,所以需要光标回位
break;
case 6:yue--;
if(yue==0)
yue=12;
write_nyr(6,yue);
temp=(yue)/10*16+(yue)%10; //十进制转换成DS1302要求的DCB码
write_1302(0x8e,0x00); //允许写,禁止写保护
write_1302(0x88,temp); //向DS1302内写月份寄存器88H写入调整后的月份数据BCD码
write_1302(0x8e,0x80); //打开写保护
write_1602com(yh+7); //因为设置液晶的模式是写入数据后,指针自动加一,所以需要光标回位
break;
case 7:nian--;
if(nian==-1)
nian=99;
write_nyr(3,nian);
temp=(nian)/10*16+(nian)%10;//十进制转换成DS1302要求的DCB码
write_1302(0x8e,0x00); //允许写,禁止写保护
write_1302(0x8c,temp); //向DS1302内写年份寄存器8cH写入调整后的年份数据BCD码
write_1302(0x8e,0x80); //打开写保护
write_1602com(yh+4); //因为设置液晶的模式是写入数据后,指针自动加一,所以需要光标回位
break;
}
}
}
}
}
void init() //定时器、计数器设置函数
{
TMOD=0x11; //指定定时/计数器的工作方式为3
TH0=0; //定时器T0的高四位=0
TL0=0; //定时器T0的低四位=0
EA=1; //系统允许有开放的中断
ET0=1; //允许T0中断
TR0=1; //开启中断,启动定时器
}
void timer0() interrupt 1 //取得并显示日历和时间,同时对A/D转换初始化
{
TH0=(65536-500)/256; //高八位
TL0=(65536-500)%256; //低八位
InitADC();
//读取秒时分周日月年七个数据
miao = BCD_Decimal(read_1302(0x81));
fen = BCD_Decimal(read_1302(0x83));
shi = BCD_Decimal(read_1302(0x85));
ri = BCD_Decimal(read_1302(0x87));
yue = BCD_Decimal(read_1302(0x89));
nian=BCD_Decimal(read_1302(0x8d));
week=BCD_Decimal(read_1302(0x8b));
//显示温度、秒、时、分数据:
write_sfm(8,miao); //秒,从第二行第8个字后开始显示(调用时分秒显示子函数)
write_sfm(5,fen); //分,从第二行第5个字符后开始显示
write_sfm(2,shi); //小时,从第二行第2个字符后开始显示
//显示日、月、年数据:
write_nyr(9,ri); //日期,从第二行第9个字符后开始显示
write_nyr(6,yue); //月份,从第二行第6个字符后开始显示
write_nyr(3,nian); //年,从第二行第3个字符后开始显示
write_week(week);
}
/*------------------------------------------------
ADC函数
------------------------------------------------*/
void InitADC() //初始AD寄存器
{
P1ASF=0Xd0; //0xff设置P1口全部为ADC通道,0X20:二进制:1100,0000设置P1.6,P1.7口为ADC通道
ADC_RES=0X00; //清除高8位缓冲数据
if(ADC_Chanul_Turn%3==0)//外部基准电压
{
ADC_CONTR=0xF6; //AD转换控制寄存器,1110,0110,(110:P16口)
_nop_();
_nop_();
_nop_();
_nop_();
ADC_CONTR=0xee; //1110,1110(清零Flag,置位Start)
}
if(ADC_Chanul_Turn%3==1)
{
ADC_CONTR=0xF7; //AD转换控制寄存器,1110,0111,(111:P17口)
_nop_();
_nop_();
_nop_();
_nop_();
ADC_CONTR=0xef; //1110,1111(清零Flag,置位Start)
}
if(ADC_Chanul_Turn%3==2)
{
ADC_CONTR=0xF4; //AD转换控制寄存器,1110,0100,(100:P14口)
_nop_();
_nop_();
_nop_();
_nop_();
ADC_CONTR=0xec; //1110,1111(清零Flag,置位Start)
}
}
void timer1() interrupt 3
{
static unsigned int hh ; //tt用来保存当前时间在一秒中的比例位置
scale=8;
hh++; //每250微秒增加1
if(hh==10) //2.5毫秒的时钟周期
{
hh=0; //使tt=0,开始新的PWM周期
led=1; //使LED灯亮
}
if(scale==hh) //按照当前占空比切换输出为高电平
led=0; //使LED灯灭
}
void timer1() interrupt 1
{
TH0=(65536-50000)/256; //开定时器0
TL0=(65536-50000)%256;
if(buzzer==1)
{
count++;
if(count>=3600)
{led=0;}
}
else
{led=1;count=0;}
}
void adc_isr() interrupt 5 //FLAG标志位置位触发中断,没有设优先级,但是同优先级下定时器0更高
{
if(ADC_Chanul_Turn%3==0) //外部基准电压
{
da1=ADC_RES; //获取转换结果
Data=((double)da1/256)*5; //取八位计算基准电压Data,
c =Data;
c=Data*100; //将转换后的值扩大100倍
}
if(ADC_Chanul_Turn%3==1)
{
da2=ADC_RES; //获取转换结果
Data=((double)da2/256)*5; //取八位计算实际值Data,
c =Data;
c=Data*100; //将转换后的值扩大100倍
}
if(ADC_Chanul_Turn%3==2)
{
da3=ADC_RES; //获取转换结果
Data=((double)da3/256)*5; //取八位计算实际值Data,
c=Data*100; //将转换后的值扩大100倍
}
ADC_CONTR&=0xEF; //标志位清零
ADC_Chanul_Turn++;
I f(ADC_Chanul_Turn==252)
ADC_Chanul_Turn=0;
}
void main()
{
du=0;
we=0;
lcd_init(); //调用液晶屏初始化子函数
ds1302_init(); //调用DS1302时钟的初始化子函数
init(); //调用定时计数器的设置子函数
buzzer=0; //蜂鸣器长响一次
delay(80);
buzzer=1;
TMOD=0x21;
TH0=(65536-500)/256; //开定时器0
TL0=(65536-500)%256;
TH1=0x06 ; //开定时器1
TL1=0x06;
EA=1; //开全局中断
ET0=1; //允许定时器零中断
ET1=1;
EADC=1; //允许ADC中断
TR0=1;
TR1=1;
while(1)
{
keyscan(); //调用键盘扫描子函数
if(c>0&&c<=100) {scale=9;} //占空90%
if(c>200&&c<250) {scale=5;} //占空50%
if(c>=350) {scale=2;} //占空20%
}
}
|