View previous topic :: View next topic |
Author |
Message |
Dewey
Joined: 07 Feb 2008 Posts: 4
|
delay_us works but not delay_ms |
Posted: Thu Feb 07, 2008 9:11 pm |
|
|
Trying to make a traffic light controller but I'm very new to this, so please be patient! I am probably doing something really dumb here, but this is driving me nuts! If I substitue us for ms in the code below it will work, although the lights flash very quickly! However, when using ms, the code stops execution at the first delay_ms. If I use us for the first delay and ms for the second, it halts at the second delay.
#include <16F877.h>
#fuses XT,NOWDT,NOPROTECT,NOPUT
#use delay(clock=4000000)
#use fixed_IO(b_outputs = PIN_B1,PIN_B2,PIN_B3)
#use fixed_IO(e_outputs = PIN_E0,PIN_E1,PIN_E2)
void main()
{
char c;
while(1)
{
output_low(PIN_E2); //turns off unused RE2 output
output_low(PIN_B1); //turns off red light
output_low(PIN_B2); //turns off yellow light
output_low(PIN_E0); //turns off don't walk light
output_high(PIN_B3); //turns on green light
output_high(PIN_E1); //turns on walk light
delay_ms(1000);
output_low(PIN_E1); //turns off walk light
for(c = 0; c < 6; c++) //loop to flash don't walk
{
output_high(PIN_E0); //turns on don't walk light
delay_ms(1000);
output_low(PIN_E0); //turns off don't walk light
delay_ms(1000);
}
output_high(PIN_E0); //turns on don't walk light
output_low(PIN_B3); //turns off green light
output_high(PIN_B2); //turns on yellow light
delay_ms(1000);
output_low(PIN_B2); //turns off yellow light
output_high(PIN_B1); //turns on red light
delay_ms(1000);
}
} |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Feb 07, 2008 9:18 pm |
|
|
Quote: | #include <16F877.h>
#fuses XT, NOWDT, NOPROTECT, NOPUT, NOLVP
#use delay(clock=4000000) |
Add NOLVP to your fuses, as shown in bold above. Without it,
when pin B3 goes high, the PIC goes into programming mode.
The lack of NOLVP may be causing your strange symptoms. |
|
|
ELCouz
Joined: 18 Jul 2007 Posts: 427 Location: Montreal,Quebec
|
|
Posted: Thu Feb 07, 2008 9:21 pm |
|
|
Quote: | If I substitue us for ms in the code below it will work, although the lights flash very quickly! |
This is the problem ,, you shouldn't see a led flashing a 1000us, it should be always on (flashing but not noticeable at least).
and if delay_ms(1000); dont work... probably it work but very slowly(ex: wrong osc. conf ? )...
what is your XT osc. you are using ?
EDIT Quote: | The lack of NOLVP may be causing your strange symptoms. |
I second that!
Best Regards,
Laurent |
|
|
Dewey
Joined: 07 Feb 2008 Posts: 4
|
|
Posted: Thu Feb 07, 2008 9:46 pm |
|
|
I tried NOLVP added to fuses and it did not help. It will work with us but not ms. When in us mode, I was trapping on it with a logic analyzer - that was how I knew it was working. Also, like I said, I can use us for the first delay and it will get past that and hang at the next delay that is ms. The osc freq is 4MHz - verified with a scope. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Feb 07, 2008 9:50 pm |
|
|
Post your compiler version. It's a 4-digit number in this format: x.xxx
You can find it at the top of the .LST file, which will be in your project
directory. |
|
|
Dewey
Joined: 07 Feb 2008 Posts: 4
|
|
Posted: Thu Feb 07, 2008 9:52 pm |
|
|
4.038 |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Feb 07, 2008 9:54 pm |
|
|
Also post a description of the external circuits that are connected to
your i/o pins. Give the component values, where applicable. |
|
|
Dewey
Joined: 07 Feb 2008 Posts: 4
|
|
Posted: Fri Feb 08, 2008 8:24 pm |
|
|
Got it working! After doing some searching on the net, I found a function similar to this:
void delay_zs(int16 t)
{
do
{
delay_us(65500);
} while(--t);
}
The function gives me about .5mS of delay, so I just call the function (which I named delay_zs just to prevent any possible conflict with the built in ms function) 20,000 times to get about 10 seconds of delay. Crude, but effective! Thanks for all your help! |
|
|
|