DOC PREVIEW
USC EE 459Lx - I2C

This preview shows page 1-2-3-4 out of 12 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Ming Hsieh Department of Electrical EngineeringEE 459Lx - Embedded Systems Design LaboratoryUsing the I2C Interface on theATmega328P and MC908JL16byAllan G. Weber1 IntroductionThe I2C or I2C (Inter-Integrated Circuit ) interface is a serial bus intended for communication between twoor more integrated circuits on the same PC or prototyping board. Also known as a “two-wire bus”, thisbus is for communication with many different types of IC’s such as EEPROMs, real-time clock, temperaturesensors, etc. The I2C interface is not particularly fast so it is typically used for connecting to IC’s that donot require large amounts of data to be transferred rapidly.Some of the microcontrollers used in EE459Lx such as the Atmel ATmega328P and the FreescaleMC908JL16 implement this interface in hardware. The Atmel documentation calls the I2C interface the“Two Wire Interface” while the Freescale documentation refers to it as “MMIIC” (Multi-Master IIC) butit’s the same thing.The hardware on these microcontrollers perform many of the lower level tasks required for transferringdata on the I2C bus. For example, to write data to a I2C device, the program sets up the transfer, startsit, keeps a data buffer full with the next byte to be sent and finally terminates the transfer at the end. Thesignaling required for sending each bit is handled by the hardware. Reading is done in a similar manner.Software-only implementations of the I2C protocol can also be used to provide this interface on micro-controllers that do not have I2C hardware.The Agilent and Tektronix oscilloscopes in the OHE 240 lab have special triggering capabilities that makeit possible to analyze I2C data transfers. Individual data transfers can be captured and viewed so that thetransfer can be examined in detail to see if the correct information is being sent or received. This capabilityof these scopes is an extremely useful tool for students trying to debug any I2C aspects of their projects.1.1 Device AddressesIn order to use the I2C interface you need to know the address of the device on the I2C bus. The address isa seven bit number but is often written as an eight-bit number where the upper seven bits are the addressand the least significant bit indicates whether a read or write is occurring. Most manufacturer’s datasheetsprovide the eight bit number with the least significant bit set to zero. Table 1 shows the 8-bit value (7-bitaddress + R/W flag) for some common devices used in EE459 projects.Some I2C devices allow you to specify one or more of the least significant bits of the seven-bit addressby connecting pins on the chip to a logical zero or one. This make it possible to have more than one deviceof the same type on the bus or to avoid address conflicts with other devices. The number of extra addressbits available is indicated in Table 1.1.2 Reading DataMany I2C devices require reading operations to be done in two steps. The first part of the reading operationconsists of writing the address of the first location to be read into an internal buffer. The write operation isEE 459Lx, Rev. 11/18/13 1IIC Device Description 8-bit Base Number of ExtraAddress Address Bits24LC256 32KB EEPROM 0xA0 3DS1307 Real-time clock 0xD0 nonePCF8563 Real-time clock 0xA2 noneDS1621 Temperature sensor 0x90 3MCP23008 8-bit port expander 0x40 3MCP23017 16-bit port expander 0x40 3MAX7311 16-bit port expander 0x40 3Table 1: I2C Device Addressesthen terminated and a read command is sent. This causes the device to start sending data from the locationgiven by the data that was previously written to it. The number of bytes that must be written before theread can be done is dependent on the device.2 I2C on the ATmega328PThe ATmega328P uses pins 27 and 28 for the I2C data and clock. When I2C is not used these pins can beused as general I/O ports PC4 and PC5.2.1 InitializingThe internal I2C hardware needs to have the baud rate (I2C clock rate) set before any transfers can takeplace. The maximum rate that the I2C device can handle should be described in the device’s datasheet,but most devices should be able to operate with a clock up to 100kHz. The clock is derived from themicrocontroller’s clock by dividing it down according to this formula.I2C Baud Rate =CPU Clock Frequency16 + 2(T W BR) · (prescalar value)The prescalar value is set by a two bit number in the TWSR register and can be either 1, 4, 16 or 64. TheT W BR value is the eight-bit contents of the TWBR register. This value can be calculated if we rearrange theabove formula to solve for TWBRT W BR =CPU Clock FrequencyI2C Baud Rate− 16)/(2 · prescalar value)A suitable value for T W BR can be calculated in the program source code using compiler preprocessorstatements. For example, if we want a baud rate of 100kHz, we can set the prescalar to one and then usethe following to determine the value to go in the TWBR register.# define FOSC 98 30 400 // Clock frequ ency = Oscilla tor freq .# define BDIV ( FOSC / 100000 - 16) / 2 + 1TWSR = 0; // Set pre scala r for 1TWBR = BDIV ; // Set bit rate regist erThe +1 at the end of the statement calculating BDIV is needed since the integer calculations done bythe preprocessor could have truncation errors resulting in a value of BDIV that is too low and giving an I2Cfrequency over 100kHz. The +1 makes sure the frequency is below 100kHz.2.2 WritingThe following is an example of a function that writes “n” bytes of data from array “p” to an I2C devicewith the 8-bit address “device_addr”. Most of code in this routine consists of slight variations of the sameEE 459Lx, Rev. 11/18/13 2thing: load the data register with data, set the control register to send it, wait for it to be sent, and checkthe status./*i2c_ write - write bytes to an I2C device*/uint8_t i2 c_write ( uint8_ t device_addr , char *p , uin t16_t n ){uint8_t status ;To start the transmission, the control register is set to signal a start condition on the bus.TWCR = (1 << TWINT ) | (1 << TWEN ) | (1 << TWSTA ); // Send start conditionwhile (!( TWCR & (1 << TWINT ))); // Wait for TWINT to be setstatus = TWSR & 0 xf8 ;if ( status != 0 x08 ) // Check that START was sent OKreturn ( status );Next, the device address, with the least significant bit a zero, is sent. The address is loaded into thedata register, the transmission is started, the program loops waiting for it to finish, and then the status ischecked.TWDR = de vic e_a ddr & 0 xfe ; // Load device ad dres s and R / W =


View Full Document

USC EE 459Lx - I2C

Download I2C
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view I2C and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view I2C 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?