寵物定時(shí)喂食器設(shè)計(jì)資料
寵物定時(shí)喂食器設(shè)計(jì)資料,寵物,定時(shí),喂食,設(shè)計(jì),資料
sbit DS1302CE=P0^0;
sbit DS1302CK=P0^2;
sbit DS1302IO=P0^1;
extern bit DS18B20_Reset(void);
extern void DS18B20_Write(unsigned char command);
extern unsigned char DS18B20_Read(void);
sbit DQ=P1^0;
#define RSCLR P0_5=0//PORTC &=~ BIT(PORTC0)
#define RSSET P0_5=1//PORTC |= BIT(PORTC0)
#define RWCLR P0_6=0//PORTC &=~ BIT(PORTC1)
#define RWSET P0_6=1//PORTC |= BIT(PORTC1)
#define ENCLR P0_7=0//PORTC &=~ BIT(PORTC2)
#define ENSET P0_7=1//PORTC |= BIT(PORTC2)
extern void lcd_delay(unsigned int delaytime);
extern void write_lcd_command(unsigned char write_command);
extern void write_lcd_data(unsigned char write_data);
extern void initize_lcd(void);
extern void lcd_displaystr(unsigned char x,unsigned char y,const unsigned char *str);
extern void lcd_displaystr_ram(unsigned char x,unsigned char y,unsigned char *str);
帶溫度測(cè)量LCD顯示的實(shí)時(shí)時(shí)鐘
extern void Init_DS1302_RTC(void);
extern unsigned char Read_DS1302_RTC(unsigned char RTC_Address);
extern void Write_DS1302_RTC(unsigned char RTC_Address,unsigned char RTC_Data);
#include
#include "1602LCDDriver.h"
#include "DS18B20Driver.h"
#include "DS1302Driver.h"
//#include "tiaozheng.h"
#include
struct TimerStruct
{
unsigned char Year;
unsigned char Month;
unsigned char Day;
unsigned char Week;
unsigned char Hour;
unsigned char Minute;
unsigned char Second;
};
//const unsigned char DATECODE1[]={"Tiaosh"};
//const unsigned char DATECODE[]={"Date:"};
//const unsigned char TIMECODE[]={"Time:"};
const unsigned char DATECODE1[]={"TH"};
const unsigned char DATECODE2[]={"DF"};
const unsigned char DATECODE3[]={"DJ"};
//const unsigned char DATECODE4[]={' '};
sbit key1=P1^2;
sbit key2=P1^3;
sbit key3=P1^4;
sbit sound=P1^1;
sbit k1=P0^3;
sbit k2=P0^4;
unsigned char mins,hours,days,months,weeks,years;
unsigned char a,b,c,k,flag1,flag2,flag3,flag4,flag5;
unsigned char Minute1,Hour1,Day1,Month1,Week1,Year1;
unsigned char Minute2,Hour2,Day2,Month2,Week2,Year2;
unsigned char DATECODE22[1]={' '};
unsigned char DATECODE33[1]={' '};
//struct TimerStruct *Real;
struct TimerStruct RealClock;
unsigned char DateBuffer1[]={"2006/08/18/5"};
unsigned char TimeBuffer1[]= {"23:50:00"};
//const unsigned char DATECODE[]={"Date:"};
//const unsigned char TIMECODE[]={"Time:"};
unsigned char DateBuffer[]={"2005/01/01/5"};
unsigned char TimeBuffer[]= {"00:00:00"};
unsigned char LCDDisplayBuffer[]={" "};
unsigned int SecondCounter;
//蜂鳴器測(cè)試
void fmq_fre(unsigned char f)
{
unsigned int i,j;
for(i=f;i>0;i--)
for(j=248;j>0;j--); //延時(shí)0.5MS
}
void fmq_delay(unsigned char delaytimef)
{
unsigned char j,h;
for(j=delaytimef;j>0;j--)
//for(k=200;k>0;k--)
for(h=248;h>0;h--)
{
sound=1;
fmq_fre(1);
sound=0;
fmq_fre(1);
}
}
//jdq的測(cè)試
void jdq_delay(unsigned char delaytimej)
{
unsigned char k,i,j;
for (k=delaytimej;k>0;k--)
for(i=200;i>0;i--)
for(j=248;j>0;j--);
}
void jdq(void)
{
k1=0;
k2=0;
}
//時(shí)間調(diào)整
void tiao_delay(void)
{
unsigned char i,j;
for(i=10;i>0;i--)
for(j=248;j>0;j--);
}
unsigned char change( void )
{
while(flag1)
{
if(key2==0) tiao_delay();
if(key2==0) a++;
if(key3==0) tiao_delay();
if(key3==0) a--;
switch(k)
{
case 1:
mins=a%60;
break;
case 2:
hours=a%24;
break;
case 3:
days=a%32;
break;
case 4:
weeks=a%8;
break;
case 5:
months=a%13;
break;
case 6:
years=a;
break;
}
TimeBuffer1[4]=mins%10+0x30; //分個(gè)位
TimeBuffer1[3]=mins/10+0x30;
TimeBuffer1[1]=hours%10+0x30; //時(shí)
TimeBuffer1[0]=hours/10+0x30;
DateBuffer1[9]=days%10+0x30; //日
DateBuffer1[8]=days/10+0x30;
DateBuffer1[6]=months%10+0x30;//月
DateBuffer1[5]=months/10+0x30;
DateBuffer1[11]=weeks%10+0x30;//星期
DateBuffer1[3]=years%10+0x30; //年
DateBuffer1[2]=years/10+0x30;
jdq_delay(2);
if(key1==0) tiao_delay();
if(key1==0) flag1=0;
write_lcd_command(12);
lcd_displaystr_ram(0,1,DateBuffer1);
lcd_displaystr_ram(1,1,TimeBuffer1);
}
}
void tiao1(void)
{
mins=mins-mins/16*6;
hours=hours-hours/16*6;
days=days-days/16*6;
months=months-months/16*6;
years=years-years/16*6;
TimeBuffer1[4]=mins%10+0x30; //分個(gè)位
TimeBuffer1[3]=mins/10+0x30;
TimeBuffer1[1]=hours%10+0x30; //時(shí)
TimeBuffer1[0]=hours/10+0x30;
DateBuffer1[9]=days%10+0x30; //日
DateBuffer1[8]=days/10+0x30;
DateBuffer1[6]=months%10+0x30;//月
DateBuffer1[5]=months/10+0x30;
DateBuffer1[11]=weeks%10+0x30;//星期
DateBuffer1[3]=years%10+0x30; //年
DateBuffer1[2]=years/10+0x30;
lcd_displaystr_ram(0,1,DateBuffer1);
lcd_displaystr_ram(1,1,TimeBuffer1);
write_lcd_command(0x0f);
write_lcd_command(0x80+0x45);
jdq_delay(2);
k=1,a=mins,flag1=1;
change(); //分
write_lcd_command(0x0f);
write_lcd_command(0x80+0x42);
jdq_delay(2);
k++,a=hours,flag1=1;
change(); //時(shí)
write_lcd_command(0x0f);
write_lcd_command(0x80+10);
jdq_delay(2);
k++,a=days,flag1=1;
change(); //日
}
void tiao2(void)
{
write_lcd_command(0x0f);
write_lcd_command(0x80+12);
jdq_delay(2);
k++,a=years,flag1=1;
change(); //星期
write_lcd_command(0x0f);
write_lcd_command(0x80+7);
jdq_delay(2);
k++,a=months,flag1=1;
change(); //月
write_lcd_command(0x0f);
write_lcd_command(0x80+4);
jdq_delay(2);
k++,a=years,flag1=1;
change(); //年
mins=mins+mins/10*6;
hours=hours+hours/10*6;
days=days+days/10*6;
months=months+months/10*6;
years=years+years/10*6;
jdq_delay(5);
}
//調(diào)整1302時(shí)間
void tiaozheng(struct TimerStruct *Real)
{
lcd_displaystr(0,14,DATECODE1);
mins=Real->Minute;
hours=Real->Hour;
days=Real->Day;
months=Real->Month;
weeks=Real->Week;
years=Real->Year;
tiao1();
tiao2();
write_lcd_command(0x01);//清屏
Write_DS1302_RTC(0x8e,0x00); //寫控制
Write_DS1302_RTC(0x80,0x00); //寫秒
Write_DS1302_RTC(0x82,mins); //寫分
Write_DS1302_RTC(0x84,hours); //寫時(shí)
Write_DS1302_RTC(0x86,days); //寫日
Write_DS1302_RTC(0x88,months); //寫月
Write_DS1302_RTC(0x8a,weeks); //寫星期
Write_DS1302_RTC(0x8c,years); //寫年
Write_DS1302_RTC(0x8e,0x80); //控制
initize_lcd();
Init_DS1302_RTC();
}
//調(diào)整蜂鳴器定時(shí)
void dingshif(void)
{
flag3=1;
lcd_displaystr(0,14,DATECODE2);
mins=Minute1;
hours=Hour1;
days=Day1;
//months=Month1;
//weeks=Week1;
//years=Year1;
tiao1();
tiao_delay();
mins=mins+mins/10*6;
hours=hours+hours/10*6;
days=days+days/10*6;
Minute1=mins;
Hour1=hours;
Day1=days;
//Month1=months;
//Week1=weeks;
//Year1=years;
write_lcd_command(0x01);//清屏
DATECODE22[0]='&';
if(flag4==1) DATECODE33[0]='!';
jdq_delay(5);
lcd_displaystr(0,14,DATECODE22);
lcd_displaystr(0,15,DATECODE33);
}
//調(diào)整繼電器定時(shí)
void dingshij(void)
{
flag4=1;
lcd_displaystr(0,14,DATECODE3);
mins=Minute2;
hours=Hour2;
days=Day2;
//months=Month2;
//weeks=Week2;
//years=Year2;
tiao1();
mins=mins+mins/10*6;
hours=hours+hours/10*6;
days=days+days/10*6;
Minute2=mins;
Hour2=hours;
Day2=days;
// Month2=months;
//Week2=weeks;
//Year2=years;
k1=k2=1;
write_lcd_command(0x01); //清屏
DATECODE33[0]='!';
if(flag3==1) DATECODE22[0]='&';
jdq_delay(5);
lcd_displaystr(0,14,DATECODE22);
lcd_displaystr(0,15,DATECODE33);
}
//蜂鳴器報(bào)警
void dingshifmq(void)
{
flag3=0;flag2=10;
if( RealClock.Hour=Hour1)
if( RealClock.Day=Day1)
//if( RealClock.Month=Month1)
// if( RealClock.Year=Year1)
{
while(flag2)
{
fmq_delay(1);
flag2--;
if(key1==0) tiao_delay();
if(key1==0) flag2=0;
if(key1==0) tiao_delay();
if(key1==0) flag2=0;
if(key1==0) tiao_delay();
if(key1==0) flag2=0;
if(key1==0) tiao_delay();
if(key1==0) flag2=0;
}
Minute1=Minute1-2;
write_lcd_command(0x01);//清屏
DATECODE22[0]=' ';
if(flag4==0) DATECODE33[0]=' ';
jdq_delay(5);
lcd_displaystr(0,14,DATECODE22);
lcd_displaystr(0,15,DATECODE33);
jdq_delay(10);
}
}
//繼電器取消
void dingshijdq(void)
{
flag4=0;
if( RealClock.Hour=Hour2)
if( RealClock.Day=Day2)
// if( RealClock.Month=Month2)
// if( RealClock.Year=Year2)
tiao_delay();
k1=0,k2=0;
write_lcd_command(0x01);//清屏
DATECODE33[0]=' ';
if(flag3==0) DATECODE22[0]=' ';
jdq_delay(5);
lcd_displaystr(0,14,DATECODE22);
lcd_displaystr(0,15,DATECODE33);
Minute2=Minute2-1;
}
//主函數(shù)
void main(void)
{
jdq();
RealClock.Minute=Minute1=Minute2;
RealClock.Hour=Hour1=Hour2;
RealClock.Day=Day1=Day2;
RealClock.Month=Month1=Month2;
RealClock.Year=Year1=Year2;
flag5=1;
fmq_delay(1);
initize_lcd();
Init_DS1302_RTC();
//Write_DS1302_RTC(0x8e,0x00); //寫控制
Write_DS1302_RTC(0x80,0x00); //寫秒
//Write_DS1302_RTC(0x82,0x30); //寫分
//Write_DS1302_RTC(0x84,0x12); //寫時(shí)
//Write_DS1302_RTC(0x86,0x15); //寫日
//Write_DS1302_RTC(0x88,0x08); //寫月
//Write_DS1302_RTC(0x8a,0x3); //寫星期
//Write_DS1302_RTC(0x8c,0x06); //寫年
//Write_DS1302_RTC(0x8e,0x80); //控制
while(1)
{
unsigned int Temp,Temperature;
unsigned char TemperatureH;
unsigned char TemperatureL;
//struct TimerStruct RealClock;
while(DS18B20_Reset());
DS18B20_Write(0xCC);
DS18B20_Write(0xBE);
TemperatureL=DS18B20_Read(); //低位
TemperatureH=DS18B20_Read(); //高位
while(DS18B20_Reset());
DS18B20_Write(0xCC);
DS18B20_Write(0x44);
Temp=TemperatureH*256+TemperatureL; //左移8位
if(Temp & 0xf800) //去除低7位
{
LCDDisplayBuffer[0]=' ';
Temp=(~Temp)+1;
TemperatureL=Temp%256;
TemperatureH=Temp/256;
}
Temp>>=4;
Temperature=Temp; //右移四位
LCDDisplayBuffer[1]=Temp/10+0x30;
LCDDisplayBuffer[2]=Temp%10+0x30;
Temp=TemperatureL;
Temp&=0x000F; //保留低四位
Temp*=625; //化小數(shù)
Temp/=100;
LCDDisplayBuffer[3]='.';
LCDDisplayBuffer[4]=Temp/10+0x30;
LCDDisplayBuffer[5]=Temp%10+0x30;
LCDDisplayBuffer[6]='C';
Temp=Read_DS1302_RTC(0x81); //秒
RealClock.Second=Temp;
TimeBuffer[7]=Temp%16+0x30;
TimeBuffer[6]=Temp/16+0x30;
Temp=Read_DS1302_RTC(0x83); //分
RealClock.Minute=Temp;
TimeBuffer[4]=Temp%16+0x30;
TimeBuffer[3]=Temp/16+0x30; //時(shí)
Temp=Read_DS1302_RTC(0x85);
RealClock.Hour=Temp;
TimeBuffer[1]=Temp%16+0x30;
TimeBuffer[0]=Temp/16+0x30;
Temp=Read_DS1302_RTC(0x87); //日
RealClock.Day=Temp;
DateBuffer[9]=Temp%16+0x30;
DateBuffer[8]=Temp/16+0x30;
Temp=Read_DS1302_RTC(0x89); //月
RealClock.Month=Temp;
DateBuffer[6]=Temp%16+0x30;
DateBuffer[5]=Temp/16+0x30;
Temp=Read_DS1302_RTC(0x8b); //星期
RealClock.Week=Temp;
DateBuffer[11]=Temp%16+0x30;
Temp=Read_DS1302_RTC(0x8d); //年
RealClock.Year=Temp;
DateBuffer[3]=Temp%16+0x30;
DateBuffer[2]=Temp/16+0x30;
lcd_displaystr_ram(0,1,DateBuffer);
lcd_displaystr_ram(1,1,TimeBuffer);
lcd_displaystr_ram(1,sizeof(TimeBuffer),LCDDisplayBuffer);
if(key1==0) tiao_delay();
if(key1==0) tiaozheng(&RealClock);
if(key2==0) tiao_delay();
if(key2==0) dingshif();
if(key3==0) dingshij();
if(key3==0) tiao_delay();
if(RealClock.Minute==Minute1) dingshifmq();
if(RealClock.Minute==Minute2) dingshijdq();
if(RealClock.Minute==0x00)
if(flag5==1)
{
fmq_delay(2),flag5=0;
}
if(RealClock.Minute==0x02) flag5=1;
//if(Temperature>=0x3500) fmq_delay(1);
//if(Temperature<=0x3300) fmq_delay(2);
}
}
1602LCDDriver
#include
#include "1602LCDDriver.h"
void lcd_delay(unsigned int delaytime)
{
while(delaytime)delaytime--;
}
void write_lcd_command(unsigned char write_command)
{
lcd_delay(25);
RSCLR;
RWCLR;
//LCDPORT=write_command;
P2_0=(bit)(write_command & 0x80);
write_command+=write_command;
P2_1=(bit)(write_command & 0x80);
write_command+=write_command;
P2_2=(bit)(write_command & 0x80);
write_command+=write_command;
P2_3=(bit)(write_command & 0x80);
write_command+=write_command;
P2_4=(bit)(write_command & 0x80);
write_command+=write_command;
P2_5=(bit)(write_command & 0x80);
write_command+=write_command;
P2_6=(bit)(write_command & 0x80);
write_command+=write_command;
P2_7=(bit)(write_command & 0x80);
ENSET;
ENCLR;
}
void write_lcd_data(unsigned char write_data)
{
lcd_delay(25);
RSSET;
RWCLR;
//LCDPORT=write_data;
P2_0=(bit)(write_data & 0x80);
write_data += write_data;
P2_1=(bit)(write_data & 0x80);
write_data+=write_data;
P2_2=(bit)(write_data & 0x80);
write_data+=write_data;
P2_3=(bit)(write_data & 0x80);
write_data+=write_data;
P2_4=(bit)(write_data & 0x80);
write_data+=write_data;
P2_5=(bit)(write_data & 0x80);
write_data+=write_data;
P2_6=(bit)(write_data & 0x80);
write_data+=write_data;
P2_7=(bit)(write_data & 0x80);
ENSET;
ENCLR;
}
void initize_lcd(void)
{
unsigned char i;
ENCLR;
for(i=200;i>0;i--)lcd_delay(248);
write_lcd_command(0x3f);
for(i=4;i>0;i--)lcd_delay(248);
write_lcd_command(0x3f);
write_lcd_command(0x0f);
write_lcd_command(0x01);
for(i=4;i>0;i--)lcd_delay(248);
write_lcd_command(0x06);
}
void lcd_displaystr(unsigned char x,unsigned char y,const unsigned char *str)
{
switch(x)
{
case 0:
write_lcd_command(0x80+y);
break;
case 1:
write_lcd_command(0xc0+y);
break;
}
while(*str)
{
write_lcd_data(*str);
str++;
}
}
void lcd_displaystr_ram(unsigned char x,unsigned char y,unsigned char *str)
{
switch(x)
{
case 0:
write_lcd_command(0x80+y);
break;
case 1:
write_lcd_command(0xc0+y);
break;
}
while(*str)
{
write_lcd_data(*str);
str++;
}
}
DS18B20Driver
#include
#include
#include "DS18B20Driver.h"
bit DS18B20_Reset(void)
{
unsigned char i;
DQ=0;
//for(i=255;i>0;i--);
for(i=255;i>0;i--);
DQ=1;
// for(i=60;i>0;i--);
for(i=60;i>0;i--);
return(DQ);
//for(i=200;i>0;i--);
for(i=200;i>0;i--);
}
void DS18B20_Write(unsigned char command)
{
unsigned char i;
unsigned char j;
for(i=8;i>0;i--)
{
if((command & 0x01)==0)
{
DQ=0;
//for(j=35;j>0;j--);
for(j=35;j>0;j--);
DQ=1;
}
else
{
DQ=0;
//for(j=2;j>0;j--);
for(j=2;j>0;j--);
DQ=1;
//for(j=33;j>0;j--);
for(j=33;j>0;j--);
}
command=_cror_(command,1);
}
}
unsigned char DS18B20_Read(void)
{
unsigned char i;
unsigned char j;
unsigned char temp;
temp=0;
for(i=8;i>0;i--)
{
temp=_cror_(temp,1);
DQ=0;
_nop_();
_nop_();
DQ=1;
//for(j=10;j>0;j--);
for(j=10;j>0;j--);
if(DQ==1)
{
temp=temp | 0x80;
}
else
{
temp=temp | 0x00;
}
// for(j=200;j>0;j--);
for(j=200;j>0;j--);
}
return(temp);
}
DS1302Driver
#include
#include "DS1302Driver.h"
#include
void Init_DS1302_RTC(void)
{
DS1302CE=0;
DS1302CK=0;
DS1302IO=1;
}
unsigned char Read_DS1302_RTC(unsigned char RTC_Address)
{
unsigned char i;
unsigned char temp;
DS1302CK=0;
DS1302CE=1;
for(i=8;i>0;i--)
{
if(RTC_Address & 0x01)DS1302IO=1;
else DS1302IO=0;
RTC_Address>>=1;
DS1302CK=1;
DS1302CK=0;
}
temp=0;
if(DS1302IO==1)temp+=0x80;
for(i=7;i>0;i--)
{
temp>>=1;
DS1302CK=1;
DS1302CK=0;
if(DS1302IO==1)temp+=0x80;
}
DS1302CE=0;
return(temp);
}
void Write_DS1302_RTC(unsigned char RTC_Address,unsigned char RTC_Data)
{
unsigned char i;
DS1302CK=0;
DS1302CE=1;
for(i=8;i>0;i--)
{
if(RTC_Address & 0x01)DS1302IO=1;
else DS1302IO=0;
RTC_Address>>=1;
DS1302CK=1;
DS1302CK=0;
}
for(i=8;i>0;i--)
{
if(RTC_Data & 0x01)DS1302IO=1;
else DS1302IO=0;
RTC_Data>>=1;
DS1302CK=1;
DS1302CK=0;
}
DS1302CE=0;
}
8
收藏