/** * Read magnetometer (raw values) */ void readMagneto(void) { int e, f; unsigned char temp, MR_Data[6]; e = I2CRegWrite(I2C0_MASTER_BASE, 0x1E, 0x00, 0x14);//set CRA_REG_M register f = I2CRegWrite(I2C0_MASTER_BASE, 0x1E, 0x01, 0x20); f = I2CRegWrite(I2C0_MASTER_BASE, 0x1E, 0x02, 0x00);//set MR_REG_M register temp = I2CRegRead(I2C0_MASTER_BASE, 0x1E, 0x02); //read MR_REG_M MR_Data[0] = I2CRegRead(I2C0_MASTER_BASE, 0x1E, 0x03); //read OUT_X_H_M (MSB) MR_Data[1] = I2CRegRead(I2C0_MASTER_BASE, 0x1E, 0x04); //read OUT_X_L_M (LSB) MR_Data[2] = I2CRegRead(I2C0_MASTER_BASE, 0x1E, 0x07); //read OUT_Y_H_M (MSB) MR_Data[3] = I2CRegRead(I2C0_MASTER_BASE, 0x1E, 0x08); //read OUT_Y_L_M (LSB) MR_Data[4] = I2CRegRead(I2C0_MASTER_BASE, 0x1E, 0x05); //read OUT_Z_H_M (MSB) MR_Data[5] = I2CRegRead(I2C0_MASTER_BASE, 0x1E, 0x06); //read OUT_Z_L_M (LSB) Mx = (int16_t) (MR_Data[0] << 8) + MR_Data[1]; My = (int16_t) (MR_Data[2] << 8) + MR_Data[3]; Mz = (int16_t) (MR_Data[4] << 8) + MR_Data[5]; if (e == 0 || f == 0 || temp != 0) { GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1, GPIO_PIN_1); //Delay for 1 millisecond. Each SysCtlDelay is about 3 clocks. SysCtlDelay(SysCtlClockGet() / (1000 * 3)); //Turn off the LED GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1, 0); } }
//***************************************************************************** // // Initializes the switch task. // //***************************************************************************** unsigned long AccelerometerTaskInit(void) { // volatile unsigned char foo = 28; RGBInit(1); RGBIntensitySet(0.3f); // // Turn on the Green LED // g_ucColorsIndx = 0; g_ulColors[g_ucColorsIndx] = 0x8000; RGBColorSet(g_ulColors); RGBDisable(); ////////////// g_pAccelerometerQueue = xQueueCreate(ACCELEROMETER_QUEUE_SIZE, ACCELEROMETER_ITEM_SIZE); I2CSetup(I2C0BASEADDR, 40000); if (((I2CRegRead(I2C0BASEADDR, SLAVEID, DEVID)) & 0xE5) == 0) { while (1); } I2CRegWrite(I2C0BASEADDR, SLAVEID, THRESH_FF, 0x06); simple_delay(); I2CRegWrite(I2C0BASEADDR, SLAVEID, TIME_FF, 0x15); simple_delay(); I2CRegWrite(I2C0BASEADDR, SLAVEID, INT_MAP, 0x00); simple_delay(); I2CRegWrite(I2C0BASEADDR, SLAVEID, POWER_CTL, 0x08); simple_delay(); I2CRegWrite(I2C0BASEADDR, SLAVEID, INT_ENABLE, 0x04); simple_delay(); if(xTaskCreate(AccelerometerTask, (signed portCHAR *)"Acclerometer", ACCELEROMETERTASKSTACKSIZE, NULL, tskIDLE_PRIORITY + PRIORITY_ACCELEROMETER_TASK, NULL) != pdTRUE) { return(1); } // // Success. // return(0); }
//***************************************************************************** // // This task reads the buttons' state and passes this information to LEDTask. // //***************************************************************************** void AccelerometerTask(void *pvParameters) { portTickType ulLastTime; unsigned char ucMessageAccelerometer = 1; volatile unsigned char temp = 0; // unsigned long ulAccelerometerDelay = 50; // // Get the current tick count. // ulLastTime = xTaskGetTickCount(); // // Loop forever. // while(1) { temp = 2; temp = I2CRegRead(I2C0BASEADDR, SLAVEID, INT_SOURCE); if (temp & FF_INT_BIT) { // RGBEnable(); if(xQueueSend(g_pAccelerometerQueue, &ucMessageAccelerometer, portMAX_DELAY) != pdPASS) { while(1); } } // // Wait for the required amount of time to check back. // vTaskDelayUntil(&ulLastTime, ACCELEROMETERTASKPERIOD / portTICK_RATE_MS); } }
/* * Read Accelerometer (raw values) */ void readAccel(void) { I2CRegWrite(I2C0_MASTER_BASE, 0x18, 0x20, 0x27); //set CTRL_REG1_A register I2CRegWrite(I2C0_MASTER_BASE, 0x18, 0x23, 0x40); //set CTRL_REG1_A register ACC_Data[0] = I2CRegRead(I2C0_MASTER_BASE, 0x18, 0x28); ACC_Data[1] = I2CRegRead(I2C0_MASTER_BASE, 0x18, 0x29); ACC_Data[2] = I2CRegRead(I2C0_MASTER_BASE, 0x18, 0x2A); ACC_Data[3] = I2CRegRead(I2C0_MASTER_BASE, 0x18, 0x2B); ACC_Data[4] = I2CRegRead(I2C0_MASTER_BASE, 0x18, 0x2C); ACC_Data[5] = I2CRegRead(I2C0_MASTER_BASE, 0x18, 0x2D); Ax = (int16_t) (ACC_Data[0] << 8) + ACC_Data[1]; Ay = (int16_t) (ACC_Data[2] << 8) + ACC_Data[3]; Az = (int16_t) (ACC_Data[4] << 8) + ACC_Data[5]; Ax = (int16_t) Ax * ACCL_GAIN; Ay = (int16_t) Ay * ACCL_GAIN; Az = (int16_t) Az * ACCL_GAIN; }
unsigned long PCA9685_readREG(unsigned char ucReg){ unsigned long result; result = I2CRegRead(I2C_BASE,PCA9685_ADDRESS,ucReg); return result; }