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
  • Unfortunately I did not learn that much about actual coding itself.

    But you did learn (or at least should have learned) quite a lot from this thread.

    Delays generated using a compiler (might be a C, Pascal or whatever compiler) does give the possibility of unexpected changes to the delay time.

    Optimization of loops may sometime result in the loop being completely removed - a compiler that doesn't notice any side effect are free to ignore the meaningless code.

    It is possible to generate longer delays, by generating delays of known length and call that delay from a loop.

    Using numeric constants larger than supported can give results that may look non-obvious.

    The recommended method for creating delays are using timers (unless the delay should be extremely short, in which case you normally just add one or more NOP statements in assembler).

    Goto isn't forbidden, but shuld normally not be used for implementing state machines or loops. Their best - and possibly only - suggested use is to break out of multiple loops.

    Using tabs to indent source, results in slightly smaller source files, but the amount of indentation can't be guaranteed.

    Mixing spaces and tabs when indenting generates code that may look really funny (or not) when displayed with a different tab size.

    Setting breakpoints - or single-stepping - the code in the simulator would have shown problems with the delay functions.

    Some compilers generates warnings that are important to look at. Some compilers do not generate so many warnings, so you can't always rely on warnings/errors to catch all possible errirs in the source.

    You got some references to literature.

    I problably missed some things in the thread, but philosophy aside: There was definitely a lot to learn about programming in this thread.

Reply
  • Unfortunately I did not learn that much about actual coding itself.

    But you did learn (or at least should have learned) quite a lot from this thread.

    Delays generated using a compiler (might be a C, Pascal or whatever compiler) does give the possibility of unexpected changes to the delay time.

    Optimization of loops may sometime result in the loop being completely removed - a compiler that doesn't notice any side effect are free to ignore the meaningless code.

    It is possible to generate longer delays, by generating delays of known length and call that delay from a loop.

    Using numeric constants larger than supported can give results that may look non-obvious.

    The recommended method for creating delays are using timers (unless the delay should be extremely short, in which case you normally just add one or more NOP statements in assembler).

    Goto isn't forbidden, but shuld normally not be used for implementing state machines or loops. Their best - and possibly only - suggested use is to break out of multiple loops.

    Using tabs to indent source, results in slightly smaller source files, but the amount of indentation can't be guaranteed.

    Mixing spaces and tabs when indenting generates code that may look really funny (or not) when displayed with a different tab size.

    Setting breakpoints - or single-stepping - the code in the simulator would have shown problems with the delay functions.

    Some compilers generates warnings that are important to look at. Some compilers do not generate so many warnings, so you can't always rely on warnings/errors to catch all possible errirs in the source.

    You got some references to literature.

    I problably missed some things in the thread, but philosophy aside: There was definitely a lot to learn about programming in this thread.

Children
No data