s_accelerometer.c File Reference


Detailed Description

Accelerometer senser driver.

This file provides an example for the ADC + Accelerometer on AVR32 UC3 devices

Author:
Atmel Corporation: http://www.atmel.com
Support and FAQ: http://support.atmel.no/

Definition in file s_accelerometer.c.

#include <stdio.h>
#include "s_accelerometer.h"
#include "board.h"
#include "gpio.h"
#include "pm.h"
#include "adc.h"

Go to the source code of this file.

Data Structures

struct  S_acc_conv_t

Functions

static void acc_get_value (volatile avr32_adc_t *adc, unsigned char adc_channel_x, unsigned char adc_channel_y, unsigned char adc_channel_z, xyz_t *val)
static char acc_slow ()
static void accelerometer_action_x (char *buf)
static void accelerometer_action_y (char *buf)
void accelerometer_init (void)
 Init the accelerometer driver.
void accelerometer_measure (U32 angle, char *buf)
 Measures the accelerometer values and converts them to X and Y angles.
static void xyz_diff (xyz_t p, xyz_t q, xyz_t *r)
static int xyz_sumsq (xyz_t p)

Variables

Avr32_acc_t acc
struct S_acc_conv_t acc_table []
volatile avr32_adc_t * adc = (volatile avr32_adc_t *) &AVR32_ADC
unsigned char adc_channel_x = 1
unsigned char adc_channel_y = 2
unsigned char adc_channel_z = 3
xyz_t g_angle


Function Documentation

static void acc_get_value ( volatile avr32_adc_t *  adc,
unsigned char  adc_channel_x,
unsigned char  adc_channel_y,
unsigned char  adc_channel_z,
xyz_t val 
) [static]

Definition at line 140 of file s_accelerometer.c.

References ACC_SHIFT, xyz_t::x, xyz_t::y, and xyz_t::z.

Referenced by accelerometer_measure().

00141 {
00142 
00143   adc_enable( adc,adc_channel_x);
00144   adc_start(  adc) ;
00145   val->x = ( adc_get_value(adc, adc_channel_x) >> ACC_SHIFT ) ;
00146   adc_disable( adc,adc_channel_x);
00147 
00148   adc_enable( adc,adc_channel_y);
00149   adc_start(  adc) ;
00150   val->y = ( adc_get_value(adc, adc_channel_y) >> ACC_SHIFT ) ;
00151   adc_disable( adc,adc_channel_y);
00152 
00153   adc_enable( adc,adc_channel_z);
00154   adc_start(  adc) ;
00155   val->z = ( adc_get_value(adc, adc_channel_z) >> ACC_SHIFT ) ;
00156   adc_disable( adc,adc_channel_z);
00157 
00158   //printf("x=%d y=%d z=%d \n\r", val->x, val->y, val->z);
00159 }

static char acc_slow (  )  [static]

Definition at line 165 of file s_accelerometer.c.

References ACC_QUIET_HI, ACC_QUIET_LO, and Avr32_acc_t::ak2.

Referenced by accelerometer_action_x(), and accelerometer_action_y().

00166 {
00167   return ( (acc.ak2 > ACC_QUIET_LO) && (acc.ak2 < ACC_QUIET_HI) ) ;
00168 }

static void accelerometer_action_x ( char *  buf  )  [static]

Definition at line 171 of file s_accelerometer.c.

References acc_slow(), Avr32_acc_t::ak, S_acc_conv_t::angle, S_acc_conv_t::sin, xyz_t::x, and xyz_t::z.

Referenced by accelerometer_measure().

00172 {
00173   if (acc_slow())
00174   {
00175       U32 i;
00176       for( i=0 ; i<sizeof(acc_table)/sizeof(struct S_acc_conv_t) ; i++)
00177       {
00178          if( abs(acc.ak.x) >= acc_table[i].sin )
00179          {
00180             if( acc.ak.z<0 )
00181                if( acc.ak.x>0 )
00182                {
00183                   g_angle.x =  acc_table[i].angle ;
00184                   //printf("1. g_angle.x = %d\n\r", g_angle.x);
00185                }
00186                else
00187                {
00188                   g_angle.x = 270 + 90-acc_table[i].angle ;
00189                   if( g_angle.x==360 )
00190                      g_angle.x=0;
00191                   //printf("2. g_angle.x = %d\n\r", g_angle.x);
00192                }
00193             else
00194                if( acc.ak.x>0 )
00195                {
00196                   g_angle.x =  90 + 90-acc_table[i].angle ;
00197                   //printf("3. g_angle.x = %d\n\r", g_angle.x);
00198                }
00199                else
00200                {
00201                   g_angle.x = 180 + acc_table[i].angle ;
00202                   //printf("4. g_angle.x = %d\n\r", g_angle.x);
00203                }
00204 
00205             //printf("g_angle.x = %d\n\r", g_angle.x);
00206             break;
00207          }
00208       }
00209    }
00210 }

static void accelerometer_action_y ( char *  buf  )  [static]

Definition at line 212 of file s_accelerometer.c.

References acc_slow(), Avr32_acc_t::ak, S_acc_conv_t::angle, S_acc_conv_t::sin, xyz_t::y, and xyz_t::z.

Referenced by accelerometer_measure().

00213 {
00214   if (acc_slow())
00215   {
00216       U32 i;
00217 
00218       for( i=0 ; i<sizeof(acc_table)/sizeof(struct S_acc_conv_t) ; i++)
00219       {
00220          if( abs(acc.ak.y) >= acc_table[i].sin )
00221          {
00222             if( acc.ak.z<0 )
00223                if( acc.ak.y>0 )
00224                {
00225                   g_angle.y =  acc_table[i].angle ;
00226                   //printf("1. g_angle.y = %d\n\r", g_angle.y);
00227                }
00228                else
00229                {
00230                   g_angle.y = 270 + 90-acc_table[i].angle ;
00231                   if( g_angle.y==360 )
00232                      g_angle.y=0;
00233                   //printf("2. g_angle.y = %d\n\r", g_angle.y);
00234                }
00235             else
00236                if( acc.ak.y>0 )
00237                {
00238                   g_angle.y =  90 + 90-acc_table[i].angle ;
00239                   //printf("3. g_angle.y = %d\n\r", g_angle.y);
00240                }
00241                else
00242                {
00243                   g_angle.y = 180 + acc_table[i].angle ;
00244                   //printf("4. g_angle.y = %d\n\r", g_angle.y);
00245                }
00246             //printf("g_angle.y = %d\n\r", g_angle.y);
00247             break;
00248          }
00249       }
00250    }
00251 }

void accelerometer_init ( void   ) 

Init the accelerometer driver.

Definition at line 258 of file s_accelerometer.c.

References adc.

Referenced by device_full_custom_task_init().

00259 {
00260   // enable GPIO pins for ADC
00261   gpio_enable_module_pin(AVR32_ADC_AD_1_PIN,   AVR32_ADC_AD_1_FUNCTION);   // ADC channel 1
00262   gpio_enable_module_pin(AVR32_ADC_AD_2_PIN,   AVR32_ADC_AD_2_FUNCTION);   // ADC channel 2
00263   gpio_enable_module_pin(AVR32_ADC_AD_3_PIN,   AVR32_ADC_AD_3_FUNCTION);   // ADC channel 3
00264 
00265 
00266   // configure ADC
00267   adc_configure(adc);
00268 }

void accelerometer_measure ( U32  angle,
char *  buf 
)

Measures the accelerometer values and converts them to X and Y angles.

Parameters:
angle Boolean (0 for X-angle, 1 for Y-angle)
buf char buffer in which the light sensor value is stored.

Definition at line 278 of file s_accelerometer.c.

References acc_get_value(), accelerometer_action_x(), accelerometer_action_y(), adc, adc_channel_x, adc_channel_y, adc_channel_z, Avr32_acc_t::ak, Avr32_acc_t::ak2, Avr32_acc_t::g, Avr32_acc_t::k, Avr32_acc_t::m, xyz_t::x, xyz_diff(), xyz_sumsq(), xyz_t::y, and xyz_t::z.

Referenced by device_full_custom_task().

00279 {
00280   // start + get from adc
00281   acc_get_value(adc, adc_channel_x, adc_channel_y, adc_channel_z, &acc.m ) ;
00282 
00283   // ak  = acceleration = m - k
00284   xyz_diff( acc.m , acc.k, &acc.ak ) ;
00285   //printf("x=%d y=%d z=%d \n\r", acc.ak.x, acc.ak.y, acc.ak.z);
00286   acc.ak2 = xyz_sumsq( acc.ak ) ;
00287   
00288   if(!angle)
00289   {
00290      accelerometer_action_x( buf );
00291      sprintf( buf, "%3d\r\n", g_angle.x);
00292   } else {
00293      accelerometer_action_y( buf );
00294      sprintf( buf, "%3d\r\n", g_angle.y);
00295   }
00296   
00297   // update g for next move
00298   acc.g.x = acc.m.x ;
00299   acc.g.y = acc.m.y ;
00300   acc.g.z = acc.m.z ;
00301 }

static void xyz_diff ( xyz_t  p,
xyz_t  q,
xyz_t r 
) [static]

Definition at line 127 of file s_accelerometer.c.

References xyz_t::x, xyz_t::y, and xyz_t::z.

Referenced by accelerometer_measure().

00128 {
00129   r->x = p.x - q.x ;
00130   r->y = p.y - q.y ;
00131   r->z = p.z - q.z ;
00132 }

static int xyz_sumsq ( xyz_t  p  )  [static]

Definition at line 134 of file s_accelerometer.c.

References xyz_t::x, xyz_t::y, and xyz_t::z.

Referenced by accelerometer_measure().

00135 {
00136   return ( (p.x*p.x) + (p.y*p.y) + (p.z*p.z) ) ;
00137 }


Variable Documentation

Initial value:

 {
   
   .m = { .x=0 , .y=0 , .z=0 } ,

   
   .left = 0 ,
   .right = 0 ,
   .up = 0 ,
   .down = 0 ,
   .topdown = 0 ,

   
   .k = { .x=ACC_ZERO_X , .y=ACC_ZERO_Y , .z=ACC_ZERO_Z } ,  
   .ak = { .x=0 , .y=0 , .z=0 } ,
   .ak2 = 0 ,

   
   .g = { .x=ACC_ZERO_X , .y=ACC_ZERO_Y , .z=ACC_MIN } ,  
   .ag = { .x=0 , .y=0 , .z=0 } ,
   .ag2 = 0 ,
   .s = { .x=0 , .y=0 , .z=0 } ,
   .s2 = 0
}

Definition at line 100 of file s_accelerometer.c.

Initial value:

{  {  .sin=(U16)(SIN90 * ACC_1G), .angle=90}
,  {  .sin=(U16)(SIN85 * ACC_1G), .angle=85}
,  {  .sin=(U16)(SIN80 * ACC_1G), .angle=80}
,  {  .sin=(U16)(SIN75 * ACC_1G), .angle=75}
,  {  .sin=(U16)(SIN70 * ACC_1G), .angle=70}
,  {  .sin=(U16)(SIN65 * ACC_1G), .angle=65}
,  {  .sin=(U16)(SIN60 * ACC_1G), .angle=60}
,  {  .sin=(U16)(SIN55 * ACC_1G), .angle=55}
,  {  .sin=(U16)(SIN50 * ACC_1G), .angle=50}
,  {  .sin=(U16)(SIN45 * ACC_1G), .angle=45}
,  {  .sin=(U16)(SIN40 * ACC_1G), .angle=40}
,  {  .sin=(U16)(SIN35 * ACC_1G), .angle=35}
,  {  .sin=(U16)(SIN30 * ACC_1G), .angle=30}
,  {  .sin=(U16)(SIN25 * ACC_1G), .angle=25}
,  {  .sin=(U16)(SIN20 * ACC_1G), .angle=20}
,  {  .sin=(U16)(SIN15 * ACC_1G), .angle=15}
,  {  .sin=(U16)(SIN10 * ACC_1G), .angle=10}
,  {  .sin=(U16)(SIN5  * ACC_1G), .angle=5 }
,  {  .sin=(U16)(SIN0  * ACC_1G), .angle=0 }
}

Definition at line 78 of file s_accelerometer.c.

volatile avr32_adc_t* adc = (volatile avr32_adc_t *) &AVR32_ADC

unsigned char adc_channel_x = 1

Definition at line 68 of file s_accelerometer.c.

Referenced by accelerometer_measure().

unsigned char adc_channel_y = 2

Definition at line 69 of file s_accelerometer.c.

Referenced by accelerometer_measure().

unsigned char adc_channel_z = 3

Definition at line 70 of file s_accelerometer.c.

Referenced by accelerometer_measure().

Definition at line 65 of file s_accelerometer.c.


Generated on Fri Feb 19 02:22:52 2010 for AVR32 - Control Panel demonstration. by  doxygen 1.5.5