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 support@ccsinfo.com

Code works on 16F872 but not 16F876A ?!

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



Joined: 02 Dec 2003
Posts: 262

View user's profile Send private message

Code works on 16F872 but not 16F876A ?!
PostPosted: Mon Aug 23, 2004 5:00 pm     Reply with quote

I have some code that is working great on a 16F872, but not the 16F876A.... I'm 99% sure it has to do with the eeprom code... any ideas ?

(btw the led does come on for 2 seconds)

Code:

#include <16F876.h>
#fuses HS,LVP,NOWDT,NOPROTECT, PUT, NOBROWNOUT
#use delay(clock=20000000)
#Define LuckyCharmsAreGreat 1

int ct=0;

void main() {
output_high(PIN_A2); //LED ON
delay_ms(2000);

output_low(PIN_A2);

while(LuckyCharmsAreGreat)
 {

 for (ct=0; ct<256; ct++)
   write_eeprom(ct, ct);

 while(LuckyCharmsAreGreat)       //Inf Loop
  output_high(PIN_A2);
 }
}


Hmmm... as a hunch I just read the eeprom, its filled all the way to the end... am I getting an error due to the 'ct' variable ? I think at the end of the code its reaching 256 and has a max of 255.... I though it should just roll over though... Instead of locking up... no ?
Guest
Guest







16F876A vs. 16F872
PostPosted: Mon Aug 23, 2004 6:44 pm     Reply with quote

Don't know nuthin' about the 16F872, but be sure to change your device header file to #include <16F876A.h> just in case. I had code that worked great on the 16F877 but not on the 16F877A. In particular, the "write_eeprom" command was screwed when I changed devices. Hmmmm......
Ttelmah
Guest







Re: Code works on 16F872 but not 16F876A ?!
PostPosted: Tue Aug 24, 2004 3:11 am     Reply with quote

iso9001 wrote:
I have some code that is working great on a 16F872, but not the 16F876A.... I'm 99% sure it has to do with the eeprom code... any ideas ?

(btw the led does come on for 2 seconds)

Code:

#include <16F876.h>
#fuses HS,LVP,NOWDT,NOPROTECT, PUT, NOBROWNOUT
#use delay(clock=20000000)
#Define LuckyCharmsAreGreat 1

int ct=0;

void main() {
output_high(PIN_A2); //LED ON
delay_ms(2000);

output_low(PIN_A2);

while(LuckyCharmsAreGreat)
 {

 for (ct=0; ct<256; ct++)
   write_eeprom(ct, ct);

 while(LuckyCharmsAreGreat)       //Inf Loop
  output_high(PIN_A2);
 }
}


Hmmm... as a hunch I just read the eeprom, its filled all the way to the end... am I getting an error due to the 'ct' variable ? I think at the end of the code its reaching 256 and has a max of 255.... I though it should just roll over though... Instead of locking up... no ?

It'll 'roll over', but this means the loop will never end.
Think about it. For the loop to 'stop', ct, must reach 256. As it stands, the code will kep looping through the EEPROM, writing as fast as it can (but using up write cycles on the EEPROM...).
You cannot test a variable that can only hold 255, against a 'target' of 256, and expect any sensible bhaviour. :-)

Best Wishes
iso9001+
Guest







PostPosted: Tue Aug 24, 2004 10:42 am     Reply with quote

Thanks... that makes sense.... stupid 8bit
Ttelmah
Guest







PostPosted: Tue Aug 24, 2004 10:54 am     Reply with quote

iso9001+ wrote:
Thanks... that makes sense.... stupid 8bit

Worth saying, why not just do it the other way?. Use the 'Do...While' loop, which does the test at the end of the loop, and exit when it gets to 255, rather than trying to get to 256!. So:
Code:

 ct=0;
 do {
   write_eeprom(ct, ct);
   while {ct++<255);

Since the increment is after the test (hence ct++, rather than ++ct), it'll exit, when the value has passed through the loop containing 255.
This is the standard approach for the same problem in 16 bit systems, when wanting to execute all 65536 loops.

Best Wishes
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