EZ430 Chronos 自带程序源码:三轴加速计示范程序

EZ430 Chronos 自带程序源码:三轴加速计示范程序
共4个文件:
1、acceleration.c
2、acceleration.h
3、vti_as.c
4、vti_as.h

acceleration.c
// *************************************************************************************************
//
//	Copyright (C) 2009 Texas Instruments Incorporated - http://www.ti.com/ 
//	 
//	 
//	  Redistribution and use in source and binary forms, with or without 
//	  modification, are permitted provided that the following conditions 
//	  are met:
//	
//	    Redistributions of source code must retain the above copyright 
//	    notice, this list of conditions and the following disclaimer.
//	 
//	    Redistributions in binary form must reproduce the above copyright
//	    notice, this list of conditions and the following disclaimer in the 
//	    documentation and/or other materials provided with the   
//	    distribution.
//	 
//	    Neither the name of Texas Instruments Incorporated nor the names of
//	    its contributors may be used to endorse or promote products derived
//	    from this software without specific prior written permission.
//	
//	  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
//	  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
//	  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
//	  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
//	  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
//	  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
//	  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
//	  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
//	  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
//	  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
//	  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// *************************************************************************************************
// Temperature measurement functions.
// *************************************************************************************************


// *************************************************************************************************
// Include section

// system
#include "project.h"

// driver
#include "display.h"
#include "vti_as.h"

// logic
#include "acceleration.h"
#include "simpliciti.h"
#include "user.h"


// *************************************************************************************************
// Global Variable section
struct accel sAccel;

// Conversion values from data to mgrav taken from CMA3000-D0x datasheet (rev 0.4, table 4)
const u16 mgrav_per_bit[7] = { 18, 36, 71, 143, 286, 571, 1142 };

// *************************************************************************************************
// Extern section

// Global flag for proper acceleration sensor operation
extern u8 as_ok;


// *************************************************************************************************
// @fn          reset_acceleration
// @brief       Reset acceleration variables.
// @param       none
// @return      none
// *************************************************************************************************
void reset_acceleration(void)
{
	// Start with Y-axis display
	sAccel.view_style 	= DISPLAY_ACCEL_Y;
	
	// Clear timeout counter
	sAccel.timeout		= 0;

	// Default mode is off
	sAccel.mode			= ACCEL_MODE_OFF;
}


// *************************************************************************************************
// @fn          sx_acceleration
// @brief       Acceleration direct function. Button UP switches between X/Y/Z values.
// @param       u8 line		LINE2
// @return      none
// *************************************************************************************************
void sx_acceleration(u8 line)
{
	if (++sAccel.view_style > 2) sAccel.view_style = 0;
	
	// Reset current acceleration value
	sAccel.data = 0;
	
	// Get data from sensor
	as_get_data(sAccel.xyz);
}


// *************************************************************************************************
// @fn          acceleration_value_is_positive
// @brief       Returns 1 if 2's complement number is positive
// @param       u8 value	2's complement number
// @return      u8			1 = number is positive, 0 = number is negavtive
// *************************************************************************************************
u8 acceleration_value_is_positive(u8 value)
{
	return ((value & BIT7) == 0);
}


// *************************************************************************************************
// @fn          convert_acceleration_value_to_mgrav
// @brief       Converts measured value to mgrav units
// @param       u8 value	g data from sensor 
// @return      u16			Acceleration (mgrav)
// *************************************************************************************************
u16 convert_acceleration_value_to_mgrav(u8 value)
{
	u16 result;
	u8 i;
	
	if (!acceleration_value_is_positive(value))
	{
		// Convert 2's complement negative number to positive number
		value = ~value;
		value += 1;
	}
	
	result = 0;
	for (i=0; i<7; i++)
	{
		result += ((value & (BIT(i)))>>i) * mgrav_per_bit[i];
	}
	
	return (result);
}



// *************************************************************************************************
// @fn          is_acceleration_measurement
// @brief       Returns 1 if acceleration is currently measured.
// @param       none
// @return      u8		1 = acceleration measurement ongoing
// *************************************************************************************************
u8 is_acceleration_measurement(void)
{
	return ((sAccel.mode == ACCEL_MODE_ON) && (sAccel.timeout > 0));
}



// *************************************************************************************************
// @fn          do_acceleration_measurement
// @brief       Get sensor data and store in sAccel struct
// @param       none
// @return      none
// *************************************************************************************************
void do_acceleration_measurement(void)
{
	// Get data from sensor
	as_get_data(sAccel.xyz);
	
	// Set display update flag
	display.flag.update_acceleration = 1;
}


// *************************************************************************************************
// @fn          display_acceleration
// @brief       Display routine.
// @param       u8 line			LINE1
//	       u8 update		         DISPLAY_LINE_UPDATE_FULL, DISPLAY_LINE_CLEAR
// @return      none
// *************************************************************************************************
void display_acceleration(u8 line, u8 update)
{
	u8 * str;
	u8 raw_data;
	u16 accel_data;

	// Show warning if acceleration sensor was not initialised properly
	if (!as_ok)
	{
		display_chars(LCD_SEG_L1_2_0, (u8*)"ERR", SEG_ON);
	}
	else
	{
		// Redraw whole screen
		if (update == DISPLAY_LINE_UPDATE_FULL)	
		{
			{
				// Start acceleration sensor
				if (!is_acceleration_measurement()) 
				{
					// Clear previous acceleration value
					sAccel.data = 0;
					
					// Start sensor
					as_start();
					
					// Set timeout counter
					sAccel.timeout = ACCEL_MEASUREMENT_TIMEOUT;
					
					// Set mode
					sAccel.mode = ACCEL_MODE_ON;
					
					// Start with Y-axis values
					sAccel.view_style = DISPLAY_ACCEL_Y;
				}
				
				// Display decimal point
				display_symbol(LCD_SEG_L1_DP1, SEG_ON);
			}
		}
		else if (update == DISPLAY_LINE_UPDATE_PARTIAL)
		{
			// Convert X/Y/Z values to mg
			switch (sAccel.view_style)
			{
				case DISPLAY_ACCEL_X: 	raw_data = sAccel.xyz[0];
										display_char(LCD_SEG_L1_3, 'X', SEG_ON);
										break;
				case DISPLAY_ACCEL_Y: 	raw_data = sAccel.xyz[1];
										display_char(LCD_SEG_L1_3, 'Y', SEG_ON);
										break;
				default: 				raw_data = sAccel.xyz[2];
										display_char(LCD_SEG_L1_3, 'Z', SEG_ON);
										break;
			}
			accel_data = convert_acceleration_value_to_mgrav(raw_data) / 10;
			
			// Filter acceleration
			accel_data = (u16)((accel_data * 0.2) + (sAccel.data * 0.8));
			
			// Store average acceleration
			sAccel.data = accel_data;	
	
			// Display acceleration in x.xx format
			str = itoa(accel_data, 3, 0);
			display_chars(LCD_SEG_L1_2_0, str, SEG_ON);
			
			// Display sign
			if (acceleration_value_is_positive(raw_data)) 
			{
				display_symbol(LCD_SYMB_ARROW_UP, SEG_ON);
				display_symbol(LCD_SYMB_ARROW_DOWN, SEG_OFF);
			}
			else 
			{
				display_symbol(LCD_SYMB_ARROW_UP, SEG_OFF);
				display_symbol(LCD_SYMB_ARROW_DOWN, SEG_ON);
			}
		}
		else if (update == DISPLAY_LINE_CLEAR)
		{
			// Stop acceleration sensor
			as_stop();
	
			// Clear mode
			sAccel.mode = ACCEL_MODE_OFF;
			
			// Clean up display
			display_symbol(LCD_SEG_L1_DP1, SEG_OFF);
			display_symbol(LCD_SYMB_ARROW_UP, SEG_OFF);
			display_symbol(LCD_SYMB_ARROW_DOWN, SEG_OFF);	
		}
	}
}


acceleration.h

// *************************************************************************************************
//
//	Copyright (C) 2009 Texas Instruments Incorporated - http://www.ti.com/ 
//	 
//	 
//	  Redistribution and use in source and binary forms, with or without 
//	  modification, are permitted provided that the following conditions 
//	  are met:
//	
//	    Redistributions of source code must retain the above copyright 
//	    notice, this list of conditions and the following disclaimer.
//	 
//	    Redistributions in binary form must reproduce the above copyright
//	    notice, this list of conditions and the following disclaimer in the 
//	    documentation and/or other materials provided with the   
//	    distribution.
//	 
//	    Neither the name of Texas Instruments Incorporated nor the names of
//	    its contributors may be used to endorse or promote products derived
//	    from this software without specific prior written permission.
//	
//	  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
//	  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
//	  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
//	  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
//	  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
//	  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
//	  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
//	  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
//	  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
//	  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
//	  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// *************************************************************************************************

#ifndef ACCELERATION_H_
#define ACCELERATION_H_


// *************************************************************************************************
// Include section


// *************************************************************************************************
// Prototypes section



// *************************************************************************************************
// Defines section
#define DISPLAY_ACCEL_X		(0u)
#define DISPLAY_ACCEL_Y		(1u)
#define DISPLAY_ACCEL_Z		(2u)

#define ACCEL_MODE_OFF		(0u)
#define ACCEL_MODE_ON		(1u)

// Stop acceleration measurement after 60 minutes to save battery
#define ACCEL_MEASUREMENT_TIMEOUT		(60*60u)


// *************************************************************************************************
// Global Variable section
struct accel
{
	// ACC_MODE_OFF, ACC_MODE_ON
	u8			mode;
	
	// Sensor raw data
	u8			xyz[3];

	// Acceleration data in 10 * mgrav
	u16			data;

	// Display X/Y/Z values	
	u8 			view_style;

	// Timeout
	u16			timeout;	
};
extern struct accel sAccel;


// *************************************************************************************************
// Extern section
extern void reset_acceleration(void);
extern void sx_acceleration(u8 line);
extern void display_acceleration(u8 line, u8 update);
extern u8 is_acceleration_measurement(void);
extern void do_acceleration_measurement(void);

#endif /*ACCELERATION_H_*/

vti_as.c 

// *************************************************************************************************
//
//	Copyright (C) 2009 Texas Instruments Incorporated - http://www.ti.com/ 
//	 
//	 
//	  Redistribution and use in source and binary forms, with or without 
//	  modification, are permitted provided that the following conditions 
//	  are met:
//	
//	    Redistributions of source code must retain the above copyright 
//	    notice, this list of conditions and the following disclaimer.
//	 
//	    Redistributions in binary form must reproduce the above copyright
//	    notice, this list of conditions and the following disclaimer in the 
//	    documentation and/or other materials provided with the   
//	    distribution.
//	 
//	    Neither the name of Texas Instruments Incorporated nor the names of
//	    its contributors may be used to endorse or promote products derived
//	    from this software without specific prior written permission.
//	
//	  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
//	  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
//	  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
//	  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
//	  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
//	  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
//	  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
//	  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
//	  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
//	  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
//	  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// *************************************************************************************************
// VTI CMA3000-D0x acceleration sensor driver functions
// *************************************************************************************************


// *************************************************************************************************
// Include section

// system
#include "project.h"

// logic
#include "simpliciti.h"

// driver
#include "vti_as.h"
#include "timer.h"
#include "display.h"


// *************************************************************************************************
// Prototypes section
void as_start(void);
void as_stop(void);
u8 as_read_register(u8 bAddress);
u8 as_write_register(u8 bAddress, u8 bData);


// *************************************************************************************************
// Defines section

// =================================================================================================
// CMA3000-D0x acceleration sensor configuration
// =================================================================================================
// DCO frequency division factor determining speed of the acceleration sensor SPI interface
// Speed in Hz = 12MHz / AS_BR_DIVIDER (max. 500kHz)
#define AS_BR_DIVIDER        (30u)

// Acceleration measurement range in g
// Valid ranges are: 2 and 8
#define AS_RANGE             (2u)

// Sample rate for acceleration values in Hz
// Valid sample rates for 2g range are:     100, 400
// Valid sample rates for 8g range are: 40, 100, 400
#define AS_SAMPLE_RATE       (100u)


// *************************************************************************************************
// Global Variable section

// Global flag for proper acceleration sensor operation
u8 as_ok;


// *************************************************************************************************
// Extern section



// *************************************************************************************************
// @fn          as_init
// @brief       Setup acceleration sensor connection, do not power up yet
// @param       none
// @return      none
// *************************************************************************************************
void as_init(void)
{
#ifdef AS_DISCONNECT	
	// Deactivate connection to acceleration sensor
	AS_PWR_OUT &= ~AS_PWR_PIN;            	// Power off
	AS_INT_OUT &= ~AS_INT_PIN;            	// Pin to low to avoid floating pins
	AS_SPI_OUT &= ~(AS_SDO_PIN + AS_SDI_PIN + AS_SCK_PIN); // Pin to low to avoid floating pins
	AS_CSN_OUT &= ~AS_CSN_PIN; 				// Pin to low to avoid floating pins
	AS_INT_DIR |= AS_INT_PIN;            	// Pin to output to avoid floating pins
	AS_SPI_DIR |= AS_SDO_PIN + AS_SDI_PIN + AS_SCK_PIN;    // Pin to output to avoid floating pins
	AS_CSN_DIR |= AS_CSN_PIN;				// Pin to output to avoid floating pins
	AS_PWR_DIR |= AS_PWR_PIN;            	// Power pin to output direction
#else
	AS_INT_DIR &= ~AS_INT_PIN;            	// Input
	AS_SPI_DIR &= ~AS_SDI_PIN; 				// Input
	AS_SPI_DIR |= AS_SDO_PIN + AS_SCK_PIN;  // Output
	AS_SPI_SEL |= AS_SDO_PIN + AS_SDI_PIN + AS_SCK_PIN; // Port pins to SDO, SDI and SCK function
	AS_CSN_OUT |= AS_CSN_PIN; 				// CSN=1
	AS_CSN_DIR |= AS_CSN_PIN;				// 
	AS_PWR_OUT |= AS_PWR_PIN;            	// VDD=1
	AS_PWR_DIR |= AS_PWR_PIN;            	// 
#endif

	// Reset global sensor flag
	as_ok = 1;
}


// *************************************************************************************************
// @fn          as_start
// @brief       Power-up and initialize acceleration sensor
// @param       none
// @return      none
// *************************************************************************************************
void as_start(void)
{
	volatile u16 Counter_u16;
	u8 bConfig;//, bStatus;
	
	// Initialize SPI interface to acceleration sensor
	AS_SPI_CTL0 |= UCSYNC | UCMST | UCMSB // SPI master, 8 data bits,  MSB first,
	               | UCCKPH;              //  clock idle low, data output on falling edge
	AS_SPI_CTL1 |= UCSSEL1;               // SMCLK as clock source
	AS_SPI_BR0   = AS_BR_DIVIDER;         // Low byte of division factor for baud rate
	AS_SPI_BR1   = 0x00;                  // High byte of division factor for baud rate
	AS_SPI_CTL1 &= ~UCSWRST;              // Start SPI hardware
  
	// Initialize interrupt pin for data read out from acceleration sensor
	AS_INT_IES &= ~AS_INT_PIN;            // Interrupt on rising edge

#ifdef AS_DISCONNECT	  
	// Enable interrupt 
	AS_INT_DIR &= ~AS_INT_PIN;            // Switch INT pin to input
	AS_SPI_DIR &= ~AS_SDI_PIN;            // Switch SDI pin to input
	AS_SPI_REN |=  AS_SDI_PIN;            // Pulldown on SDI pin
	AS_SPI_SEL |=  AS_SDO_PIN + AS_SDI_PIN + AS_SCK_PIN; // Port pins to SDO, SDI and SCK function
	AS_CSN_OUT |=  AS_CSN_PIN;            // Deselect acceleration sensor
	AS_PWR_OUT |=  AS_PWR_PIN;            // Power on active high
#endif

	// Delay of >5ms required between switching on power and configuring sensor
	Timer0_A4_Delay(CONV_MS_TO_TICKS(10));
	
	// Initialize interrupt pin for data read out from acceleration sensor
	AS_INT_IFG &= ~AS_INT_PIN;            // Reset flag
	AS_INT_IE  |=  AS_INT_PIN;            // Enable interrupt
	
	// Configure sensor and start to sample data
#if (AS_RANGE == 2)
  bConfig = 0x80;
  #if (AS_SAMPLE_RATE == 100)
    bConfig |= 0x02;
  #elif (AS_SAMPLE_RATE == 400)
    bConfig |= 0x04;
  #else
    #error "Sample rate not supported"
  #endif
#elif (AS_RANGE == 8)
  bConfig = 0x00;
  #if (AS_SAMPLE_RATE == 40)
    bConfig |= 0x06;
  #elif (AS_SAMPLE_RATE == 100)
    bConfig |= 0x02;
  #elif (AS_SAMPLE_RATE == 400)
    bConfig |= 0x04;
  #else
    #error "Sample rate not supported"
  #endif
#else
  #error "Measurement range not supported"    
#endif  

	// Reset sensor
	as_write_register(0x04, 0x02);   
	as_write_register(0x04, 0x0A);   
	as_write_register(0x04, 0x04);   
	
	// Wait 5 ms before starting sensor output
	Timer0_A4_Delay(CONV_MS_TO_TICKS(5));
	
	// Set 2g measurement range, start to output data with 100Hz rate
	as_write_register(0x02, bConfig);   
}



// *************************************************************************************************
// @fn          as_stop
// @brief       Power down acceleration sensor
// @param       none
// @return      none
// *************************************************************************************************
void as_stop(void)
{
	// Disable interrupt 
	AS_INT_IE  &=  ~AS_INT_PIN;            	// Disable interrupt

#ifdef AS_DISCONNECT
	// Power-down sensor
	AS_PWR_OUT &= ~AS_PWR_PIN;            	// Power off
	AS_INT_OUT &= ~AS_INT_PIN;            	// Pin to low to avoid floating pins
	AS_SPI_OUT &= ~(AS_SDO_PIN + AS_SDI_PIN + AS_SCK_PIN); // Pins to low to avoid floating pins
	AS_SPI_SEL &= ~(AS_SDO_PIN + AS_SDI_PIN + AS_SCK_PIN); // Port pins to I/O function
	AS_CSN_OUT &= ~AS_CSN_PIN; 				// Pin to low to avoid floating pins
	AS_INT_DIR |= AS_INT_PIN;            	// Pin to output to avoid floating pins
	AS_SPI_DIR |= AS_SDO_PIN + AS_SDI_PIN + AS_SCK_PIN;    // Pins to output to avoid floating pins
	AS_CSN_DIR |= AS_CSN_PIN;				// Pin to output to avoid floating pins
#else
	// Reset sensor -> sensor to powerdown
	as_write_register(0x04, 0x02);   
	as_write_register(0x04, 0x0A);   
	as_write_register(0x04, 0x04);   
#endif
}


// *************************************************************************************************
// @fn          as_read_register
// @brief       Read a byte from the acceleration sensor
// @param       u8 bAddress		Register address
// @return      u8					Register content
// *************************************************************************************************
u8 as_read_register(u8 bAddress)
{
  u8 bResult;
  u16 timeout;
  
  // Exit function if an error was detected previously
  if (!as_ok) return (0);

  bAddress <<= 2;                     // Address to be shifted left by 2 and RW bit to be reset

  AS_SPI_REN &= ~AS_SDI_PIN;          // Pulldown on SDI pin not required
  AS_CSN_OUT &= ~AS_CSN_PIN;          // Select acceleration sensor

  bResult = AS_RX_BUFFER;             // Read RX buffer just to clear interrupt flag

  AS_TX_BUFFER = bAddress;            // Write address to TX buffer
  
  timeout = SPI_TIMEOUT;
  while (!(AS_IRQ_REG & AS_RX_IFG) && (--timeout>0));  // Wait until new data was written into RX buffer
  if (timeout == 0)
  {
  	as_ok = 0;
  	return (0);
  }
  bResult = AS_RX_BUFFER;             // Read RX buffer just to clear interrupt flag

  AS_TX_BUFFER = 0;                   // Write dummy data to TX buffer
  
  timeout = SPI_TIMEOUT;
  while (!(AS_IRQ_REG & AS_RX_IFG) && (--timeout>0));  // Wait until new data was written into RX buffer
  if (timeout == 0)
  {
  	as_ok = 0;
  	return (0);
  }
  bResult = AS_RX_BUFFER;             // Read RX buffer

  AS_CSN_OUT |=  AS_CSN_PIN;          // Deselect acceleration sensor
  AS_SPI_REN |=  AS_SDI_PIN;          // Pulldown on SDI pin required again

  // Return new data from RX buffer
  return bResult;
}


// *************************************************************************************************
// @fn          as_write_register
// @brief  		Write a byte to the acceleration sensor
// @param       u8 bAddress		Register address
//				u8 bData			Data to write
// @return      u8					
// *************************************************************************************************
u8 as_write_register(u8 bAddress, u8 bData)
{
  u8 bResult;
  u16 timeout;
  
  // Exit function if an error was detected previously
  if (!as_ok) return (0);

  bAddress <<= 2;                     // Address to be shifted left by 1
  bAddress |= BIT1;                   // RW bit to be set
  
  AS_SPI_REN &= ~AS_SDI_PIN;          // Pulldown on SDI pin not required
  AS_CSN_OUT &= ~AS_CSN_PIN;          // Select acceleration sensor

  bResult = AS_RX_BUFFER;             // Read RX buffer just to clear interrupt flag

  AS_TX_BUFFER = bAddress;            // Write address to TX buffer
  
  timeout = SPI_TIMEOUT;
  while (!(AS_IRQ_REG & AS_RX_IFG) && (--timeout>0));  // Wait until new data was written into RX buffer
  if (timeout == 0)
  {
  	as_ok = 0;
  	return (0);
  }
  bResult = AS_RX_BUFFER;             // Read RX buffer just to clear interrupt flag

  AS_TX_BUFFER = bData;               // Write data to TX buffer

  timeout = SPI_TIMEOUT;
  while (!(AS_IRQ_REG & AS_RX_IFG) && (--timeout>0));  // Wait until new data was written into RX buffer
  if (timeout == 0) 
  {
  	as_ok = 0;
  	return (0);
  }
  bResult = AS_RX_BUFFER;             // Read RX buffer

  AS_CSN_OUT |=  AS_CSN_PIN;          // Deselect acceleration sensor
  AS_SPI_REN |=  AS_SDI_PIN;          // Pulldown on SDI pin required again

  return bResult;
}


// *************************************************************************************************
// @fn          as_get_data
// @brief       Service routine to read acceleration values.
// @param       none
// @return      none
// *************************************************************************************************
void as_get_data(u8 * data)
{
	// Exit if sensor is not powered up
	if ((AS_PWR_OUT & AS_PWR_PIN) != AS_PWR_PIN) return;
  
  	// Store X/Y/Z acceleration data in buffer
	*(data+0) = as_read_register(0x06);
	*(data+1) = as_read_register(0x07);
	*(data+2) = as_read_register(0x08);
}


vti_as.h
// *************************************************************************************************
//
//	Copyright (C) 2009 Texas Instruments Incorporated - http://www.ti.com/ 
//	 
//	 
//	  Redistribution and use in source and binary forms, with or without 
//	  modification, are permitted provided that the following conditions 
//	  are met:
//	
//	    Redistributions of source code must retain the above copyright 
//	    notice, this list of conditions and the following disclaimer.
//	 
//	    Redistributions in binary form must reproduce the above copyright
//	    notice, this list of conditions and the following disclaimer in the 
//	    documentation and/or other materials provided with the   
//	    distribution.
//	 
//	    Neither the name of Texas Instruments Incorporated nor the names of
//	    its contributors may be used to endorse or promote products derived
//	    from this software without specific prior written permission.
//	
//	  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
//	  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
//	  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
//	  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
//	  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
//	  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
//	  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
//	  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
//	  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
//	  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
//	  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// *************************************************************************************************

#ifndef VTI_AS_H_
#define VTI_AS_H_


// *************************************************************************************************
// Include section


// *************************************************************************************************
// Prototypes section
extern void as_init(void);
extern void as_start(void);
extern void as_stop(void);
extern u8 as_read_register(u8 bAddress);
extern u8 as_write_register(u8 bAddress, u8 bData);
extern void as_get_data(u8 * data);


// *************************************************************************************************
// Defines section

// Disconnect power supply for acceleration sensor when not used
#define AS_DISCONNECT

// Port and pin resource for SPI interface to acceleration sensor
// SDO=MOSI=P1.6, SDI=MISO=P1.5, SCK=P1.7
#define AS_SPI_IN            (P1IN)
#define AS_SPI_OUT           (P1OUT)
#define AS_SPI_DIR           (P1DIR)
#define AS_SPI_SEL           (P1SEL)
#define AS_SPI_REN           (P1REN)
#define AS_SDO_PIN           (BIT6)
#define AS_SDI_PIN           (BIT5)
#define AS_SCK_PIN           (BIT7)

// CSN=PJ.1
#define AS_CSN_OUT			 (PJOUT)
#define AS_CSN_DIR			 (PJDIR)
#define AS_CSN_PIN           (BIT1)

#define AS_TX_BUFFER         (UCA0TXBUF)
#define AS_RX_BUFFER         (UCA0RXBUF)
#define AS_TX_IFG            (UCTXIFG)
#define AS_RX_IFG            (UCRXIFG)
#define AS_IRQ_REG           (UCA0IFG) 
#define AS_SPI_CTL0          (UCA0CTL0)
#define AS_SPI_CTL1          (UCA0CTL1) 
#define AS_SPI_BR0           (UCA0BR0)
#define AS_SPI_BR1           (UCA0BR1)

// Port and pin resource for power-up of acceleration sensor, VDD=PJ.0
#define AS_PWR_OUT           (PJOUT)
#define AS_PWR_DIR           (PJDIR)
#define AS_PWR_PIN           (BIT0)

// Port, pin and interrupt resource for interrupt from acceleration sensor, CMA_INT=P2.5
#define AS_INT_IN            (P2IN)
#define AS_INT_OUT           (P2OUT)
#define AS_INT_DIR           (P2DIR)
#define AS_INT_IE            (P2IE)
#define AS_INT_IES           (P2IES)
#define AS_INT_IFG           (P2IFG)
#define AS_INT_PIN           (BIT5)

// SPI timeout to detect sensor failure
#define SPI_TIMEOUT				(1000u)


// *************************************************************************************************
// Global Variable section


// *************************************************************************************************
// Extern section


#endif /*VTI_AS_H_*/

你可能感兴趣的:(EZ430 Chronos 自带程序源码:三轴加速计示范程序)