CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

PIC16F876A delay_ms not working [Solved]

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
ocean65



Joined: 16 Jun 2016
Posts: 3

View user's profile Send private message

PIC16F876A delay_ms not working [Solved]
PostPosted: Thu Jun 16, 2016 7:26 am     Reply with quote

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: 19561

View user's profile Send private message

PostPosted: Thu Jun 16, 2016 10:25 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Jun 16, 2016 10:39 am     Reply with quote

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 . Wink
Maybe it's a fault in my circuit.
Confused
Ttelmah



Joined: 11 Mar 2010
Posts: 19561

View user's profile Send private message

PostPosted: Thu Jun 16, 2016 12:08 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Jun 16, 2016 4:04 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Jun 16, 2016 6:06 pm     Reply with quote

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. Smile Solved.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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