Wednesday, April 12, 2006

accelerometer reading from cxl04lp3 and cxldk

Using this accelerometer and this interface, was able to get readings today after enabling the RX interrupt and capturing the approriate bytes from the sent string. Currently just storing the values in a byte array, next step is an object encompassing the reading and the time read (then to be stored in some collection for data sampling):

#include /* for EnableInterrupts macro */
#include "derivative.h" /* include peripheral declarations */
#include "m68demo908gb60.h"

#define EMPTY 0
#define READY 1
#define HAVE_DATA 1
#define ENABLED 1
#define DISABLED 0
#define PRESCALAR 7
#define MODULUS_50hz 1475
#define MODULUS_200hz (MODULUS_50hz /4)

#define STATE_WAITING_CALIBRATION 1
#define STATE_WAITING_INITIALIZATION 2
#define STATE_RECEIVE_READY 3
#define STATE_RECEIVING 4
#define STATE_CALIBRATION_HOSED 5
#define STATE_DO_READ 6

#define CALIBRATION_RESPONSE_CHAR 72
#define REQUEST_BYTE_STRING_CHAR 71
#define RESPONSE_BYTE_STRING_SIZE 10

int state = STATE_WAITING_CALIBRATION;

#define INITIALIZED 1
#define INITIALIZED_NECESSARY 0
int initialized = INITIALIZED_NECESSARY;

long receiveCnt = 0;
int head = 0;
int readOverflow = 0;
#define BUFFER_LENGTH 5
byte readbuffer[BUFFER_LENGTH] ;
long cntr=0;

int tickCntr = 0;
int seconds = 0;

int readByteCounter = 0;



void MCU_init(void); /* Device initialization function declaration */
byte readBuffer;
byte tempReadBuffer;

void enableTransmission(){
SCI2C2_TE = ENABLED;
SCI2C2_RE = ENABLED;

}
void enableReceive(){
SCI2C2_TE = ENABLED;//transmitter enabled
SCI2C2_RE = ENABLED;
}

void calibrateAccelerometer(){
enableTransmission();
SCI2D = 0b01010010;

readBuffer = SCI2D;
if(readBuffer != CALIBRATION_RESPONSE_CHAR){
state = STATE_CALIBRATION_HOSED;
}
readbuffer[0] = 0;
readbuffer[1] = 0;
readbuffer[2] = 0;
readbuffer[3] = 0;
readbuffer[4] = 0;
}





void initSCI2(){

//SCI2BDL=0b00100001;//37878, 33 = 20Mhz/16*384000, where BaudRate = clock/(16*prescalar)
SCI2BDL=0b00100000;//39062, 32 = 20Mhz/16*384000, where BaudRate = clock/(16*prescalar)
SCI2C2_RIE = 1;

}

void initTimer2_200hz(){//max rate that accelerometer can sample
/*configure Internal Clock Generator [ICG]*/
/*MFD[]={4,6,8,10,12,14,16,18}*/
ICGC2_MFD = 7;
ICGC2_RFD = 0; /* RFD[]={1,2,4,8,16,32,64,128}*/

ICGC1 = 0b00111000;

while((ICGS1_LOCK==0)||(ICGS1_ERCS==0)){
/*Ensure COP doesn't reset device whilst waiting for clock lock*/
__RESET_WATCHDOG(); /* kicks the dog */
}
ICGC2_LOCRE = 1; /*enable reset if clock fails*/

TPM2C0SC_CH0IE = 1; //enable channel 0

TPM2SC_CLKSA = 1;/*Select BUS clock*/
TPM2SC_CLKSB = 0;

TPM2SC_PS = PRESCALAR;/*clock source divided by prescalar*/
TPM2MOD = MODULUS_50hz;

TPM2SC_CPWMS = 1;
/*configure PWM mode and pulse*/
TPM2C0SC_MS0B = 1; /*MS0B=1, MS0A=0; << Edge align PWM*/
TPM2C0SC_ELS0A = 1; /*Select low as true*/
TPM2C0V = MODULUS_50hz-1; //100% PWM
}

/*
* Enable read, copy SCI2D to readBuffer.
*/
void readSerialInput(){
enableReceive();
if(SCI2S1_RDRF == HAVE_DATA){
//readBuffer = SCI2D;
readbuffer[head] = SCI2D;
LED2 = ~LED2;
if(head == BUFFER_LENGTH){
readOverflow = 1;
head = 0;
} else{

head++;
}

}

}

void interrupt VTimer2Chnl0 intTimer2Chnl0(){
//read the input from SCI2 and place in buffer

tickCntr ++;
if(tickCntr == 100){
seconds++;
tickCntr = 0;
}

TPM2C0SC_CH0F = 0;//reset overflow flag

}
void initKeyboardInterrupt(){
KBI1SC_KBIE = 1; //KBIE =>Keyboard Interrupt Enable
KBI1PE_KBIPE4 = 1; //Keyboard Interrupt Port Enable 4 (switch1)
}


void interrupt Vkeyboard intSwitch(){
if(SWITCH1 == DOWN){
state = STATE_DO_READ;
LED1 = ~LED1;
}
KBI1SC_KBACK = 1;//ack
}

void interrupt VSCI2_RX intRx(){
tempReadBuffer = SCI2S1;
tempReadBuffer = SCI2D;
if(tempReadBuffer != CALIBRATION_RESPONSE_CHAR){

if(readByteCounter == 2
|| readByteCounter ==3
|| readByteCounter == 4
|| readByteCounter ==5
|| readByteCounter == 6
|| readByteCounter == 7){

readbuffer[head] = tempReadBuffer;
head++;
LED2 = ~LED2;

}

readByteCounter ++;

if(readByteCounter == 10){
state = STATE_RECEIVE_READY;
head=0;

} else{
state = STATE_RECEIVING;
}

}
}

void readAccelValues(){
enableTransmission();
state = STATE_RECEIVE_READY;
readByteCounter = 0;

SCI2D = REQUEST_BYTE_STRING_CHAR; //send the 'G' char to initialize the accelerometer


}


void main(void) {

EnableInterrupts; /* enable interrupts */
PTADD = 0; //initialize as input (Data Direction Register)
PTAPE = 0xf0; //Pullups on upper 4 bits
/*initialize bits 0-3 of Port F as outputs (connected to led's)*/
PTFDD = 0x0f;
PTDDD = 0x0f;

LED1 = OFF;
LED2 = OFF;
LED3 = OFF;
LED4 = OFF;
LED5 = OFF;

initTimer2_200hz();
initSCI2();
initKeyboardInterrupt();

SCI2C2_TE = DISABLED;
SCI2C2_RE = DISABLED;
calibrateAccelerometer();
state = STATE_WAITING_CALIBRATION;

readAccelValues();
for(;;) {
__RESET_WATCHDOG(); /* feeds the dog */

if(state == STATE_DO_READ){
readAccelValues();
}





} /* loop forever */
/* please make sure that you never leave this function */
}

No comments: