Ejemplo n.º 1
0
/*******************************************************************************
* Outline      : CB_CMT_Accelerometer
* Description  : CMT interrupt callback function. This callback function is
*                executed after every period of the CMT timer. The function 
*                fetches the ADXL345 accelerometer readings & updates the LED's.
* Argument     : none
* Return value : none
*******************************************************************************/
void CB_CMT_Accelerometer(void)
{

#ifdef ACCELEROMETER_DEBUG    
    /* Declare display buffer */
    uint8_t  lcd_buffer[13];
#endif    
    uint16_t led;

    short adjusted_X, adjusted_Y, adjusted_Z;
    short slope = 0;

    /* Display the contents of lcd_buffer onto the debug LCD */

    Accel_X = Accel_AxisRd(ADXL345_DATAX0_REG);
    Accel_Y = Accel_AxisRd(ADXL345_DATAY0_REG);
    Accel_Z = Accel_AxisRd(ADXL345_DATAZ0_REG);        

    adjusted_X = Accel_X - Accel_X_Zero;
    adjusted_Y = Accel_Y - Accel_Y_Zero;
    adjusted_Z = Accel_Z - Accel_Z_Zero;

    /* calculate the slope, make sure not dividing by zero */
    if ( adjusted_X == 0 )
         adjusted_X =  1;
    slope = (100 * adjusted_Y) / adjusted_X;

#ifdef ACCELEROMETER_DEBUG        
    sprintf((char *)lcd_buffer, " x = %d" , adjusted_X);
    DisplayLCD(LCD_LINE4, lcd_buffer);

    sprintf((char *)lcd_buffer, " y = %d" , adjusted_Y);    
    DisplayLCD(LCD_LINE5, lcd_buffer);

    sprintf((char *)lcd_buffer, " z = %d" , adjusted_Z);    
    DisplayLCD(LCD_LINE6, lcd_buffer);
#endif

    // turn all LEDs off

    /* Port D: All LED's off */
    R_IO_PORT_Write( PDL_IO_PORT_D, 0xFF );    

    /* Port E: All LED's off */
    R_IO_PORT_Write( PDL_IO_PORT_E, 0x0F );    

    /* ignore baseline noise */    
    if ((adjusted_X < 2) && (adjusted_X > -2 ) &&        
        (adjusted_Y < 2) && (adjusted_Y > -2 ))
        return;

    if ((slope > 0) && (slope < 55))
    {
        if ( adjusted_X < 0 )
            led = PDL_IO_PORT_D_2;    // LED 6
        else
            led = PDL_IO_PORT_D_3;    // LED 12
    }
    else if ((slope >= 55) && (slope < 170))
    {
        if ( adjusted_X < 0 )
            led = PDL_IO_PORT_E_3;    // LED 5
        else
            led = PDL_IO_PORT_D_7;    // LED 11
    }
    else if (slope >= 170)
    {
        if ( adjusted_X < 0 )
            led = PDL_IO_PORT_D_5;    // LED 4
        else
            led = PDL_IO_PORT_D_1;    // LED 10
    }
    else if ((slope <= 0) && (slope > -55))
    {
        if ( adjusted_X > 0 )
            led = PDL_IO_PORT_E_1;    // LED 13
        else
            led = PDL_IO_PORT_E_0;    // LED 7        
    }
    else if ((slope <= -55) && (slope > -170))
    {
        if ( adjusted_X > 0 )
            led = PDL_IO_PORT_D_0;    // LED 14
        else
            led = PDL_IO_PORT_D_4;    // LED 8
    }
    else if (slope <= -170)
    {
        if ( adjusted_X > 0 )
            led = PDL_IO_PORT_D_6;    // LED 15
        else
            led = PDL_IO_PORT_E_2;    // LED 9
    }

    adjusted_Z = adjusted_Z ; /* Suppress compiler warning about not using this var */
    
    // turn on the appropriate LED
    R_IO_PORT_Modify( led, PDL_IO_PORT_XOR, 1 );

}
Ejemplo n.º 2
0
/**
 * System timer tick 
 *
 * This function is called from timer interrupt to increment a 
 * system timer tick.  Also an LED on the target board is 
 * toggled to show activity. 
 *
 */
void int_cmt0_isr(void)
{
    R_IO_PORT_Modify( LED4, PDL_IO_PORT_XOR, 1 );
	      
    timer_clock_ticks++;
}