Download presentation
Presentation is loading. Please wait.
Published byFlorence McDonald Modified over 8 years ago
1
www.ee.ntou.edu.tw Department of Electrical Engineering, National Taiwan Ocean University I2C Bus 5/16/2013 Richard Kuo Assistant Professor
2
Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw OutLine ► 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)
3
Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw NuMicro MCU Learning Board I2C signals ► I2C0 : –I2C0_SCL : GPA9 –I2C0_SDA : GPA8 ► I2C1 : –I2C1_SCL : GPA11 –I2C1_SDA : GPA10 Nu-LB-NUC140
4
Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw I2C Schematic
5
Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw I2C Bus Introduction ► 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.
6
Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw I2C controller ► 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
7
Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw I2C protocol ► Standard I2C protocol contain the following 4 sections : –START or Repeated START signal generation –Slave address transfer –Data transfer –STOP signal generation
8
Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw START, Repeated START, STOP 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 Transmission starts with a START condition Transmission ends with a STOP condition
9
Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Slave Address Transfer 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 Mater transmits a 7-bit Slave address and 1-bit R/W
10
Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Data Transfer 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 Data transfer stage (example 1 byte data)
11
Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Acknowledge Each transferred byte is followed by an ACK or a NACK at 9th clock Acknowledge Position NACK: High level ACK: Low level
12
Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw 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
13
Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw I2CON : I2C Control Register 76543210 EIENSISTASTOSIAAReserved 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
14
Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw I2C Control Register 用途 SSlave Address/WADATA0ADATA1AP To set the [STA,STO,SI,AA] for above list conditions. [1,0,0,x] [0,0,1,0] SI flag is set by hardware 7-bit slave address with a write bit DATA0 DATA1 [0,1,1,0]
15
Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw I2C Status Register ► 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.
16
Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw TMP100 I2C Timing Diagram for Write Word Temperature Sensor
17
Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw TMP100 I2C Timing Diagram for Read Word
18
Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Smpl_I2C_TMP100
19
Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Smpl_I2C_TMP100 // Smpl_I2C_TMP100 : Temperature Sensor IC // // TMP100 pin connections // pin1 SCL to I2C1_SCL / GP11 // pin2GND to GND // pin3 ADD1 to GND // pin4VDD to Vcc +5V // pin5ADD0 to GND // pin6SDA to I2C1_SDA / GPA10 #include #include "DrvSYS.h" #include "DrvI2C.h" #include "DrvGPIO.h" #include "NUC1xx-LB_002\LCD_Driver.h"
20
Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Initial_I2C() // 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) }
21
Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw I2C_Write_Data 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; }
22
Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw I2C_Read_Data 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);//Set STA & Clear SI flag while(!I2C1->I2CON.SI);//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 }
23
Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw I2C_Read_Data else //I2C_Data_Length = 1 bytes { //Read Data DrvI2C_Ctrl(I2C_PORT1, 0, 0, 1, 0);//Clear SI flag & Set AA while(!I2C1->I2CON.SI);//Waitting SI flag I2C_Data[i] = I2C1->I2CDAT;//Read I2C Data } //Send I2C Stop DrvI2C_Ctrl(I2C_PORT1, 0, 1, 1, 0);//Set STOP while(I2C1->I2CON.SI!=0);//Waiting SI flag clear return 0; }
24
Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Smpl_I2C_TMP100 int32_t main (void) { float Temperature; char TEXT2[16] = " "; 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); TMP100_Set_TLow(21.5f); while(1) { Temperature = TMP100_Get_Temperature(); sprintf(TEXT2,"%f", Temperature); print_lcd(2,TEXT2); DrvSYS_Delay(10000); }
25
Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Smpl_I2C_MMA7455
26
Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw MMA7455 : 3-axis Accelerometer // 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
27
Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw I2CWrite_MMA7455 #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
28
Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw I2CWrite_MMA7455 //send i2c stop DrvI2C_Ctrl(I2C_PORT0, 0, 1, 1, 0); //send stop while( I2C0->I2CON.STO); //while( I2C0->CON.SI == 0 ); }
29
Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw I2CRead_MMA7455 uint8_t I2CRead_MMA7455(uint8_t INDEX) { uint8_t TEMP; //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; DrvI2C_Ctrl(I2C_PORT0, 0, 0, 1, 0); //clr si while( I2C0->I2CON.SI == 0 ); //poll si flag //send INDEX I2C0->I2CDAT = INDEX; DrvI2C_Ctrl(I2C_PORT0, 0, 0, 1, 0); //clr si while( I2C0->I2CON.SI == 0 ); //poll si flag
30
Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw I2CRead_MMA7455 //send i2c start DrvI2C_Ctrl(I2C_PORT0, 1, 0, 1, 0);//set start while (I2C0->I2CON.SI == 0);//poll si flag //send to Read port I2C0->I2CDAT = (I2C_ADDR+1); DrvI2C_Ctrl(I2C_PORT0, 0, 0, 1, 0);//clr si while( I2C0->I2CON.SI == 0 );//poll si flag //receive data I2C0->I2CDAT = 0xFF; DrvI2C_Ctrl(I2C_PORT0, 0, 0, 1, 0); //clr si while( I2C0->I2CON.SI == 0 );//poll si flag TEMP = I2C0->I2CDAT; //send i2c stop DrvI2C_Ctrl(I2C_PORT0, 0, 1, 1, 0); //clr si and set stop while( I2C0->I2CON.STO); DrvI2C_Close(I2C_PORT0); return TEMP; }
31
Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Smpl_I2C_MMA7455 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);// Waiting for 12M Xtal stalble DrvSYS_SelectHCLKSource(0); LOCKREG(); Initial_panel();//Initialize LCD pannel clr_all_panel(); print_lcd(0,"Smpl_I2C_MMA7455"); Initial_I2C(I2C_PORT0, 200000);//Initialize I2C0 to 50Kbps I2CWrite_MMA7455(0x16, 0x05); // 0x16 set to 0x05 for +2g mode
32
Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Smpl_I2C_MMA7455 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); }
33
Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Smpl_I2C_L3G4200D
34
Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw L3G4200D : 3-axis Gyroscope // 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)
35
Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw I2CWrite_L3G4200D void I2CWrite_L3G4200D(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 //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); }
36
Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw I2CRead_L3G4200D uint8_t I2CRead_L3G4200D(uint8_t INDEX) { uint8_t TEMP; //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; DrvI2C_Ctrl(I2C_PORT0, 0, 0, 1, 0); //clr si while( I2C0->I2CON.SI == 0 ); //poll si flag //send INDEX I2C0->I2CDAT = INDEX; DrvI2C_Ctrl(I2C_PORT0, 0, 0, 1, 0); //clr si while( I2C0->I2CON.SI == 0 ); //poll si flag
37
Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw I2CRead_L3G4200D //send i2c start DrvI2C_Ctrl(I2C_PORT0, 1, 0, 1, 0); //set start while (I2C0->I2CON.SI == 0); //poll si flag //send to Read port I2C0->I2CDAT = (I2C_ADDR+1); DrvI2C_Ctrl(I2C_PORT0, 0, 0, 1, 0); //clr si while( I2C0->I2CON.SI == 0 ); //poll si flag //receive data I2C0->I2CDAT = 0xFF; DrvI2C_Ctrl(I2C_PORT0, 0, 0, 1, 0); //clr si while( I2C0->I2CON.SI == 0 );//poll si flag TEMP = I2C0->I2CDAT; //send i2c stop DrvI2C_Ctrl(I2C_PORT0, 0, 1, 1, 0); //clr si and set stop while( I2C0->I2CON.STO); DrvI2C_Close(I2C_PORT0); return TEMP; }
38
Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Smpl_I2C_L3G4200D 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);// Waiting for 12M Xtal stalble DrvSYS_SelectHCLKSource(0); LOCKREG(); Initial_panel();// Initialize LCD pannel clr_all_panel(); print_lcd(0,"Smpl_I2C_L3G4200"); Initial_I2C(I2C_PORT0, 400000); // Initialize I2C Init_L3G4200D();// Initialize L3G4200D
39
Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Smpl_I2C_L3G4200D 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); }
40
Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Smpl_I2C_MPU6050
41
Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw MPU-6050 : Accelero + Gyroscope // // 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.
42
Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw I2CWrite_MPU6050 void I2CWrite_MPU6050(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 //send i2c stop DrvI2C_Ctrl(I2C_PORT0, 0, 1, 1, 0); //send stop while( I2C0->I2CON.STO); //while( I2C0->CON.SI == 0 ); }
43
Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw I2CRead_MPU6050 uint8_t I2CRead_MPU6050(uint8_t INDEX) { uint8_t TEMP; //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; DrvI2C_Ctrl(I2C_PORT0, 0, 0, 1, 0);//clr si while( I2C0->I2CON.SI == 0 );//poll si flag //send INDEX I2C0->I2CDAT = INDEX; DrvI2C_Ctrl(I2C_PORT0, 0, 0, 1, 0);//clr si while( I2C0->I2CON.SI == 0 );//poll si flag //send i2c start DrvI2C_Ctrl(I2C_PORT0, 1, 0, 1, 0); //set start while (I2C0->I2CON.SI == 0);//poll si flag //send to Read port I2C0->I2CDAT = (I2C_ADDR+1); DrvI2C_Ctrl(I2C_PORT0, 0, 0, 1, 0);//clr si while( I2C0->I2CON.SI == 0 );//poll si flag
44
Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw I2CRead_MPU6050 //receive data I2C0->I2CDAT = 0xFF; DrvI2C_Ctrl(I2C_PORT0, 0, 0, 1, 0); //clr si while( I2C0->I2CON.SI == 0 );//poll si flag TEMP = I2C0->I2CDAT; //send i2c stop DrvI2C_Ctrl(I2C_PORT0, 0, 1, 1, 0); //clr si and set stop while( I2C0->I2CON.STO); return TEMP; }
45
Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Smpl_I2C_MPU6050 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);// Waiting for 12M Xtal stalble SYSCLK->CLKSEL0.HCLK_S=0; LOCKREG(); Initial_panel(); clr_all_panel(); print_lcd(0,"Smpl_I2C_MPU6050"); Initial_I2C(I2C_PORT0, 200000); // Initialize I2C Init_MPU6050(); // Initialize MPU6050
46
Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Smpl_I2C_MPU6050 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;
47
Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Smpl_I2C_MPU6050 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;
48
Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Smpl_I2C_MPU6050 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); }
49
Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw General Disclaimer 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.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.peer reviewfeatured article 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);Creative Commons Attribution-Sharealike 3.0 Unported License GNU Free Documentation License
50
Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw General Disclaimer 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-BY- SA 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.personality rights
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.