Presentation is loading. Please wait.

Presentation is loading. Please wait.

3/17/2014 Richard Kuo Assistant Professor

Similar presentations


Presentation on theme: "3/17/2014 Richard Kuo Assistant Professor"— Presentation transcript:

1 3/17/2014 Richard Kuo Assistant Professor
I2C and MEMS sensors 3/17/2014 Richard Kuo Assistant Professor

2 Outline 10.NuMicro_I2C.ppt I2C Bus Introduction
Exercise : 3-axis accelerometer & gyroscope Lab. smpl_I2C_MMA7455 (3-axis accelerometer ) Lab. smpl_I2C_L3G4200 (3-axis gyroscope) Lab. smpl_I2C_MPU6050 (3-axis accelero+3-axis gyro) Lab. smpl_I2C_MPU6050_tilt (tilt angle) Exercise : barometer, temperature, luminance sensors Lab. smpl_I2C_BMP085 (barometric pressure sensor) Lab. smpl_I2C_HMC5883 (E-compass) Lab. smpl_I2C_BH1750 (Light Sensor) Homework : Motion Sensing L3G4200 MMA7455 MPU6050 BMP085 HMC5883 BH1750

3 I2C pins Based on NUC140 LQFP 100pin I2C0 I2C0SDA/GPA8 : pin 12
I2C0SCL/GPA9 : pin 11 I2C1 I2C1SDA/GPA10 : pin 10 I2C1SCL/GPA11 : pin 9 Based on Nano102 LQFP 64pin I2C0 I2C0SDA/PC0 /PA12 I2C0SCL/PC1 /PA13 I2C1 I2C1SDA/PC2 /PA14 I2C1SCL/PC3 /PA14

4 I2C Bus Introduction Vdd SDA (Serial Data Line)
SCL (Serial Clock Line) SCLKN1 out DATA1 out SCLK in DATA in Device

5 I2C protocol START or Repeated START signal generation
Slave address transfer Data transfer STOP signal generation

6 I2C Start & Stop Condition

7 I2C Bit Transfer

8 I2C Acknowledge

9 Mater transmits a 7-bit Slave address and 1-bit R/W
I2C protocol Data transfer stage Acknowledge STOP Mater transmits a 7-bit Slave address and 1-bit R/W Transmission starts with a START condition

10 I2C controller Compatible with Philips I2C standard.
Support Master/Slave mode Support 7 bit addressing mode Built-in a 14-bit time-out counter to avoid the I2C bus hang-up Multiple address recognition ( Four slave address with mask option)

11 I2C Functional Registers
I2CON : I2C Control Register I2DAT : I2C Data Register I2STATUS : I2C Status Register I2CLK : I2C Clock Rate Register I2ADDRx, x=0~3 : I2C Address Register I2ADRMx, x=0~3 : I2C Address Mask Register I2TOC : I2C Time-out Counter Register

12 I2CON : I2C Control Register
7 6 5 4 3 2 1 EI ENSI STA STO SI AA Reserved Assert ACK control bit When I2C STATUS state changes, SI is set by HW and cleared by writing ‘1’ to this bit. To transmit a “STOP” in master mode It is cleared by HW automatically. To transmit a “START” “Repeat START” when bus is free I2C hardware enable I2C interrupt enable

13 I2C Control Register Usage
Slave Address /W A DATA0 DATA1 P SI flag is set by hardware 7-bit slave address with a write bit DATA0 DATA1 [0,0,1,0] [0,0,1,0] [0,0,1,0] [0,1,1,0] [1,0,0,x] To set the [STA,STO,SI,AA] for above list conditions.

14 I2C_SingleWrite (index, data)
void I2C_SingleWrite(uint8_t index, uint8_t data) { I2C_START(I2C_SLAVE_PORT); //Start I2C_WAIT_READY(I2C_SLAVE_PORT); I2C_SLAVE_PORT->INTSTS |= I2C_INTSTS_INTSTS_Msk; //clear flag I2C_SET_DATA(I2C_SLAVE_PORT, I2C_SLAVE_ADDR); //send slave address I2C_SET_CONTROL_REG(I2C_SLAVE_PORT, I2C_SI); I2C_SET_DATA(I2C_SLAVE_PORT, index); //send index I2C_SET_DATA(I2C_SLAVE_PORT, data); //send data I2C_SET_CONTROL_REG(I2C_SLAVE_PORT, I2C_SI|I2C_STO); //Stop }

15 I2C_SingleRead(index)
uint8_t I2C_SingleRead(uint8_t index) { uint8_t tmp; I2C_START(I2C_SLAVE_PORT); //Start I2C_WAIT_READY(I2C_SLAVE_PORT); I2C_SLAVE_PORT->INTSTS |= I2C_INTSTS_INTSTS_Msk; //clear flag I2C_SET_DATA(I2C_SLAVE_PORT, I2C_SLAVE_ADDR); //send slave address (W) I2C_SET_CONTROL_REG(I2C_SLAVE_PORT, I2C_SI); I2C_SLAVE_PORT->INTSTS |= I2C_INTSTS_INTSTS_Msk; //clear flag I2C_SET_DATA(I2C_SLAVE_PORT, index); //send index

16 I2C_SingleRead(index)
I2C_SET_CONTROL_REG(I2C_SLAVE_PORT, I2C_STA | I2C_SI); //Start I2C_WAIT_READY(I2C_SLAVE_PORT); I2C_SLAVE_PORT->INTSTS |= I2C_INTSTS_INTSTS_Msk; //clear flag I2C_SET_DATA(I2C_SLAVE_PORT, (I2C_SLAVE_ADDR+1)); //send slave address+R I2C_SET_CONTROL_REG(I2C_SLAVE_PORT, I2C_SI); tmp = I2C_GET_DATA(I2C_SLAVE_PORT); //read data I2C_SET_CONTROL_REG(I2C_SLAVE_PORT, I2C_SI|I2C_STO);//Stop return tmp; }

17 I2C_Open (i2c_port, clock)
void Init_LM75A(void) { I2C_Open(I2C_SLAVE_PORT,400000); }

18 3-axis Accelerometer an accelerometer at rest on the surface of the earth will measure an acceleration g= 9.81 m/s2 straight upwards, due to its weight Source: MEMS Trends: Smaller, Cheaper, Everywhere

19 Freescale MMA7455 Source: Freescale MMA7455L datasheet

20 Accelerometer sensing direction and its output
2g mode

21 Accelerameter output per mode

22 Measuring Tilt with low-g accelerometer
Sensing Axis for an accelerometer with X,Y,Z axis Gravity component of tilted X-axis accelerometer Gravity component of tilted Z-axis accelerometer

23 Smpl_I2C_MMA7455

24 Smpl_I2C_MMA7455 // // Smpl_I2C_MMA7455 : 3-axis Accelerometer // CS: 0 = SPI, 1 = I2C // MMA7455 pin connections // pin1 VCC :to Vcc5V // pin2 NC : N.C. // pin3 NC : N.C. // pin4 INT1 : N.C. // pin5 INT2 : N.C. // pin6 GND : to Gnd // pin7 GND : to Gnd // pin8 CS : to Vcc5V // pin9 SDO : N.C. // pin10 SDA/SDI : to I2C0_SDA (GPA8) // pin11 SCL : to I2C1_SCL (GPA9) // pin12 VCC : to Vcc5V

25 MMA7455.c void Init_MMA7455(void) { DrvGPIO_InitFunction(E_FUNC_I2C0); // set I2C0 pins DrvI2C_Open(I2C_PORT0,150000); //set I2C to 150KHz I2C_SingleWrite(MMA7455_MCTL, 0x05); // 0x16 set to 0101 for +-2g mode } int8_t Read_MMA7455_DataX(void) return I2C_SingleRead(MMA7455_XOUT8); int8_t Read_MMA7455_DataY(void) return I2C_SingleRead(MMA7455_YOUT8); int8_t Read_MMA7455_DataZ(void) return I2C_SingleRead(MMA7455_ZOUT8);

26 main.c #include <stdio.h> #include "SYS.h" #include "GPIO.h" #include "I2C.h" #include "LCD.h" #include "MMA7455.h" int32_t main (void) { char TEXT[16]; int8_t tmp; int8_t accX, accY, accZ; UNLOCKREG(); SYSCLK->PWRCON.XTL12M_EN=1; DrvSYS_Delay(5000);// Waiting for 12M Xtal stable DrvSYS_SelectHCLKSource(0); LOCKREG(); init_LCD(); //Initialize LCD pannel clear_LCD(); print_Line(0,"Smpl_I2C_MMA7455"); Init_MMA7455(); while(1) { // read 3-axis Accelerometer accX = Read_MMA7455_DataX(); // read X value accY = Read_MMA7455_DataY(); // read Y value accZ = Read_MMA7455_DataZ(); // read Z value // display on LCD sprintf(TEXT,"x: %4d", accX); print_Line(1,TEXT); sprintf(TEXT,"y: %4d", accY); print_Line(2,TEXT); sprintf(TEXT,"z: %4d", accZ); print_Line(3,TEXT); }

27 Gyroscope A gyroscope is a device for measuring or maintaining orientation, based on the principles of angular momentum, it is to measure the rate of changes of the angles (deg/s) Turn Coordinator

28 Gyroscope Theory Rigidity in space refers to the principle that a wheel with a heavily weighted rim spun rapidly will remain in a fixed position in the plane in which it is spinning. Source: Gyroscopic Flight Instruments

29 MEMS Gyroscope

30 Gyroscope Gyroscopes are physical sensors that detect and measure the angular motion of an object relative to an inertial frame of reference, which is the rate of changes of the angles (deg/s) Axz - is the angle between the Rxz (projection of R on XZ plane) and Z axis Ayz - is the angle between the Ryz (projection of R on YZ plane) and Z axis

31 Smpl_I2C_L3G4200D

32 Smpl_I2C_L3G4200D // Smpl_I2C_L3G4200 : 3-axis Gyroscope // // CS: 0 = SPI, 1 = I2C // L3G4200D pin connections // pin1 SDO N.C. // pin2 CS to Vcc +5V // pin3 DR N.C. // pin4 INT N.C. // pin5 GND to Gnd // pin6 SDA to I2C0_SDA (GPA8) // pin7 SCL to I2C0_SCL (GPA9) // pin8 Vcc to Vcc5V // I2C_Addr = SAD[6:1] + SDO + R/W // when SDO no connection // I2C slave ADDR = 0xD2 (Write) // I2C slave ADDR = 0xD3 (Read)

33 L3G4200D.c void Init_L3G4200D(void) {
DrvGPIO_InitFunction(E_FUNC_I2C0); // set I2C0 pins DrvI2C_Open(I2C_PORT0,400000); // set I2C to 400KHz I2C_SingleWrite(L3G4200D_CTRL_REG1, 0x0f); // Xen,Yen,Zen,PD I2C_SingleWrite(L3G4200D_CTRL_REG2, 0x00); // HPF I2C_SingleWrite(L3G4200D_CTRL_REG3, 0x08); // I2C_SingleWrite(L3G4200D_CTRL_REG4, 0x30); //+-2000dps I2C_SingleWrite(L3G4200D_CTRL_REG5, 0x00); } iint16_t Read_DataX_L3G4200D(void) { uint8_t LoByte, HiByte; LoByte=I2C_Read(L3G4200D_OUT_X_L); // read X_Low value HiByte=I2C_Read(L3G4200D_OUT_X_H); // read X_High value return((HiByte<<8) | LoByte); } iint16_t Read_DataY_L3G4200D(void) LoByte=I2C_Read(L3G4200D_OUT_Y_L); // read X_Low value HiByte=I2C_Read(L3G4200D_OUT_Y_H); // read X_High value iint16_t Read_DataZ_L3G4200D(void) LoByte=I2C_Read(L3G4200D_OUT_Z_L); // read X_Low value HiByte=I2C_Read(L3G4200D_OUT_Z_H); // read X_High value

34 Main.c #include <stdio.h> #include <stdint.h> #include "SYS.h" #include "GPIO.h" #include "I2C.h" #include "LCD.h" #include "L3G4200D.h" int32_t main (void) { char TEXT[16]; int16_t tmp; int16_t gyroX, gyroY, gyroZ; UNLOCKREG(); SYSCLK->PWRCON.XTL12M_EN=1; DrvSYS_Delay(5000); // Waiting for 12M Xtal stalble DrvSYS_SelectHCLKSource(0); LOCKREG(); init_LCD(); // Initialize LCD clear_LCD(); print_Line(0,"Smpl_I2C_L3G4200"); Init_L3G4200D(); // Initialize L3G4200D while(1) { gyroX = Read_L3G4200D_DataX(); gyroY = Read_L3G4200D_DataY(); gyroZ = Read_L3G4200D_DataZ(); sprintf(TEXT,"X: %6d", gyroX); print_Line(1,TEXT); sprintf(TEXT,"Y: %6d", gyroY); print_Line(2,TEXT); sprintf(TEXT,"Z: %6d", gyroZ); print_Line(3,TEXT); }

35 Accelerometer + Gyroscope
Source: Teardown Compares Combo Sensors by EETimes

36 MPU6050

37 Smpl_I2C_MPU6050

38 MPU6050.c (read accelerometer)
uint16_t Read_AccX_MPU6050(void) { uint8_t LoByte, HiByte; LoByte = I2C_Read(MPU6050_ACCEL_XOUT_L); // read Accelerometer X_Low value HiByte = I2C_Read(MPU6050_ACCEL_XOUT_H); // read Accelerometer X_High value return((HiByte<<8) | LoByte); } uint16_t Read_AccY_MPU6050(void) LoByte = I2C_Read(MPU6050_ACCEL_YOUT_L); // read Accelerometer X_Low value HiByte = I2C_Read(MPU6050_ACCEL_YOUT_H); // read Accelerometer X_High value uint16_t Read_AccZ_MPU6050(void) LoByte = I2C_Read(MPU6050_ACCEL_ZOUT_L); // read Accelerometer X_Low value HiByte = I2C_Read(MPU6050_ACCEL_ZOUT_H); // read Accelerometer X_High value

39 MPU6050.c (read gryoscope) uint16_t Read_GyroX_MPU6050(void) { uint8_t LoByte, HiByte; LoByte = I2C_Read(MPU6050_GYRO_XOUT_L); // read Accelerometer X_Low value HiByte = I2C_Read(MPU6050_GYRO_XOUT_H); // read Accelerometer X_High value return ((HiByte<<8) | LoByte); } uint16_t Read_GyroY_MPU6050(void) LoByte = I2C_Read(MPU6050_GYRO_YOUT_L); // read Accelerometer X_Low value HiByte = I2C_Read(MPU6050_GYRO_YOUT_H); // read Accelerometer X_High value return((HiByte<<8) | LoByte); uint16_t Read_GyroZ_MPU6050(void) LoByte = I2C_Read(MPU6050_GYRO_ZOUT_L); // read Accelerometer X_Low value HiByte = I2C_Read(MPU6050_GYRO_ZOUT_H); // read Accelerometer X_High value

40 smpl_I2C_MPU6050 // // smpl_I2C_MPU6050 // Board : NuTiny-EVB-Nano102 // MCU : Nano102SC2AN (LQFP64) // Connections : // MPU axis accelerometer & 3-axis gyroscope // PC2 /I2C1-SCL : LQFP64-pin9 connected to MPU6050-SCL // PC3 /I2C1-SDA : LQFP64-pin10 connected to MPU6050-SDA #include <stdio.h> #include <math.h> #include <string.h> #include "Nano1X2Series.h" #include "i2c.h" #include "MPU6050.h"

41 smpl_I2C_MPU6050 int32_t main() { int16_t AccX, AccY, AccZ; int16_t GyroX, GyroY, GyroZ; SYS_Init(); Init_MPU6050(); while(1) { AccX = Read_MPU6050_AccX(); AccY = Read_MPU6050_AccY(); AccZ = Read_MPU6050_AccZ(); printf("Accelerometer: %6d, %6d, %6d\n", AccX, AccY, AccZ); GyroX = Read_MPU6050_GyroX(); GyroY = Read_MPU6050_GyroY(); GyroZ = Read_MPU6050_GyroZ(); printf("Gyroscope : %6d, %6d, %6d\n", GyroX, GyroY, GyroZ); }

42 Accelerometer Angle : Axr = arccos(Rx/R) Ayr = arccos(Ry/R) Azr = arccos(Rz/R) R^2 = Rx^2 + Ry^2 + Rz^2

43 3-axis for measuring Tilt Angle

44 smpl_I2C_MPU6050_tilt #include <stdio.h> #include <stdint.h> #include <math.h> #include "SYS.h" #include "GPIO.h" #include "I2C.h" #include "I2C_setup.h" #include "LCD.h" #include "MPU6050.h" int32_t main (void) { char TEXT1[16], TEXT2[16], TEXT3[16]; int16_t tmp; float accX, accY, accZ; UNLOCKREG(); SYSCLK->PWRCON.XTL12M_EN=1; DrvSYS_Delay(5000); // Waiting for 12M Xtal stalble SYSCLK->CLKSEL0.HCLK_S=0; LOCKREG(); init_LCD(); clear_LCD(); print_Line(0,"MPU6050_Tilt"); init_MPU6050(); // Initialize MPU6050 while(1) { tmp = Read_AccX_MPU6050(); accX = (float) tmp/32768 *2; tmp = Read_AccY_MPU6050(); accY = (float) tmp/32768 *2; tmp = Read_AccZ_MPU6050(); accZ = (float) tmp/32768 *2; // calculate tilt (* = degree of angle) Axr = * atan(accX / sqrt(pow(accY,2)+pow(accZ,2))); Ayr = * atan(accY / sqrt(pow(accX,2)+pow(accZ,2))); Azr = * atan(sqrt(pow(accX,2)+pow(accY,2))/accZ); sprintf(TEXT1,"Axr: %f", Axr); print_Line(1,TEXT1); sprintf(TEXT2,"Ayr: %f", Ayr); print_Line(2,TEXT2); sprintf(TEXT3,"Azr: %f", Azr); print_Line(3,TEXT3); }

45 6-axis inertial measurement units (IMUs)
STMicroelectronics and Bosch use five die, but InvenSense only uses two – one die for a six-axis accelerometer/gyroscope and one for a three-axis magnetometer STMicro 4x4mm^2 LGA Bosch InvenSense 3x3mm^2 QFN Source: Teardown Compares Combo Sensors by EETimes

46 STMicro LSM9DS0 six-axis IMU
The accelero/gyro sensor in the STMicroelectronics LSM9DS0 nine-axis IMU. (Source: System Plus Consulting report from December 2013) Source: Teardown Compares Combo Sensors by EETimes

47 Bosch BMX055 six-axis MEMS IMU
The three-axis magnetometer in the Bosch Sensortec BMX055 nine-axis MEMS IMU. (Source: System Plus Consulting report from December 2013) Source: Teardown Compares Combo Sensors by EETimes

48 InvenSense MPU-9250 six-axis IMU
The MEMS accelero/gyro sensor in the InvenSense MPU-9250 nine-axis IMU. (Source: System Plus Consulting report from March 2014) Source: Teardown Compares Combo Sensors by EETimes

49 Barometric Pressure Sensor
Bosch BMP085 Vin : 3~5V DC Logic : 3~5V compliant Pressure Sensing Range : 300~1100 hPa (altitude +9000m ~ -500m) Low power: 5uA at 1 sample per second Low noise : 0.06hPa (0.5m) in ultra low power mode 0.03hPa (0.25m) ultra high resolution mode Temperature : +-2 degree accuracy Operating Temperature : -40 ~ 85 °C I2C Interface

50 smpl_I2C_BMP085

51 Ambient Light Sensor (ALS)
BH1750FVI is an digital Ambient Light Sensor IC For adjusting LCD and Kepad backlight power of mobile phone I2C bus interface, max. freq = 400KHz Spectral responsibility is approximately human eye response Illuminance to Digital Converter Wide range and High resolution (1 ~ lux) 50/60Hz Light noise reject-function 1.8V logic input interface, Vcc = 3~3.6V Adjustable measurement time for influence of optical window, possible to detect min lux, max lux

52 smpl_I2C_BH1750

53 E-Compass (電子羅盤) HMC5883L is an digital E-compass IC
3-Axis Magnetoresistive Sensors 12-Bit ADC Coupled with Low Noise AMR Sensors Achieves 5 milli-gauss Resolution in ±8 Gauss Fields a 12-bit ADC that enables 1° to 2° compass heading accuracy Low Voltage Operations (2.16 to 3.6V) and Low Power Consumption (100 μA) 75Hz Continuous or 160 Hz Single measured Maximum Output Rate I2C interface (up to 400KHz) HMC5883L utilizes Honeywell’s Anisotropic Magnetoresistive (AMR) technology that provides advantages over other magnetic sensor technologies

54 smpl_I2C_HMC5883 HMC5883L

55 Important Notice ! This educational material are neither intended nor warranted for usage in systems or equipment, any malfunction or failure of which may cause loss of human life, bodily injury or severe property damage. Such applications are deemed, “Insecure Usage”. Insecure usage includes, but is not limited to: equipment for surgical implementation, atomic energy control instruments, airplane or spaceship instruments, the control or operation of dynamic, brake or safety systems designed for vehicular use, traffic signal instruments, all types of safety devices, and other applications intended to support or sustain life. All Insecure Usage shall be made at user’s own risk, and in the event that third parties lay claims to the author as a result of customer’s Insecure Usage, the user shall indemnify the damages and liabilities thus incurred by using this material. Please note that all lecture and sample codes are subject to change without notice. All the trademarks of products and companies mentioned in this material belong to their respective owners.


Download ppt "3/17/2014 Richard Kuo Assistant Professor"

Similar presentations


Ads by Google