This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

pedestrian road crossing - c code

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;
        }

Parents
  • 'goto' is so strongly frowned upon that I think you might have difficulty finding anyone who's every actually used one in 'C'!! :-0

    s0:
            green_traf=1;
            red_ped=1;
            if (button==1) goto s1;
            else goto s0;
    
    s1:
    

    Consider how you could do this with a while loop.

            goto s2;
    
    s2:
            red_traf=1;
    


    gotos like this are totally pointless: the code flow naturally proceeds from one statement to the next - the is no point to a goto that simply goes to the next statement!

Reply
  • 'goto' is so strongly frowned upon that I think you might have difficulty finding anyone who's every actually used one in 'C'!! :-0

    s0:
            green_traf=1;
            red_ped=1;
            if (button==1) goto s1;
            else goto s0;
    
    s1:
    

    Consider how you could do this with a while loop.

            goto s2;
    
    s2:
            red_traf=1;
    


    gotos like this are totally pointless: the code flow naturally proceeds from one statement to the next - the is no point to a goto that simply goes to the next statement!

Children
  • I do know that goto is not very popular in c. However last time I did any programing I was using BASIC on C64 and I was five years old. Besides the problem is somewhere else. I will try to tidy up the program when it actually works. I did run the program in Keil simulator, but what happens is that all bits of P1 and P2 are constantly high when they should be flashing according to the four states I have defined.