|
|
View previous topic :: View next topic |
Author |
Message |
iso9001
Joined: 02 Dec 2003 Posts: 262
|
Code works on 16F872 but not 16F876A ?! |
Posted: Mon Aug 23, 2004 5:00 pm |
|
|
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 |
Posted: Mon Aug 23, 2004 6:44 pm |
|
|
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 ?! |
Posted: Tue Aug 24, 2004 3:11 am |
|
|
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
|
|
Posted: Tue Aug 24, 2004 10:42 am |
|
|
Thanks... that makes sense.... stupid 8bit |
|
|
Ttelmah Guest
|
|
Posted: Tue Aug 24, 2004 10:54 am |
|
|
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 |
|
|
|
|
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
|