Www.ee.ntou.edu.tw Department of Electrical Engineering, National Taiwan Ocean University I2C Bus 5/16/2013 Richard Kuo Assistant Professor.

Slides:



Advertisements
Similar presentations
I2C bus Inter Integrated Circuits bus by Philips Semiconductors
Advertisements

INPUT-OUTPUT ORGANIZATION
Dr. Rabie A. Ramadan Al-Azhar University Lecture 3
PROGRAMMABLE PERIPHERAL INTERFACE -8255
MSP430G2553 launchpad and MPU6050 Introduction
Interrupt Controller Introduction to 8259.
IMPORTANT READ CAREFULLY BEFORE USING THIS PRODUCT LICENSE AGREEMENT AND LIMITED WARRANTY BY INSTALLING OR USING THE SOFTWARE, FILES OR OTHER ELECTRONIC.
Embedded Systems I2CI2C. Feature 3 wire GND SCL(clock) SDA(data) All devices share the same bus wire Using wire and, each device gain access to bus (become.
SPI on the ATmega SS’ line only used in Slave mode SPDR
Advanced Operating Systems The I 2 C Bus. Inter-Integrated Circuit Bus Designed for low-cost, medium data rate applications. Characteristics: –Synchronous;
Two Wire Interface Another popular serial peripheral interface bus -More flexible than SPI -Master and slave modes supported -7-bit slave address -400khz.
Lecture 27: LM3S9B96 Microcontroller – Inter- Integrated Circuit (I 2 C) Interface.
INPUT-OUTPUT ORGANIZATION
Serial Peripheral Interface (SPI) Bus. SPI Bus There is no official specification for the SPI bus. It is necessary to consult the data sheets of the devices.
Department of Electrical Engineering, National Taiwan Ocean University NuMicro Learning Board Introduction 1/29/2015 Richard Kuo Assistant.
LSU 10/22/2004Serial I/O1 Programming Unit, Lecture 5.
Embedded Bus – i2c David E. Culler Lab 2 Feb 2, 2015
I2C UNIT 23 로봇 SW 교육원 조용수. 학습 목표 I2C I2C Protocol 구조 N051 I2C 동작 I2C Register 2.
2/26/2015 Richard Kuo Assistant Professor
7/23 Inter-chip Serial Communication: SPI and I 2 C Computer Science & Engineering Department Arizona State University Tempe, AZ Dr. Yann-Hang Lee.
Universal Asynchronous RX/TX
Department of Electrical Engineering, National Taiwan Ocean University NuMicro MCU Learning Board SDK Installation 3/7/2013 Richard.
MICROPROCESSOR INPUT/OUTPUT
Department of Electrical Engineering, National Taiwan Ocean University Pulse Width Modulation 4/25/2013 Richard Kuo Assistant Professor.
Department of Electrical Engineering, National Taiwan Ocean University Motor Control 5/2/2013 Richard Kuo Assistant Professor.
Department of Electrical Engineering, National Taiwan Ocean University C Programming 3/14/2013 Richard Kuo Assistant Professor.
Department of Electrical Engineering, National Taiwan Ocean University Analog-to-Digital Converter (ADC) 4/18/2013 Richard Kuo Assistant.
Department of Electrical Engineering, National Taiwan Ocean University LCD Display 3/28/2013 Richard Kuo Assistant Professor.
UART: Universal Asynchronous RX/TX
DEVICES AND COMMUNICATION BUSES FOR DEVICES NETWORK
Cortex-M0 MCU Interface and Driver Design - Course Overview
Department of Electrical Engineering, National Taiwan Ocean University Basic Electronics 3/7/2013 Richard Kuo Assistant Professor.
3/17/2014 Richard Kuo Assistant Professor
Department of Electrical Engineering, National Taiwan Ocean University ARM Cortex-M0 Introduction 3/1/2012 Richard Kuo Assistant Professor.
Autonomous Helicopter James LydenEE 496Harris Okazaki.
Department of Electrical Engineering, National Taiwan Ocean University Universal Serial Bus & SD card 5/30/2013 Richard Kuo Assistant.
8279 KEYBOARD AND DISPLAY INTERFACING
Department of Electrical Engineering, National Taiwan Ocean University Timer/WatchDog /RTC 4/11/2013 Richard Kuo Assistant Professor.
PCA9557: REMOTE 8-BIT I 2 C AND SMBus LOW- POWER I/O EXPANDER.
Adobe Dreamweaver CS3 Revealed CHAPTER SIX: MANAGING A WEB SERVER AND FILES.
1 Synchronous Serial IO Send a separate clock line with data –SPI (serial peripheral interface) protocol –I 2 C (or I2C) protocol Encode a clock with data.
Serial Peripheral Interface
12/16/  List the elements of 8255A Programmable Peripheral Interface (PPI)  Explain its various operating modes  Develop a simple program to.
Refer to Chapter 15 in the reference book
©2008 R. Gupta, UCSD COSMOS Summer 2008 Peripheral Interfaces Rajesh K. Gupta Computer Science and Engineering University of California, San Diego.
© 2009, Renesas Technology America, Inc., All Rights Reserved 1 Course Introduction  Purpose:  This course provides an overview of the serial communication.
Oracle Fusion Applications 11gR1 ( ) Functional Overview (L2) Manage Inbound Logistics (L3) Manage Supplier Returns.
8279 KEYBOARD AND DISPLAY INTERFACING
PS/2 Mouse/Keyboard Port
Oracle Fusion Applications 11gR1 ( ) Functional Overview (L2) Manage Inbound Logistics (L3) Manage and Disposition Inventory Returns.
S4525A Peripherals & Enhanced FLASH 1 © 1999 Microchip Technology Incorporated. All Rights Reserved. S4525A Peripherals & Enhanced FLASH 1 Peripherals.
Programmable Interrupt Controller (PIC)
Essentials of Communication This simple model requires many guarantees. Sender Receiver Communication Link Data.
General Purpose Input/Output
© 2008, Renesas Technology America, Inc., All Rights Reserved 1 Course Introduction Purpose  This course provides an introduction to the peripheral functions.
Microcontroller basics Embedded systems for mortals.
Department of Electrical Engineering, National Taiwan Ocean University Controller Area Network bus 5/30/2013 Richard Kuo Assistant Professor.
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Oracle Proprietary and Confidential. 1.
Connectivity to bank and sample account structure
UNIT 24 I2C Test 로봇 SW 교육원 조용수.
Serial Peripheral Interface
Outline Analog to digital conversion (ADC) of NuMaker TRIO
CS4101 Introduction to Embedded Systems Lab 11: Sensors
Chapter 11: Inter-Integrated Circuit (I2C) Interface
I2C Synchronous Serial Two wire communications (plus ground)
I2C PROTOCOL SPECIFICATION
Communication Lines Fundamentals.
BJ Furman ME 106 Fundamentals of Mechatronics 15NOV2012
I2C and RTC Chapter 18 Sepehr Naimi
I2C Protocol and RTC Interfacing
Presentation transcript:

Department of Electrical Engineering, National Taiwan Ocean University I2C Bus 5/16/2013 Richard Kuo Assistant Professor

Department of Electrical Engineering, National Taiwan Ocean University 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)

Department of Electrical Engineering, National Taiwan Ocean University NuMicro MCU Learning Board I2C signals ► I2C0 : –I2C0_SCL : GPA9 –I2C0_SDA : GPA8 ► I2C1 : –I2C1_SCL : GPA11 –I2C1_SDA : GPA10 Nu-LB-NUC140

Department of Electrical Engineering, National Taiwan Ocean University I2C Schematic

Department of Electrical Engineering, National Taiwan Ocean University 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.

Department of Electrical Engineering, National Taiwan Ocean University 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

Department of Electrical Engineering, National Taiwan Ocean University I2C protocol ► Standard I2C protocol contain the following 4 sections : –START or Repeated START signal generation –Slave address transfer –Data transfer –STOP signal generation

Department of Electrical Engineering, National Taiwan Ocean University 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

Department of Electrical Engineering, National Taiwan Ocean University 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

Department of Electrical Engineering, National Taiwan Ocean University 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)

Department of Electrical Engineering, National Taiwan Ocean University Acknowledge  Each transferred byte is followed by an ACK or a NACK at 9th clock Acknowledge Position NACK: High level ACK: Low level

Department of Electrical Engineering, National Taiwan Ocean University 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

Department of Electrical Engineering, National Taiwan Ocean University I2CON : I2C Control Register 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

Department of Electrical Engineering, National Taiwan Ocean University 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]

Department of Electrical Engineering, National Taiwan Ocean University 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.

Department of Electrical Engineering, National Taiwan Ocean University TMP100 I2C Timing Diagram for Write Word Temperature Sensor

Department of Electrical Engineering, National Taiwan Ocean University TMP100 I2C Timing Diagram for Read Word

Department of Electrical Engineering, National Taiwan Ocean University Smpl_I2C_TMP100

Department of Electrical Engineering, National Taiwan Ocean University 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"

Department of Electrical Engineering, National Taiwan Ocean University 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) }

Department of Electrical Engineering, National Taiwan Ocean University 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; }

Department of Electrical Engineering, National Taiwan Ocean University 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 }

Department of Electrical Engineering, National Taiwan Ocean University 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; }

Department of Electrical Engineering, National Taiwan Ocean University 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, ); 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); }

Department of Electrical Engineering, National Taiwan Ocean University Smpl_I2C_MMA7455

Department of Electrical Engineering, National Taiwan Ocean University 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

Department of Electrical Engineering, National Taiwan Ocean University 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

Department of Electrical Engineering, National Taiwan Ocean University I2CWrite_MMA7455 //send i2c stop DrvI2C_Ctrl(I2C_PORT0, 0, 1, 1, 0); //send stop while( I2C0->I2CON.STO); //while( I2C0->CON.SI == 0 ); }

Department of Electrical Engineering, National Taiwan Ocean University 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

Department of Electrical Engineering, National Taiwan Ocean University 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; }

Department of Electrical Engineering, National Taiwan Ocean University 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, );//Initialize I2C0 to 50Kbps I2CWrite_MMA7455(0x16, 0x05); // 0x16 set to 0x05 for +2g mode

Department of Electrical Engineering, National Taiwan Ocean University 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); }

Department of Electrical Engineering, National Taiwan Ocean University Smpl_I2C_L3G4200D

Department of Electrical Engineering, National Taiwan Ocean University 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)

Department of Electrical Engineering, National Taiwan Ocean University 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); }

Department of Electrical Engineering, National Taiwan Ocean University 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

Department of Electrical Engineering, National Taiwan Ocean University 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; }

Department of Electrical Engineering, National Taiwan Ocean University 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, ); // Initialize I2C Init_L3G4200D();// Initialize L3G4200D

Department of Electrical Engineering, National Taiwan Ocean University 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); }

Department of Electrical Engineering, National Taiwan Ocean University Smpl_I2C_MPU6050

Department of Electrical Engineering, National Taiwan Ocean University 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.

Department of Electrical Engineering, National Taiwan Ocean University 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 ); }

Department of Electrical Engineering, National Taiwan Ocean University 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

Department of Electrical Engineering, National Taiwan Ocean University 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; }

Department of Electrical Engineering, National Taiwan Ocean University 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, ); // Initialize I2C Init_MPU6050(); // Initialize MPU6050

Department of Electrical Engineering, National Taiwan Ocean University 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;

Department of Electrical Engineering, National Taiwan Ocean University 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;

Department of Electrical Engineering, National Taiwan Ocean University 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); }

Department of Electrical Engineering, National Taiwan Ocean University 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

Department of Electrical Engineering, National Taiwan Ocean University 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