Quantcast
Channel: MSP430 Technical Forums
Viewing all articles
Browse latest Browse all 2077

Accelerometer help

$
0
0

Recently, i picked up msp430f2274 to program an accelerometer with CCS to play around since it is holiday for me. However, i could not seem to get it working.

 

Below is a code i found online :

#include <msp430.h>
#define InfoMemoB 0x1040					// Info memory Segment B
// Global Variables
unsigned int adc[3] = {0};	// This will hold the x,y and z axis values
unsigned int X_Axis = 0;
unsigned int Y_Axis = 0;
unsigned int Z_Axis = 0;

//Function Prototypes
void Setup_HW(void);		// Setup watchdog timer, clockc, ADC ports
void Read_Acc(void);	// This function reads the ADC and stores the x, y and z values


int main(void)
{

	Setup_HW();			// Setup watchdog timer, clocks, ADC ports

	while (1)			// Code run continuously inside the while loop

		{
			Read_Acc();	// This function reads the ADC and stores the x, y and z values
			
		}
}

// ADC10 interrupt service routine
#pragma vector=ADC10_VECTOR
__interrupt void ADC10_ISR(void)
{
  __bic_SR_register_on_exit(CPUOFF);        // Clear CPUOFF bit from 0(SR)
}

void Setup_HW(void)
{
	  WDTCTL = WDTPW + WDTHOLD;
	  // Stop watch dog timer

	  BCSCTL1 = DIVA_0;     	// Set range
	  DCOCTL = CALDCO_1MHZ,		// Set DCO clock frequency

	  ADC10CTL1 = INCH_3 + CONSEQ_1;
	  // ADC10CTL1 control Register 1
	  // INCH_3 selects channel 3 as the highest channel for a sequence of conversions A3/A2/A1
	  // CONSEQ_1 conversion sequence mode 3 repeat sequence of channels
	  ADC10CTL0 = ADC10SHT_1 + MSC + ADC10ON + ADC10IE + REFON; // + ADC10SR;
	  // ADC10CTL0 control Register 0
	  // ADC10SHT_1 sample and hold time, 2 = 16 x ADC10CLK
	  // MSC multiple sample and conversions
	  // ADC10ON adc10 on
	  // ADC10IE adc10 interrupt enable
	  // REFON internal reference voltage is on and set to 1.5V
	  ADC10DTC1 = 0x03;
	  // Data Transfer Control register, defines the number of transfers in each block, 3 in this case
	  ADC10AE0 |= 0x0E;
	  // ADC10 analog enable bits P1.3,P1.2 and P1.1 (bit1 = 0x02, bit2 = 0x04 and bit3 = 0x08 therefore 14 in hex = 0x0E)
}

void Read_Acc(void)
{
    ADC10CTL0 &= ~ENC;
    while (ADC10CTL1 & BUSY);               // Wait if ADC10 core is active
    ADC10SA = (unsigned int)adc;			// Copies data in ADC10SA to unsigned int adc array
    ADC10CTL0 |= ENC + ADC10SC;             // Sampling and conversion start
    __bis_SR_register(CPUOFF + GIE);        // LPM0, ADC10_ISR will force exit


    X_Axis = adc[2];						// adc array 0 copied to the variable X_Axis
    Y_Axis = adc[1];						// adc array 1 copied to the variable Y_Axis
    Z_Axis = adc[0];						// adc array 2 copied to the variable Z_Axis
}
 

There are no problem in the building and debugging processes but when i watch the expressions named 'adc' with continuous refresh, it will show an error: target is running. However, sometimes it will not show an error and the values of X_Axis , Y_Axis and Z_Axis just randomly changed even though i never move/ shake the accelerometer. I wanted to know if there is any problem in implementing this code in msp430f2274.

Any help are appreciated! Thanks! :smile:


Viewing all articles
Browse latest Browse all 2077

Trending Articles