I am trying to program the 8051 for a "Pelican" type pedestrian road crossing. It should control the red, amber and green lights for the traffic, the red and green pedestrian lights, and the bleeper. I have written some code but unfortunately it does not work. Bits 0 to 5 of P1 are supposed to control the lights and the bleeper and bit 0 of P2 is supposed to control the pedestrian button. Any ideas where the problem is? Code size limit is 2K. Thanks! Marko
#include <reg51.h> sbit red_traf=P1^0; sbit amber_traf=P1^1; sbit green_traf=P1^2; sbit red_ped=P1^3; sbit green_ped=P1^4; sbit bleeper=P1^5; sbit button=P2^0; unsigned char count; void delay_vshort() { unsigned int x; for (x=0;x<65536;x++); } void delay_short() { unsigned int x; for (x=0;x<131072;x++); } void delay_long() { unsigned int x; for (x=0;x<1048576;x++); } main() { P2 = 0xff; s0: green_traf=1; red_ped=1; if (button==1) goto s1; else goto s0; s1: amber_traf=1; red_ped=1; delay_short(); goto s2; s2: red_traf=1; green_ped=1; bleeper=1; delay_long(); goto s3; s3: for (count=0;count<5;count++) { amber_traf = 1; green_ped = 1; delay_vshort(); amber_traf = 0; green_ped = 0; delay_vshort(); } goto s4; s4: amber_traf=1; red_ped=1; delay_short(); goto s0; }
I had to make your code readable to comment on it, so I include it for the benefit of otheres
include <reg664.h> //Register map for 8051. sbit red_traf=P1^0; //Red traffic light is connected to bit 0 of P1. sbit amber_traf=P1^1; //Amber traffic light is connected to bit 1 of P1. sbit green_traf=P1^2; //Green traffic light is connected to bit 2 of P1. sbit red_ped=P1^3; //Red pedestrian light is connected to bit 3 of P1. sbit green_ped=P1^4; //Green pedestrian light is connected to bit 4 of P1. sbit bleeper=P1^5; //Bleeper is connected to bit 5 of P1. sbit button=P2^0; //Pedestrian button is connected to bit 0 of P2. unsigned char count; void delay_vshort() //This defines a very short delay. { unsigned int x; for (x=0;x<65536;x++); } void delay_short() //This defines a short delay. { unsigned int x; for (x=0;x<131072;x++); } void delay_long() //This defines a long delay. { unsigned int x; for (x=0;x<1048576;x++); } main() { P1 = 0x00; //P1 defined as an output port. P2 = 0xff; //P2 defined as an input port. s0: green_traf=1; //Green traffic light is on. red_ped=1; //Red pedstrian light is on. if (button==1) goto s1; //Check if button was pressed. else goto s0; s1: green_traf=0; //Switch off green traffic light. amber_traf=1; //Switch on amber traffic light. delay_short(); //Leave amber traffic light on for a short time. s2: green_traf=0; //Switch off green traffic light. red_traf=1; //Swich on red traffic light. red_ped=0; //Switch off red pedestrian light. green_ped=1; //Switch on green pedestrian light. bleeper=1; //Switch on the bleeper. delay_long(); //Remain in the same state for a long time. s3: red_traf=0; //Switch off red traffic light. for (count=0;count<5;count++) //This will make amber traffic and green pedestrian light flash five times. { amber_traf = 1; //Switch amber traffic light on. green_ped = 1; //Switch green pedestrian light on. delay_vshort(); //Wait for a very short time. amber_traf = 0; //Switch both previous lights off. green_ped = 0; delay_vshort(); } s4: bleeper=0; //Turn off the bleeper. green_ped=0; //Turn off green pedestrian light. amber_traf=1; //Turn on amber traffic light. red_ped=1; //Turn on red pedestrian light. delay_short(); //Wait for a short time. goto s0; //Return to the beginning. }
go through your code and follow it under the assumption that the button is held only for a millisecond
fix what you see by that and then come back
Erik
I am afraid I do not understand.
step through your code 'on paper' follow the process when the button is only detected pressed once.
THAT is known as debugging