Materials:
-PIC16F877A
-7 segments display
-IC BCD to 7-segments decoder
-6 Leds
-12 push-buttons
-DC motor
Programme:
unsigned short X=0,i=0,y=0;
char ep=0;
void led_off()
{
PORTC.B2=0;
PORTC.B3=0;
PORTC.B4=0;
PORTC.B5=0;
PORTC.B6=0;
PORTC.B7=0;
}
void motor_right()
{
PORTC=1;
}
void motor_lift()
{
PORTC=2;
}
void flower_14()
{ led_off();
motor_lift();
do
{
if(portd>4)
{
motor_lift();
portd=portd-1;
Delay_ms(3000);
}
else if(portd<4)
{
motor_right();
portd=portd+1;
Delay_ms(3000);
}
portc.b1=0;
portc.b0=0;
portc.b2=0;
portc.b3=0;
portc.b4=0;
portc.b5=0;
portc.b6=1;
portc.b7=0;
ep&=~(1<<4);
y=4;
Delay_ms(1200);
}while(portd!=4);
}
void flower_13()
{
y=PORTD;
led_off();
while(portd!=3)
{
if(portd>3)
{
motor_lift();
portd=portd-1;
Delay_ms(3000);
}
else if(portd<3)
{
motor_right();
portd=portd+1;
Delay_ms(3000);
}
}
portc.b1=0;
portc.b0=0;
portc.b2=0;
portc.b3=0;
portc.b4=0;
portc.b5=1;
portc.b6=0;
portc.b7=0;
ep&=~(1<<3);
//y=3 ;
Delay_ms(1200);
}
void flower_12()
{ // y=PORTD;
led_off();
while(portd!=2)
{
if(portd>2)
{
motor_lift();
portd=portd-1;
Delay_ms(3000);
}
else if(portd<2)
{
motor_right();
portd=portd+1;
Delay_ms(3000);
}
}
portc.b1=0;
portc.b0=0;
portc.b2=0;
portc.b3=0;
portc.b4=1;
portc.b5=0;
portc.b6=0;
portc.b7=0;
ep&=~(1<<2);
//y=2;
Delay_ms(1200);
}
void flower_11()
{ // y=PORTD;
led_off();
while(portd!=1)
{
if(portd>1)
{
motor_lift();
portd=portd-1;
Delay_ms(3000);
}
else if(portd<1)
{
motor_right();
portd=portd+1;
Delay_ms(3000);
}
}
portc.b1=0;
portc.b0=0;
portc.b2=0;
portc.b3=1;
portc.b4=0;
portc.b5=0;
portc.b6=0;
portc.b7=0;
ep&=~(1<<1);
//y=1;
Delay_ms(1200);
}
/////////////////////////////////flower_0 function configration ////////////////////
void flower_0()
{
// y=PORTD;
led_off();
motor_lift();
while(portd!=0)
{
if(portd>0)
{
if((ep.b4==1)&&(y>=4))
{
flower_14();
}
if((ep.b3==1)&&(y>=3))
{
flower_13();
}
if((ep.b2==1)&&(y>=2))
{
flower_12();
}
if((ep.b1==1)&&(y>=1))
{
flower_11();
}
portd=portd-1;
motor_lift();
Delay_ms(3000);
}
else if(portd==0)
portd=1;
motor_lift();
}
Delay_ms(1200);
portc.b1=0;
portc.b0=0;
portc.b2=1;
portc.b3=0;
portc.b4=0;
portc.b5=0;
portc.b6=0;
portc.b7=0;
ep&=~(1);
y=0;
i=0;
Delay_ms(1200);
}
/////////////////////////////////flower_1 function configration ////////////////////
void flower_1()
{
led_off();
while(portd!=1)
{
if(portd>1)
{
//motor_lift();
if((ep.b4==1)&&(y>=4))
{
flower_14();
}
if((ep.b3==1)&&(y>=3))
{
flower_13();
}
if((ep.b2==1)&&(y>=2))
{
flower_12();
}
portd=portd-1;
motor_lift();
Delay_ms(3000);
}
else if(portd<1)
{
motor_right();
portd=portd+1;
Delay_ms(3000);
}
}
portc.b1=0;
portc.b0=0;
portc.b1=0;
portc.b0=0;
portc.b2=0;
portc.b3=1;
portc.b4=0;
portc.b5=0;
portc.b6=0;
portc.b7=0;
ep&=~(1<<1);
i=1;
Delay_ms(1200);
}
/////////////////////////////////flower_2 function configration ////////////////////
void flower_2()
{
led_off();
while(portd!=2)
{
if(portd>2)
{
motor_lift();
if((ep.b4==1)&&(y>=4))
{
flower_14();
}
if((ep.b3==1)&&(y>=3))
{
flower_13();
}
motor_lift();
portd=portd-1;
Delay_ms(3000);
}
else if(portd<2)
{
motor_right();
if((ep.b1==1&&(y<=1)))
{
flower_1();
}
portd=portd+1;
Delay_ms(3000);
}
}
portc.b1=0;
portc.b0=0;
portc.b1=0;
portc.b0=0;
portc.b2=0;
portc.b3=0;
portc.b4=1;
portc.b5=0;
portc.b6=0;
portc.b7=0;
ep&=~(1<<2);
i=2;
Delay_ms(1200);
}
/////////////////////////////////flower_3 function configration ////////////////////
void flower_3()
{
led_off();
while(portd!=3)
{
if(portd>3)
{
motor_lift();
if((ep.b4==1)&&(y>=4))
{
flower_14();
}
motor_lift();
portd=portd-1;
Delay_ms(3000);
}
else if(portd<3)
{
if((ep.b1==1)&&(y<=1))
{
flower_1();
}
if((ep.b2==1)&&(y<=2))
{
flower_2();
}
motor_right();
portd=portd+1;
Delay_ms(3000);
}
}
portc.b1=0;
portc.b0=0;
portc.b2=0;
portc.b3=0;
portc.b4=0;
portc.b5=1;
portc.b6=0;
portc.b7=0;
ep&=~(1<<3);
Delay_ms(1200);
}
/////////////////////////////////flower_4 function configration ////////////////////
void flower_4()
{
led_off();
while(portd!=4)
{
if(portd>4)
{
motor_lift();
portd=portd-1;
Delay_ms(3000);
}
else if(portd<4)
{
if((ep.b1==1)&&(y<=1))
{
flower_1();
}
if((ep.b2==1)&&(y<=2))
{
flower_2();
}
if((ep.B3==1)&&(y<=3))
{
flower_3();
}
motor_right();
portd=portd+1;
Delay_ms(3000);
}
}
portc.b1=0;
portc.b0=0;
portc.b2=0;
portc.b3=0;
portc.b4=0;
portc.b5=0;
portc.b6=1;
portc.b7=0;
ep&=~(1<<4);
y=4;
Delay_ms(1200);
}
/////////////////////////////////flower_5 function configration ////////////////////
void flower_5()
{
led_off();
motor_right();
while(portd!=5)
{
if(portd>5)
{
motor_lift();
portd=portd-1;
Delay_ms(3000);
}
else if(portd<5)
{
motor_right();
if((ep.b1==1)&&(y<=1))
{
flower_1();
}
if((ep.b2==1)&&(y<=2))
{
flower_2();
}
if((ep.B3==1)&&(y<=3))
{
flower_3();
}
if((ep.B4==1)&&(y<=4))
{
flower_4();
}
motor_right();
portd=portd+1;
Delay_ms(3000);
}
}
Delay_ms(1200);
portc.b1=0;
portc.b0=0;
portc.b2=0;
portc.b3=0;
portc.b4=0;
portc.b5=0;
portc.b6=0;
portc.b7=1;
ep&=~(1<<5);
y=5;
Delay_ms(1200);
}
//////////////////////////////////// floor_check function configration /////////////////////// ///
void floor_check()
{
if(portb.b0==1)
{
ep|=1; // floor states register
}
else if(portb.b1==1)
{
ep|=1<<1;
}
else if(portb.b2==1)
{
ep|=1<<2;
}
else if(portb.b3==1)
{
ep|=1<<3;
}
else if (portb.b4==1)
{
ep|=1<<4;
}
else if (portb.b5==1)
{
ep|=1<<5;
}
}
////////////////////////scan button function ///////////////////////
void interrupt()
{
y=PORTD;
if(intcon.b2==1)
{
x++;
if(x==5)
{
X=0;
floor_check();
}
intcon.b2=0;
tmr0=6;
}
}
void main()
{
option_reg=0b10000100;
intcon=0b11100000;
tmr0=6;
TRISA=255;
TRISD=0;
PORTD=0;
trisb=255;
trisc=0;
portc=0;
ADCON0=0B0110;
while(1)
{
if((ep.b0)==1) flower_0();
if((ep.b1)==1) flower_1();
if((ep.b2)==1) flower_2();
if((ep.b3)==1) flower_3();
if((ep.b4)==1) flower_4();
if((ep.b5)==1) flower_5();
}
}