闲来无事,给家里狗狗制作了自动喂食机,
代码如下:
#include<reg52.h>
sbit led1=P2^0;
sbit led2=P2^1;
sbit led3=P2^2;
sbit led4=P2^3;
sbit led5=P2^4;
sbit led6=P2^5;
sbit FZ =P3^7;
sbit IN1=P1^3;
sbit IN2=P1^2;
sbit IN3=P2^7;
sbit IN4=P2^6;
void Delay10ms(unsigned int c) ;
unsigned char code LedChar[]={
0x40, 0x79, 0x24, 0x30, 0x19, 0x12, 0x02, 0x78,
0x00, 0x10
};
unsigned char LedBuff[]={
0xff, 0xff, 0xff, 0xff, 0xff, 0xff
};
unsigned int cnt = 0;
void main()
{
unsigned long sec;
sec=0x00;
TMOD = 0x01;
TH0 = 0xFC;
TL0 = 0x66;
TR0 = 1;
EA = 1;
ET0 = 1;
while(1)
{
if(cnt >= 1000)
{
cnt = 0;
sec++;
//if(sec==20)
//// {
// FZ=0;Delay5ms(50) ;FZ=1;Delay5ms(50) ; FZ=0;Delay5ms(50); FZ=1;Delay5ms(50);
// IN1=1;IN2=0; Delay5ms(50) ;IN1=0;IN2=1; Delay5ms(50) ;
// IN1=1;IN2=0; Delay5ms(50) ;IN1=0;IN2=1; Delay5ms(50) ;
// IN1=IN2=0;
// }
LedBuff[0] = LedChar[sec%3600%60%10];
LedBuff[1] = LedChar[sec%3600%60/10];
LedBuff[2] = LedChar[sec%3600/60%10];
LedBuff[3] = LedChar[sec%3600/60/10];
LedBuff[4] = LedChar[sec/3600%10];
LedBuff[5] = LedChar[sec/3600/10];
if(sec>=43200)
{
sec=0;
}
}
}
}
unsigned char i = 0;
void InterruptTimer0() interrupt 1
{
TH0 = 0xFC;
TL0 = 0x66;
cnt++;
P0=P2 = 0xff;
switch(i)
{
case 0:led1=0; i++; P0=LedBuff[0];break;
case 1:led2=0; i++; P0=LedBuff[1];break;
case 2:led3=0; i++; P0=LedBuff[2];break;
case 3:led4=0; i++; P0=LedBuff[3];break;
case 4:led5=0; i++; P0=LedBuff[4];break;
case 5:led6=0; i=0; P0=LedBuff[5];break;
default:break;
}
}//
void Delay10ms(unsigned int c)
{
unsigned char a, b;
for (;c>0;c--)
{
for (b=249;b>0;b--)
{
for (a=17;a>0;a--);
}
}
}
|