11.NuMicro_I2C.ppt
Download
Report
Transcript 11.NuMicro_I2C.ppt
I2C Bus
Department of Electrical Engineering,
National Taiwan Ocean University
5/16/2013
Richard Kuo
Assistant Professor
www.ee.ntou.edu.tw
OutLine
Department of Electrical Engineering,
National Taiwan Ocean University
►
I2C Bus Introduction
11.NuMicro_I2C.ppt
►
►
►
►
Exercise: I2C interface with Temperature Sensor IC
(Smpl_I2C_TMP100)
Exercise: I2C interface with 3-axis accelerometer
(Smpl_I2C_MMA7445)
Exercise: I2C interface with 3-axis Gyroscope
(Smpl_I2C_L3G4200)
Exercise: I2C interface with 3-axis Accelero + Gyro
(Smpl_I2C_MPU6050)
www.ee.ntou.edu.tw
NuMicro MCU Learning Board
Department of Electrical Engineering,
National Taiwan Ocean University
I2C signals
►I2C0 :
– I2C0_SCL : GPA9
– I2C0_SDA : GPA8
►I2C1 :
– I2C1_SCL : GPA11
– I2C1_SDA : GPA10
www.ee.ntou.edu.tw
Nu-LB-NUC140
I2C Schematic
Department of Electrical Engineering,
National Taiwan Ocean University
www.ee.ntou.edu.tw
I2C Bus Introduction
Department of Electrical Engineering,
National Taiwan Ocean University
►
►
www.ee.ntou.edu.tw
A two-wired and bi-directional bus, comprised by SDA and SCL,
may include multi masters and multi slaves
Both SDA and SCL are bi-directional with Open-Drain driving type,
connected to a positive supply voltage via pull-up resistors.
I2C controller
Department of Electrical Engineering,
National Taiwan Ocean University
►
►
►
►
►
►
►
www.ee.ntou.edu.tw
Up to two I2C controllers.
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)
Modes of Operation
–
–
–
–
–
Master transmitter
Master receiver
Slave transmitter
Slave receiver
General call
I2C protocol
Department of Electrical Engineering,
National Taiwan Ocean University
►
www.ee.ntou.edu.tw
Standard I2C protocol contain the following 4 sections :
–
–
–
–
START or Repeated START signal generation
Slave address transfer
Data transfer
STOP signal generation
SCL
1
2
SDA
A6
A5
7
8
9
1
2
3-7
8
9
A0
R/W
ACK
D7
D6
D5 - D1
D0
NACK
ACK
P
S
or
Sr
MSB
A4 - A1
LSB
MSB
LSB
P
or
Sr
Sr
START, Repeated START, STOP
Department of Electrical Engineering,
National Taiwan Ocean University
www.ee.ntou.edu.tw
When the bus is free, I2C Hardware can initiate a transfer by sending a
START signal.
The master can use a Repeated START to continue a next transmission
without releasing the bus.
The master can terminate the communication by generating a STOP signal
SCL
1
2
7
8
9
1
2
3-7
8
9
D0
NACK
ACK
P
SDA
A6
S
or
Sr
A5
A4 - A1
A0
MSB
Transmission starts with a
START condition
R/W
LSB
ACK
D7
MSB
D6
D5 - D1
LSB
Transmission ends with a STOP
condition
P
or
Sr
Sr
Slave Address Transfer
Department of Electrical Engineering,
National Taiwan Ocean University
www.ee.ntou.edu.tw
The first byte transferred by the master after the START signal is the slave
address.
This is a 7-bit slave address followed by a R/W bit.
No two slaves have the same address
SCL
1
2
SDA
A6
A5
7
8
9
1
2
3-7
8
9
A0
R/W
ACK
D7
D6
D5 - D1
D0
NACK
ACK
P
S
or
Sr
MSB
A4 - A1
LSB
Mater transmits a 7-bit Slave
address and 1-bit R/W
MSB
LSB
P
or
Sr
Sr
Data Transfer
Department of Electrical Engineering,
National Taiwan Ocean University
www.ee.ntou.edu.tw
When successful slave addressing has been achieved, the data transfer can
proceed on byte-by-byte basis in the direction specified by the R/W bit.
The data length is based on actual application
SCL
1
2
7
8
9
1
2
3-7
8
9
D0
NACK
ACK
P
SDA
A6
S
or
Sr
MSB
A5
A4 - A1
A0
R/W
ACK
LSB
Data transfer stage
(example 1 byte data)
D7
MSB
D6
D5 - D1
LSB
P
or
Sr
Sr
Acknowledge
Department of Electrical Engineering,
National Taiwan Ocean University
www.ee.ntou.edu.tw
Each transferred byte is followed by an ACK or a NACK at 9th clock
Acknowledge
Position
SCL FROM
MASTER
1
2
clock pulse for
acknowledgement
8
DATA OUTPUT BY
TRANSMITTER
9
NACK: High level
not acknowledge
DATA OUTPUT BY
RECEIVER
S
START
condition
acknowledge
ACK: Low level
I2C Functional Registers
Department of Electrical Engineering,
National Taiwan Ocean University
►
►
►
►
►
►
►
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
www.ee.ntou.edu.tw
I2CON : I2C Control Register
Department of Electrical Engineering,
National Taiwan Ocean University
www.ee.ntou.edu.tw
7
6
5
4
3
2
1
0
EI
ENSI
STA
STO
SI
AA
Reserved
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
I2C Control Register用途
Department of Electrical Engineering,
National Taiwan Ocean University
S
Slave Address
/W
A
DATA0
www.ee.ntou.edu.tw
A
DATA1
A
P
SI flag is set by hardware
7-bit slave address
with a write bit
DATA0
DATA1
[0,0,1,0]
[1,0,0,x]
[0,0,1,0]
[0,0,1,0]
To set the [STA,STO,SI,AA] for above list conditions.
[0,1,1,0]
I2C Status Register
Department of Electrical Engineering,
National Taiwan Ocean University
►
►
www.ee.ntou.edu.tw
26 possible status codes.
All I2C status codes correspond to defined I2C states.
– Ex: When a START has been transmitted, the status code is 08H.
TMP100 I2C Timing Diagram for Write Word
Department of Electrical Engineering,
National Taiwan Ocean University
www.ee.ntou.edu.tw
Temperature
Sensor
TMP100 I2C Timing Diagram for Read Word
Department of Electrical Engineering,
National Taiwan Ocean University
www.ee.ntou.edu.tw
Smpl_I2C_TMP100
Department of Electrical Engineering,
National Taiwan Ocean University
www.ee.ntou.edu.tw
Smpl_I2C_TMP100
Department of Electrical Engineering,
National Taiwan Ocean University
// Smpl_I2C_TMP100 : Temperature Sensor IC
//
// TMP100 pin connections
// pin1
SCL
to I2C1_SCL / GP11
// pin2
GND
to GND
// pin3
ADD1 to GND
// pin4
VDD to Vcc +5V
// pin5
ADD0 to GND
// pin6
SDA to I2C1_SDA / GPA10
#include <stdio.h>
#include <stdint.h>
#include "DrvSYS.h"
#include "DrvI2C.h"
#include "DrvGPIO.h"
#include "NUC1xx-LB_002\LCD_Driver.h"
www.ee.ntou.edu.tw
Initial_I2C()
Department of Electrical Engineering,
National Taiwan Ocean University
www.ee.ntou.edu.tw
// Initial I2C
void Initial_I2C(E_I2C_PORT port, uint32_t u32BusClock)
{
//Set I2C I/O
if(port==I2C_PORT0)
DrvGPIO_InitFunction(E_FUNC_I2C0);
else if(port==I2C_PORT1) DrvGPIO_InitFunction(E_FUNC_I2C1);
//
}
SystemCoreClock = DrvSYS_GetHCLKFreq(); //Open I2C, and set clock = u32BusClock bps
DrvI2C_Open(port, u32BusClock);
I2C1->I2CTOC.DIV4 = 1;
I2C1->I2CTOC.TIF = 1; //Clear TIF
I2C1->I2CTOC.ENTI = 1; //Enable I2C bus timeout count(Move to before I2C start)
I2C_Write_Data
Department of Electrical Engineering,
National Taiwan Ocean University
www.ee.ntou.edu.tw
uint8_t I2C_Write_Data(uint8_t I2C_Address, uint8_t *I2C_Data, uint16_t I2C_Data_Length)
{
uint32_t i;
//Send I2C Start
DrvI2C_Ctrl(I2C_PORT1, 1, 0, 1, 0);
//Set STA & Clear SI flag
while(!I2C1->I2CON.SI);
//Waitting SI flag
//Send I2C Address & R/W Command
I2C1->I2CDAT = (I2C_Address<<1) | 0x00;
//Load I2C Address and Command
DrvI2C_Ctrl(I2C_PORT1, 0, 0, 1, 1);
//Clear SI flag & Set AA
while(!I2C1->I2CON.SI);
//Waitting SI flag
//Send I2C Data
for(i=0; i<I2C_Data_Length; i++)
{
I2C1->I2CDAT = I2C_Data[i];
//Load I2C Data
DrvI2C_Ctrl(I2C_PORT1, 0, 0, 1, 1);
//Clear SI flag & Set AA
while(!I2C1->I2CON.SI);
//Waitting SI flag
}
//Send I2C Stop
DrvI2C_Ctrl(I2C_PORT1, 0, 1, 1, 0);
//Set STOP
while(I2C1->I2CON.SI!=0);
//Waitting SI flag clear
return 0;
}
I2C_Read_Data
Department of Electrical Engineering,
National Taiwan Ocean University
www.ee.ntou.edu.tw
uint8_t I2C_Read_Data(uint8_t I2C_Address, uint8_t *I2C_Data, uint16_t I2C_Data_Length)
{
uint32_t i;
//Send I2C Start
DrvI2C_Ctrl(I2C_PORT1, 1, 0, 1, 0);
while(!I2C1->I2CON.SI);
//Set STA & Clear SI flag
//Waitting SI flag
//Send I2C Address & R/W Command
I2C1->I2CDAT = (I2C_Address<<1) | 0x01;
//Load I2C Address and Command
DrvI2C_Ctrl(I2C_PORT1, 0, 0, 1, 1);
//Clear SI flag & Set AA
while(!I2C1->I2CON.SI);
//Waitting SI flag
//Read I2C Data
for(i=0; i<I2C_Data_Length; i++)
{
if((I2C_Data_Length-i)>1)
//I2C_Data_Length > 1 bytes
{
//Read Data
DrvI2C_Ctrl(I2C_PORT1, 0, 0, 1, 1);
//Clear SI flag & Set AA
while(!I2C1->I2CON.SI);
//Waiting SI flag
I2C_Data[i] = I2C1->I2CDAT;
//Read I2C Data
}
I2C_Read_Data
Department of Electrical Engineering,
National Taiwan Ocean University
www.ee.ntou.edu.tw
else
//I2C_Data_Length = 1 bytes
{
//Read Data
DrvI2C_Ctrl(I2C_PORT1, 0, 0, 1, 0);
while(!I2C1->I2CON.SI);
I2C_Data[i] = I2C1->I2CDAT;
//Clear SI flag & Set AA
//Waitting SI flag
//Read I2C Data
}
}
//Send I2C Stop
DrvI2C_Ctrl(I2C_PORT1, 0, 1, 1, 0);
while(I2C1->I2CON.SI!=0);
return 0;
}
//Set STOP
//Waiting SI flag clear
Smpl_I2C_TMP100
Department of Electrical Engineering,
National Taiwan Ocean University
int32_t main (void)
{
float Temperature;
char TEXT2[16] = "
www.ee.ntou.edu.tw
TMP100_Set_TLow(21.5f);
while(1)
{
Temperature = TMP100_Get_Temperature();
sprintf(TEXT2,"%f", Temperature);
print_lcd(2,TEXT2);
DrvSYS_Delay(10000);
}
";
UNLOCKREG();
SYSCLK->PWRCON.XTL12M_EN=1;
DrvSYS_Delay(5000);
DrvSYS_SelectHCLKSource(0);
LOCKREG();
Initial_I2C(I2C_PORT1, 400000);
Initial_panel();
clr_all_panel();
print_lcd(0,"Smpl_I2C_TMP100 ");
print_lcd(1,"Temperature: ");
TMP100_Set_Configuration(_TMP100_RESOLUTION_12BITS);
TMP100_Set_THigh(70.0f);
}
Smpl_I2C_MMA7455
Department of Electrical Engineering,
National Taiwan Ocean University
www.ee.ntou.edu.tw
MMA7455 : 3-axis Accelerometer
Department of Electrical Engineering,
National Taiwan Ocean University
// 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
www.ee.ntou.edu.tw
I2CWrite_MMA7455
Department of Electrical Engineering,
National Taiwan Ocean University
#define I2C_ADDR 0x3A
void I2CWrite_MMA7455(uint8_t INDEX, uint8_t DATA)
{
//send i2c start
DrvI2C_Ctrl(I2C_PORT0, 1, 0, 1, 0);
//set start
while (I2C0->I2CON.SI == 0);
//poll si flag
//send to Write port
I2C0->I2CDAT = I2C_ADDR;
//send writer command
DrvI2C_Ctrl(I2C_PORT0, 0, 0, 1, 0);
//clr si flag
while( I2C0->I2CON.SI == 0 );
//poll si flag
//send write address
I2C0->I2CDAT = INDEX;
DrvI2C_Ctrl(I2C_PORT0, 0, 0, 1, 0);
//clr si
while( I2C0->I2CON.SI == 0 );
//poll si flag
//send write data
I2C0->I2CDAT = DATA;
DrvI2C_Ctrl(I2C_PORT0, 0, 0, 1, 0);
//clr si
while( I2C0->I2CON.SI == 0 );
//poll si flag
www.ee.ntou.edu.tw
I2CWrite_MMA7455
Department of Electrical Engineering,
National Taiwan Ocean University
//
}
//send i2c stop
DrvI2C_Ctrl(I2C_PORT0, 0, 1, 1, 0); //send stop
while( I2C0->I2CON.STO);
while( I2C0->CON.SI == 0 );
www.ee.ntou.edu.tw
I2CRead_MMA7455
Department of Electrical Engineering,
National Taiwan Ocean University
uint8_t I2CRead_MMA7455(uint8_t INDEX)
{
uint8_t TEMP;
//send i2c start
DrvI2C_Ctrl(I2C_PORT0, 1, 0, 1, 0);
while (I2C0->I2CON.SI == 0);
//send to Write port
I2C0->I2CDAT = I2C_ADDR;
DrvI2C_Ctrl(I2C_PORT0, 0, 0, 1, 0);
while( I2C0->I2CON.SI == 0 );
//send INDEX
I2C0->I2CDAT = INDEX;
DrvI2C_Ctrl(I2C_PORT0, 0, 0, 1, 0);
while( I2C0->I2CON.SI == 0 );
//set start
//poll si flag
//clr si
//poll si flag
//clr si
//poll si flag
www.ee.ntou.edu.tw
I2CRead_MMA7455
Department of Electrical Engineering,
National Taiwan Ocean University
//send i2c start
DrvI2C_Ctrl(I2C_PORT0, 1, 0, 1, 0);
while (I2C0->I2CON.SI == 0);
//send to Read port
I2C0->I2CDAT = (I2C_ADDR+1);
DrvI2C_Ctrl(I2C_PORT0, 0, 0, 1, 0);
while( I2C0->I2CON.SI == 0 );
//receive data
I2C0->I2CDAT = 0xFF;
DrvI2C_Ctrl(I2C_PORT0, 0, 0, 1, 0);
while( I2C0->I2CON.SI == 0 );
TEMP = I2C0->I2CDAT;
//send i2c stop
DrvI2C_Ctrl(I2C_PORT0, 0, 1, 1, 0);
while( I2C0->I2CON.STO);
DrvI2C_Close(I2C_PORT0);
return TEMP;
}
//set start
//poll si flag
//clr si
//poll si flag
//clr si
//poll si flag
//clr si and set stop
www.ee.ntou.edu.tw
Smpl_I2C_MMA7455
Department of Electrical Engineering,
National Taiwan Ocean University
int32_t main (void)
{
uint8_t tmpX, tmpY, tmpZ;
char TEXT1[16] = "X =
char TEXT2[16] = "Y =
char TEXT3[16] = "Z =
";
";
";
UNLOCKREG();
SYSCLK->PWRCON.XTL12M_EN=1;
DrvSYS_Delay(5000);
DrvSYS_SelectHCLKSource(0);
LOCKREG();
Initial_panel();
clr_all_panel();
print_lcd(0,"Smpl_I2C_MMA7455");
Initial_I2C(I2C_PORT0, 200000);
I2CWrite_MMA7455(0x16, 0x05);
// Waiting for 12M Xtal stalble
//Initialize LCD pannel
//Initialize I2C0 to 50Kbps
// 0x16 set to 0x05 for +2g mode
www.ee.ntou.edu.tw
Smpl_I2C_MMA7455
Department of Electrical Engineering,
National Taiwan Ocean University
while(1)
{
tmpX = I2CRead_MMA7455(0x06); // read X value
sprintf(TEXT1+4,"%d", tmpX);
tmpY = I2CRead_MMA7455(0x07); // read Y value
sprintf(TEXT2+4,"%d", tmpY);
tmpZ = I2CRead_MMA7455(0x08); // read Z value
sprintf(TEXT3+4,"%d", tmpZ);
print_lcd(1,TEXT1);
print_lcd(2,TEXT2);
print_lcd(3,TEXT3);
}
}
www.ee.ntou.edu.tw
Smpl_I2C_L3G4200D
Department of Electrical Engineering,
National Taiwan Ocean University
www.ee.ntou.edu.tw
L3G4200D : 3-axis Gyroscope
Department of Electrical Engineering,
National Taiwan Ocean University
// 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)
www.ee.ntou.edu.tw
I2CWrite_L3G4200D
Department of Electrical Engineering,
National
Taiwan OceanINDEX,uint8_t
University
void I2CWrite_L3G4200D(uint8_t
DATA)
{
//send i2c start
DrvI2C_Ctrl(I2C_PORT0, 1, 0, 1, 0);
//set start
while (I2C0->I2CON.SI == 0);
//poll si flag
//send to Write port
I2C0->I2CDAT = I2C_ADDR;
//send writer command
DrvI2C_Ctrl(I2C_PORT0, 0, 0, 1, 0); /
/clr si flag
while( I2C0->I2CON.SI == 0 );
//poll si flag
//send write address
I2C0->I2CDAT = INDEX;
DrvI2C_Ctrl(I2C_PORT0, 0, 0, 1, 0);
//clr si
while( I2C0->I2CON.SI == 0 );
//poll si flag
//send write data
I2C0->I2CDAT = DATA;
DrvI2C_Ctrl(I2C_PORT0, 0, 0, 1, 0);
//clr si
while( I2C0->I2CON.SI == 0 );
//poll si flag
//send i2c stop
DrvI2C_Ctrl(I2C_PORT0, 0, 1, 1, 0);
//send stop
while( I2C0->I2CON.STO);//while( I2C0->CON.SI == 0 );
DrvI2C_Close(I2C_PORT0);
}
www.ee.ntou.edu.tw
I2CRead_L3G4200D
Department of Electrical Engineering,
National Taiwan Ocean University
uint8_t I2CRead_L3G4200D(uint8_t INDEX)
{
uint8_t TEMP;
//send i2c start
DrvI2C_Ctrl(I2C_PORT0, 1, 0, 1, 0);
while (I2C0->I2CON.SI == 0);
//send to Write port
I2C0->I2CDAT = I2C_ADDR;
DrvI2C_Ctrl(I2C_PORT0, 0, 0, 1, 0);
while( I2C0->I2CON.SI == 0 );
//send INDEX
I2C0->I2CDAT = INDEX;
DrvI2C_Ctrl(I2C_PORT0, 0, 0, 1, 0);
while( I2C0->I2CON.SI == 0 );
//set start
//poll si flag
//clr si
//poll si flag
//clr si
//poll si flag
www.ee.ntou.edu.tw
I2CRead_L3G4200D
Department of Electrical Engineering,
National Taiwan Ocean University
//send i2c start
DrvI2C_Ctrl(I2C_PORT0, 1, 0, 1, 0);
while (I2C0->I2CON.SI == 0);
//send to Read port
I2C0->I2CDAT = (I2C_ADDR+1);
DrvI2C_Ctrl(I2C_PORT0, 0, 0, 1, 0);
while( I2C0->I2CON.SI == 0 );
//receive data
I2C0->I2CDAT = 0xFF;
DrvI2C_Ctrl(I2C_PORT0, 0, 0, 1, 0);
while( I2C0->I2CON.SI == 0 );
TEMP = I2C0->I2CDAT;
//send i2c stop
DrvI2C_Ctrl(I2C_PORT0, 0, 1, 1, 0);
while( I2C0->I2CON.STO);
DrvI2C_Close(I2C_PORT0);
return TEMP;
}
//set start
//poll si flag
//clr si
//poll si flag
//clr si
//poll si flag
//clr si and set stop
www.ee.ntou.edu.tw
Smpl_I2C_L3G4200D
Department of Electrical Engineering,
National Taiwan Ocean University
int32_t main (void)
{
char TEXT1[16] = "X =
";
char TEXT2[16] = "Y =
";
char TEXT3[16] = "Z =
";
uint8_t tmpX_L, tmpX_H;
uint8_t tmpY_L, tmpY_H;
uint8_t tmpZ_L, tmpZ_H;
float temp;
int tmp;
UNLOCKREG();
SYSCLK->PWRCON.XTL12M_EN=1;
DrvSYS_Delay(5000);
DrvSYS_SelectHCLKSource(0);
LOCKREG();
// Waiting for 12M Xtal stalble
Initial_panel();
clr_all_panel();
print_lcd(0,"Smpl_I2C_L3G4200");
// Initialize LCD pannel
Initial_I2C(I2C_PORT0, 400000);
Init_L3G4200D();
// Initialize I2C
// Initialize L3G4200D
www.ee.ntou.edu.tw
Smpl_I2C_L3G4200D
Department of Electrical Engineering,
National Taiwan Ocean University
while(1)
{
tmpX_L = I2CRead_L3G4200D(OUT_X_L); // read X_Low value
tmpX_H = I2CRead_L3G4200D(OUT_X_H); // read X_High value
tmp = (tmpX_H<<8)+tmpX_L;
temp = (float)tmp * 0.07;
sprintf(TEXT1+4,"%f", temp);
tmpY_L = I2CRead_L3G4200D(OUT_Y_L); // read Y_Low value
tmpY_H = I2CRead_L3G4200D(OUT_Y_H); // read Y_High value;
tmp = (tmpY_H<<8)+tmpY_L;
temp = (float)tmp * 0.07;
sprintf(TEXT2+4,"%f", temp);
tmpZ_L = I2CRead_L3G4200D(OUT_Z_L); // read Y_Low value
tmpZ_H = I2CRead_L3G4200D(OUT_Z_H); // read Y_High value
tmp = (tmpZ_H<<8)+tmpZ_L;
temp = (float)tmp * 0.07;
sprintf(TEXT3+4,"%f", temp);
print_lcd(1,TEXT1);
print_lcd(2,TEXT2);
print_lcd(3,TEXT3);
}
}
www.ee.ntou.edu.tw
Smpl_I2C_MPU6050
Department of Electrical Engineering,
National Taiwan Ocean University
www.ee.ntou.edu.tw
MPU-6050 : Accelero + Gyroscope
Department of Electrical Engineering,
National Taiwan Ocean University
//
// Smpl_I2C_MPU6050
//
// MPU6050 : 3-axis Gyroscope + 3-axis accelerometer + temperature
// Interface: I2C
// pin1: Vcc to Vcc (+5V)
// pin2: Gnd to Gnd
// pin3: SCL to I2C0_SCL/GPA9
// pin4: SDA to I2C0_SDA/GPA8
// pin5: XDA -- N.C.
// pin6: XCL -- N.C.
// pin7: AD0 -- N.C.
// pin8: INT -- N.C.
www.ee.ntou.edu.tw
I2CWrite_MPU6050
Department of Electrical Engineering,
National Taiwan Ocean University
void I2CWrite_MPU6050(uint8_t INDEX,uint8_t DATA)
{
//send i2c start
DrvI2C_Ctrl(I2C_PORT0, 1, 0, 1, 0);
while (I2C0->I2CON.SI == 0);
//send to Write port
I2C0->I2CDAT = I2C_ADDR;
DrvI2C_Ctrl(I2C_PORT0, 0, 0, 1, 0);
while( I2C0->I2CON.SI == 0 );
//send write address
I2C0->I2CDAT = INDEX;
DrvI2C_Ctrl(I2C_PORT0, 0, 0, 1, 0);
while( I2C0->I2CON.SI == 0 );
//send write data
I2C0->I2CDAT = DATA;
DrvI2C_Ctrl(I2C_PORT0, 0, 0, 1, 0);
while( I2C0->I2CON.SI == 0 );
//send i2c stop
DrvI2C_Ctrl(I2C_PORT0, 0, 1, 1, 0);
while( I2C0->I2CON.STO);
//
while( I2C0->CON.SI == 0 );
}
//set start
//poll si flag
//send writer command
//clr si flag
//poll si flag
//clr si
//poll si flag
//clr si
//poll si flag
//send stop
www.ee.ntou.edu.tw
I2CRead_MPU6050
Department of Electrical Engineering,
National Taiwan Ocean University
uint8_t I2CRead_MPU6050(uint8_t INDEX)
{
uint8_t TEMP;
//send i2c start
DrvI2C_Ctrl(I2C_PORT0, 1, 0, 1, 0);
while (I2C0->I2CON.SI == 0);
//send to Write port
I2C0->I2CDAT = I2C_ADDR;
DrvI2C_Ctrl(I2C_PORT0, 0, 0, 1, 0);
while( I2C0->I2CON.SI == 0 );
//send INDEX
I2C0->I2CDAT = INDEX;
DrvI2C_Ctrl(I2C_PORT0, 0, 0, 1, 0);
while( I2C0->I2CON.SI == 0 );
//send i2c start
DrvI2C_Ctrl(I2C_PORT0, 1, 0, 1, 0);
while (I2C0->I2CON.SI == 0);
//send to Read port
I2C0->I2CDAT = (I2C_ADDR+1);
DrvI2C_Ctrl(I2C_PORT0, 0, 0, 1, 0);
while( I2C0->I2CON.SI == 0 );
//set start
//poll si flag
//clr si
//poll si flag
//clr si
//poll si flag
//set start
//poll si flag
//clr si
//poll si flag
www.ee.ntou.edu.tw
I2CRead_MPU6050
Department of Electrical Engineering,
National Taiwan Ocean University
//receive data
I2C0->I2CDAT = 0xFF;
DrvI2C_Ctrl(I2C_PORT0, 0, 0, 1, 0);
while( I2C0->I2CON.SI == 0 );
TEMP = I2C0->I2CDAT;
//send i2c stop
DrvI2C_Ctrl(I2C_PORT0, 0, 1, 1, 0);
while( I2C0->I2CON.STO);
return TEMP;
}
//clr si
//poll si flag
//clr si and set stop
www.ee.ntou.edu.tw
Smpl_I2C_MPU6050
Department of Electrical Engineering,
National Taiwan Ocean University
int32_t main (void)
{
char TEXT1[16] = "x: X: ";
char TEXT2[16] = "y: Y: ";
char TEXT3[16] = "z:
Z:
";
uint8_t tmpL, tmpH;
uint16_t tmp;
float temp;
int accX, accY, accZ, gyroX, gyroY, gyroZ;
UNLOCKREG();
SYSCLK->PWRCON.XTL12M_EN=1;
DrvSYS_Delay(5000);
SYSCLK->CLKSEL0.HCLK_S=0;
LOCKREG();
// Waiting for 12M Xtal stalble
Initial_panel();
clr_all_panel();
print_lcd(0,"Smpl_I2C_MPU6050");
Initial_I2C(I2C_PORT0, 200000); // Initialize I2C
Init_MPU6050();
// Initialize MPU6050
www.ee.ntou.edu.tw
Smpl_I2C_MPU6050
Department of Electrical Engineering,
National Taiwan Ocean University
www.ee.ntou.edu.tw
while(1)
{
tmpL = I2CRead_MPU6050(ACCEL_XOUT_L); // read Accelerometer X_Low value
tmpH = I2CRead_MPU6050(ACCEL_XOUT_H); // read Accelerometer X_High value
tmp = (tmpH<<8)+tmpL;
temp = (float)tmp;
accX = (int)temp;
tmpL = I2CRead_MPU6050(ACCEL_YOUT_L); // read Accelerometer Y_Low value
tmpH = I2CRead_MPU6050(ACCEL_YOUT_H); // read Accelerometer Y_High value
tmp = (tmpH<<8)+tmpL;
temp = (float)tmp;
accY = (int)temp;
tmpL = I2CRead_MPU6050(ACCEL_ZOUT_L); // read Accelerometer Z_Low value
tmpH = I2CRead_MPU6050(ACCEL_ZOUT_H); // read Accelerometer Z_High value
tmp = (tmpH<<8)+tmpL;
temp = (float)tmp;
accZ = (int)temp;
Smpl_I2C_MPU6050
Department of Electrical Engineering,
National Taiwan Ocean University
tmpL = I2CRead_MPU6050(GYRO_XOUT_L); // read Gyroscope X_Low value
tmpH = I2CRead_MPU6050(GYRO_XOUT_H); // read Gyroscope X_High value
tmp = (tmpH<<8)+tmpL;
temp = (float)tmp;
gyroX = (int)temp;
tmpL = I2CRead_MPU6050(GYRO_YOUT_L); // read Gyroscope Y_Low value
tmpH = I2CRead_MPU6050(GYRO_YOUT_H); // read Gyroscope Y_High value
tmp = (tmpH<<8)+tmpL;
temp = (float)tmp;
gyroY = (int)temp;;
tmpL = I2CRead_MPU6050(GYRO_ZOUT_L); // read Gyroscope Z_Low value
tmpH = I2CRead_MPU6050(GYRO_ZOUT_H); // read Gyroscope Z_High value
tmp = (tmpH<<8)+tmpL;
temp = (float)tmp;
gyroZ = (int)temp;
www.ee.ntou.edu.tw
Smpl_I2C_MPU6050
Department of Electrical Engineering,
National Taiwan Ocean University
sprintf(TEXT1,"x:%d X:%d",accX, gyroX);
sprintf(TEXT2,"y:%d Y:%d",accY, gyroY);
sprintf(TEXT3,"z:%d Z:%d",accZ, gyroZ);
print_lcd(1,TEXT1);
print_lcd(2,TEXT2);
print_lcd(3,TEXT3);
}
}
www.ee.ntou.edu.tw
General Disclaimer
Department of Electrical Engineering,
National Taiwan Ocean University
www.ee.ntou.edu.tw
The Lecture is strictly used for educational purpose.
MAKES NO GUARANTEE OF VALIDITY
►The
lecture cannot guarantee the validity of the information found here. The lecture may recently have been
changed, vandalized or altered by someone whose opinion does not correspond with the state of knowledge in the relevant
fields. Note that most other encyclopedias and reference works also have similar disclaimers.
No formal peer review
►The
lecture is not uniformly peer reviewed; while readers may correct errors or engage in casual peer review, they have
no legal duty to do so and thus all information read here is without any implied warranty of fitness for any purpose or use
whatsoever. Even articles that have been vetted by informal peer review or featured article processes may later have been
edited inappropriately, just before you view them.
No contract; limited license
►Please
make sure that you understand that the information provided here is being provided freely, and that no kind of
agreement or contract is created between you and the owners or users of this site, the owners of the servers upon which it
is housed, the individual Wikipedia contributors, any project administrators, sysops or anyone else who is in any way
connected with this project or sister projects subject to your claims against them directly. You are being granted a limited
license to copy anything from this site; it does not create or imply any contractual or extracontractual liability on the part
of Wikipedia or any of its agents, members, organizers or other users.
►There is no agreement or understanding between you and the content provider regarding your use or modification
of this information beyond the Creative Commons Attribution-Sharealike 3.0 Unported License (CC-BY-SA) and the
GNU Free Documentation License (GFDL);
General Disclaimer
Department of Electrical Engineering,
National Taiwan Ocean University
www.ee.ntou.edu.tw
Trademarks
►Any
of the trademarks, service marks, collective marks, design rights or similar rights that are mentioned, used or cited
in the lectures are the property of their respective owners. Their use here does not imply that you may use them for any
purpose other than for the same or a similar informational use as contemplated by the original authors under the CC-BYSA and GFDL licensing schemes. Unless otherwise stated , we are neither endorsed by nor affiliated with any of the
holders of any such rights and as such we cannot grant any rights to use any otherwise protected materials. Your use of any
such or similar incorporeal property is at your own risk.
Personality rights
►The
lecture may portray an identifiable person who is alive or deceased recently. The use of images of living or recently
deceased individuals is, in some jurisdictions, restricted by laws pertaining to personality rights, independent from their
copyright status. Before using these types of content, please ensure that you have the right to use it under the laws which
apply in the circumstances of your intended use. You are solely responsible for ensuring that you do not infringe someone
else's personality rights.