2.0.2015020801

Correction from Johan PA3FPQ on OPTION_HH12_10_BIT_READINGS
This commit is contained in:
Anthony Good 2015-02-08 19:24:13 -05:00
parent cdad7efcc9
commit 41bc6ec491
3 changed files with 153 additions and 142 deletions

290
hh12.cpp Executable file → Normal file
View File

@ -1,141 +1,149 @@
#if defined(ARDUINO) && ARDUINO >= 100 #if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h" #include "Arduino.h"
#else #else
#include "WProgram.h" #include "WProgram.h"
#endif #endif
#include "hh12.h" #include "hh12.h"
/* /*
Code adapted from here: http://www.madscientisthut.com/forum_php/viewtopic.php?f=11&t=7 Code adapted from here: http://www.madscientisthut.com/forum_php/viewtopic.php?f=11&t=7
Updated 2015-02-07 for 12 bit readings - Thanks Johan PA3FPQ Updated 2015-02-07 for 12 bit readings - Thanks Johan PA3FPQ
*/ */
int hh12_clock_pin = 0; int hh12_clock_pin = 0;
int hh12_cs_pin = 0; int hh12_cs_pin = 0;
int hh12_data_pin = 0; int hh12_data_pin = 0;
int inputstream = 0; //one bit read from pin int inputstream = 0; //one bit read from pin
long packeddata = 0; //two bytes concatenated from inputstream long packeddata = 0; //two bytes concatenated from inputstream
long angle = 0; //holds processed angle value long angle = 0; //holds processed angle value
float floatangle = 0; float floatangle = 0;
#ifdef OPTION_HH12_10_BIT_READINGS #ifdef OPTION_HH12_10_BIT_READINGS
long anglemask = 65472; //0x1111111111000000: mask to obtain first 10 digits with position info long anglemask = 65472; //0x1111111111000000: mask to obtain first 10 digits with position info
#else #else
long anglemask = 262080; // 0x111111111111000000: mask to obtain first 12 digits with position info long anglemask = 262080; // 0x111111111111000000: mask to obtain first 12 digits with position info
#endif //OPTION_HH12_10_BIT_READINGS #endif //OPTION_HH12_10_BIT_READINGS
long statusmask = 63; //0x000000000111111; mask to obtain last 6 digits containing status info long statusmask = 63; //0x000000000111111; mask to obtain last 6 digits containing status info
long statusbits; //holds status/error information long statusbits; //holds status/error information
int DECn; //bit holding decreasing magnet field error data int DECn; //bit holding decreasing magnet field error data
int INCn; //bit holding increasing magnet field error data int INCn; //bit holding increasing magnet field error data
int OCF; //bit holding startup-valid bit int OCF; //bit holding startup-valid bit
int COF; //bit holding cordic DSP processing error data int COF; //bit holding cordic DSP processing error data
int LIN; //bit holding magnet field displacement error data int LIN; //bit holding magnet field displacement error data
//----------------------------------------------------------------------------------------------------- //-----------------------------------------------------------------------------------------------------
hh12::hh12(){ hh12::hh12(){
} }
//----------------------------------------------------------------------------------------------------- //-----------------------------------------------------------------------------------------------------
void hh12::initialize(int _hh12_clock_pin, int _hh12_cs_pin, int _hh12_data_pin){ void hh12::initialize(int _hh12_clock_pin, int _hh12_cs_pin, int _hh12_data_pin){
hh12_clock_pin = _hh12_clock_pin; hh12_clock_pin = _hh12_clock_pin;
hh12_cs_pin = _hh12_cs_pin; hh12_cs_pin = _hh12_cs_pin;
hh12_data_pin = _hh12_data_pin; hh12_data_pin = _hh12_data_pin;
pinMode(hh12_clock_pin, OUTPUT); pinMode(hh12_clock_pin, OUTPUT);
pinMode(hh12_cs_pin, OUTPUT); pinMode(hh12_cs_pin, OUTPUT);
pinMode(hh12_data_pin, INPUT); pinMode(hh12_data_pin, INPUT);
} }
//----------------------------------------------------------------------------------------------------- //-----------------------------------------------------------------------------------------------------
float hh12::heading(){ float hh12::heading(){
digitalWrite(hh12_cs_pin, HIGH); // CSn high digitalWrite(hh12_cs_pin, HIGH); // CSn high
digitalWrite(hh12_clock_pin, HIGH); // CLK high digitalWrite(hh12_clock_pin, HIGH); // CLK high
digitalWrite(hh12_cs_pin, LOW); // CSn low: start of transfer digitalWrite(hh12_cs_pin, LOW); // CSn low: start of transfer
delayMicroseconds(HH12_DELAY); // delay for chip initialization delayMicroseconds(HH12_DELAY); // delay for chip initialization
digitalWrite(hh12_clock_pin, LOW); // CLK goes low: start clocking digitalWrite(hh12_clock_pin, LOW); // CLK goes low: start clocking
delayMicroseconds(HH12_DELAY); // hold low delayMicroseconds(HH12_DELAY); // hold low
for (int x=0; x <16; x++) // clock signal, 16 transitions, output to clock pin #ifdef OPTION_HH12_10_BIT_READINGS
{ for (int x=0; x <16; x++) // clock signal, 16 transitions, output to clock pin
digitalWrite(hh12_clock_pin, HIGH); //clock goes high #else
delayMicroseconds(HH12_DELAY); // for (int x=0; x <18; x++) // clock signal, 18 transitions, output to clock pin
inputstream =digitalRead(hh12_data_pin); // read one bit of data from pin #endif //OPTION_HH12_10_BIT_READINGS
#ifdef DEBUG_HH12 {
Serial.print(inputstream, DEC); digitalWrite(hh12_clock_pin, HIGH); //clock goes high
#endif delayMicroseconds(HH12_DELAY); //
packeddata = ((packeddata << 1) + inputstream);// left-shift summing variable, add pin value inputstream =digitalRead(hh12_data_pin); // read one bit of data from pin
digitalWrite(hh12_clock_pin, LOW); #ifdef DEBUG_HH12
delayMicroseconds(HH12_DELAY); // end of one clock cycle Serial.print(inputstream, DEC);
} #endif
// end of entire clock cycle packeddata = ((packeddata << 1) + inputstream);// left-shift summing variable, add pin value
digitalWrite(hh12_clock_pin, LOW);
delayMicroseconds(HH12_DELAY); // end of one clock cycle
}
#ifdef DEBUG_HH12 // end of entire clock cycle
Serial.print("hh12: packed:");
Serial.println(packeddata,DEC);
Serial.print("hh12: pack bin: ");
Serial.println(packeddata,BIN); #ifdef DEBUG_HH12
#endif Serial.print("hh12: packed:");
Serial.println(packeddata,DEC);
angle = packeddata & anglemask; // mask rightmost 6 digits of packeddata to zero, into angle Serial.print("hh12: pack bin: ");
Serial.println(packeddata,BIN);
#ifdef DEBUG_HH12 #endif
Serial.print("hh12: mask: ");
Serial.println(anglemask, BIN); angle = packeddata & anglemask; // mask rightmost 6 digits of packeddata to zero, into angle
Serial.print("hh12: bin angle:");
Serial.println(angle, BIN); #ifdef DEBUG_HH12
Serial.print("hh12: angle: "); Serial.print("hh12: mask: ");
Serial.println(angle, DEC); Serial.println(anglemask, BIN);
#endif Serial.print("hh12: bin angle:");
Serial.println(angle, BIN);
angle = (angle >> 6); // shift 16-digit angle right 6 digits to form 10-digit value Serial.print("hh12: angle: ");
Serial.println(angle, DEC);
#ifdef DEBUG_HH12 #endif
Serial.print("hh12: angleshft:");
Serial.println(angle, BIN); angle = (angle >> 6); // shift 16-digit angle right 6 digits to form 10-digit value
Serial.print("hh12: angledec: ");
Serial.println(angle, DEC); #ifdef DEBUG_HH12
#endif Serial.print("hh12: angleshft:");
Serial.println(angle, BIN);
floatangle = angle * 0.3515; // angle * (360/1024) == actual degrees Serial.print("hh12: angledec: ");
Serial.println(angle, DEC);
#ifdef DEBUG_HH12 #endif
statusbits = packeddata & statusmask;
DECn = statusbits & 2; // goes high if magnet moved away from IC #ifdef OPTION_HH12_10_BIT_READINGS
INCn = statusbits & 4; // goes high if magnet moved towards IC floatangle = angle * 0.3515; // angle * (360/1024) == actual degrees
LIN = statusbits & 8; // goes high for linearity alarm #else
COF = statusbits & 16; // goes high for cordic overflow: data invalid floatangle = angle * 0.08789; // angle * (360/4096) == actual degrees
OCF = statusbits & 32; // this is 1 when the chip startup is finished #endif //OPTION_HH12_10_BIT_READINGS
if (DECn && INCn) {
Serial.println("hh12: magnet moved out of range"); #ifdef DEBUG_HH12
} else { statusbits = packeddata & statusmask;
if (DECn) { DECn = statusbits & 2; // goes high if magnet moved away from IC
Serial.println("hh12: magnet moved away from chip"); INCn = statusbits & 4; // goes high if magnet moved towards IC
} LIN = statusbits & 8; // goes high for linearity alarm
if (INCn) { COF = statusbits & 16; // goes high for cordic overflow: data invalid
Serial.println("hh12: magnet moved towards chip"); OCF = statusbits & 32; // this is 1 when the chip startup is finished
} if (DECn && INCn) {
} Serial.println("hh12: magnet moved out of range");
if (LIN) { } else {
Serial.println("hh12: linearity alarm: magnet misaligned? data questionable"); if (DECn) {
} Serial.println("hh12: magnet moved away from chip");
if (COF) { }
Serial.println("hh12: cordic overflow: magnet misaligned? data invalid"); if (INCn) {
} Serial.println("hh12: magnet moved towards chip");
#endif //DEBUG_HH12 }
}
if (LIN) {
return(floatangle); Serial.println("hh12: linearity alarm: magnet misaligned? data questionable");
}
if (COF) {
} Serial.println("hh12: cordic overflow: magnet misaligned? data invalid");
}
#endif //DEBUG_HH12
return(floatangle);
}

1
hh12.h Executable file → Normal file
View File

@ -4,6 +4,7 @@
#define HH12_DELAY 100 // microseconds #define HH12_DELAY 100 // microseconds
//#define OPTION_HH12_10_BIT_READINGS //#define OPTION_HH12_10_BIT_READINGS
class hh12 { class hh12 {
public: public:

View File

@ -315,9 +315,11 @@
OPTION_HH12_10_BIT_READINGS in hh12.h (thanks Johan PA3FPQ) OPTION_HH12_10_BIT_READINGS in hh12.h (thanks Johan PA3FPQ)
Correction from Johan PA3FPQ on OPTION_HH12_10_BIT_READINGS
*/ */
#define CODE_VERSION "2.0.2015020702" #define CODE_VERSION "2.0.2015020801"
#include <avr/pgmspace.h> #include <avr/pgmspace.h>
#include <EEPROM.h> #include <EEPROM.h>