4 degrees of freedom mems sensor improvements
TRANSCRIPT
Last Updated: Dec 1st 2012
4 Degrees of
Freedom
MEMS Sensor
Improvements
Design Document
Team Dec12-09 Name: Xin Zhou
Yue Zhang
Ang Lv
Nicholas Everett
Jenn Grubb
Advisor: Degang Chen
Client: Bee Line Company – Bob
1
Contents
Contents ................................................................................................................................................. 1
Problem Statement ................................................................................................................................ 4
Project Objectives .................................................................................................................................. 4
System Block Diagram ........................................................................................................................... 5
Voltage Regulator ............................................................................................................................... 6
Inclinometer ....................................................................................................................................... 7
Gyro: Angular Rate Sensors ................................................................................................................ 8
Micro-Processor ................................................................................................................................. 9
System Description .............................................................................................................................. 10
Operating environment .................................................................................................................... 10
User interface description ................................................................................................................ 10
Functional Requirements ................................................................................................................. 10
Non-Functional Requirements ......................................................................................................... 11
Expected Deliverables ......................................................................................................................... 11
Work Plan ............................................................................................................................................. 12
Work Breakdown .............................................................................................................................. 12
Costs ................................................................................................................................................. 13
Project Schedule ............................................................................................................................... 14
Risks .................................................................................................................................................. 14
2
Detailed Design .................................................................................................................................... 15
Microcontroller................................................................................................................................. 15
Inclinometer ..................................................................................................................................... 15
Gyro .................................................................................................................................................. 16
Interconnects .................................................................................................................................... 16
Schematic Design ................................................................................................................................. 17
Microcontroller and JTAG ................................................................................................................. 17
Gyro Schematic Design ..................................................................................................................... 18
Inclinometers Schematic Design ....................................................................................................... 19
Regulators Schematic Design ............................................................................................................ 20
Testing Points on Schematic ............................................................................................................. 21
Layout Design ....................................................................................................................................... 23
Gyro Footprint Information .............................................................................................................. 23
Inclinometer Footprint Information ................................................................................................. 24
Regulator Footprint Information ...................................................................................................... 24
Overview of Layout ........................................................................................................................... 25
Final Product ..................................................................................................................................... 26
Test Plan ............................................................................................................................................... 27
Connectivity Test ............................................................................................................................... 27
Ground level test ............................................................................................................................... 33
3
Regulators Voltage Test .................................................................................................................... 35
SPI Connection Test & Rough Board Function Test .......................................................................... 37
Appendix .............................................................................................................................................. 39
4
Problem Statement
The Beeline Company in Bettendorf produces equipment for performing wheel alignments on larger
commercial vehicles. Currently, they bring the vehicle into a large bay and prepare to perform the
alignment of the wheels. They use two different circuit boards connected to a computer to perform the
alignment. The first circuit board contains a microprocessor (to process the information from the
accelerometer and the gyro) and a 3-axis accelerometer (to ensure no movement is occurring). The
second circuit contains a 1-axis gyro that can measure the angle of the wheel being aligned and is
connected to the first board. The client has found that the circuit boards currently have a high failure
rate. Many times during the alignment, the circuit board will stop responding or provide data that is out
of the tolerance that is necessary. This requires the technicians to reinitialize the boards and begin the
alignment again.
Project Objectives
Beeline has requested for the team to design a new circuit board for their alignment process to
replace their current circuit boards. The team has the option of either placing all components on one
circuit board or maintaining their current setup by using two circuit boards. The circuits need to be able
to measure 0.001 degrees of accuracy from an accelerometer or inclinometer and 0.1 degree or better
for at least 2 minutes from the gyro.
5
System Block Diagram:
Figure 1 System Block Diagram
6
Voltage Regulator
Figure 2 Voltage Regulator
The voltage regulator is designed for portable and wireless applications with demanding
performance and space requirements. The voltage regulator is stable with a small 1uF ±30% ceramic or
high-quality tantalum output capacitor. It is optimized for battery powered systems to deliver ultra-low
noise, extremely low dropout voltage and low quiescent current.
Key Specifications:
2.5 to 6.0V input range
150mA guaranteed output
50dB PSRR at 1kHz @ VIN = VOUT + 0.2V
≤1.5μA quiescent current when shut down
Fast Turn-On time: 200 μs
100mV maximum dropout with 150mA load
30μVrms output noise over 10Hz to 100kHz
−40 to +125°C junction temperature range for operation
2.5V, 2.6V, 2.7V, 2.8V, 2.85V, 2.9V, 3.0V, 3.1V, 3.2V,
3.3V, 4.7V, 4.8V and 5.0V outputs standard
There are two regulators currently used on the circuit board. One is 3.2V, and the other one is 4.7V.
7
Inclinometer
The Inclinometer consists of two different sensing devices, one that measures angular changes
about the X-axis and another for the Y-axis. The two signals are then used to calculate an angular
displacement in one direction. Therefore, for our circuit, we will have to use two of these devices
placed orthogonal to each other.
Figure 3 Inclinometer Functional Diagram
We will be using the SCA103t inclinometer from VTI technologies. The SCA103T Series is a 3D-MEMS-
based single axis inclinometer family that uses the differential measurement principle. The high
calibration accuracy combines extremely low temperature dependency, high resolution and low noise
together with a robust sensing element design, to make the SCA103T an ideal choice for high accuracy
leveling instruments. The VTI inclinometers are insensitive to vibration due to having over damped
sensing elements plus they can withstand mechanical shocks of 20000 g.
8
Gyro: Angular Rate Sensors
Gyros measure angular velocity, how fast something is spinning about an axis. Unlike inclinometers,
gyros are not affected by gravity, so they make a great complement to each other. The angular velocity
is in units of rotations per minute, or degrees per second. They can also tell you the amount of angular
displacement from a specified position. We will be using the L3G4200D gyroscope from ST
Microelectronics.
Figure 4 Gyro Block Diagram
9
Micro-Processor
A microprocessor incorporates the functions of a computer’s central processing unit on a single
integrated circuit. The microprocessor which is in this system collects analog data from data bus
transceiver, and then it converts the analog data into digital signals for analyzing. It also receives 3 axis
analog data from the accelerometer, and then converts from analog to digital. The microprocessor also
controls some LEDs which represent different warnings.
10
System Description
Operating environment
The boards could possibly be used in a Thermotron oven that heats up the air from about 40
degrees Fahrenheit to around 100 degree Fahrenheit. Therefore, the components on the boards need
to be able to withstand the temperature swing and still operate normally.
User interface description
Beeline interfaces with their current sensor boards with connections to the microprocessor through
a JTAG port to an Olimex USB programmer. The two separate boards also interface with each other
using a Board to Board RA 0.025 header connection.
Functional Requirements
Inclinometers must be able to measure roll and pitch axis within 0.001 degrees accuracy.
Gyro needs to be able to measure a single axis (yaw) and have an accuracy of at least 0.1
degrees for at least 2 minutes.
Circuit needs to have low power consumption since batteries will be used to power them.
Circuit board must be able to function properly in a temperature range of 0-100 degrees
Fahrenheit.
Circuit Board containing microprocessor must be able to connect to other equipment via SPI
connection.
11
Non-Functional Requirements
Circuit boards should be 1.6 inches by 1.15 inches in dimension.
Circuits should be testable via test equipment supplied by Beeline Company.
Expected Deliverables
The final expected product will be a complete circuit board system capable of running on lithium ion
batteries. The board will contain a microprocessor, an accelerometer sensor and a gyro sensor. It will
connect to Beeline’s current programming setup with JTAG connections. There will either be one
complete board with both accelerometer and gyro sensors or two separate boards capable of
connecting together.
12
Work Plan
Work Breakdown
The following are tasks that the team needs to accomplish over the two semesters to ensure a
successful finished product.
Project Definition
Component Selection
Circuit Design
Initial Design
Testing
Finalized Design
Board Design
Board Fabrication
Product Testing and Validation
Final Documentation
13
Costs
Material costs for the project have not yet been determined and will depend on the component
selection. The client has requested that the total costs for new parts be less than $150. The following is
a preliminary cost estimate.
Item Notes Cost
Microprocessor $3.50
Inclinometer Two are required $41.10 *2 = $82.20
Gyro $7.90
PCB Components Surface mount components,
input/output jacks, battery
enclosure, etc.
Unknown as of 04/17/12
PCB Fabrication $40 (Estimated. Unknown as
of 04/17/12)
Enclosure Unknown as of 04/17/12
Total $93.60
14
Project Schedule
Risks
The major risk that we will face is the possibility of destroying any of the sensors or the
microprocessor as they would be expensive to replace. The team can take precautionary measures by
thoroughly analyzing and simulating a circuit before starting any testing procedures. This will help by
reducing the risk of the circuit failing and damaging any of the components.
Project Definition
Component Selection
Initial Circuit Design
Testing
Final Circuit Design
Board Design
Board Fabrication
Product Testing
Final Documentaion
Completed
Remaining
15
Detailed Design
Microcontroller
We will be using the same microcontroller as the current board setup in order to reduce cost for our
client. The microcontroller is a MSP430F247 from Texas Instruments, which has low power modes that
benefit battery-operated devices such as ours. It features a 16-bit RISC CPU, 16-bit registers, 48 I/O
pins, and has not been a cause of problems for our client with the current design.
Inclinometer
The board will use two inclinometers positioned in perpendicular angles to measure the amount
that the device tilts. Once the board no longer detects a change in angle, it will output a reading. The
device we chose to use was a SCA103T series MEMS single axis inclinometer that has high resolution and
low noise which contributes to its high accuracy. The device has a 12 bit data output, a current
consumption of 5mA and a supply voltage range of 4.75-5.25V. The noise for this inclinometer is around
0.004 °/ , or 6.96 , making this an acceptable component for our client’s specifications. The
device will communicate with the microcontroller via SPI communication procedures.
16
Gyro
The gyro we will be using is the L3G4200D from STMicroelectronics. The L3G4200D is a low-power
three-axis angular rate sensor able to provide unprecedented stability of zero rate level and sensitivity
over temperature and time. It includes a sensing element and an IC interface capable of providing the
measured angular rate through a digital interface (SPI). It is an ultra-stable three axis digital output
gyroscope that has three selectable ranges (250/500/2000 °/s) which has a 16 bit data output. Some
other features are that it is very stable over a wide temperature and time range, has a low power
consumption of 6.1 mA during operation, and a wide supply voltage range. We will only be using the
yaw axis of the gyro to satisfy the client’s requirements and this may save on power consumption since
the other two axes will not be in use.
Interconnects
The board to board connections will be made by using a board to board 0.025 header. This will
allow the gyro board to interface with the microprocessor on the other circuit board. The
microprocessor board will receive its power via a Molex connector. All other data with be transferred
via SPI between components.
17
Schematic Design
Microcontroller and JTAG
Figure 5, MSP430 and JTAG Schematic view
By following the description that given in MSP430 data sheet, it is decided to use two groups of
capacitors to stabilize the voltage. Use a JTAG to build up interface between microcontroller and the
computer, so that the microcontroller can be programmed. There are two groups of SPI, one is
connecting to the main board, and the other on group are controlling two inclinometers and a gyro,
program PIN SSEL0, SSEL1 , SSEL2, so that we can choose which component that we are going to send or
receive data. SCL0 is used to be clock signal, and MISO is called master input slave output which is used
to receive data from sensors. MOSI is called master output slave input, which is used to send control
signals to the sensors.
18
Gyro Schematic Design
Figure 6 Gyro Shcematic View
Power supply decoupling capacitors (100 nF ceramic or
polyester +10 μF) should be placed as near as possible to the device
(common design practice).
If Vdd and Vdd_IO are not connected together, power supply
decoupling capacitors (100 nF and 10 μF between Vdd and common
ground, 100 nF between
Vdd_IO and
common ground) should be placed as near as possible to the
device (common design practice).
The L3G4200D IC includes a PLL (phase locked loop) circuit to
Figure 7 Gyro (Vdd Connection)
19
synchronize driving and
sensing interfaces. Capacitors and resistors must be added at the PLLFILT pin (as shown in
As using a group of capacitors to stabilize the gyro Vdd, then use a PLL circuit to drive and sense
interfaces. Then used SPI connection MOSI0 and MISO0 to control it, use SCK0 to clock it, and SSEL0 to
select gyro when we want its data send off to microcontroller.
Inclinometers Schematic Design
Figure 9 Two Inclinometers
Figure 8 PLL of gyro
20
Figure 10 Inclinometer Connection Example
As the example found from data sheet above, the inclinometer need a Vdd power which is
about 5V, and then need connect a 100n capacitor to it. It also needs one SPI control since we only need
its digital data. MOSI0 and MISO0 used as SPI, SCK0 used as clock, and SSEL1 and SSEL2 to select two
inclinometers when we want its data send off to microcontroller.
Regulators Schematic Design
Figure 11 Two Regulators
21
The decision of the values of the regulators are made due to the power requirement of gyro and
inclinometers. The gyro need typical Vdd of 3V and inclinometer need 5V, so we use 3V and 5V
regulators.
As left figure 12 shown, it requires 3
capacitors, two of them are 1 uF, and another
one with star is called optional noise capacitor,
which can be 1 uF too.
Testing Points on Schematic
Figure 13 Testing Points Schematic View
Since all of our components send off digital signal, we do not need to have a lot of testing points
for our schematic design. For users more convenient, we still add five testing PINs to make sure
everything is working fine on the sensor board. There are two voltage needed to be checked since they
are produced by regulators, and then it will be the SPI connections, in order to check if it has a good SPI
connection.
Figure 12 Regulator Connection
22
23
Layout Design
For the layout part, some components that we have to create our own library of footprint since it is not
possible to find one online. There is no worry to build the layout of microcontroller, because most of
the software have the library layout for TI’s MSP430. We do need to build our own footprint of gyro,
inclinometers and regulators.
Gyro Footprint Information
24
Inclinometer Footprint Information
Figure 14 Footprint of Inclinometer size in (mm)
Regulator Footprint Information
Figure 15 Regulator Footprintin inches or (mm)
25
Overview of Layout
Figure 16 Layout Overview
There are total 2 layers for this sensor board, and every component is fit on the top board.
26
Final Product
Figure 17 Manufactured Sensor Board
27
Test Plan
Testing list: 1. Connections test
2. Ground level test
3. Regulators voltage test
4. SPI connection test & Rough function test
1)Connectivity Test
The purpose of this test is to ensure that all the connections are correct on the circuit
board prior to connecting any components or applying power. Required Items: Ohmmeter x 1 Connection leads x 2 People Required: Technician x 2 Instructions: This test will require two people (person A and person B). Person A will read a
connection that should be checked while person B will use the ohmmeter to verify the connection. If a connection is made, the ohmmeter should indicate that by being very close to 0 ohms. If it is open, the ohmmeter will not be close to 0. All of the connections below should be near 0 ohms when checked. In the event one of the connections is not near 0 ohms, Person B should check to see whether either or both of those pins are connecting to any other connection.
Example of Bad Connection: If Microcontroller pin 5 to Gyro pin 1 is reading 5M ohms, then person B should check
Microcontroller pin 5 to every other pin on the board to ensure that it is not wired to the incorrect place. Person B should also do the same for Gyro pin 1. If any connections from either of those pins are found, consult the schematic to verify whether that connection should be there. If there is a connection made that should not be there, the board may be inoperable and should not have any components placed on the board. The entire team should also be notified if a bad connection is found.
Other Information: Below are the pin outs of the microcontroller, gyro, and inclinometers. After a
connection is verified, a check mark should be placed next to it by person A. Always consult the schematic if needed.
28
29
30
List of Connections to be Verified
Microcontroller pins Checked Good Checked Bad
1 to 3V in
8 to Micro pin 1
28 to J2 pin 3
29 to J2 pin 2
30 to J2 pin 6
31 to J2 pin 4
32 to U4 (Inclin 2) pin 7
44 to U3 (Inclin1) pin 7
45 to U3 (Inclin1) pin 4
45 to U4 (Inclin 2) pin 4
45 to U2 (Gyro) pin 3
46 to U3 (Inclin1) pin 3
46 to U4 (Inclin2) pin 3
46 to U2 (Gyro) pin 4
47 to U3 (Inclin1) pin 1
47 to U4 (Inclin2) pin1
47 to U2 (Gyro) pin 2
48 to U2 (Gyro) pin 5
53 to Ground
53 to JTAG pin 1
53 to JTAG pin 8
54 to JTAG pin 2
55 to JTAG pin 3
56 to JTAG pin 4
57 to JTAG pin 5
62 to Ground
63 to Ground
64 to L1 (Inductor)
31
Gyro Pins Checked Good Checked Bad
14 to C9 (Capacitor)
14 to C10 (Capacitor)
8 to Ground
9 to Ground
10 to Ground
11 to Ground
12 to Ground
13 to Ground
1 to 3V in
15 to Gyro pin 1
16 to Gyro pin 1
1 to C7
1 to C8
Inclinometer 1 pins Checked Good Checked Bad
6 to Ground
12 to C5
12 to 5V in
Inclinometer 2 pins Checked Good Checked Bad
6 to Ground
12 to C6
12 to 5V in
U5 pins Checked Good Checked Bad
1 to J2 pin 7
3 to U5 pin 1
2 to C14
2 to C15
2 to C16
4 to C15
5 to C16
5 to 3V in
32
U6 pins Checked Good Checked Bad
1 to U6 pin 3
1 to J2 Pin 7
1 to C11
2 to Ground
2 to C14
2 to C15
2 to C16
4 to C15
5 to C16
5 to 3V in
Other Pins Checked Good Checked Bad
C1 to Ground
C1 to 3V in
C2 to Ground
C2 to 3V in
C3 to Ground
C3 to L1
C4 to Ground
C4 to L1
C9 to R1
C10 to Ground
R1 to Ground
C5 to Ground
C6 to Ground
C7 to Ground
C8 to Ground
C14 to J2 pin 7
C11 to J2 pin 7
L1 to 3V in
JTAG pin 6 to 3V in
JTAG pin 7 to J2 pin 5
33
2) Ground level test
Check all of the PINs of every component which are grounded, make sure that all the GND PINs are really close or equals 0 while giving a voltage supply.
Required Items: 1. Power Supply
2. Banana to Alligator x 2
3. Connection leads x 2
4. DMM (Digit Multimeter)
People Required: Technician x 2
Instructions: Step 1,
Person A control power supply, and adjust the voltage to 5V, then use one “banana to Alligator” connect to PIN “VDD” at Board Header, and then use the other one connect to G0. PIN G0 to G13 are shown in picture below.
Step 2, Person A turns on power supply. Person B use two connection leads connect to DMM to
measure DC voltage, and then connect the leads to the PCB at desired PINs from G1 to G13. At the same time person A read from DMM, and check if values are equal to 0, and use
pen to check on verification sheet.
If failure and non-zero values show up, we have to recheck the connection between power supply and board header, check if G0 equals to 0. Then again measure the failed ground PIN. If it still fails, we have to solder and fix it.
34
Ground Level Verification Sheet Measure the voltage between G1 ~ G13 and G0, G0 and COM of power supply.
PIN name
Voltage value Verify
G0 ~ COM
G1
G2
G3
G4
G5
G6
G7
G8
G9
G10
G11
G12
G13
35
3) Regulators Voltage Test
Assume to get voltage 5V from main board, and it is battery based. We need to supply a 5V of voltage to see if we can get a constant voltage output from regulators.
Required Items: 1. Power Supply
2. Banana to Alligator x 2
3. Connection leads x 2
4. DMM (Digit Multimeter)
People Required: Technician x 2
Instructions: Step 1,
Person A control power supply, and adjust the voltage to 5V, then use one “banana to Alligator” connect to PIN “VDD” at Board Header, and then use the other one connect to G0. PIN G0 to G13 are shown in picture below.
Step 2,
Person A turn on power supply. Person B use connection leads the same as in part 2), but except measure the R1 and R2 on board, at the same time, Person A record voltage value while person B measure the voltage of two regulators, and we also need measure the voltage which were sent out to gyro and inclinometers.
36
Voltage Regulators Verification Sheet
Measure the voltage from PINs to GND, and record in the sheet below.
PIN name GND V_desired (V) V_measured (V)
Verify
R1
G11 +3
R2
G12 +5
A1
G7 +5
A2
G9 +5
Gy
G5 +3
Gy
G4 +3
M
G1 +3
M
G2 +3
M
G3 +3
J
G13 +3
Reference: R: Regulators A: Inclinometers Gy: Gyro M: Microcontroller J: JTAG G: Ground
37
4) SPI Connection Test & Rough Board Function Test
This test is to verify if we have a good connection of SPI,
Required Items: 1. Power Supply
2. Banana to Alligator x 2
3. JTAG Cable to PC
4. Oscilloscope BNC Connector
People Required: Technician x 2
Instructions:
Step 1,
Person A control power supply, and adjust the voltage to 5V, then use one “banana to Alligator” connect to PIN “VDD” at Board Header, and then use the other one connect to G0. PIN G0 to G13 are shown in picture below.
Person B sets up computer, and connect JTAG cable to PC, get program ready. Person A set up oscilloscope.
Step 2,
Person A turn on power supply, and observe signals . Person B use BNC connector to connect between circuit board and oscilloscope. First connect to MOSI and MISO, person A control PC and program. Person B and observe signals, and take picture of it.
Step 3,
Person A keep control PC and program, Person B change BNC connector to SEL and CLK PINs, observe signals on oscilloscope.
Step 4,
Move around circuit board and then observe if signals at MISO are changed.
Connection Specification:
S*: Select C: Clock Pin: SPI receiving data
38
Pout: SPI send command
PIN connection Verify
S0 to G3
S1 to G3
S2 to G3
C to G3
Pin to G3
Pout to G3
Take Pictures just in case if we need to analyze it again in the future.
39
Use software to modulate SPI signals, and plot outputs of MISO0, MOSI0, SSEL0.
Modulate signals sending out from microprocessor to inclinometers and gyro, connect
MOSI0 to “Terminal 1” of Oscilloscope, and observe the input signals on “Terminal 1”. Connect
MISO0 to “Terminal 2” to observe signals which are coming from inclinometers and gyro.
Compare the signals observed from scope and modulation plots to ensure they are correct
according to datasheets.
Figure 18, Example
As clock is set to be at frequency T, the signals should be the same in each selected period T,
since the sensor is set at flat plane and not moving.
40
Appendix Code: /* * main.c */ #include <msp430x24x.h> #include <math.h> #define pi 3.141592653589 #define uint unsigned int //// #define uchar unsigned char //// #define CTRL_REG1 0x20 // #define CTRL_REG2 0x21 // #define CTRL_REG4 0x23 #define REFERENCE 0x25 #define out_temp 0x26 #define STATUS_REG 0x27 #define OUT_X_L 0x28 #define OUT_X_H 0x29 #define OUT_Y_L 0x2A #define OUT_Y_H 0x2B #define OUT_Z_L 0x2C #define OUT_Z_H 0x2D #define reset_cs P5DIR&=~BIT4; //assign P5^4 = 0; #define set_cs P5DIR|=BIT4; //assign P5^4 = 1; #define reset_clk P5DIR&=~BIT3; //P5^3 = 0; #define set_clk P5DIR|=BIT3; //P5^3 = 1; #define reset_sdi P5DIR&=~BIT2; //P5^2 = 0; #define set_sdi P5DIR|=BIT2; //P5^2 = 1; #define reset_sdo P5DIR&=~BIT1; //P5^1 = 0; #define set_sdo P5DIR|=BIT1; //P5^1 = 1; typedef unsigned char BYTE; unsigned char incl_one_OutData; ///< SPI master to slave(inclinometer 1) data unsigned int incl_one_X_Data = 0; ///< SPI slave(inclinometer 1) to master data unsigned int incl_one_Y_Data = 0; unsigned char incl_two_OutData; ///< SPI master to slave(inclinometer 2) data unsigned int incl_two_X_Data = 0; ///< SPI slave(inclinometer 2) to master data unsigned int incl_two_Y_Data = 0; unsigned char incl_read_X = 0x10; // < SPI command to read X data unsigned char incl_read_Y = 0x11; ///< SPI command to read Y data volatile unsigned int gyro_OutData; ///< SPI master to slave(gyro) data unsigned int gyro_InData; ///< SPI slave(gyro) to master data volatile unsigned int gyro_offset = 0; //holds the value for the offset
41
of the gyro unsigned int tempData; int diff_1 = 0; //holds difference value for inclinometer 1 int diff_2 = 0; //holds difference value for inclinometer 2 //volatile double angle_1; //holds value for the angle of inclinometer 1 //volatile double angle_2; //holds value for the angle of inclinometer 2 volatile double angle; volatile union { int ang_1[4]; float angle_1; } val_to_send_1; volatile union { int ang_2[4]; float angle_2; } val_to_send_2; //initialize input and output pins *********not needed******** //delay void delay (int n) { int i =0; for (i = 0; i<n; i++) { // _nop_(); ///one clock cycle? } } //initialize master clock to 8MHz void initClk(void) { int i = 0; WDTCTL = WDTPW + WDTHOLD; // Stop watch dog timer if (CALBC1_8MHZ == 0xFF || CALDCO_8MHZ == 0xFF) // Clock to 8Mhz { while(1); // If calibration constants erased do not load, trap CPU!! } BCSCTL1 = CALBC1_8MHZ; // Set Basic Clock to 8MHz DCOCTL = CALDCO_8MHZ; // Set DCO to 8MHz for(i=16800;i>0;i--); // Wait for DCO to stabilize. }
42
//initialize SPI bus as 3 wire master to gyro/inclinometer1/inclinometer2 void initSpi(void) { //SPI to gyro and inclinometers P3DIR |= 0x10; // set p3.4 to output for inclinometer 2 select P3OUT |= 0x10; // set slave reset high - p3.4 P5DIR |= 0x11; // set P5.0,4 to output for inclinometer 1 select and gyro select P5OUT |= 0x11; // Set slave reset high - P5.0,4 P5SEL = 0x0E; // P5.1,2,3 USCI_B1 option select UCB1CTL0 |= UCCKPH + UCMST + UCSYNC + UCMSB; // 3-pin, 8-bit SPI master MSB First UCB1CTL1 |= UCSSEL_2 + UCSWRST; // SMCLK for BRCLK UCB1BR0 |= 0x10; // SPICLK = SMCLK / 16 = 0.5MHz Spi Clock ---- Max allowed for Inclinometers UCB1BR1 = 0; UCB1CTL1 &= ~UCSWRST; // SPI enable delay(50); } //set all unused pins to output and output low to avoid interference void init_unused_pins() { P1DIR = 0xFF; P1OUT = 0x00; P2DIR = 0xFF; P2OUT = 0x00; P3DIR |= 0xE0; P3OUT |= 0x1F; P4DIR = 0xFF; P4OUT = 0x00; P5DIR |= 0xE0; P5OUT |= 0x1F; P6DIR = 0XFF; P6OUT = 0X00; } //! initializes SPI bus as 3 wire slave to ARM void initArmSpi(void) { //Spi to ARM P3DIR |=0x01; // Set P3.0 to input for SPI slave select P3SEL |= 0x0E; // P3.1,2,3 SPI USCI_B0 option select UCB0CTL0 |= UCSYNC + UCMSB; // 3-pin, 8-bit SPI slave MSB First UCB0CTL1 &= ~UCSWRST; // SPI enable UC0IE |= UCB0RXIE; // Enable USCI RX interrupt } //Gyro write
43
void GyroWrite (uchar address, uchar write) { uchar i; set_cs; delay(50); reset_cs; //pull down the CS signal delay(30); address = address & 0x3f; //read address bit by bit for (i=0; i<8; i++) { if ((address&0x80) == 0x80) { set_sdi; } else { reset_sdi; } delay(20); reset_clk; delay(20); set_clk; delay(20); address = address <<1; } for (i=0; i<8; i++) //write address bit by bit { if ((write & 0x80) == 0x80) { set_sdi; } else { reset_sdi; } delay(20); reset_clk; delay(20); set_clk; delay(20); write = write <<1; } delay(40); set_cs; } //Gyro Read uint GyroRead (uchar address) { uchar i; uint ReadData; set_cs; delay(50); reset_cs;
44
set_clk; delay(20); address = address & 0x3f; address = address | 0xc0; for (i = 0;i<8;i++) { if ((address&0x80) == 0x80) { set_sdi; } else { reset_sdi; } delay(20); reset_clk; delay(20); set_clk; delay(20); address = address <<1; } ReadData = 0; for (i=0;i<16;i++) { reset_sdi; delay(20); reset_clk; delay(20); set_clk; delay(20); if (P5DIR|0x02 == 0x02) //#define reset_sdo P5DIR&=~BIT1; //P5^1 = 0; { ReadData |= 0x0001; } else { ReadData &= 0xfffe; } if (i<15) { ReadData = (ReadData <<1)& 0xfffe; } delay(20); } delay(50); set_cs; return ReadData; } //initialize gyro void GyroInit(void) { GyroWrite(CTRL_REG1, 0x8f); //sets ODR to 400 Hz and BW to 00 for cutoff of 20
45
} //initialize inclinometer 1 *******not needed******** //initialize inclinometer 2 *******not needed******** //get gyro data uint GyroDataZ(void) { uint dataZ; dataZ = GyroRead(OUT_Z_L); return dataZ; } //get inclinometer 1 data float getIncl_one_data() { int i = 0; angle = 0; //read in the X data P5OUT &= ~0x01; // Spi Select low for(i = 0; i < 5; i++); // delay between select and SPI clk while (!(IFG2 & UCB1TXIFG)); // USCI_B1 TX buffer ready? UCB1TXBUF = incl_read_X; while (!(IFG2 & UCB1RXIFG)); // USCI_B1 RX buffer ready? incl_one_X_Data = UCB1RXBUF; // Receive first 8 bits incl_one_X_Data = incl_one_X_Data << 8; //shift first 8 to make room for next 8 while (!(IFG2 & UCB1RXIFG)); // USCI_B1 RX buffer ready? tempData = UCB1RXBUF; // receive next 8 bits incl_one_X_Data = incl_one_X_Data | tempData; //combine 16 bits together incl_one_X_Data = incl_one_X_Data >> 5; //toss out lowest 5 since only need 11 bits P5OUT |= 0x01; // Spi Select high for(i = 0; i < 5; i++); // delay between deselect and select //read in the Y data P5OUT &= ~0x01; // Spi Select low for(i = 0; i < 5; i++); // delay between select and SPI clk
46
/* for(i = 0; i < 19; i++) { incl_one_Y_Data = incl_one_Y_Data << 1; //shift first bit to make room for next one while (!(IFG2 & UCB1TXIFG)); // USCI_B1 TX buffer ready? UCB1TXBUF = incl_read_Y; while (!(IFG2 & UCB1RXIFG)); // USCI_B1 RX buffer ready? incl_one_Y_Data = UCB1RXBUF; // Receive bit incl_read_Y = incl_read_Y >> 1; //shift next bit to be sent into position }*/ while (!(IFG2 & UCB1TXIFG)); // USCI_B1 TX buffer ready? UCB1TXBUF = incl_read_Y; while (!(IFG2 & UCB1RXIFG)); // USCI_B1 RX buffer ready? incl_one_Y_Data = UCB1RXBUF; // Receive first 8 bits incl_one_Y_Data = incl_one_Y_Data << 8; //shift first 8 to make room for next 8 while (!(IFG2 & UCB1RXIFG)); // USCI_B1 RX buffer ready? tempData = UCB1RXBUF; // receive next 8 bits incl_one_Y_Data = incl_one_Y_Data | tempData; //combine 16 bits together incl_one_Y_Data = incl_one_Y_Data >> 5; //toss out lowest 5 since only need 11 bits P5OUT |= 0x01; // Spi Select high //calculate angle diff_1 = incl_one_X_Data - incl_one_Y_Data; //find difference between X and Y values angle = asin(((double)diff_1)/6554) *180/pi; //calculate angle for inclinometer return (float)angle; } //get inclinometer 2 data *******not yet completed******* float getIncl_two_data() { int i = 0; angle = 0; //read in the X data P3OUT &= ~0x10; // Spi Select low for(i = 0; i < 5; i++); // delay between select and SPI clk while (!(IFG2 & UCB1TXIFG)); // USCI_B1 TX buffer ready? UCB1TXBUF = incl_read_X; while (!(IFG2 & UCB1RXIFG)); // USCI_B1 RX buffer ready? incl_two_X_Data = UCB1RXBUF; // Receive first 8 bits incl_two_X_Data = incl_two_X_Data << 8; //shift first 8 to make room for next 8
47
while (!(IFG2 & UCB1RXIFG)); // USCI_B1 RX buffer ready? tempData = UCB1RXBUF; // receive next 8 bits incl_two_X_Data = incl_two_X_Data | tempData; //combine 16 bits together incl_two_X_Data = incl_two_X_Data >> 5; //toss out lowest 5 since only need 11 bits P3OUT |= 0x10; // Spi Select high for(i = 0; i < 5; i++); // delay between deselect and select //read in the Y data P3OUT &= ~0x10; // Spi Select low for(i = 0; i < 5; i++); // delay between select and SPI clk while (!(IFG2 & UCB1TXIFG)); // USCI_B1 TX buffer ready? UCB1TXBUF = incl_read_Y; while (!(IFG2 & UCB1RXIFG)); // USCI_B1 RX buffer ready? incl_two_Y_Data = UCB1RXBUF; incl_two_Y_Data = incl_two_Y_Data << 8; //shift first 8 to make room for next 8 while (!(IFG2 & UCB1RXIFG)); // USCI_B1 RX buffer ready? tempData = UCB1RXBUF; // receive next 8 bits incl_two_Y_Data = incl_two_Y_Data | tempData; //combine 16 bits together incl_two_Y_Data = incl_two_Y_Data >> 5; //toss out lowest 5 since only need 11 bits P3OUT |= 0x10; // Spi Select high //calculate angle diff_2 = incl_two_X_Data - incl_two_Y_Data; //find difference between X and Y values angle = asin(((double)diff_2)/6554) *180/pi; //calculate angle for inclinometer return (float)angle; } //send data to ARM *******not yet completed******* void send_data() { int i = 0; //send out angle 1 for(i = 0; i < 4; i++) {
48
while (!(IFG1 & UCB0TXIFG)); // USCI_B0 TX buffer ready? UCB0TXBUF = val_to_send_1.ang_1[i]; //send upper portion of angle 1 and shift up accordingly } /* while (!(IFG1 & UCB0TXIFG)); // USCI_B0 TX buffer ready? UCB0TXBUF = angle_1; //send upper portion of angle 1 and shift up accordingly angle_1 = angle_1 << 8; while (!(IFG1 & UCB0TXIFG)); // USCI_B0 TX buffer ready? UCB0TXBUF = angle_1; //send upper portion of angle 1 and shift up accordingly angle_1 = angle_1 << 8; while (!(IFG1 & UCB0TXIFG)); // USCI_B0 TX buffer ready? UCB0TXBUF = angle_1; //send upper portion of angle 1 and shift up accordingly angle_1 = angle_1 << 8; while (!(IFG1 & UCB0TXIFG)); // USCI_B0 TX buffer ready? UCB0TXBUF = angle_1; //send upper portion of angle 1 and shift up accordingly angle_1 = angle_1 << 8; */ //send out angle 2 for (i = 0; i < 4; i++) { while (!(IFG1 & UCB0TXIFG)); // USCI_B0 TX buffer ready? UCB0TXBUF = val_to_send_2.ang_2[i]; //send upper portion of angle 1 and shift up accordingly } //send out gyro data for (i = 0; i < 2; i++) { while (!(IFG1 & UCB0TXIFG)); // USCI_B0 TX buffer ready? UCB0TXBUF = gyro_OutData << (8*i); //send upper portion of gyro data and shift up accordingly } } //Calculate the offset of the gyro when it is first turned on unsigned int calc_offset() { unsigned int temp = 0; int i; for (i = 0; i < 50; i++) { temp += GyroDataZ(); } temp = temp / 50; return temp; } //averages the gyro data to get a more reliable reading unsigned int avg_gyro() { unsigned int temp = 0;
49
int i; for (i = 0; i < 25; i++) { temp += GyroDataZ(); } temp = temp / 25; return temp; } void main() { int i = 0; unsigned int temp2 = 0; initClk(); initSpi(); initArmSpi(); GyroInit(); gyro_offset = calc_offset(); for(i = 0; i<50;i++); while(1) { val_to_send_1.angle_1 = getIncl_one_data(); val_to_send_2.angle_2 = getIncl_two_data(); //need loop to average gyro readings //then gyro angle = gyro angle + h (0.0025 for 400 Hz) * R (Raw data in LSB's from gyro after corrected for offset and threshold) * Scale Factor for axis (only needed for super high accuracy) temp2 = avg_gyro(); gyro_OutData = gyro_OutData + 0.0025 * (temp2 - gyro_offset); send_data(); } }