|
|
View previous topic :: View next topic |
Author |
Message |
ocean65
Joined: 16 Jun 2016 Posts: 3
|
PIC16F876A delay_ms not working [Solved] |
Posted: Thu Jun 16, 2016 7:26 am |
|
|
Hello,
I'm using the latest version of CCS C Compiler.
When I run the program in Proteus everything works perfect, when I program the hex file using IC-Prog on my PIC it doesn't work.
From the code, I can see by reading the EEPROM that the code stops when delay_ms(1000); is called.
I have tried everything I can imagine, fuse bits XT to HS, different crystals, etc...
I'm running the PIC with external clock XTAL 4MHz (2 - capacitor 15 pF), MCLR is connect to 5V.
I also tried to use #use delay with clock, crystal and both as you can see.
Any suggestions ?
Thank you very much.
Fuse bits : PWRT and BODEN ON ; XT
Code :
main.c
Code: |
#include <main.h>
#use delay (clock=4000000)
#define LAST_VOLUME 10
#define LAST_VOLUME1 11
#define LAST_VOLUME2 12
#define LAST_VOLUME3 12
void main()
{
while(TRUE)
{
int volume = 15;
write_eeprom(LAST_VOLUME,volume);
OUTPUT_C(0x00);
volume++;
write_eeprom(LAST_VOLUME1,volume);
delay_ms(1000);
volume++;
write_eeprom(LAST_VOLUME3,volume);
OUTPUT_C(0xFF);
delay_ms(1000);
}
}
|
main.h
Code: |
#include <16F876A.h>
#device ADC=16
#use delay(crystal=4000000)
|
Last edited by ocean65 on Thu Jun 16, 2016 6:00 pm; edited 1 time in total |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19545
|
|
Posted: Thu Jun 16, 2016 10:25 am |
|
|
Probably nothing to do with the oscillator, and nothing to do with delay_ms.
The chip is watch-dogging and restarting.
NOWDT fuse needed. The chip defaults to watchdog enabled.
If you look at the data sheet, and Register 14-1, you will see that the WDTEN fuse defaults to '1' (enabled).
As written, it'll actually kill the EEPROM in typically about 2000 seconds, so I hope you haven't left it switched on for long running this code (watchdog at typically 18mSec, two EEPROM writes each time it starts, EEPROM life 100K cycles). |
|
|
ocean65
Joined: 16 Jun 2016 Posts: 3
|
|
Posted: Thu Jun 16, 2016 10:39 am |
|
|
Hi Ttelmah,
Thank you very much for your reply. Correct IF the code didn't stop in the first delay. Even, with 2 sec delay it would take a little longer.
What I have notice in my circuit is that I have about 5V in the output pin, for this reason the code stops in the first delay call. Nothing connect, I see the output pins with a multimeter and I see what I expect (0V 2 sec .... 5V 2 sec ... ).
Thank you for the information about the EEPROM life .
Maybe it's a fault in my circuit.
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19545
|
|
Posted: Thu Jun 16, 2016 12:08 pm |
|
|
No. It is watch-dogging in the delay, restarting etc. etc..
It starts. Does the two writes (4mSec each typically), then enters the delay. Approximately 10mSec later the watchdog triggers, so it restarts, and the whole cycle begins again.
So every watchdog timeout, it does two writes. It restarts, so the delay doesn't get executed....
Just add the NOWDT fuse, and see what happens. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Jun 16, 2016 4:04 pm |
|
|
I don't see how you get that. If I compile his program in Release mode
with PCM vs. 5.059, the .LST file shows the compiler defaults to NOWDT:
Quote: | Configuration Fuses:
Word 1: 3F71 XT NOWDT PUT BROWNOUT NOLVP NOCPD NOWRT NODEBUG NOPROTECT |
His code, made into one source file. He has no #fuses line.
Code: | #include <16F876A.h>
#device ADC=16
#use delay(crystal=4000000)
#define LAST_VOLUME 10
#define LAST_VOLUME1 11
#define LAST_VOLUME2 12
#define LAST_VOLUME3 12
void main()
{
while(TRUE)
{
int volume = 15;
write_eeprom(LAST_VOLUME,volume);
OUTPUT_C(0x00);
volume++;
write_eeprom(LAST_VOLUME1,volume);
delay_ms(1000);
volume++;
write_eeprom(LAST_VOLUME3,volume);
OUTPUT_C(0xFF);
delay_ms(1000);
}
}
|
|
|
|
ocean65
Joined: 16 Jun 2016 Posts: 3
|
|
Posted: Thu Jun 16, 2016 6:06 pm |
|
|
Hello Ttelmah & PCM programmer,
Thank you so much for your you answer's. The fault wasn't in the compiler but in a bad 7805 (voltage regulator) that had a current problem.
For this reason, after the output it just blocked the microprocessor.
Sorry and thank you very much for your time. Solved. |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|