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; }
Unfortunately I did not learn that much about actual coding itself.
That makes me want to sit down and cry.
Why did you not learn anything about coding ?
The first problem your code runs into was described in great detail.
Ways to tackle the next problems you're going to run into were described in good detail.
Good coding practices were described (I'm sorry if you mistook them for philosophical issues. Once you've been trying to unravel a sparsely commented, goto-infested hunk of spaghetti code, you are going to realize that good coding practices are essential in everyday programming and not an abstract matter of philosophy.).
Ah, here's another one: Comments ! Uncommented source code is worthless. Everyone (including yourself) will have a hard time understanding what the code is supposed to do without comments. If you don't have a clear perception of what the code is supposed to do, it becomes very hard (close to impossible) to find out where and why the code is not doing what it is supposed to do.
Don't play the macho tough guy programmer who can memorize every tiny bit of information about his programs and keep that memory for months or years. Chances are, you can't (like most others programmers). Even if you can, you're going to make the life of any other programmer you deal with (that includes those you ask to look at your source code and give you hints) miserable.
Good commenting practices are even more important than having ultra-elite coding skills. Once you've got the comments down (yes, write them first), generating the appropriate code is a fairly simple matter (where most problems can be solved by just consulting a textbook).
Uncommented source code
there is no such thing, if there are no comments, it is not code, it is scribbles
Erik