/** * System timer tick initialization * * This function initializes a timer interrupt with 100ms tick. * */ void timer_init(void) { /* Declare error flag */ bool err = true; /* CMT is configured for a 100ms interval, and executes the callback function CB_CompareMatch on every compare match */ err &= R_CMT_Create ( 3, PDL_CMT_PERIOD, 100E-3, int_cmt0_isr, 3 ); /* Halt in while loop when RPDL errors detected */ while(!err); }
/******************************************************************************* * Outline : Init_Thermal_Sensor * Description : This function configures the ADT7420 thermal device and starts * a timer to periodically read the temperature. * Argument : none * Return value : none *******************************************************************************/ void Init_Thermal_Sensor(void) { /* Declare error flag */ bool err = true; uint8_t target_reg, target_data; DisplayLCD(LCD_LINE5, "Temp:"); /* Configure the ADT7420 */ target_reg = ADT7420_CONFIG_REG; err = R_IIC_MasterSend( IIC_CHANNEL, PDL_NO_DATA, ADT7420_ADDR, &target_reg, 1, PDL_NO_FUNC, 0 ); target_data = 0x00; err = R_IIC_MasterSend( IIC_CHANNEL, PDL_NO_DATA, ADT7420_ADDR, &target_data, 1, PDL_NO_FUNC, 0 ); /* Configure CMT1 to execute callback function every 250ms */ err &= R_CMT_Create( 1, PDL_CMT_PERIOD, 250E-3, CB_CMT_Thermal, 3 ); /* Halt in while loop when RPDL errors detected */ while (!err); }
/******************************************************************************* * Outline : Init_Accelerometer * Description : This function configures the ADXL345 accelerometer and starts * a timer to periodically read it. * Argument : none * Return value : none *******************************************************************************/ void Init_Accelerometer(void) { /* Declare error flag */ bool err = true; uint8_t target_reg, target_data; uint16_t i; uint8_t target_reg_data[2]; // Read the DEVID register and verify the device target_reg = 0x00; err &= R_IIC_MasterSend( IIC_CHANNEL, PDL_NO_DATA, ADXL345_ADDR, &target_reg, 1, PDL_NO_FUNC, 0 ); err &= R_IIC_MasterReceive( IIC_CHANNEL, PDL_NO_DATA, ADXL345_ADDR, &target_data, 1, PDL_NO_FUNC, 0 ); if ( target_data != 0xE5 ) return; // set the data format #if 0 // example of writing slave address and data as separate function calls target_reg = ADXL345_DATA_FORMAT_REG; err &= R_IIC_MasterSend( IIC_CHANNEL, PDL_IIC_STOP_DISABLE, ADXL345_ADDR, &target_reg, 1, PDL_NO_FUNC, 0 ); target_data = 3; err &= R_IIC_MasterSend( IIC_CHANNEL, PDL_IIC_START_DISABLE, ADXL345_ADDR, &target_data, 1, PDL_NO_FUNC, 0 ); #else // example of writing slave address and data in same function call target_reg_data[0] = ADXL345_DATA_FORMAT_REG; target_reg_data[1] = 3; err &= R_IIC_MasterSend( IIC_CHANNEL, PDL_NO_DATA, ADXL345_ADDR, &target_reg_data[0], 2, PDL_NO_FUNC, 0 ); #endif // put FIFO into bypass mode target_reg_data[0] = ADXL345_FIFO_CTL_REG; target_reg_data[1] = 0; err &= R_IIC_MasterSend( IIC_CHANNEL, PDL_NO_DATA, ADXL345_ADDR, &target_reg_data[0], 2, PDL_NO_FUNC, 0 ); // set the measure bit in the power control register target_reg_data[0] = ADXL345_POWER_CTL_REG; target_reg_data[1] = 8; err &= R_IIC_MasterSend( IIC_CHANNEL, PDL_NO_DATA, ADXL345_ADDR, &target_reg_data[0], 2, PDL_NO_FUNC, 0 ); // get baseline readings to calibrate accelerometer Accel_X_Zero = 0; Accel_Y_Zero = 0; Accel_Z_Zero = 0; for (i = 0; i < 8; i++) { Accel_X_Zero += Accel_AxisRd(0x32); Accel_Y_Zero += Accel_AxisRd(0x34); Accel_Z_Zero += Accel_AxisRd(0x36); } // determine the average reading Accel_X_Zero = Accel_X_Zero / 8; Accel_Y_Zero = Accel_Y_Zero / 8; Accel_Z_Zero = Accel_Z_Zero / 8; // run self test err &= Accel_SelfTest(); // Activate X, Y Z to detect activity target_reg_data[0] = ADXL345_ACT_INACT_CTL_REG; target_reg_data[1] = 0x70; err &= R_IIC_MasterSend( IIC_CHANNEL, PDL_NO_DATA, ADXL345_ADDR, &target_reg_data[0], 2, PDL_NO_FUNC, 0 ); #ifdef ACCELEROMETER_DEBUG // read all the registers for debug target_reg = ADXL345_THRESH_TAP_REG; err &= R_IIC_MasterSend( IIC_CHANNEL, PDL_NO_DATA, ADXL345_ADDR, &target_reg, 1, PDL_NO_FUNC, 0 ); err &= R_IIC_MasterReceive( IIC_CHANNEL, PDL_NO_DATA, ADXL345_ADDR, &target_string[0], 29, PDL_NO_FUNC, 0 ); #endif /* Configure CMT2 to execute callback function every 100ms */ err &= R_CMT_Create( 2, PDL_CMT_PERIOD, 100E-3, CB_CMT_Accelerometer, 3 ); /* Halt in while loop when RPDL errors detected */ while (!err); }