Hi-
Yes it was not hard writing an accelerometer driver (see below) but I am seeing some odd results. Probably my driver is doing something stupid but I haven't figured it out yet.
My program configures AN0-AN7 as ADC inputs and then samples all of these values in sequential one-shot mode. It prints them to the console after averaging over a number of values.
What I see is AN0 correctly reads the pot and AN4 gives a nice strong signal for the X axis of the accelerometer. However I am seeing no Y or Z signals coming from AN5 and AN6.
Note that there is one little wrinkle: port TA bit 2 needs to be a digital output set high to ensure that the accelerometer does not sleep. Bits 0 and 1 can be left as inputs -- the accelerometer manual says that if these pins float, it default to the most sensitive setting.
Finally I am wondering just a bit what a 6 G accelerometer is doing on a demo board. If I have my students turn these boards into Wii controllers they're all going to get broken
.
John Regehr
Code:
#include "support_common.h" /* include peripheral declarations and more */#include <stdio.h>static void init_porttc (void){ //user LEDs /* Enable signals as GPIO */ MCF_GPIO_PTCPAR = 0 | MCF_GPIO_PTCPAR_DTIN3_GPIO | MCF_GPIO_PTCPAR_DTIN2_GPIO | MCF_GPIO_PTCPAR_DTIN1_GPIO | MCF_GPIO_PTCPAR_DTIN0_GPIO; /* Enable signals as digital outputs */ MCF_GPIO_DDRTC = 0 | MCF_GPIO_DDRTC_DDRTC3 | MCF_GPIO_DDRTC_DDRTC2 | MCF_GPIO_DDRTC_DDRTC1 | MCF_GPIO_DDRTC_DDRTC0; MCF_GPIO_PORTTC = 0x00; // TURN LEDS OFF}static void init_portta (void){ MCF_GPIO_PORTTA = 0; MCF_GPIO_DDRTA = MCF_GPIO_DDRTA_DDRTA2; MCF_GPIO_PTAPAR = 0; // set bit 2 high -- no sleep for accelerometer MCF_GPIO_PORTTA = 0x4;}static void init_port_an (void){ // all 8 are ADC inputs MCF_GPIO_DDRAN = 0; MCF_GPIO_PANPAR |= MCF_GPIO_PANPAR_PANPAR0 | MCF_GPIO_PANPAR_PANPAR1 | MCF_GPIO_PANPAR_PANPAR2 | MCF_GPIO_PANPAR_PANPAR3 | MCF_GPIO_PANPAR_PANPAR4 | MCF_GPIO_PANPAR_PANPAR5 | MCF_GPIO_PANPAR_PANPAR6 | MCF_GPIO_PANPAR_PANPAR7; }static void init_adc (void){ /* Initialise LOLIM, HILIM and OFFST registers of enabled channels: */ MCF_ADC_ADLLMT0 = 0; MCF_ADC_ADHLMT0 = MCF_ADC_ADHLMT_HLMT(0xfff); MCF_ADC_ADOFS0 = 0; MCF_ADC_ADLLMT1 = 0; MCF_ADC_ADHLMT1 = MCF_ADC_ADHLMT_HLMT(0xfff); MCF_ADC_ADOFS1 = 0; MCF_ADC_ADLLMT2 = 0; MCF_ADC_ADHLMT2 = MCF_ADC_ADHLMT_HLMT(0xfff); MCF_ADC_ADOFS2 = 0; MCF_ADC_ADLLMT3 = 0; MCF_ADC_ADHLMT3 = MCF_ADC_ADHLMT_HLMT(0xfff); MCF_ADC_ADOFS3 = 0; MCF_ADC_ADLLMT4 = 0; MCF_ADC_ADHLMT4 = MCF_ADC_ADHLMT_HLMT(0xfff); MCF_ADC_ADOFS4 = 0; MCF_ADC_ADLLMT5 = 0; MCF_ADC_ADHLMT5 = MCF_ADC_ADHLMT_HLMT(0xfff); MCF_ADC_ADOFS5 = 0; MCF_ADC_ADLLMT6 = 0; MCF_ADC_ADHLMT6 = MCF_ADC_ADHLMT_HLMT(0xfff); MCF_ADC_ADOFS6 = 0; MCF_ADC_ADLLMT7 = 0; MCF_ADC_ADHLMT7 = MCF_ADC_ADHLMT_HLMT(0xfff); MCF_ADC_ADOFS7 = 0; /* Initialise ADC */ MCF_ADC_ADZCC = 0; MCF_ADC_ADLST1 = MCF_ADC_ADLST1_SAMPLE0(0) | MCF_ADC_ADLST1_SAMPLE1(1) | MCF_ADC_ADLST1_SAMPLE2(2) | MCF_ADC_ADLST1_SAMPLE3(3); MCF_ADC_ADLST2 = MCF_ADC_ADLST2_SAMPLE4(4) | MCF_ADC_ADLST2_SAMPLE5(5) | MCF_ADC_ADLST2_SAMPLE6(6) | MCF_ADC_ADLST2_SAMPLE7(7); MCF_ADC_ADSDIS = 0; MCF_ADC_CAL = 0; MCF_ADC_CTRL1 = 0; MCF_ADC_CTRL2 = MCF_ADC_CTRL2_STOP1 | MCF_ADC_CTRL2_DIV(0x2); /* Power up ADC converter(s) in use */ MCF_ADC_POWER = MCF_ADC_POWER_PUDELAY(0xd) | MCF_ADC_POWER_PD2; /* Wait for converter A power up to complete */ while (MCF_ADC_POWER & MCF_ADC_POWER_PSTS0) ; /* Wait for converter B power up to complete */ while (MCF_ADC_POWER & MCF_ADC_POWER_PSTS1) ;}int main(void){ int i = 0; init_porttc(); init_portta(); init_port_an(); init_adc(); while (1) { #define N 10 int res[N][8], avg[8]; int j, k; // for (x=0; x<100000; x++) asm { nop; } MCF_ADC_CTRL1 = 0; MCF_ADC_CTRL1 = MCF_ADC_CTRL1_START0; #define ALL_RDY (MCF_ADC_ADSTAT_RDY0 | MCF_ADC_ADSTAT_RDY1 | MCF_ADC_ADSTAT_RDY2 | MCF_ADC_ADSTAT_RDY3 | MCF_ADC_ADSTAT_RDY4 | MCF_ADC_ADSTAT_RDY5 | MCF_ADC_ADSTAT_RDY6 | MCF_ADC_ADSTAT_RDY7) while ((MCF_ADC_ADSTAT & ALL_RDY) != ALL_RDY) ; res[i][0] = MCF_ADC_ADRSLT0 >> 3; res[i][1] = MCF_ADC_ADRSLT1 >> 3; res[i][2] = MCF_ADC_ADRSLT2 >> 3; res[i][3] = MCF_ADC_ADRSLT3 >> 3; res[i][4] = MCF_ADC_ADRSLT4 >> 3; res[i][5] = MCF_ADC_ADRSLT5 >> 3; res[i][6] = MCF_ADC_ADRSLT6 >> 3; res[i][7] = MCF_ADC_ADRSLT7 >> 3; i++; if (i >= N) i=0; MCF_ADC_CTRL1 |= MCF_ADC_CTRL1_STOP0; for (k=0; k<8; k++) { int t = 0; for (j=0; j<N; j++) t += res[j][k]; avg[k] = t/N; } printf ("AN0 %4d AN1 %4d AN2 %4d AN3 %4d AN4 %4d AN5 %4d AN6 %4d AN7 %4d\n", avg[0], avg[1], avg[2], avg[3], avg[4], avg[5], avg[6], avg[7]); // blink a LED MCF_GPIO_PORTTC ^= 0x8; }}