Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix (interpolation overflow) #23

Merged
merged 1 commit into from
Apr 16, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Cheers!
Updates
=======

2014-04-04 Release v1.00
2014-04-11 Release v1.01 (Increase approximative ramping steps to 64)
2014-04-15 Release v1.02 (Improved temperature averaging)
2014-04-04 Release v1.00 First release
2014-04-11 Release v1.01 Increase approximative ramping steps to 64
2014-04-15 Release v1.02 Improved temperature averaging
2014-04-16 Release v1.03 Added leaky integration filtering, improved averaging (+ bugfix)
57 changes: 28 additions & 29 deletions page0.c
Original file line number Diff line number Diff line change
Expand Up @@ -416,9 +416,13 @@ static void interrupt_service_routine(void) __interrupt 0 {
*/
void main(void) __naked {
unsigned int millisx60=0;
unsigned int ad_filter;

init();

// Initialize 'leaky' integrator
ad_filter = ((ADRESH << 8) | ADRESL) << 6;

//Loop forever
while (1) {

Expand All @@ -431,43 +435,38 @@ void main(void) __naked {
TMR6IF = 0;
}

if(TMR4IF){
if(TMR4IF) {

millisx60++;

if((millisx60 & 0x1) == 0){
unsigned char i;
int temp = 0;
unsigned int ad_result;
// Read and accumulate AD value, note at this point temperature variable is not temp, but AD acc
ad_result = (ADRESH << 8) | ADRESL;

// Alarm on sensor error (AD result out of range)
LATA0 = (ad_result >= 992 || ad_result < 32);

// Interpolate between lookup table points
for (i = 0; i < 32; i++) {
if((ad_result & 0x1f) <= i){
temp += ad_lookup[((ad_result >> 5) & 0x1f)];
} else {
temp += ad_lookup[((ad_result >> 5) & 0x1f) + 1];
}
}

// Divide by 32 and accumulate
temperature += (temp >> 5);
// Accumulate and filter A/D values (leaky integrator)
ad_filter = ad_filter - (ad_filter >> 6) + ((ADRESH << 8) | ADRESL);

// Start new conversion
ADGO = 1;
// Start new conversion
ADGO = 1;


}
// Alarm on sensor error (AD result out of range)
// LATA0 = (ad_result >= 992 || ad_result < 32);
LATA0 = (ad_filter >= 63488 || ad_filter <= 2047);

// Only run every 16th time called, that is 16x60ms = 960ms
// Close enough to 1s for our purposes.
if((millisx60 & 0xf) == 0){

temperature >>= 3; // Divide by 8 to get back to a temperature
if((millisx60 & 0xf) == 0) {
{
unsigned char i;
long temp = 0;

// Interpolate between lookup table points
for (i = 0; i < 64; i++) {
if(((ad_filter >> 5) & 0x3f) <= i) {
temp += ad_lookup[((ad_filter >> 11) & 0x1f)];
} else {
temp += ad_lookup[((ad_filter >> 11) & 0x1f) + 1];
}
}
// Divide by 64 to get back to normal temperature
temperature = (temp >> 6);
}

temperature += eeprom_read_config(EEADR_TEMP_CORRECTION);

Expand Down
Loading