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; }
How do I program three different delays each 0.5s, 4s and 10s long respectively?
Use timers. That's what your controller has them for.
To get rid of these warnings I was using unnecessary goto statements at the end of each state.
As opposed to just throwing out those useless labels, that is? Wow, that's impressively sick.
I definetly need to learn a lot more about programming. But I do think that you cold do well with a simple etiquette course. Calling people sick just because they do not know how to write good code is a bit over the edge.
Calling people sick just because they do not know how to write good code is a bit over the edge
In many peoples eyes using 'goto' is 'sick' but they did not say so (I would, in most cases, consider a goto 'stupid' but did not say so.) All restrained themselves and said 'not advisable'
Now you compound the issue by adding a goto for no other purpose than getting rid of an 'unused label' warning and I can easily see that makes somebody not able to hold his tongue.
Do note, you added these labels after being informed that goto was 'not advisable'
So, there is no reason to get miffed becuase of a word, I could easily get miffed over some of your statements, but shall not give examples.
Erik
I definetly need to learn a lot more about programming.
Not really. Your statement that you know this code was poorly written means you already know more about programming than is evident in the code --- but you explictly decided not to make use of that knowledge in this case. What you apparently need to learn is to actually apply that knowledge.
Calling people sick just because [...] is a bit over the edge
Well, then I guess we're all happy that I did no such thing. That comment was directed at an action, not a person.
Goto in and of itself isn't sick. There are valid reasons to use one in specific situations. But adding a goto to get rid of a warning about an unused label is unexcusable.