: : Thanks for a quick response!
: : The code does compile.(BTW I am using Keil Vision3). The code is supposed to run a 8051 based microcontroller. P1 is an 8-bit output port
: : of 8051 and P2 is an 8-bit input port of 8051. Bits 0 to 5 of P1 should control the light signals and the bleeper and bit 0 of P2 is supposed to control the pedestrian button(P2 = 0xff means P2 is set as an input port = all bits are 1). The program is supposed to be an infinite loop, traffic lights do usually run continuously.
: : Regards,
: : Marko
: :
:
: I think I know the problem:
:
: P1^1 means bit P1 xor 00000001 = xxxxxxx1
: x is either 0 or 1... it depends on the corresponding bits of P1
: I think what you want is (P1 & 00000001) = 0000000x, where x = 0 if P1's first bit is 0 and x = 1 if P1's first bit is 1.
:
: Furthermore, the fourth bit is not given by P1 & 3, but by P1 & 8, or better yet P1 & (1 << 3). Continuing this logic:
: Check if the n-th bit of P1 is set <-> P1 & (1 << n)
:
: On a further note, if it eventually is doing something, but not exactly what you'd expect, check if the bits of P1 truly correspond with what you think they do (for instance, the bit order might be reversed and what you think is the 1st bit is actually the 8th bit)
:
: Best Regards,
: Richard
:
: The way I see it... Well, it's all pretty blurry
:
:
Well... I am not quiet sure what P1^1 means Port1 bit 1 in your compiler. But most compilers P1.1 means Port1 bit 1.
Assuming that P1^1 means Port1 bit 1, What i have noticed is that you never cleared the green_traf and the red_traf.
I also suggest you avoid using the goto statement ( I am not saying its wrong but it makes the code difficult to read ). I suggest you use the while loop. Maybe you should try it like this:
main()
{
P1 = 0x00; //This would clear all LEDs
while(1)
{
green_traf=1;
red_ped=1;
while(!button); //wait for button to be pressed
green_traf=0; //switch off green LED
amber_traf=1; //switch on Orange LED
delay_short();
amber_traf=0; //switch off Orange LED
for (count=0;count<5;count++)
{
red_traf=1; //switch on Red LED
green_ped = 1;
delay_vshort();
green_ped = 0;
delay_vshort();
green_traf=1;
delay_short();
green_traf=0;
amber_traf=1; //switch on Orange LED
delay_vshort();
amber_traf=0; //switch off Orange LED
}
amber_traf=1;
red_ped=1;
delay_short();
amber_traf=0;
}
}
It did not test the code, but i hope it helps in your mission.
loco