I 2 C Bus Department of Electrical Engineering
I 2 C Bus Department of Electrical Engineering, National Taiwan Ocean University 5/16/2013 Richard Kuo Assistant Professor www. ee. ntou. edu. tw
Out. Line Department of Electrical Engineering, National Taiwan Ocean University ► I 2 C Bus Introduction 11. Nu. Micro_I 2 C. ppt Exercise: I 2 C interface with Temperature Sensor IC (Smpl_I 2 C_TMP 100) ► Exercise: I 2 C interface with 3 -axis accelerometer (Smpl_I 2 C_MMA 7445) ► Exercise: I 2 C interface with 3 -axis Gyroscope (Smpl_I 2 C_L 3 G 4200) ► Exercise: I 2 C interface with 3 -axis Accelero + Gyro (Smpl_I 2 C_MPU 6050) ► www. ee. ntou. edu. tw
Nu. Micro MCU Learning Board Department of Electrical Engineering, National Taiwan Ocean University I 2 C signals ► I 2 C 0 : – I 2 C 0_SCL : GPA 9 – I 2 C 0_SDA : GPA 8 ► I 2 C 1 : – I 2 C 1_SCL : GPA 11 – I 2 C 1_SDA : GPA 10 www. ee. ntou. edu. tw Nu-LB-NUC 140
I 2 C Schematic Department of Electrical Engineering, National Taiwan Ocean University www. ee. ntou. edu. tw
I 2 C 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. ►
I 2 C controller Department of Electrical Engineering, National Taiwan Ocean University ► ► ► ► www. ee. ntou. edu. tw Up to two I 2 C controllers. Compatible with Philips I 2 C standard. Support Master/Slave mode Support 7 bit addressing mode Built-in a 14 -bit time-out counter to avoid the I 2 C 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
I 2 C protocol Department of Electrical Engineering, National Taiwan Ocean University ► Standard I 2 C protocol contain the following 4 sections : – – START or Repeated START signal generation Slave address transfer Data transfer STOP signal generation www. ee. ntou. edu. tw
START, Repeated START, STOP Department of Electrical Engineering, National Taiwan Ocean University Ø Ø Ø www. ee. ntou. edu. tw When the bus is free, I 2 C 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
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 Mater transmits a 7 -bit Slave address and 1 -bit R/W
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 Data transfer stage (example 1 byte data)
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 9 th clock Acknowledge Position NACK: High level ACK: Low level
I 2 C Functional Registers Department of Electrical Engineering, National Taiwan Ocean University ► ► ► ► I 2 CON : I 2 C Control Register I 2 DAT : I 2 C Data Register I 2 STATUS : I 2 C Status Register I 2 CLK : I 2 C Clock Rate Register I 2 ADDRx, x=0~3 : I 2 C Address Register I 2 ADRMx, x=0~3 : I 2 C Address Mask Register I 2 TOC : I 2 C Time-out Counter Register www. ee. ntou. edu. tw
I 2 CON : I 2 C 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 Assert ACK control bit When I 2 C 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 I 2 C hardware enable I 2 C interrupt enable
I 2 C Control Register用途 Department of Electrical Engineering, National Taiwan Ocean University S Slave Address /W A DATA 0 www. ee. ntou. edu. tw A DATA 1 A P SI flag is set by hardware 7 -bit slave address with a write bit DATA 0 DATA 1 [0, 0, 1, 0] [1, 0, 0, x] [0, 0, 1, 0] To set the [STA, STO, SI, AA] for above list conditions. [0, 1, 1, 0]
I 2 C Status Register Department of Electrical Engineering, National Taiwan Ocean University www. ee. ntou. edu. tw 26 possible status codes. ► All I 2 C status codes correspond to defined I 2 C states. – Ex: When a START has been transmitted, the status code is 08 H. ►
TMP 100 I 2 C Timing Diagram for Write Word Department of Electrical Engineering, National Taiwan Ocean University www. ee. ntou. edu. tw Temperature Sensor
TMP 100 I 2 C Timing Diagram for Read Word Department of Electrical Engineering, National Taiwan Ocean University www. ee. ntou. edu. tw
Smpl_I 2 C_TMP 100 Department of Electrical Engineering, National Taiwan Ocean University www. ee. ntou. edu. tw
Smpl_I 2 C_TMP 100 Department of Electrical Engineering, National Taiwan Ocean University // Smpl_I 2 C_TMP 100 : Temperature Sensor IC // // TMP 100 pin connections // pin 1 SCL to I 2 C 1_SCL / GP 11 // pin 2 GND to GND // pin 3 ADD 1 to GND // pin 4 VDD to Vcc +5 V // pin 5 ADD 0 to GND // pin 6 SDA to I 2 C 1_SDA / GPA 10 #include <stdio. h> #include <stdint. h> #include "Drv. SYS. h" #include "Drv. I 2 C. h" #include "Drv. GPIO. h" #include "NUC 1 xx-LB_002LCD_Driver. h" www. ee. ntou. edu. tw
Initial_I 2 C() Department of Electrical Engineering, National Taiwan Ocean University www. ee. ntou. edu. tw // Initial I 2 C void Initial_I 2 C(E_I 2 C_PORT port, uint 32_t u 32 Bus. Clock) { //Set I 2 C I/O if(port==I 2 C_PORT 0) Drv. GPIO_Init. Function(E_FUNC_I 2 C 0); else if(port==I 2 C_PORT 1) Drv. GPIO_Init. Function(E_FUNC_I 2 C 1); // } System. Core. Clock = Drv. SYS_Get. HCLKFreq(); //Open I 2 C, and set clock = u 32 Bus. Clock bps Drv. I 2 C_Open(port, u 32 Bus. Clock); I 2 C 1 ->I 2 CTOC. DIV 4 = 1; I 2 C 1 ->I 2 CTOC. TIF = 1; //Clear TIF I 2 C 1 ->I 2 CTOC. ENTI = 1; //Enable I 2 C bus timeout count(Move to before I 2 C start)
I 2 C_Write_Data Department of Electrical Engineering, National Taiwan Ocean University www. ee. ntou. edu. tw uint 8_t I 2 C_Write_Data(uint 8_t I 2 C_Address, uint 8_t *I 2 C_Data, uint 16_t I 2 C_Data_Length) { uint 32_t i; //Send I 2 C Start Drv. I 2 C_Ctrl(I 2 C_PORT 1, 1, 0); //Set STA & Clear SI flag while(!I 2 C 1 ->I 2 CON. SI); //Waitting SI flag //Send I 2 C Address & R/W Command I 2 C 1 ->I 2 CDAT = (I 2 C_Address<<1) | 0 x 00; //Load I 2 C Address and Command Drv. I 2 C_Ctrl(I 2 C_PORT 1, 0, 0, 1, 1); //Clear SI flag & Set AA while(!I 2 C 1 ->I 2 CON. SI); //Waitting SI flag //Send I 2 C Data for(i=0; i<I 2 C_Data_Length; i++) { I 2 C 1 ->I 2 CDAT = I 2 C_Data[i]; //Load I 2 C Data Drv. I 2 C_Ctrl(I 2 C_PORT 1, 0, 0, 1, 1); //Clear SI flag & Set AA while(!I 2 C 1 ->I 2 CON. SI); //Waitting SI flag } //Send I 2 C Stop Drv. I 2 C_Ctrl(I 2 C_PORT 1, 0, 1, 1, 0); //Set STOP while(I 2 C 1 ->I 2 CON. SI!=0); //Waitting SI flag clear return 0; }
I 2 C_Read_Data Department of Electrical Engineering, National Taiwan Ocean University www. ee. ntou. edu. tw uint 8_t I 2 C_Read_Data(uint 8_t I 2 C_Address, uint 8_t *I 2 C_Data, uint 16_t I 2 C_Data_Length) { uint 32_t i; //Send I 2 C Start Drv. I 2 C_Ctrl(I 2 C_PORT 1, 1, 0); while(!I 2 C 1 ->I 2 CON. SI); //Set STA & Clear SI flag //Waitting SI flag //Send I 2 C Address & R/W Command I 2 C 1 ->I 2 CDAT = (I 2 C_Address<<1) | 0 x 01; //Load I 2 C Address and Command Drv. I 2 C_Ctrl(I 2 C_PORT 1, 0, 0, 1, 1); //Clear SI flag & Set AA while(!I 2 C 1 ->I 2 CON. SI); //Waitting SI flag //Read I 2 C Data for(i=0; i<I 2 C_Data_Length; i++) { if((I 2 C_Data_Length-i)>1) //I 2 C_Data_Length > 1 bytes { //Read Data Drv. I 2 C_Ctrl(I 2 C_PORT 1, 0, 0, 1, 1); //Clear SI flag & Set AA while(!I 2 C 1 ->I 2 CON. SI); //Waiting SI flag I 2 C_Data[i] = I 2 C 1 ->I 2 CDAT; //Read I 2 C Data }
I 2 C_Read_Data Department of Electrical Engineering, National Taiwan Ocean University www. ee. ntou. edu. tw else //I 2 C_Data_Length = 1 bytes { //Read Data Drv. I 2 C_Ctrl(I 2 C_PORT 1, 0, 0, 1, 0); while(!I 2 C 1 ->I 2 CON. SI); I 2 C_Data[i] = I 2 C 1 ->I 2 CDAT; //Clear SI flag & Set AA //Waitting SI flag //Read I 2 C Data } } //Send I 2 C Stop Drv. I 2 C_Ctrl(I 2 C_PORT 1, 0, 1, 1, 0); while(I 2 C 1 ->I 2 CON. SI!=0); return 0; } //Set STOP //Waiting SI flag clear
Smpl_I 2 C_TMP 100 Department of Electrical Engineering, National Taiwan Ocean University int 32_t main (void) { float Temperature; char TEXT 2[16] = " www. ee. ntou. edu. tw TMP 100_Set_TLow(21. 5 f); while(1) { Temperature = TMP 100_Get_Temperature(); sprintf(TEXT 2, "%f", Temperature); print_lcd(2, TEXT 2); Drv. SYS_Delay(10000); } "; UNLOCKREG(); SYSCLK->PWRCON. XTL 12 M_EN=1; Drv. SYS_Delay(5000); Drv. SYS_Select. HCLKSource(0); LOCKREG(); Initial_I 2 C(I 2 C_PORT 1, 400000); Initial_panel(); clr_all_panel(); print_lcd(0, "Smpl_I 2 C_TMP 100 "); print_lcd(1, "Temperature: "); TMP 100_Set_Configuration(_TMP 100_RESOLUTION_12 BITS); TMP 100_Set_THigh(70. 0 f); }
Smpl_I 2 C_MMA 7455 Department of Electrical Engineering, National Taiwan Ocean University www. ee. ntou. edu. tw
MMA 7455 : 3 -axis Accelerometer Department of Electrical Engineering, National Taiwan Ocean University // Smpl_I 2 C_MMA 7455 : 3 -axis Accelerometer // // CS: 0 = SPI, 1 = I 2 C // MMA 7455 pin connections // pin 1 VCC : to Vcc 5 V // pin 2 NC : N. C. // pin 3 NC : N. C. // pin 4 INT 1 : N. C. // pin 5 INT 2 : N. C. // pin 6 GND : to Gnd // pin 7 GND : to Gnd // pin 8 CS : to Vcc 5 V // pin 9 SDO : N. C. // pin 10 SDA/SDI : to I 2 C 0_SDA (GPA 8) // pin 11 SCL : to I 2 C 1_SCL (GPA 9) // pin 12 VCC : to Vcc 5 V www. ee. ntou. edu. tw
I 2 CWrite_MMA 7455 Department of Electrical Engineering, National Taiwan Ocean University #define I 2 C_ADDR 0 x 3 A void I 2 CWrite_MMA 7455(uint 8_t INDEX, uint 8_t DATA) { //send i 2 c start Drv. I 2 C_Ctrl(I 2 C_PORT 0, 1, 0); //set start while (I 2 C 0 ->I 2 CON. SI == 0); //poll si flag //send to Write port I 2 C 0 ->I 2 CDAT = I 2 C_ADDR; //send writer command Drv. I 2 C_Ctrl(I 2 C_PORT 0, 0, 0, 1, 0); //clr si flag while( I 2 C 0 ->I 2 CON. SI == 0 ); //poll si flag //send write address I 2 C 0 ->I 2 CDAT = INDEX; Drv. I 2 C_Ctrl(I 2 C_PORT 0, 0, 0, 1, 0); //clr si while( I 2 C 0 ->I 2 CON. SI == 0 ); //poll si flag //send write data I 2 C 0 ->I 2 CDAT = DATA; Drv. I 2 C_Ctrl(I 2 C_PORT 0, 0, 0, 1, 0); //clr si while( I 2 C 0 ->I 2 CON. SI == 0 ); //poll si flag www. ee. ntou. edu. tw
I 2 CWrite_MMA 7455 Department of Electrical Engineering, National Taiwan Ocean University // } //send i 2 c stop Drv. I 2 C_Ctrl(I 2 C_PORT 0, 0, 1, 1, 0); //send stop while( I 2 C 0 ->I 2 CON. STO); while( I 2 C 0 ->CON. SI == 0 ); www. ee. ntou. edu. tw
I 2 CRead_MMA 7455 Department of Electrical Engineering, National Taiwan Ocean University uint 8_t I 2 CRead_MMA 7455(uint 8_t INDEX) { uint 8_t TEMP; //send i 2 c start Drv. I 2 C_Ctrl(I 2 C_PORT 0, 1, 0); while (I 2 C 0 ->I 2 CON. SI == 0); //send to Write port I 2 C 0 ->I 2 CDAT = I 2 C_ADDR; Drv. I 2 C_Ctrl(I 2 C_PORT 0, 0, 0, 1, 0); while( I 2 C 0 ->I 2 CON. SI == 0 ); //send INDEX I 2 C 0 ->I 2 CDAT = INDEX; Drv. I 2 C_Ctrl(I 2 C_PORT 0, 0, 0, 1, 0); while( I 2 C 0 ->I 2 CON. SI == 0 ); //set start //poll si flag //clr si //poll si flag www. ee. ntou. edu. tw
I 2 CRead_MMA 7455 Department of Electrical Engineering, National Taiwan Ocean University //send i 2 c start Drv. I 2 C_Ctrl(I 2 C_PORT 0, 1, 0); while (I 2 C 0 ->I 2 CON. SI == 0); //send to Read port I 2 C 0 ->I 2 CDAT = (I 2 C_ADDR+1); Drv. I 2 C_Ctrl(I 2 C_PORT 0, 0, 0, 1, 0); while( I 2 C 0 ->I 2 CON. SI == 0 ); //receive data I 2 C 0 ->I 2 CDAT = 0 x. FF; Drv. I 2 C_Ctrl(I 2 C_PORT 0, 0, 0, 1, 0); while( I 2 C 0 ->I 2 CON. SI == 0 ); TEMP = I 2 C 0 ->I 2 CDAT; //send i 2 c stop Drv. I 2 C_Ctrl(I 2 C_PORT 0, 0, 1, 1, 0); while( I 2 C 0 ->I 2 CON. STO); Drv. I 2 C_Close(I 2 C_PORT 0); return TEMP; } //set start //poll si flag //clr si and set stop www. ee. ntou. edu. tw
Smpl_I 2 C_MMA 7455 Department of Electrical Engineering, National Taiwan Ocean University int 32_t main (void) { uint 8_t tmp. X, tmp. Y, tmp. Z; char TEXT 1[16] = "X = char TEXT 2[16] = "Y = char TEXT 3[16] = "Z = "; "; "; UNLOCKREG(); SYSCLK->PWRCON. XTL 12 M_EN=1; Drv. SYS_Delay(5000); Drv. SYS_Select. HCLKSource(0); LOCKREG(); Initial_panel(); clr_all_panel(); print_lcd(0, "Smpl_I 2 C_MMA 7455"); Initial_I 2 C(I 2 C_PORT 0, 200000); I 2 CWrite_MMA 7455(0 x 16, 0 x 05); // Waiting for 12 M Xtal stalble //Initialize LCD pannel //Initialize I 2 C 0 to 50 Kbps // 0 x 16 set to 0 x 05 for +2 g mode www. ee. ntou. edu. tw
Smpl_I 2 C_MMA 7455 Department of Electrical Engineering, National Taiwan Ocean University while(1) { tmp. X = I 2 CRead_MMA 7455(0 x 06); // read X value sprintf(TEXT 1+4, "%d", tmp. X); tmp. Y = I 2 CRead_MMA 7455(0 x 07); // read Y value sprintf(TEXT 2+4, "%d", tmp. Y); tmp. Z = I 2 CRead_MMA 7455(0 x 08); // read Z value sprintf(TEXT 3+4, "%d", tmp. Z); print_lcd(1, TEXT 1); print_lcd(2, TEXT 2); print_lcd(3, TEXT 3); } } www. ee. ntou. edu. tw
Smpl_I 2 C_L 3 G 4200 D Department of Electrical Engineering, National Taiwan Ocean University www. ee. ntou. edu. tw
L 3 G 4200 D : 3 -axis Gyroscope Department of Electrical Engineering, National Taiwan Ocean University // Smpl_I 2 C_L 3 G 4200 : 3 -axis Gyroscope // // CS: 0 = SPI, 1 = I 2 C // L 3 G 4200 D pin connections // pin 1 SDO N. C. // pin 2 CS to Vcc +5 V // pin 3 DR N. C. // pin 4 INT N. C. // pin 5 GND to Gnd // pin 6 SDA to I 2 C 0_SDA (GPA 8) // pin 7 SCL to I 2 C 0_SCL (GPA 9) // pin 8 Vcc to Vcc 5 V // I 2 C_Addr = SAD[6: 1] + SDO + R/W // when SDO no connection // I 2 C slave ADDR = 0 x. D 2 (Write) // I 2 C slave ADDR = 0 x. D 3 (Read) www. ee. ntou. edu. tw
I 2 CWrite_L 3 G 4200 D Department of Electrical Engineering, National Taiwan Ocean University void I 2 CWrite_L 3 G 4200 D(uint 8_t INDEX, uint 8_t DATA) { //send i 2 c start Drv. I 2 C_Ctrl(I 2 C_PORT 0, 1, 0); //set start while (I 2 C 0 ->I 2 CON. SI == 0); //poll si flag //send to Write port I 2 C 0 ->I 2 CDAT = I 2 C_ADDR; //send writer command Drv. I 2 C_Ctrl(I 2 C_PORT 0, 0, 0, 1, 0); / /clr si flag while( I 2 C 0 ->I 2 CON. SI == 0 ); //poll si flag //send write address I 2 C 0 ->I 2 CDAT = INDEX; Drv. I 2 C_Ctrl(I 2 C_PORT 0, 0, 0, 1, 0); //clr si while( I 2 C 0 ->I 2 CON. SI == 0 ); //poll si flag //send write data I 2 C 0 ->I 2 CDAT = DATA; Drv. I 2 C_Ctrl(I 2 C_PORT 0, 0, 0, 1, 0); //clr si while( I 2 C 0 ->I 2 CON. SI == 0 ); //poll si flag //send i 2 c stop Drv. I 2 C_Ctrl(I 2 C_PORT 0, 0, 1, 1, 0); //send stop while( I 2 C 0 ->I 2 CON. STO); //while( I 2 C 0 ->CON. SI == 0 ); Drv. I 2 C_Close(I 2 C_PORT 0); } www. ee. ntou. edu. tw
I 2 CRead_L 3 G 4200 D Department of Electrical Engineering, National Taiwan Ocean University uint 8_t I 2 CRead_L 3 G 4200 D(uint 8_t INDEX) { uint 8_t TEMP; //send i 2 c start Drv. I 2 C_Ctrl(I 2 C_PORT 0, 1, 0); while (I 2 C 0 ->I 2 CON. SI == 0); //send to Write port I 2 C 0 ->I 2 CDAT = I 2 C_ADDR; Drv. I 2 C_Ctrl(I 2 C_PORT 0, 0, 0, 1, 0); while( I 2 C 0 ->I 2 CON. SI == 0 ); //send INDEX I 2 C 0 ->I 2 CDAT = INDEX; Drv. I 2 C_Ctrl(I 2 C_PORT 0, 0, 0, 1, 0); while( I 2 C 0 ->I 2 CON. SI == 0 ); //set start //poll si flag //clr si //poll si flag www. ee. ntou. edu. tw
I 2 CRead_L 3 G 4200 D Department of Electrical Engineering, National Taiwan Ocean University //send i 2 c start Drv. I 2 C_Ctrl(I 2 C_PORT 0, 1, 0); while (I 2 C 0 ->I 2 CON. SI == 0); //send to Read port I 2 C 0 ->I 2 CDAT = (I 2 C_ADDR+1); Drv. I 2 C_Ctrl(I 2 C_PORT 0, 0, 0, 1, 0); while( I 2 C 0 ->I 2 CON. SI == 0 ); //receive data I 2 C 0 ->I 2 CDAT = 0 x. FF; Drv. I 2 C_Ctrl(I 2 C_PORT 0, 0, 0, 1, 0); while( I 2 C 0 ->I 2 CON. SI == 0 ); TEMP = I 2 C 0 ->I 2 CDAT; //send i 2 c stop Drv. I 2 C_Ctrl(I 2 C_PORT 0, 0, 1, 1, 0); while( I 2 C 0 ->I 2 CON. STO); Drv. I 2 C_Close(I 2 C_PORT 0); return TEMP; } //set start //poll si flag //clr si and set stop www. ee. ntou. edu. tw
Smpl_I 2 C_L 3 G 4200 D Department of Electrical Engineering, National Taiwan Ocean University int 32_t main (void) { char TEXT 1[16] = "X = "; char TEXT 2[16] = "Y = "; char TEXT 3[16] = "Z = "; uint 8_t tmp. X_L, tmp. X_H; uint 8_t tmp. Y_L, tmp. Y_H; uint 8_t tmp. Z_L, tmp. Z_H; float temp; int tmp; UNLOCKREG(); SYSCLK->PWRCON. XTL 12 M_EN=1; Drv. SYS_Delay(5000); Drv. SYS_Select. HCLKSource(0); LOCKREG(); // Waiting for 12 M Xtal stalble Initial_panel(); clr_all_panel(); print_lcd(0, "Smpl_I 2 C_L 3 G 4200"); // Initialize LCD pannel Initial_I 2 C(I 2 C_PORT 0, 400000); Init_L 3 G 4200 D(); // Initialize I 2 C // Initialize L 3 G 4200 D www. ee. ntou. edu. tw
Smpl_I 2 C_L 3 G 4200 D Department of Electrical Engineering, National Taiwan Ocean University while(1) { tmp. X_L = I 2 CRead_L 3 G 4200 D(OUT_X_L); // read X_Low value tmp. X_H = I 2 CRead_L 3 G 4200 D(OUT_X_H); // read X_High value tmp = (tmp. X_H<<8)+tmp. X_L; temp = (float)tmp * 0. 07; sprintf(TEXT 1+4, "%f", temp); tmp. Y_L = I 2 CRead_L 3 G 4200 D(OUT_Y_L); // read Y_Low value tmp. Y_H = I 2 CRead_L 3 G 4200 D(OUT_Y_H); // read Y_High value; tmp = (tmp. Y_H<<8)+tmp. Y_L; temp = (float)tmp * 0. 07; sprintf(TEXT 2+4, "%f", temp); tmp. Z_L = I 2 CRead_L 3 G 4200 D(OUT_Z_L); // read Y_Low value tmp. Z_H = I 2 CRead_L 3 G 4200 D(OUT_Z_H); // read Y_High value tmp = (tmp. Z_H<<8)+tmp. Z_L; temp = (float)tmp * 0. 07; sprintf(TEXT 3+4, "%f", temp); print_lcd(1, TEXT 1); print_lcd(2, TEXT 2); print_lcd(3, TEXT 3); } } www. ee. ntou. edu. tw
Smpl_I 2 C_MPU 6050 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_I 2 C_MPU 6050 // // MPU 6050 : 3 -axis Gyroscope + 3 -axis accelerometer + temperature // Interface: I 2 C // pin 1: Vcc to Vcc (+5 V) // pin 2: Gnd to Gnd // pin 3: SCL to I 2 C 0_SCL/GPA 9 // pin 4: SDA to I 2 C 0_SDA/GPA 8 // pin 5: XDA -- N. C. // pin 6: XCL -- N. C. // pin 7: AD 0 -- N. C. // pin 8: INT -- N. C. www. ee. ntou. edu. tw
I 2 CWrite_MPU 6050 Department of Electrical Engineering, National Taiwan Ocean University void I 2 CWrite_MPU 6050(uint 8_t INDEX, uint 8_t DATA) { //send i 2 c start Drv. I 2 C_Ctrl(I 2 C_PORT 0, 1, 0); while (I 2 C 0 ->I 2 CON. SI == 0); //send to Write port I 2 C 0 ->I 2 CDAT = I 2 C_ADDR; Drv. I 2 C_Ctrl(I 2 C_PORT 0, 0, 0, 1, 0); while( I 2 C 0 ->I 2 CON. SI == 0 ); //send write address I 2 C 0 ->I 2 CDAT = INDEX; Drv. I 2 C_Ctrl(I 2 C_PORT 0, 0, 0, 1, 0); while( I 2 C 0 ->I 2 CON. SI == 0 ); //send write data I 2 C 0 ->I 2 CDAT = DATA; Drv. I 2 C_Ctrl(I 2 C_PORT 0, 0, 0, 1, 0); while( I 2 C 0 ->I 2 CON. SI == 0 ); //send i 2 c stop Drv. I 2 C_Ctrl(I 2 C_PORT 0, 0, 1, 1, 0); while( I 2 C 0 ->I 2 CON. STO); // while( I 2 C 0 ->CON. SI == 0 ); } //set start //poll si flag //send writer command //clr si flag //poll si flag //clr si //poll si flag //send stop www. ee. ntou. edu. tw
I 2 CRead_MPU 6050 Department of Electrical Engineering, National Taiwan Ocean University uint 8_t I 2 CRead_MPU 6050(uint 8_t INDEX) { uint 8_t TEMP; //send i 2 c start Drv. I 2 C_Ctrl(I 2 C_PORT 0, 1, 0); while (I 2 C 0 ->I 2 CON. SI == 0); //send to Write port I 2 C 0 ->I 2 CDAT = I 2 C_ADDR; Drv. I 2 C_Ctrl(I 2 C_PORT 0, 0, 0, 1, 0); while( I 2 C 0 ->I 2 CON. SI == 0 ); //send INDEX I 2 C 0 ->I 2 CDAT = INDEX; Drv. I 2 C_Ctrl(I 2 C_PORT 0, 0, 0, 1, 0); while( I 2 C 0 ->I 2 CON. SI == 0 ); //send i 2 c start Drv. I 2 C_Ctrl(I 2 C_PORT 0, 1, 0); while (I 2 C 0 ->I 2 CON. SI == 0); //send to Read port I 2 C 0 ->I 2 CDAT = (I 2 C_ADDR+1); Drv. I 2 C_Ctrl(I 2 C_PORT 0, 0, 0, 1, 0); while( I 2 C 0 ->I 2 CON. SI == 0 ); //set start //poll si flag //clr si //poll si flag www. ee. ntou. edu. tw
I 2 CRead_MPU 6050 Department of Electrical Engineering, National Taiwan Ocean University //receive data I 2 C 0 ->I 2 CDAT = 0 x. FF; Drv. I 2 C_Ctrl(I 2 C_PORT 0, 0, 0, 1, 0); while( I 2 C 0 ->I 2 CON. SI == 0 ); TEMP = I 2 C 0 ->I 2 CDAT; //send i 2 c stop Drv. I 2 C_Ctrl(I 2 C_PORT 0, 0, 1, 1, 0); while( I 2 C 0 ->I 2 CON. STO); return TEMP; } //clr si //poll si flag //clr si and set stop www. ee. ntou. edu. tw
Smpl_I 2 C_MPU 6050 Department of Electrical Engineering, National Taiwan Ocean University int 32_t main (void) { char TEXT 1[16] = "x: X: "; char TEXT 2[16] = "y: Y: "; char TEXT 3[16] = "z: Z: "; uint 8_t tmp. L, tmp. H; uint 16_t tmp; float temp; int acc. X, acc. Y, acc. Z, gyro. X, gyro. Y, gyro. Z; UNLOCKREG(); SYSCLK->PWRCON. XTL 12 M_EN=1; Drv. SYS_Delay(5000); SYSCLK->CLKSEL 0. HCLK_S=0; LOCKREG(); // Waiting for 12 M Xtal stalble Initial_panel(); clr_all_panel(); print_lcd(0, "Smpl_I 2 C_MPU 6050"); Initial_I 2 C(I 2 C_PORT 0, 200000); // Initialize I 2 C Init_MPU 6050(); // Initialize MPU 6050 www. ee. ntou. edu. tw
Smpl_I 2 C_MPU 6050 Department of Electrical Engineering, National Taiwan Ocean University www. ee. ntou. edu. tw while(1) { tmp. L = I 2 CRead_MPU 6050(ACCEL_XOUT_L); // read Accelerometer X_Low value tmp. H = I 2 CRead_MPU 6050(ACCEL_XOUT_H); // read Accelerometer X_High value tmp = (tmp. H<<8)+tmp. L; temp = (float)tmp; acc. X = (int)temp; tmp. L = I 2 CRead_MPU 6050(ACCEL_YOUT_L); // read Accelerometer Y_Low value tmp. H = I 2 CRead_MPU 6050(ACCEL_YOUT_H); // read Accelerometer Y_High value tmp = (tmp. H<<8)+tmp. L; temp = (float)tmp; acc. Y = (int)temp; tmp. L = I 2 CRead_MPU 6050(ACCEL_ZOUT_L); // read Accelerometer Z_Low value tmp. H = I 2 CRead_MPU 6050(ACCEL_ZOUT_H); // read Accelerometer Z_High value tmp = (tmp. H<<8)+tmp. L; temp = (float)tmp; acc. Z = (int)temp;
Smpl_I 2 C_MPU 6050 Department of Electrical Engineering, National Taiwan Ocean University tmp. L = I 2 CRead_MPU 6050(GYRO_XOUT_L); // read Gyroscope X_Low value tmp. H = I 2 CRead_MPU 6050(GYRO_XOUT_H); // read Gyroscope X_High value tmp = (tmp. H<<8)+tmp. L; temp = (float)tmp; gyro. X = (int)temp; tmp. L = I 2 CRead_MPU 6050(GYRO_YOUT_L); // read Gyroscope Y_Low value tmp. H = I 2 CRead_MPU 6050(GYRO_YOUT_H); // read Gyroscope Y_High value tmp = (tmp. H<<8)+tmp. L; temp = (float)tmp; gyro. Y = (int)temp; ; tmp. L = I 2 CRead_MPU 6050(GYRO_ZOUT_L); // read Gyroscope Z_Low value tmp. H = I 2 CRead_MPU 6050(GYRO_ZOUT_H); // read Gyroscope Z_High value tmp = (tmp. H<<8)+tmp. L; temp = (float)tmp; gyro. Z = (int)temp; www. ee. ntou. edu. tw
Smpl_I 2 C_MPU 6050 Department of Electrical Engineering, National Taiwan Ocean University sprintf(TEXT 1, "x: %d X: %d", acc. X, gyro. X); sprintf(TEXT 2, "y: %d Y: %d", acc. Y, gyro. Y); sprintf(TEXT 3, "z: %d Z: %d", acc. Z, gyro. Z); print_lcd(1, TEXT 1); print_lcd(2, TEXT 2); print_lcd(3, TEXT 3); } } 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 (CCBY-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-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.
- Slides: 50