Interfacing CMR3000-D01 to an MSP430 ultra low-power microcontroller

Similar documents
Interfacing CMA3000-D01 to an MSP430 ultra low-power microcontroller

ECE2049: Embedded Computing in Engineering Design C Term Spring 2018

4 Degrees of Freedom MEMS Sensor Improvements

EEL 4924 Electrical Engineering Design (Senior Design) Team Baudiophile. Wireless Headphones

volatile int results[5]; // variables para la interrupcion del acelerometro //Funciones del acelerometro para mandar y recibir información

Texas Instruments Mixed Signal Processor Tutorial Abstract

// Conditions for 9600/4=2400 Baud SW UART, SMCLK = 1MHz #define Bitime_5 0x05*4 // ~ 0.5 bit length + small adjustment #define Bitime 13*4//0x0D

Timers and Clocks CS4101 嵌入式系統概論. Prof. Chung-Ta King. Department of Computer Science National Tsing Hua University, Taiwan

What is an Interrupt?

Lab 1: I/O, timers, interrupts on the ez430-rf2500

In-Depth with MSP430 s New Communication Interfaces

CPE/EE 323 Introduction to Embedded Computer Systems Homework V

MSP430. More on MSP430

CPE 323: MSP430 Serial Communication

CPE 323 Introduction to Embedded Computer Systems: MSP430 System Architecture An Overview

MSP430 Interrupts. Change value of internal variable (count) Read a data value (sensor, receive) Write a data value (actuator, send)

CPE 323: MSP430 Timers

Network Embedded Systems Sensor Networks Fall Hardware. Marcus Chang,

Interrupts, Low Power Modes

Lecture 5: MSP430 Interrupt

CPE 325: Embedded Systems Laboratory Laboratory #7 Tutorial MSP430 Timers, Watchdog Timer, Timers A and B

Serial Peripheral Interface (SPI) Last updated 8/7/18

Two Wire Interface (TWI) also commonly called I2C

An SPI Temperature Sensor Interface with the Z8 Encore! SPI Bus

IV B.Tech. I Sem (R13) ECE : Embedded Systems : UNIT -2 1 UNIT 2

Copyright 2015 by Stephen A. Zajac & Gregory M. Wierzba. All rights reserved..spring 2015.

6. General purpose Input/Output

Hands-On: Implementing an RF link with MSP430 and CC1100

Getting Started with the Texas Instruments ez430

IV B.Tech. I Sem (R13) ECE : Embedded Systems : UNIT -4 1 UNIT 4

Serial Peripheral Interface (SPI)

Texas Instruments Microcontroller HOW-TO GUIDE Interfacing Keypad with MSP430F5529

ECE2049: Embedded Computing in Engineering Design C Term Spring Lecture #7: More Digital IO

Design and development of embedded systems for the Internet of Things (IoT) Fabio Angeletti Fabrizio Gattuso

University of Texas at El Paso Electrical and Computer Engineering Department. EE 3176 Laboratory for Microprocessors I.

Product Family Specification

C-CODE EXAMPLE FOR SCP1000-D01 PRESSURE SENSOR

INTRODUCTION TO FLEXIO

CPE/EE 421 Microcomputers

MSP430F149 P3.4/UTXD0 P3.5/URXD0 P1.5 P1.6 P1.7 MSP430F149 P1.0 P5.4 P5.3 P5.2 P5.1. Figure B-1. BSL Replicator Block Diagram

University of Texas at El Paso Electrical and Computer Engineering Department. EE 3176 Laboratory for Microprocessors I.

임베디드시스템기초 (# ) #11. Serial Communications 한림대학교전자공학과이선우

ECE2049: Embedded Computing in Engineering Design C Term Spring 2018 Lecture #20: Using SPI The DAC

Introduction to I2C & SPI. Chapter 22

17. I 2 C communication channel

2.996/6.971 Biomedical Devices Design Laboratory Lecture 6: Microprocessors II

LC-E201A. Application note. 1. Mechanical Specification: LC-E021A-application note page 1 of 16 Revision:

Why embedded systems?

ECE2049 Homework #2 The MSP430 Architecture & Basic Digital IO (DUE Friday 9/8/17 at 4 pm in class)

Name: Clint Furrer Project Number: TI003 Project Description: Safety Walking Lights. Description:

Asservissement en température numérique d'une cavité ultra-stable au LPL pour le Strontium

Lab 4: Interrupt. CS4101 Introduction to Embedded Systems. Prof. Chung-Ta King. Department of Computer Science National Tsing Hua University, Taiwan

ECE PRACTICE EXAM #2 Clocks, Timers, and Digital I/O

ECE2049: Embedded Computing in Engineering Design C Term Spring 2019 Lecture #22: MSP430F5529 Operating Mode & the WDT

Amarjeet Singh. January 30, 2012

ECE2049: Embedded Computing in Engineering Design A Term Fall Lecture #8: Making it work: LEDs, Buttons & Keypad

Freescale Semiconductor, I

Embedded Systems. 3. Hardware Software Interface. Lothar Thiele. Computer Engineering and Networks Laboratory

Using FlexIO to emulate communications and timing peripherals

Using the Z8051 MCU s USI Peripheral as an SPI Interface

Wireless Sensor Networks (WSN)

Arduino Uno R3 INTRODUCTION

ArduCAM-M-2MP Camera Shield

Emulating Dual SPI Using FlexIO

Microcontrollers and Interfacing

I2C on the HMC6352 Compass

Lab 4 Interrupts ReadMeFirst

Implementation of MCU Invariant I2C Slave Driver Using Bit Banging

Master Thesis. Sub-Gigahertz Wireless Sensors For Monitoring Of Food Transportation. Faisal Ali Khan. Submitted by. Universität Bremen

Section 16. Basic Sychronous Serial Port (BSSP)

Emulating I2C Bus Master by using FlexIO

ECE 4510/5530 Microcontroller Applications Week 10

Getting Started with ESPI Interface Using the Z8 Encore! XP F1680

Hello, and welcome to this presentation of the STM32 Low Power Universal Asynchronous Receiver/Transmitter interface. It covers the main features of

NuSpeech Family N5132 High Sound Quality Voice Synthesizer Technical Reference Manual

Design of a Simple 3-Lead ECG Acquisition System Based on MSP430F149

ECGR 4101/5101, Fall 2016: Lab 1 First Embedded Systems Project Learning Objectives:

RL78 Serial interfaces

Abstract. SD Card Standard. Comparison with Other Technologies. SD Electrical Interface. F. Foust Dept. of Electrical and Computer Engineering

AVR XMEGA Product Line Introduction AVR XMEGA TM. Product Introduction.

Inter-Integrated Circuit Bus IIC I2C TWI

@databasescaling Wednesday, 18 th April 2013

8051 Microcontroller

M68HC08 Microcontroller The MC68HC908GP32. General Description. MCU Block Diagram CPU08 1

MSP430FG4618 Programming Reference Revision 3

File: /media/j/texas/my msp430/g3tr/g3tr.c Pagina 1 di 5

Microcontroller basics

RFIC TX/RX Configuration

Serial communications with SPI

Application Note One Wire Digital Output. 1 Introduction. 2 Electrical Parameters for One Wire Interface. 3 Start and Data Transmission

University of Texas at El Paso Electrical and Computer Engineering Department. EE 3176 Laboratory for Microprocessors I.

App Note Application Note: Addressing Multiple FPAAs Using a SPI Interface

CPE 325: Embedded Systems Laboratory Laboratory #11 Tutorial Analog-to-Digital Converter and Digital-to-Analog Converter

ECE2049: Embedded Computing in Engineering Design C Term Spring Lecture #11: More Clocks and Timers

Serial Peripheral Interface. What is it? Basic SPI. Capabilities. Protocol. Pros and Cons. Uses

1. Features and Benefits

ZigBee Compliant Platform 2.4G RF Low Power Transceiver Module for IEEE Standard. DATA SHEET Version B

Hello and welcome to this Renesas Interactive module that covers the Independent watchdog timer found on RX MCUs.

Designing for Ultra-Low Power with MSP430

CPE 323 Introduction to Embedded Computer Systems: ADC12 and DAC12. Instructor: Dr Aleksandar Milenkovic Lecture Notes

Transcription:

Interfacing CMR3000-D01 to an MSP430 ultra low-power microcontroller 1 INTRODUCTION The objective of this document is to show how to set up SPI/I2C communication between VTI Technologies CMR3000-D01 digital angular rate sensor component and a Texas Instruments MSP430 microcontroller. In the code examples: The MSP430 MCU is configured CMR3000-D01 measurement mode is activated An interrupt is used to read angular rate output data registers when new data is available. When using the interrupt with I2C bus please note that Fast Mode (400 kbit/s) must be used to get all samples, with Standard Mode (100 kbit/s) the bus does not have time to handle the communication until a new interrupt is generated since CMR3000-D01 update rate is 2 khz. Please refer to document "CMR3000-D0X Product Family Specification 82112900" for further information on CMR3000-D01 register addressing and SPI/I2C communication. For MSP430 related information please see Texas Instruments web pages (http://www.ti.com). 2 DEVELOPMENT HARDWARE Figure 1. From left to right, ez430-rf2500, adapter PCB VTI29631 and CMR3000-D01 chip carrier VTI Technologies Oy 1/16

ez430-rf2500 connector VTI chip carrier PCB Figure 2. Adapter PCB VTI29631 circuit diagram 3 C-CODE The example code is written for Texas Instruments ez430-rf2500 Development Tool (MSP430F2274) using IAR Embedded Workbench IDE v5.10 for MSP430. To interface CMR3000- D01 to the ez430-rf2500 an adapter PCB VTI29631 is used (available through Hy-Line's web shop (http://www.hy-line.de). The C-language software examples for SPI and I2C buses on the next pages shows an easy way to implement communication with the CMR3000-D01. Universal Serial Communication Interface peripheral inside the MSP430F2274 is used for communication. The SPI code sets up an interrupt to wake up the MCU from low power mode thru CMR3000-D01 INT-pin when new data from the sensor is available. When no data is available (no interrupt) the MCU is kept in low power mode 4 (LPM4) to achieve lower current consumption. Clocks are set up so that the MCU clock frequency is 16 MHz, SPI clock is 250 khz and I2C clock 400 khz. Multiple read operation mode (decrement reading) is used to read all angular rate output data registers (0Ch...11h) within a single SPI/I2C read frame. VTI Technologies Oy 2/16

3.1 SPI Interface Example Code flowchart: Figure 3. Example Code Flowchart VTI Technologies Oy 3/16

C-Code Example, SPI Interface //****************************************************************************** // MSP430F2274 Demo - USCI_B0, SPI Interface to CMR3000 Gyro Sensor // // Uses Texas Instruments ez430-rf2500 Development Tool with VTI Adapter PCB // VTI29631A0. Wireless connection not used. // //****************************************************************************** // CONSTANTS #define XTAL 16000000L #define TICKSPERMS (XTAL / 1000 / 5-1) #define TICKSPERUS (TICKSPERMS / 1000) #define SPICLOCK 500 #define SPIFRAMEDELAY ((1000 / SPICLOCK) * 6) #define SPICSBDELAY ((1000 / SPICLOCK) / 2) // SPI clock = 500kHz // SPI interframe delay [us] = 6 * Tsck // SCK -> CSB delay [us] = 0.5 * Tsck // LIBRARIES #include "msp430x22x4.h" // PORT DEFINITIONS #define PORT_INT_IN P2IN #define PORT_INT_OUT P2OUT #define PORT_INT_DIR P2DIR #define PORT_INT_IE P2IE #define PORT_INT_IES P2IES #define PORT_INT_IFG P2IFG #define PORT_INT_VECTOR PORT2_VECTOR #define PORT_CSB_OUT #define PORT_CSB_DIR #define PORT_SPI_DIR #define PORT_SPI_SEL P4OUT P4DIR P3DIR P3SEL // REGISTER AND FLAG DEFINITIONS #define TX_BUFFER UCB0TXBUF #define RX_BUFFER UCB0RXBUF #define IRQ_REG IFG2 #define RX_IFG UCB0RXIFG #define SPI_CTL0 UCB0CTL0 #define SPI_CTL1 UCB0CTL1 #define SPI_BR0 UCB0BR0 #define SPI_BR1 UCB0BR1 // CMR3000-DOX Registers #define WHO_AM_I 0x00 #define REVID 0x01 #define CTRL 0x02 #define STATUS 0x03 #define X_LSB 0x0C #define X_MSB 0x0D #define Y_LSB 0x0E #define Y_MSB 0x0F #define Z_LSB 0x10 #define Z_MSB 0x11 #define I2C_ADDR 0x22 VTI Technologies Oy 4/16

// Control Register setup #define RESET 0x80 // Set device in reset #define INT_LEVEL_LOW 0x40 // INT active high #define I2C_DIS 0x10 // I2C disabled #define MODE_PD 0x00 // Power Down #define MODE_STBY 0x02 // Standby mode #define MODE_20 0x04 // Measurement mode BW=20 Hz #define MODE_80 0x06 // Measurement mode BW=80 Hz #define INT_DIS 0x01 // Interrupts disabled // PIN DEFINITIONS #define PIN_INT #define PIN_CSB #define PIN_MOSI #define PIN_MISO #define PIN_SCK BIT4 BIT3 BIT1 BIT2 BIT3 // FUNCTION PROTOTYPES unsigned char ReadRegister(unsigned char Address); unsigned char WriteRegister(unsigned char Address, unsigned char Data); void ReadAngularRates(unsigned short *Xrate, unsigned short *Yrate, unsigned short *Zrate); void wait_ms(unsigned short ms); void wait_us(unsigned short us); unsigned char Data; unsigned char Data_MSB; unsigned char Data_LSB; unsigned char RevID; unsigned short Xdata; unsigned short Ydata; unsigned short Zdata; void main(void) WDTCTL = WDTPW + WDTHOLD; BCSCTL1 = CALBC1_16MHZ; DCOCTL = CALDCO_16MHZ; BCSCTL2 = DIVS_3; PORT_INT_DIR &= ~PIN_INT; PORT_INT_IE = PIN_INT; PORT_INT_IES = 0x00; PORT_INT_IFG &= ~PIN_INT; PORT_CSB_DIR = PIN_CSB; PORT_CSB_OUT = PIN_CSB; // Stop watchdog timer // Set DCO to calibrated 16MHz // SMCLK to 2 MHz // INT pin interrupt enabled // Generate interrupt on Lo to Hi edge // Clear interrupt flag // Unselect gyro sensor PORT_SPI_SEL = PIN_MOSI PIN_MISO PIN_SCK; // P3.3, 2,1 USCI_B0 option select PORT_SPI_DIR = BIT0; // P3.0 output direction // Initialize SPI interface to gyro sensor SPI_CTL0 = UCSYNC UCMST UCMSB UCCKPH; // SPI master, 8 data bits, MSB first, // clock idle low, data output on falling edge SPI_CTL1 = UCSSEL1; // SMCLK as clock source SPI_BR0 = 0x04; // Low byte of division factor for baud rate (500kHz) SPI_BR1 = 0x00; // High byte of division factor for baud rate SPI_CTL1 &= ~UCSWRST; // Start SPI hardware VTI Technologies Oy 5/16

Data = WriteRegister(CTRL, RESET); // Reset device wait_ms(2); // Wait for reset to complete Data = WriteRegister(CTRL, I2C_DIS MODE_80); // INT level high, I2C disabled, // Measurement mode BW=80Hz, Interrupt enabled RevID = ReadRegister(REVID); // Read REVID register bis_sr_register(lpm4_bits + GIE); // Enter LPM4 w/interrupt // Port 2 interrupt service routine, INT pin #pragma vector=port_int_vector interrupt void Port_INT_ISR(void) if (PORT_INT_IN & PIN_INT) ReadAngularRates(&Xdata, &Ydata, &Zdata); PORT_INT_IFG &= ~PIN_INT; // Clear interrupt flag // Read a byte from the gyro sensor unsigned char ReadRegister(unsigned char Address) unsigned char Result; Address <<= 2; PORT_CSB_OUT &= ~PIN_CSB; TX_BUFFER = Address; TX_BUFFER = 0; wait_us(spicsbdelay); PORT_CSB_OUT = PIN_CSB; wait_us(spiframedelay); return Result; // Address to be shifted left by 2 and RW bit to be reset // Select gyro sensor // Read RX buffer just to clear interrupt flag // Write address to TX buffer // Wait until new data was written into RX buffer // Read RX buffer just to clear interrupt flag // Write dummy data to TX buffer // Wait until new data was written into RX buffer // Read RX buffer // Delay to satisfy 1/2 clk period delay from // falling edge of clk to CSB // Deselect gyro sensor // Delay to satisfy 6x clk period for CS high state // Return new data from RX buffer VTI Technologies Oy 6/16

// Write a byte to the gyro sensor unsigned char WriteRegister(unsigned char Address, unsigned char Data) unsigned char Result; Address <<= 2; // Address to be shifted left by 2 Address = 2; // RW bit to be set PORT_CSB_OUT &= ~PIN_CSB; TX_BUFFER = Address; TX_BUFFER = Data; wait_us(spicsbdelay); PORT_CSB_OUT = PIN_CSB; wait_us(spiframedelay); // Select gyro sensor // Read RX buffer just to clear interrupt flag // Write address to TX buffer // Wait until new data was written into RX buffer // Read RX buffer just to clear interrupt flag // Write data to TX buffer // Wait until new data was written into RX buffer // Read RX buffer // Delay to satisfy 1/2 clk period delay from // falling edge of clk to CSB // Deselect gyro sensor // Delay to satisfy 6x clk period for CS high state return Result; // Read all 6 rate registers using the decrement reading feature // and convert to 16-bit unsigned values void ReadAngularRates(unsigned short *Xrate, unsigned short *Yrate, unsigned short *Zrate) unsigned char Result; unsigned char Address = Z_MSB; Address <<= 2; PORT_CSB_OUT &= ~PIN_CSB; TX_BUFFER = Address; // Addr shifted left by 2 and RW bit to be reset // Select gyro sensor // Read RX buffer just to clear interrupt flag // Write address to TX buffer // Wait until new data was written into RX buffer // Read RX buffer TX_BUFFER = 0; // Write dummy data to TX buffer // Wait until new data was written into RX buffer // Read RX buffer *Zrate = (unsigned short) ((Result << 8) & 0xFF00); // store Z MSByte TX_BUFFER = 0; // Write dummy data to TX buffer // Wait until new data was written into RX buffer // Read RX buffer *Zrate = (unsigned short) (Result & 0x00FF); // store Z LSByte TX_BUFFER = 0; *Yrate = (unsigned short) ((Result << 8) & 0xFF00); // store Y MSByte VTI Technologies Oy 7/16

TX_BUFFER = 0; *Yrate = (unsigned short) (Result & 0x00FF); // store Y LSByte TX_BUFFER = 0; *Xrate = (unsigned short) ((Result << 8) & 0xFF00); // store X MSByte TX_BUFFER = 0; *Xrate = (unsigned short) (Result & 0x00FF); // store X LSByte wait_us(spicsbdelay); PORT_CSB_OUT = PIN_CSB; wait_us(spiframedelay); // Delay to satisfy 1/2 clk period delay from // falling edge of clk to CSB // Deselect gyro sensor // Delay to satisfy 6x clk period for CSB high state // wait ms void wait_ms(unsigned short ms) unsigned short a, b; for (a = ms; a > 0; a--) // outer loop takes 5 ck per round for (b = TICKSPERMS; b > 0; b--) // inner loop takes 5 ck per round asm("nop"); // wait us void wait_us(unsigned short us) unsigned short a; us *= TICKSPERUS; for (a = us; a > 0; a--) // loop takes 5 ck per round asm("nop"); VTI Technologies Oy 8/16

3.2 I2C Interface Example VTI adapter PCB VTI29631 does not support I2C use with CMR3000-D01 so to enable I2C communication with the ez430-rf2500 the PCB needs to be modified as follows: - Cut MISO/SDA trace close to J2 - Cut SCK/SCL trace close to J2 - Wire J2-4 to ground - Wire P2-15 to J2-6 - Add 10 kohm pull-up resistors from MOSI/SDA and J2-6 to +3.6V Figure 4. PCB VTI29631 modification for I2C use Cut traces Figure 5. Modified VTI29631 PCB VTI Technologies Oy 9/16

Code flowchart: Figure 6. I2C Example Code Flowchart VTI Technologies Oy 10/16

C-Code Example, I2C Interface //****************************************************************************** // MSP430F2274 Demo - USCI_B0, I2C Interface to CMR3000 Gyro Sensor // // Uses Texas Instruments ez430-rf2500 Development Tool with VTI Adapter PCB // VTI29631A0. Wireless connection not used. Adapter PCB VTI29631 needs to be // modified for I2C to work. // //****************************************************************************** // CONSTANTS #define XTAL 16000000L #define TICKSPERMS (XTAL / 1000 / 5-1) #define TICKSPERUS (TICKSPERMS / 1000) // LIBRARIES #include "msp430x22x4.h" // PORT DEFINITIONS #define PORT_INT_IN P2IN #define PORT_INT_DIR P2DIR #define PORT_INT_IE P2IE #define PORT_INT_IES P2IES #define PORT_INT_IFG P2IFG #define PORT_INT_VECTOR PORT2_VECTOR #define PORT_I2C_DIR #define PORT_I2C_SEL #define PORT_I2C_OUT P3DIR P3SEL P3OUT // REGISTER AND FLAG DEFINITIONS #define TX_BUFFER UCB0TXBUF #define RX_BUFFER UCB0RXBUF #define IRQ_REG IFG2 #define RX_IFG UCB0RXIFG #define TX_IFG UCB0TXIFG // CMR3000-DOX Registers #define WHO_AM_I 0x00 #define REVID 0x01 #define CTRL 0x02 #define STATUS 0x03 #define X_LSB 0x0C #define X_MSB 0x0D #define Y_LSB 0x0E #define Y_MSB 0x0F #define Z_LSB 0x10 #define Z_MSB 0x11 #define I2C_ADDR 0x22 // Control Register setup #define RESET 0x80 // Set device in reset #define INT_LEVEL_LOW 0x40 // INT active high #define I2C_DIS 0x10 // I2C disabled #define MODE_PD 0x00 // Power Down mode #define MODE_STBY 0x02 // Standby mode #define MODE_80 0x04 // Measurement mode BW=80 Hz #define MODE_20 0x06 // Measurement mode BW=20 Hz #define INT_DIS 0x01 // Interrupts disabled VTI Technologies Oy 11/16

// PIN DEFINITIONS #define PIN_INT #define PIN_CSB #define PIN_MOSI #define PIN_MISO #define PIN_SCK BIT4 BIT3 BIT1 BIT2 BIT3 // FUNCTION PROTOTYPES unsigned char ReadRegister(unsigned char Address); unsigned char WriteRegister(unsigned char Address, unsigned char Data); void ReadAngularRates(unsigned short *Xrate, unsigned short *Yrate, unsigned short *Zrate); void wait_ms(unsigned short ms); void wait_us(unsigned short us); unsigned char Data; unsigned char Data_MSB; unsigned char Data_LSB; unsigned char RevID; unsigned short Xdata; unsigned short Ydata; unsigned short Zdata; void main(void) WDTCTL = WDTPW + WDTHOLD; BCSCTL1 = CALBC1_16MHZ; DCOCTL = CALDCO_16MHZ; BCSCTL2 = DIVS_3; PORT_INT_DIR &= ~PIN_INT; PORT_INT_IE = PIN_INT; PORT_INT_IES = 0x00; PORT_INT_IFG &= ~PIN_INT; // Initialize I2C interface to gyro sensor PORT_I2C_DIR = BIT0; PORT_I2C_OUT = BIT0; UCB0CTL1 = UCSWRST; UCB0CTL1 = UCSSEL_2; PORT_I2C_SEL = BIT2 BIT1; UCB0CTL0 = UCMST + UCSYNC + UCMODE_3; UCB0BR0 = 5; UCB0BR1 = 0; UCB0I2CSA = 0x1E; UCB0CTL1 &= ~UCSWRST; Data = WriteRegister(CTRL, RESET); wait_ms(2); Data = WriteRegister(CTRL, MODE_80 INT_DIS); RevID = ReadRegister(REVID); bis_sr_register(lpm4_bits + GIE); // Stop watchdog timer // Set DCO to calibrated 16MHz // SMCLK to 2 MHz // INT pin interrupt enabled // Generate interrupt on Lo to Hi edge // Clear interrupt flag // Set port 3 pin 0 as output and set high. // Put state machine in reset // SMCLK as clock source // Set I/O pins 1 and 2 for port 3 to be for the // I2C peripheral // I2C mode, master, synchronous // 2MHz/5 = 400kHz // Slave Address is 1Eh // Start I2C state machine // Reset device // Wait for reset to complete // INT level high, I2C enabled, // Measurement mode BW=80Hz, Interrupt disabled // Read REVID register // Enter LPM4 w/interrupt VTI Technologies Oy 12/16

// Port 2 interrupt service routine, INT pin #pragma vector=port_int_vector interrupt void Port_INT_ISR(void) if (PORT_INT_IN & PIN_INT) ReadAngularRates(&Xdata, &Ydata, &Zdata); PORT_INT_IFG &= ~PIN_INT; // Read angular rates // Clear interrupt flag // Read a byte from the gyro sensor unsigned char ReadRegister(unsigned char Address) unsigned char Result; // Read data from I2C slave device at the DeviceAddress specified. UCB0CTL1 = UCTR + UCTXSTT; while (!(IRQ_REG & TX_IFG)); TX_BUFFER = Address; while (!(IRQ_REG & TX_IFG)); IRQ_REG &= ~TX_IFG; // I2C TX, start condition // Wait for slave address transmit to complete // Load TX buffer with register address // Wait for transmit to complete // Clear USCI_B0 TX int flag // Send restart with transmit/receive bit NOT set UCB0CTL1 &= ~UCTR; UCB0CTL1 = UCTXSTT; while(ucb0ctl1 & UCTXSTT); UCB0CTL1 = UCTXSTP; while (UCB0CTL1 & UCTXSTP); IRQ_REG &= ~RX_IFG; return Result; // Toggle transmitter bit // I2C start condition // Wait for start to complete // I2C stop condition // Wait for receive buffer to fill // Fill receive data buffer with received byte // Wait for stop condition to complete // Clear USCI_B0 RX int flag // Return new data from RX buffer // Write a byte to the gyro sensor unsigned char WriteRegister(unsigned char Address, unsigned char Data) // Write data to I2C slave device at the DeviceAddress specified. UCB0CTL1 = UCTR + UCTXSTT; while (!(IRQ_REG & TX_IFG)); TX_BUFFER = Address; // I2C TX, start condition // Wait for slave address transmit to complete // Load TX buffer with register address VTI Technologies Oy 13/16

while (!(IRQ_REG & TX_IFG)); TX_BUFFER = Data; while (!(IRQ_REG & TX_IFG)); UCB0CTL1 = UCTXSTP; while (UCB0CTL1 & UCTXSTP); IRQ_REG &= ~TX_IFG; // Wait for transmit to complete // Load TX buffer with data byte // Wait for transmit to complete // I2C stop condition // Wait for stop condition to complete // Clear USCI_B0 TX int flag return 0; // Read all 6 rate registers using the decrement reading feature // and convert to 16-bit unsigned values void ReadAngularRates(unsigned short *Xrate, unsigned short *Yrate, unsigned short *Zrate) unsigned char Result; unsigned char Address = Z_MSB; // Read data from I2C slave device at the DeviceAddress specified. UCB0CTL1 = UCTR + UCTXSTT; while (!(IRQ_REG & TX_IFG)); TX_BUFFER = Address; while (!(IRQ_REG & TX_IFG)); IRQ_REG &= ~TX_IFG; // Send restart with transmit/receive bit set UCB0CTL1 &= ~UCTR; UCB0CTL1 = UCTXSTT; while(ucb0ctl1 & UCTXSTT); // I2C TX, start condition // Wait for slave address transmit to complete // Load TX buffer with register address // Wait for transmit to complete // Clear USCI_B0 TX int flag // Toggle transmitter bit // I2C start condition // Wait for start to complete // Wait for receive buffer to fill // Fill receive data buffer with received byte *Zrate = (unsigned short) ((Result << 8) & 0xFF00); // store Z MSByte *Zrate = (unsigned short) (Result & 0x00FF); // Wait for receive buffer to fill // Fill receive data buffer with received byte // store Z LSByte // Wait for receive buffer to fill // Fill receive data buffer with received byte *Yrate = (unsigned short) ((Result << 8) & 0xFF00); // store Y MSByte *Yrate = (unsigned short) (Result & 0x00FF); // Wait for receive buffer to fill // Fill receive data buffer with received byte // store Y LSByte VTI Technologies Oy 14/16

// Wait for receive buffer to fill // Fill receive data buffer with received byte *Xrate = (unsigned short) ((Result << 8) & 0xFF00); // store X MSByte UCB0CTL1 = UCTXSTP; *Xrate = (unsigned short) (Result & 0x00FF); while (UCB0CTL1 & UCTXSTP); IRQ_REG &= ~RX_IFG; // I2C stop condition // Wait for receive buffer to fill // Fill receive data buffer with received byte // store X LSByte // Wait for stop condition to complete // Clear USCI_B0 RX int flag // wait ms void wait_ms(unsigned short ms) unsigned short a, b; for (a = ms; a > 0; a--) // outer loop takes 5 ck per round for (b = TICKSPERMS; b > 0; b--) // inner loop takes 5 ck per round asm("nop"); // wait us void wait_us(unsigned short us) unsigned short a; us *= TICKSPERUS; for (a = us; a > 0; a--) asm("nop"); // loop takes 5 ck per round VTI Technologies Oy 15/16

4 RESULT WAVEFORMS There is no display on the development hardware so a logic analyzer was used to verify the results. Figure 7 (SPI) and Figure 9 (I2C) show how the register reading is triggered from the CMR3000-D01 INT-pin. After the interrupt has activated, the output registers are read out in sequence using multiple read operation mode (decrement reading). In Figure 8 (SPI) and Figure 10 (I2C) it can be seen how the interrupt takes place immediately after CMR3000-D01 has new data available for reading. It also shows how the INT pin is automatically cleared by reading the rate output data. Note that in case of I2C the Fast Mode (400 kbit/s) has been used. With Standard Mode (100 kbit/s) the read frame takes longer than interval of the CMR3000-D01 data ready interrupts (500 us, 2 khz) so it will not be possible to read every sample and the software must take care that two read operations are not attempted concurrently. Use of separate interrupt (timer) is recommended in this case. Figure 7. SPI waveforms when reading X-, Z- and Y-registers (decrement reading) Figure 8. Register reading is triggered by CMR3000-D01's INT signal, SPI bus Figure 9. I2C waveforms when reading X-, Z- and Y-registers Figure 10. Register reading is triggered by CMR3000-D01's INT signal, I2C bus VTI Technologies Oy 16/16