We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
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; }
'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!
"unfortunately it does not work"
So what does it do? You cannot hope to solve a problem if you can't even describe its symptoms!
Are you sure that your LEDs and button are wired correctly & working?
Have you run the code in the simulator?
Hi.
You have Evalution version with restriction for compile program for single 51 !!!
Please read >
# The startup code generated includes LJMPs. Code generated cannot be used in single-chip devices that support 2K Bytes or less of program space. # Programs start at offset 0x0800. Programs generated with the evaluation software may not be programmed into single-chip devices with less than 2K Bytes of on-chip ROM.
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.
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. I think that the problem is in a way I defined which bits of P1 should be high or low and the way P2 bits are defined. But because of my limited knowledge I am unable to pinpoint the problem.
Hi! I am using the Phillips P89C664 device and I have done some very basic stuff on it so far and everything worked in simulator and on the actual board. However I was using assembler(including LJMP command) and not c.
However I was using assembler(including LJMP command) and not c.
The LJMP command is not the issue. It's the limitations of the compiler (regarding program size and location) that you may run into at some point.
http://www.keil.com/demo/limits.asp
Back to your program:
1. You're writing delay routines in C. That is an absolute no-no. Don't. You will never get a specific delay this way. You may not get any delay at all, since the optimizer is free to completely remove these functions and any calls to them from the code, since they do not have side effects and do not return a value. For delays, use assembly or a timer.
2. "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."
You did read and internalize the specifics of the '51s ports ? Such as that there is a port state and a latch state, which may be different depending on the circuitry of the port (both internal and external) ?
3.
unsigned int x; for (x=0;x<65536;x++);
You do realize that the '51 is not a 32 bit system, and that unsigned int is a 16 bit value (0...65535) ?
None of your delay functions will ever return. They're endless loops !
4. Please please get some sort of textbook on elementary, basic C programming. Experienced programmers may refuse to further look at your code when they see the first unnecessary goto (as a rule of thumb: use goto only to exit deeply-nested loops and in certain cases of error handling). Read up on the various types of control structures that C offers (for, while, do/while, switch/case) and use them.
main() { P2 = 0xff; s0:
Are your lights on or off when a port pin is high?
Erik
re your lights on or off when a port pin is high?
Erik,
no need to get that technical .... yet. Did you look at the "delay" functions ?
They never return.
well, I only look for the first i see, then, when that is corrected, I will look at the following.
I would expect a decent compiler to generate a warning about too large constant, if it sees the value 65536 without the suffixed "l" or "ul". Preferably, the compiler should warn about a constant larger than 32767 if not suffixed with "u".
About goto: Yes, I have used it a number of times. But never for a state machine. That is very poor programming, and completely unmaintainable. The only way a goto is good to have, is as a multi-loop break statement, instead of inventing one or stupid exit variables.
I would expect a decent compiler to generate a warning about too large constant, if it sees the value 65536 without the suffixed "l" or "ul". <p>
I would even expect that a compiler warns about meaningless comparisons, such as (unsigned value < 0) or (16 bit unsigned value > 65536).
But, then again, some people tend to ignore compiler warnings, since they don't abort the compilation process.
I'd like to know where this code is to be used, I'd be scared to cross the street when the sign control is coded by a programmer that inexperiencecd.
Marko, get the experience in an area where life and limb is not affected by 'mistakes' in the code.
I'd like to know where this code is to be used,
I guess (and hope) that the code is merely being written for self-educational purposes.
"4. Please please get some sort of textbook on elementary, basic C programming."
Don't even mention "BASIC"...! ;-)
Reading list: http://www.keil.com/books/8051books.asp www.8052.com/books.phtml
The ACCU (Association of C & C++ Users) also has a whole load of book reviews - including a section on embedded:
brian.accu.org/.../