int main () { struct intvector mag; // integer vector of magnetometer measurements #define magx mag.x.i16 // x axis #define magy mag.y.i16 // y axis #define magz mag.z.i16 // z axis microcontroller_init(); uart1_open(38400); // open UART1 printf ("Starting test...\n\r"); InitI2C(); // Initialise the I2C(1) module init_HMC5843(); // initialize the HMC5843 chip test_HMC5843(); // display configuration and ID registers while(1) { HMC5843(&mag); // update magnetometer data printf( "%5d %5d %5d\r",magx,magy,magz); microcontroller_delay_ms(100); } // end while } // end main
TWDR = data; TWCR = (1<<TWINT)|(1<<TWEN); } // ask for a byte, wait for it arrive, and return it unsigned char HMC5843::receiveByte(void) { waitForReady(); TWCR = ((TWCR&0x0F)|(1<<TWINT)|(1<<TWEA)); waitForReady(); return(TWDR); } // get status register. the bits 0 and 1 are zeroed in init. see datasheet unsigned char HMC5843::readStatus() { waitForReady(); return(TWSR); } // wait for TWINT to be set before touching the other registers. void HMC5843::waitForReady(void) { // timeout after some time to avoid locking up if something goes wrong int timeout = 200; while ((!(TWCR & (1<<TWINT))) && (timeout--)); } // Set the default object HMC5843 HMC = HMC5843();