Beginners: please read this post and this post before posting to the forum.
0 Members and 1 Guest are viewing this topic.
void EnableCompass(void){ // init compass //configure PORTC PC0 and PC1 for output ... probably want to move this to a global init() function. bool retval =false; uint8_t address = 33; uint8_t TWI_transBuff[4] = {0,0,0,0}; uint8_t result=0; PORT_SetPinAsOutput(&PORTC,PIN0_bp); PORT_SetPinAsOutput(&PORTC,PIN1_bp); // I know this can be optimized. /* Initialize TWI master. */ TWI_MasterInit(&twiMaster, &TWIC, TWI_MASTER_INTLVL_LO_gc, TWI_BAUDSETTING); /* Enable LO interrupt level. */ PMIC.CTRL |= PMIC_LOLVLEN_bm; sei(); while (twiMaster.status != TWIM_STATUS_READY) {// /* Wait until transaction is complete. */ } TWI_transBuff[0]=0x47; // Write to RAM command retval=TWI_MasterWrite(&twiMaster,address,TWI_transBuff,1); delay_us(70); while (twiMaster.status != TWIM_STATUS_READY) {// /* Wait until transaction is complete. */ } TWI_transBuff[0]=0x74; // RAM memory Location retval = TWI_MasterWrite(&twiMaster,address,TWI_transBuff,1); while (twiMaster.status != TWIM_STATUS_READY) {// /* Wait until transaction is complete. */ } delay_us(70); TWI_transBuff[0]=0x71; // Operational Mode Setup retval = TWI_MasterWrite(&twiMaster,address,TWI_transBuff,1); _delay_ms(1000); // long wait to let HMC perform command// Copy Operational Mode into EEPROMwhile (twiMaster.status != TWIM_STATUS_READY) {// /* Wait until transaction is complete. */ } TWI_transBuff[0]=0x4C; retval = TWI_MasterWrite(&twiMaster,address,TWI_transBuff,1); TWI_MasterTransactionFinished(&twiMaster,result); _delay_ms(1000); // long wait to let HMC perform command}float GetHeading(void){ float heading=0.0; // initialize heading uint8_t TWI_Operation; uint8_t BufPos = 0; uint8_t TWI_recBuff[3] = {0,0,0}; uint8_t TWI_transBuff[4] = {0,0,0,0}; unsigned char data[2]={0,0}; uint8_t address = 33; // read address bool retval =false; while (twiMaster.status != TWIM_STATUS_READY) { /* Wait until transaction is complete. */ } TWI_transBuff[0]='A'; // retval=TWI_MasterRead(&twiMaster,address,2); retval = TWI_MasterWriteRead(&twiMaster,address,TWI_transBuff,1,2); delay_us(6000); while (twiMaster.status != TWIM_STATUS_READY) { /* Wait until transaction is complete. */ } TempValue=((twiMaster.readData[0] << 8) | (twiMaster.readData[1])); // USART_PutChar(&USARTF1,(char)heading); USART_TXBuffer_PutByte(&USART_data,TempValue); heading = (float)((twiMaster.readData[0] << 8) | (twiMaster.readData[1]))/10.0; return heading;}/*! TWIC Master Interrupt vector. */ISR(TWIC_TWIM_vect){ TWI_MasterInterruptHandler(&twiMaster);}
float GetavgHeading(void){ int hcount=0; float heading=0; float avgHeading=0; for (hcount=0;hcount<50; hcount++) { heading+=GetHeading(); _delay_ms(500); } avgHeading = heading/hcount; return avgHeading;}
void read_compass2x(){ unsigned char i2c_data[3]; unsigned int ix; i2c_data[0] = 0x41; // read compass twice to clear last reading i2cread(0x22, (unsigned char *)i2c_data, 2, SCCB_ON); delayUS(20000); i2c_data[0] = 0x41; i2cread(0x22, (unsigned char *)i2c_data, 2, SCCB_ON); ix = (((unsigned int)i2c_data[0] << 8) + i2c_data[1]) / 10; printf("##$C %3d\r\n", ix);}