Quote:
Originally Posted by kE7JLM
We have a Devantech CMPS03 Digital Compass I would apresiate if someone could give me some step by step instructs on hw to do this, please.
|
I've been wanting to publish the code for this, as well as some other code, for quite some time, but I can't get a hold of Kevin Watson to allow me to reprint parts of his code. So here's the best I can do.
Step one: Get a copy of Kevin Watson's interrupt code
here.
Step two: Enable timer 1, set the prescaler to 1:8, change register operations to 8-bit accesses, and disable the timer 1 interrupt. These are all documented in Initialize_Timer_1(). Also, enable interrupt 1 in Initialize_Interrupts().
Step three: Add these declarations to the top of interrupts.c. Increasing the value of SAMPLES should decrease the noise in your readings, to an extent:
Code:
#define SAMPLES 1
unsigned char Temp_Buf;
unsigned int Timer_Snapshot;
unsigned int Readings[SAMPLES];
unsigned char p=0;
Step four: Replace the blank Int_1_Handler() with:
Code:
void Int_1_Handler(void)
{
Temp_Buf = TMR1L;
Timer_Snapshot = TMR1H;
if(Temp_Buf == 0xFF)
Timer_Snapshot--;
Timer_Snapshot <<= 8;
Timer_Snapshot += Temp_Buf;
TMR1L = 0;
TMR1H = 0;
Readings[p++] = ((Timer_Snapshot - 0x4000) / 125) % 360;
p %= SAMPLES;
}
Step five: Add this to the end of interrupts.c (and add the appropriate prototype to interrupts.h):
Code:
unsigned int Get_Compass(void)
{
int i,x=0;
for(i=0;i<SAMPLES;i++)
x+=Readings[i];
return x/SAMPLES;
}
Step six: Wire pin 1 on the compass to +5V on digital I/O 1 on the RC, pin 4 to signal, and pin 9 to ground.
Step seven: Call Get_Compass() to get the current reading from the compass... I can't remember right now if the value is in degrees or tenths of degrees, but you should be able to figure it out pretty quickly.
Best of luck!