Você está na página 1de 7

Accelerometer Interface using PSoC®

AN52678
Author: Archana Yarlagadda
Associated Project: Yes
Associated Part Family: CY8C21x34/ 21x24/ 24x33/
24x34A/ 24x94/ 27x43/ 29x66
Software Version: PSoC Designer™ 5.0
Associated Application Notes: AN2010, AN2352, AN2099

Application Note Abstract


This application note describes how to interface accelerometers with PSoC®. It analyzes the types of accelerometers and the
applications in which they are used. The example project accompanying this application note demonstrates the use of a three
dimensional analog accelerometer to detect the orientation and movement of mobile phones.

Introduction flexible. With digital accelerometers, parameters such


as resolution, sampling speed, bandwidth, and SNR are
Accelerometers are electromechanical devices that measure limited to a narrow selection range.
different types of acceleration such as static and dynamic
acceleration. Static acceleration includes the orientation with Microprocessors with some external hardware can interface
respect to earth and dynamic acceleration involves with the digital communication output accelerometers.
movement and vibration. Acceleration is measured in terms However, this does not work with digital PWM and analog
of “g”, the earth’s gravitational acceleration at sea level output accelerometers. Microcontrollers can interface with
(9.81 m/s2). This application note discusses the types of digital PWM and digital communication output types, but not
accelerometers, their application, and interface with to the analog output accelerometers. The PSoC device,
Programmable System-on-Chip™ (PSoC). considered in this application note, has the flexibility of
interfacing with all three types. This is illustrated in Figure 1.
Types of Accelerometers Figure 1. Interface Devices for Different Accelerometers
Accelerometers are divided based on their output type,
sensing method, manufacturing technology, and other
properties. When output type is considered, accelerometers
can have either analog or digital output. The analog output Microprocessor +
accelerometers often have voltage as the output, while Microcontrollers PSoC
External Hardware
digital output accelerometers have Pulse Width Modulated
(PWM) waveform, I2C, or SPI. Digital Communication Digital PWM Analog
Accelerometers Accelerometers Accelerometers
The choice between the analog and digital type depends on
the interface hardware.

 Analog accelerometers output the most raw data form


and their measurement is highly flexible. The speed and
accuracy with which the analog output is measured
changes with the hardware it is interfaced with. The analog output type provides the most raw data format,
and is flexible and cost effective. The PSoC has high
 Digital PWM output accelerometers involve intensive accuracy programmable and reconfigurable analog
computation and timing analysis to get the acceleration capability. Therefore, this application note focuses on the
from the waveform. interface of analog accelerometers with PSoC.
All accelerometers have a mechanical mass that converts
 Accelerometers with digital communication output vibration into displacement. This displacement is sensed
(I2C/SPI) have Analog to Digital Convertors (ADC) and using a sensor. The classification of accelerometers based
digital communication modules integrated into the chip on sensor type is listed in Table 1.
along with the basic analog output accelerometer. This
added hardware is fixed function and increases the cost
of the accelerometer. These accelerometers provide
easy interface to a larger digital system, but are least

April 7, 2009 Document No. 001-52678 Rev. ** 1

[+] Feedback
AN52678

Table 1. Accelerometer Types and Properties Figure 2. Functional Block Diagram of KXSC7-2050

Accelerator Type Properties Measurement


Capacitive Good DC accuracy, Tilt, Vibration
low noise, good
temperature
performance
Piezoelectric AC response only Vibration,
Shock
Piezoresistive Good DC accuracy, Tilt, Vibration
poor temperature
performance
Electromechanical Good DC accuracy, Tilt
Servo low frequency only,
fragile
The sensor element in the accelerometer works on the
The choice of accelerometer depends on the type of principle of differential capacitance. Here, the acceleration
application in which it is used. causes displacement in the silicon structure, resulting in a
change in capacitance. The ASIC is included inside the
package to convert change in capacitance into analog
Applications voltage, which is proportional to acceleration. It also features
Some applications in which accelerometers are used are as a programmable low pass filter. Thus the output voltages
follows: can be read directly by the ADC and processed accordingly.

 Mobile Phones: Used for motion activated functions, Interface with PSoC
gaming, free-fall detection, and vibration control.
This document assumes that the reader is familiar with the
 Vehicles: Used for crash testing, robotics, motion PSoC User Modules (UM). If you are new to PSoC, AN2010
control, and skid detection. Getting Started with PSoC is a good reference. The project
 Computers: Used in computers and computer accompanying this application note uses CY8C24533. The
peripherals such as mouse, for motion activated following sections are based on the same PSoC family. The
functions, gaming, and tilt sensing. accelerometer project can also be implemented with other
 Medical Appliances: Used in pacemaker and blood PSoC families.
pressure measurement applications.
Block Diagram and Working
 Home Appliances: Used in appliances such as washing
The voltages on the X, Y, and Z axes are measured directly
machines for spin and vibration measurement.
with a single ADC by multiplexing the input ports. The
This application note focuses on the use of accelerometers accelerometer has an internal low pass filter and thus it is
in mobile phones; therefore, both tilt and vibration are not implemented in PSoC. The block diagram of the system
measured. Considering the analog benefits of PSoC and the is shown in Figure 3.
properties of capacitive sensing, an analog output capacitive
sensing accelerometer is ideal for this application. Examples Figure 3. Block Diagram of Accelerometer with PSoC
of analog accelerometers that can be used are BMA140,
KXSC7–1050/KXSC7–2050, MMA7360L, ADXL330, and PSoC
LIS344AL. KXSC7 series accelerometer is selected here X
P0[2]
based on the requirements. Accelerometer Y
MUX ADC I2C Display
(KXSC7-2050) P0[1]
Z
The KXSC7-2050 Accelerometer P0[0]
PSoC Internal
VDD GND Mode ST/MOT Enable
KXSC7-2050 is a tri-axis, analog voltage output device. The P1[7] P1[6] P1[5] P1[5] P1[3] PSoC Hardware Module

output range of this device is ±2g, or 19.9 m/s2. The voltage Control PSoC Software Module

on the three outputs change based on the static and External Devices

dynamic acceleration of the device. The functional block The output of the ADC block can be processed using digital
diagram of the accelerometer is shown in Figure 2. This blocks, displayed using an LCD module, or transmitted
diagram is reproduced from the data sheet of KXSC7 using the UART, I2C, or SPI digital communication modules
accelerometer on the Kionix website. available in this chip.
The example project makes use of I2C UM and transfers the
data via the external I2C-USB Bridge hardware. The output
is then plotted using the Cypress I2C-USB Bridge software.
For more details about I2C Bridge usage and working, refer
to AN2352.

April 7, 2009 Document No. 001-52678 Rev. ** 2

[+] Feedback
AN52678

User Modules and Routing Resources Used in Different PSoC Families


The user modules used in the example project are SAR8 Different UMs must be used to implement the same project
UM (ADC) and EzI2Cs (I2C communication). The routing in in different PSoC families. Table 2 shows the resources
PSoC Designer™ is shown in Figure 4 and is based on the used by different PSoC families when the UMs for each
block diagram shown in Figure 3. family are chosen appropriately to implement the same
project.
Multiplexing ports into the SAR8 block is done with software,
by changing the contents of registers. The sampled data Table 2. Resources used for Accelerometer Project
from the ADC is then given as the input to the I2C UM in the
software code. The code for the project is provided later in

Analog Blocks

SRAM (Bytes)
Digital Blocks

I2C Controller

User Modules
Part Number

Flash (KB)

Decimator
this application note.

CY8C
Figure 4. Routing in PSoC with SAR8 as ADC

A U A U A U A U A U A U A=Available, U=Used

24x33 4 0 4 0 8 0.5 256 6 1 0 1 1 SAR8, EzI2C

29x66 16 1 12 2 32 0.5 2048 14 1 1 1 1 ADCINC, PGA, EzI2C

27x43 8 1 12 2 16 0.5 256 14 1 1 1 1 ADCINC, PGA, EzI2C

24x94 4 1 6 2 16 0.5 1024 14 1 1 1 1 ADCINC, PGA, EzI2C

The same functionality can be achieved by using an 24x23A 4 1 6 2 4 0.5 256 14 1 1 1 1 ADCINC, PGA, EzI2C
ADCINC to get a higher resolution than SAR8. 21x34 4 1 4 2 8 0.8 512 17 0 0 1 1 ADC8, EzI2C
Many PSoC families can be used to implement this project.
21x23 4 1 4 2 4 0.8 256 17 0 0 1 1 ADC8, EzI2C
The benefit of using this PSoC chip over others is that this
chip family has the hardware SAR8 block and does not use
the Switched Capacitor (SC) analog block. The use of SAR8 Software Code and Control
not only saves analog blocks, but also digital blocks that are A software code is used to start all the user modules and
used by most ADC UMs in other PSoC chips. connect the appropriate inputs to the SAR8 UM and process
The SC block also requires the input to be buffered through the data obtained. The different functions performed are as
PGA for two reasons. Only Port-2 pins can be connected to follows:
the SC blocks directly and are limited by one pin to a Get_Acceleration Function
particular block. For example, ASC10 block in PSoC
For each axis connection, the ADC input is sampled and the
Designer can be connected to only Port2[3] and not other
ADC counts are converted to the voltage equivalent value in
pins of Port-2. Thus multiplexing to use one ADC is not
this function. The KXSC7-2050 has an offset voltage of
possible, if connected directly to the SC block. The second
1.65V and this is subtracted from the voltage measured.
problem with connecting the input directly to a SC block
Then voltage is converted to the “g” equivalent, based on
ADC is the loading that happens on the SC circuit when
the sensitivity of the accelerometer.
connected to a high impedance node.
For a higher resolution, use an SC ADC at the expense of IIR Filter
analog blocks. The routing when an ADCINC UM is chosen The acceleration obtained is filtered using software IIR
is shown in Figure 5 method by taking a part of the old value and adding to a part
of the new value. This method is explained in application
Figure 5. Routing in PSoC with ADCINC as ADC note AN2352, Single-Pole IIR Filter.

I2C Communication
The acceleration on each axis after converting to “g” is
stored in the I2C RAM buffer. The PSoC is configured as an
I2C slave using the EzI2Cs UM. These values are
transferred over the I2C Bridge and displayed using the I2C
Bridge software.

April 7, 2009 Document No. 001-52678 Rev. ** 3

[+] Feedback
AN52678

fAccl_Volt = (float)bAccl_Count *
Code with SAR8 ADC_VOLTS_PER_COUNT;
#include <m8c.h> //Convert Volts to "g"
#include "PSoCAPI.h" fAccl_g = (fAccl_Volt-
ACCL_OFFSET)*ACCL_SENSITIVITY;
//Volts per count=(3.3V/2^8)=0.0128 return(fAccl_g);
#define ADC_VOLTS_PER_COUNT 0.0128 }
//KXSC7-2050 1/sensitivity=1.515 g/V
#define ACCL_SENSITIVITY 1.515 Code with ADCINC and PGA
//KXSC7-2050 Offset = 1.65 V
#define ACCL_OFFSET 1.65 The code for a 14 bit ADCINC ADC is given below.
#include <m8c.h>
BYTE bAccl_Count; #include "PSoCAPI.h"
float fAccl_Volt;
float fAccelarometer_g[3]; //Volts per count=(3.3V/2^14)=0.0002
float Get_Accelaration(void); #define ADC_VOLTS_PER_COUNT 0.0002
//KXSC7-2050 1/sensitivity=1.515 g/V
void main() #define ACCL_SENSITIVITY 1.515
{ //KXSC7-2050 Offset = 1.65 V
//Setting I2C data size and address #define ACCL_OFFSET 1.65
EzI2Cs_SetRamBuffer(sizeof(fAccelarometer_g)
,0,(BYTE *)&fAccelarometer_g); int iAccl_Count;
float fAccl_Volt;
//Set Enable=1,Vdd=1,GND=0 and Mode=1 float fAccelarometer_g[3];
PRT1DR = (PRT1DR|0xA8)&(~0x40); float Get_Accelaration(void);
//Enable Global Interrupts
M8C_EnableGInt; void main()
{
//Start all the UMs and initialize //Setting I2C data size and address
SAR8_Start(); EzI2Cs_SetRamBuffer(sizeof(fAccelarometer_g)
EzI2Cs_Start(); ,0,(BYTE *)&fAccelarometer_g);
while(1)
{ //Set Enable=1,Vdd=1,GND=0 and Mode=1
//SAR8 input to P0[2]and sample X data PRT1DR =(PRT1DR|0xA8)&(~0x40);
SAR8_SelectADCChannel(SAR8_P0_2); //Enable Global Interrupts
//IIR filter : New_Value M8C_EnableGInt;
=0.9*Old_Value+0.1*New_Value
fAccelarometer_g[0]=0.9*(fAccelarometer_g[0] //Start all the UMs and initialize
)+0.1*(Get_Accelaration()); ADCINC_Start(ADCINC_HIGHPOWER);
PGA_Start(PGA_HIGHPOWER);
//SAR8 input to P0[1]and sample Y data AMUX8_Start();
SAR8_SelectADCChannel(SAR8_P0_1); while(1)
fAccelarometer_g[1]=0.9*(fAccelarometer_g[1] {
)+0.1*(Get_Accelaration()); //Connect PGA to P0[2]and sample X
AMUX8_InputSelect(AMUX8_PORT0_2);
//SAR8 input to P0[0]and sample Z data //IIR filter : New_Value
SAR8_SelectADCChannel(SAR8_P0_0); =0.1*Old_Value+0.9*New_Value
fAccelarometer_g[2]=0.9*(fAccelarometer_g[2] fAccelarometer_g[0] =
)+0.1*(Get_Accelaration()); 0.1*(fAccelarometer_g[0]) +
0.9*(Get_Accelaration());
}
} //Connect PGA to P0[1]and sample Y
float Get_Accelaration(void) AMUX8_InputSelect(AMUX8_PORT0_1);
{ fAccelarometer_g[1] =
float fAccl_g; 0.1*(fAccelarometer_g[1]) +
bAccl_Count=0; 0.9*(Get_Accelaration());
fAccl_Volt=0;
fAccl_g=0; //Connect PGA to P0[0]and sample Z
AMUX8_InputSelect(AMUX8_PORT0_0);
SAR8_Trigger(); fAccelarometer_g[2] =
//Wait till the ADC data is available 0.1*(fAccelarometer_g[2]) +
while (0 == SAR8_fIsDataAvailable()); 0.9*(Get_Accelaration());
//get the ADC counts data }
bAccl_Count = SAR8_bGetData(); float Get_Accelaration(void)
//convert ADC counts to Volts {

April 7, 2009 Document No. 001-52678 Rev. ** 4

[+] Feedback
AN52678

float fAccl_g; Figure 8. Board Inline with “g” and X=1g, Y=0g and Z=0g
iAccl_Count=0;
fAccl_Volt=0;
fAccl_g=0;

ADCINC_GetSamples(1);
//Wait till the ADC data is available
while (0 == ADCINC_fIsDataAvailable());
//get the ADC counts data
iAccl_Count = ADCINC_iClearFlagGetData();
//convert ADC counts to Volts
fAccl_Volt = (float)iAccl_Count *
ADC_VOLTS_PER_COUNT; Figure 9. Board Inline with “g” and X= 0g, Y= -1 g and Z= 0g
//Convert Volts to "g"
fAccl_g = (fAccl_Volt-
ACCL_OFFSET)*ACCL_SENSITIVITY;
return(fAccl_g);
}
Setup for Experiment

The Kionix KXSC7-2050 is interfaced with PSoC using the


Eval 1 board as shown in Figure 6. The pin connections for
the accelerometer board are illustrated in Figure 3. The port
pins of PSoC and the accelerometer pins are connected.
The control signals are set high and low in firmware and the Figure 10. Board Inline with “g” and X= -1g, Y=0g and Z=0g
results are observed using the I2C-USB Bridge.
Figure 6. Board Setup to Test Accelerometer Interface

Figure 11. Accelerometer Chip Perpendicular to Earth and


on Top

Results
The output in the graph is expressed in terms of “g”.
Because KXSC7-2050 is a 3-axis, 2g accelerometer, the g
values for the X, Y, and Z axes are between –1 and +1.
Different orientation of the board leads to different “g” values
on the output. Some combinations are shown in Figure 7
through Figure 12. The orientation of the board is changed Figure 12. Accelerometer Chip Perpendicular to Earth and
manually. Therefore, the outputs slightly vary from the on Bottom
expected values.
Figure 7. Board Inline with “g” and X=0g, Y=1g and Z=0g

April 7, 2009 Document No. 001-52678 Rev. ** 5

[+] Feedback
AN52678

The graph obtained when the board is changed from one Summary
position to another is shown in Figure 13.
The accelerometer type is selected based on the application
Figure 13. Accelerometer Moved Along Different Axes to be implemented. It is possible to interface both digital and
analog output accelerometers with PSoC. This application
note demonstrates the interface of an analog output
accelerometer. Many PSoC families can be used to
implement the same functionality. This application note
serves as the basic format of interface and the data
obtained can be used to generate control signals based on
the target application.

When there is free fall, X, Y, and Z have 0g acceleration.

About the Author


Name: Archana Yarlagadda
Title: Applications Engineer
Background: Applications Engineer in Cypress with
focus on PSoC.
Masters in Analog VLSI from University
of Tennessee, Knoxville
Contact yara@cypress.com

April 7, 2009 Document No. 001-52678 Rev. ** 6

[+] Feedback
AN52678

Document History
Document Title: Accelerometer Interface using PSoC®
Document Number: 001-52678
Revision ECN Orig. of Submission Description of Change
Change Date
** 2683340 YARA 04/03/09 New application note.

PSoC is a registered trademark of Cypress Semiconductor Corp. "Programmable System-on-Chip" and PSoC Designer are trademarks of
Cypress Semiconductor Corp. All other trademarks or registered trademarks referenced herein are the property of their respective owners.

Cypress Semiconductor
198 Champion Court
San Jose, CA 95134-1709
Phone: 408-943-2600
Fax: 408-943-4730
http://www.cypress.com/

© Cypress Semiconductor Corporation, 2009. The information contained herein is subject to change without notice. Cypress Semiconductor
Corporation assumes no responsibility for the use of any circuitry other than circuitry embodied in a Cypress product. Nor does it convey or imply any
license under patent or other rights. Cypress products are not warranted nor intended to be used for medical, life support, life saving, critical control or
safety applications, unless pursuant to an express written agreement with Cypress. Furthermore, Cypress does not authorize its products for use as
critical components in life-support systems where a malfunction or failure may reasonably be expected to result in significant injury to the user. The
inclusion of Cypress products in life-support systems application implies that the manufacturer assumes all risk of such use and in doing so indemnifies
Cypress against all charges.
This Source Code (software and/or firmware) is owned by Cypress Semiconductor Corporation (Cypress) and is protected by and subject to worldwide
patent protection (United States and foreign), United States copyright laws and international treaty provisions. Cypress hereby grants to licensee a
personal, non-exclusive, non-transferable license to copy, use, modify, create derivative works of, and compile the Cypress Source Code and derivative
works for the sole purpose of creating custom software and or firmware in support of licensee product to be used only in conjunction with a Cypress
integrated circuit as specified in the applicable agreement. Any reproduction, modification, translation, compilation, or representation of this Source
Code except as specified above is prohibited without the express written permission of Cypress.
Disclaimer: CYPRESS MAKES NO WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, WITH REGARD TO THIS MATERIAL, INCLUDING, BUT
NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. Cypress reserves the
right to make changes without further notice to the materials described herein. Cypress does not assume any liability arising out of the application or
use of any product or circuit described herein. Cypress does not authorize its products for use as critical components in life-support systems where a
malfunction or failure may reasonably be expected to result in significant injury to the user. The inclusion of Cypress’ product in a life-support systems
application implies that the manufacturer assumes all risk of such use and in doing so indemnifies Cypress against all charges.
Use may be limited by and subject to the applicable Cypress software license agreement.

April 7, 2009 Document No. 001-52678 Rev. ** 7

[+] Feedback

Você também pode gostar