#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%
}
}
|