I feel like an idiot here, and I can't believe there aren't any examples of this on the web already somewhere...
Here is my schematic, very straight forward. 16F877 chip with a MAX232 interface chip, and a Devantech CMPS03 compass module.
**broken link removed**
And here is my code (written in CCS-C):
I expect to get a value between 0 and 3600 displayed once per second. I expect that while I am just sitting here, the value will be fairly consistent. What I actually get is this:
Does anyone have any idea what I'm doing wrong? If I had to guess I would say that it's got something to do with a timing issue (notice the 6553x every 1-2 reads).
-- Rob
Here is my schematic, very straight forward. 16F877 chip with a MAX232 interface chip, and a Devantech CMPS03 compass module.
**broken link removed**
And here is my code (written in CCS-C):
Code:
#include <16F877.h>
#device adc=10
#FUSES NOWDT //No Watch Dog Timer
#FUSES HS //High speed Osc (> 4mhz)
#FUSES NOPUT //No Power Up Timer
#FUSES NOPROTECT //Code not protected from reading
#FUSES BROWNOUT //Reset when brownout detected
#FUSES LVP //Low Voltage Programming on B3(PIC16) or B5(PIC18)
#FUSES NOCPD //No EE protection
#FUSES NOWRT //Program memory not write protected
#FUSES NODEBUG //No Debug mode for ICD
#use delay(clock=20000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
#use i2c(Master,Fast,sda=PIN_C4,scl=PIN_C3)
long read_compass()
{
long value;
i2c_start();
i2c_write( 0xC0 ); // The address of the compass (READ LOW)
i2c_write( 0x02 ); // Command (read 360 degrees)
i2c_start(); // Restart to change data dir
i2c_write( 0xC1 ); // The address of the compass (READ HIGH)
// Read 2 bytes (HI/LO) and combine into one LONG
value = i2c_read() << 8; // Read the HI value * 256
value = value + i2c_read(); // Read the LO value and add it
i2c_stop();
return( value );
}
void main()
{
long heading;
setup_adc_ports(AN0_AN1_AN2_AN3_AN4);
setup_adc(ADC_CLOCK_INTERNAL);
setup_psp(PSP_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
do
{
// Take a compass reading
heading = read_compass();
printf("%Lu\r\n", heading );
delay_ms( 1000 );
} while( TRUE );
}
I expect to get a value between 0 and 3600 displayed once per second. I expect that while I am just sitting here, the value will be fairly consistent. What I actually get is this:
Code:
65534
1023
2710
65535
2710
65535
2713
65535
2706
65535
2713
245
65534
1023
2711
65535
2710
65535
2712
65535
2716
1525
Does anyone have any idea what I'm doing wrong? If I had to guess I would say that it's got something to do with a timing issue (notice the 6553x every 1-2 reads).
-- Rob