Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Upcoming SlideShare
Serial Peripheral Interface
Next
Download to read offline and view in fullscreen.

Share

I2C programming with C and Arduino

Download to read offline

How to program I2C with C and Arduino

Related Books

Free with a 30 day trial from Scribd

See all

Related Audiobooks

Free with a 30 day trial from Scribd

See all

I2C programming with C and Arduino

  1. 1. How to use I2C with AVRSunday, April 28, 13
  2. 2. I2C abstract(1)AVR1 AVR2SCLSDAVddI2C is one of communication systems for Microcomputer.I2C uses 2 lines, SCL(clock), SDA(data).Sunday, April 28, 13
  3. 3. I2C abstract(2)AVR1 AVR2SCLSDASCL(clock) is used to send clock for synchronization.SDA(data) is used for sending data.1 bit data is transferred every clock.1 0 1 0Sunday, April 28, 13
  4. 4. I2C abstract(3)MasterYou can connect more than 2 boards.One board must be Master and the others are Slave.Each slave has own address.Master can send data to each slave using the address.Slave1 Slave2 Slave3 Slave4SDAaddress1 address2 address3 address4SCLSunday, April 28, 13
  5. 5. I2C abstract(4)MasterFor example, if Master wants to send data to Slave2, Mastersends address2 to SDA line.All Slaves receives the address.Only Slave2 replies.Slave1 Slave2 Slave3 Slave4SDAaddress1 address2 address3 address4SCLaddress2replyThe data is mine!I must send replay.The data is not mine.I will ignore it.The data is not mine.I will ignore it.The data is not mine.I will ignore it.Sunday, April 28, 13
  6. 6. I2C abstract(5)MasterNext Master sends data to SDA line. Only Slave2 receivesthe data and reply.Slave1 Slave2 Slave3 Slave4SDAaddress1 address2 address3 address4SCLdatareplyI’ll receive this data.I must send replay.The data is not mine.I will discard it.The data is not mine.I will discard it.The data is not mine.I will discard it.discard discard discardSunday, April 28, 13
  7. 7. Program I2C with AVRIn this instance, you use arduino uno.Arduino uno has Atmega168.I am going to explain how to programAtmega168 with C, withoutArduino library.Sunday, April 28, 13
  8. 8. ConnectionSDA pin and SCL pin are fixed. On Arduino Uno, analog4 = SDA, analog 5 = SCL.Connect A4 and A5 of each Arduino.VccSunday, April 28, 13
  9. 9. C programming on Arduino IDEArduino language is based on C. You can use all construction of C.When you write C on Arduino IDE, you have to replace “main and while” with “setup() andloop()”. Initialize process must be inside setup() and loop process must be inside loop().#include<avr.io.h>void main(int){//initialize processwhile(1){//loop process}}#include<avr.io.h>void setup(){//initialize process}void loop(){//loop process}Normal C C on Arduino IDESunday, April 28, 13
  10. 10. I2C registers(Atmega168 version)If you want to use I2C function on Microcomputer, you have to use registers for I2C.(Sometimes different microcomputers have different registers, you have to see data sheet.)These are I2C resisters(NOT all).TWAR is TWI(Slave) Address Register. Slave address is set in this register.TWBR is TWI Bit Rate Register. This is to decide SCL clock.TWCR is TWI Control Register.You can control I2C with this register.TWDR is TWI Data Register.The data to be transferred or received is set in this register.TWSR is TWI Status Register. The status of I2C communication is set in this register.TWA6 TWA5 TWA4 TWA3 TWA2 TWA1 TWA0 TWGCETWARTWBR7 TWBR6 TWBR5 TWBR4 TWBR3 TWBR2 TWBR1 TWBR0TWINT TWEA TWSTA TWSTO TWWC TWEN - TWIETWD7 TWD6 TWD5 TWD4 TWD3 TWD2 TWD1 TWD0TWS7 TWS6 TWS5 TWS4 TWS3 - TWPS1 TWPS0TWBRTWCRTWDRTWSRSunday, April 28, 13
  11. 11. The simplest I2C sequenceThis is the simplest sequence of I2C.If you want to send data more than 1 byte.You have to repeat (4)(5).Master Slave(1)Start(2)Slave Adress(3)Ack(4)Data(5)Ack(6)StopSunday, April 28, 13
  12. 12. How to use TWBR and TWSRFirst, you have to decide the communication speed(SCL) with TWBR and TWSR.SCL = CPU clock/(16+2(TWBR)xPrescaler)TWBR7 TWBR6 TWBR5 TWBR4 TWBR3 TWBR2 TWBR1 TWBR0TWS7 TWS6 TWS5 TWS4 TWS3 - TWPS1 TWPS0TWBRTWSRPrescaler parameter(ex)TWBR =0b00000001=1TWBR =0b00000011=3TWBR =0b11111111 = 255(ex)CPU clock = 1MhzTWBR=255Prescaler=1SCL = 1000000/(16+2(255)x1)=2kHz(ex)TWBR =(0<<TWPS1)|(0<<TWPS0) : prescaler is 1TWBR =(0<<TWPS1)|(1<<TWPS0) : prescaler is 4TWBR =(1<<TWPS1)|(1<<TWPS0) : prescaler is 64Sunday, April 28, 13
  13. 13. How to use TWCRYou can control I2C message with TWCR.TWINT TWEA TWSTA TWSTO TWWC TWEN - TWIETWCRSend start conditionTWINT TWEA TWSTA TWSTO TWWC TWEN - TWIETWCRClear TWINT flag after sending messageTWINT TWEA TWSTA TWSTO TWWC TWEN - TWIETWCRSend stop conditionTWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN); //Start conditionwhile(!(TWCR & 1<<TWINT)) {} //After TWCR, always you have to check whether TWINT is updatedTWCR = 0b10000100; //flag clearwhile(!(TWCR & 1<<TWINT)) {}TWCR = 0b10010100; //finish translationwhile(!(TWCR & 1<<TWINT)) {}Source code sampleSunday, April 28, 13
  14. 14. How to use TWSRYou can check status of I2C sequence from TWSR.TWS7 TWS6 TWS5 TWS4 TWS3 - TWPS1 TWPS0TWSRSource code sample//check conditionif((TWSR & 0xF8) == 0x08){TWDR = 0b00000010; //set slave addressTWCR = 0b10000100; //flag clearwhile(!(TWCR & 1<<TWINT)) {}_delay_ms(1);if((TWSR & 0xF8) == 0x18){TWDR = 0b10101010; //write data(1byte)TWCR = 0b10000100; //send datawhile(!(TWCR & (1<<TWINT)));_delay_ms(1);}}Sunday, April 28, 13
  15. 15. The simplest sample(1)Connect A4 and A5 between Master and Slave(You need pull-up resisters)(2)Connect LED and resister to Slave digital 9(3)Connect MasterV5 to SlaveVin(4)Connect Master GND and Slave GND(5)Connect Master to PC with USB cable(6)Install program from next pageVccMasterSlaveLEDGNDThis sample is to turn on LED connected to Slaveusing data from MasterSunday, April 28, 13
  16. 16. Sample code(master)#include <avr/io.h>#include <util/delay.h>void setup(){TWSR = (0<<TWPS1)|(0<<TWPS0); //PrescalerTWBR = 0xFF; //TWBR=255}void loop(){TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN); //Start conditionwhile(!(TWCR & 1<<TWINT)) {} //wait for flag update_delay_ms(1);if((TWSR & 0xF8) == 0x08){ //check conditionTWDR = 0b00000010; //send slave addressTWCR = 0b10000100; //flag clearwhile(!(TWCR & 1<<TWINT)) {}_delay_ms(1);if((TWSR & 0xF8) == 0x18){ //check conditionTWDR = 0b10101010; //write data(1byte)TWCR = 0b10000100; //send datawhile(!(TWCR & (1<<TWINT)));_delay_ms(1);}}TWCR = 0b10010100; //finish translation_delay_ms(2);}Sunday, April 28, 13
  17. 17. Sample code(slave)#include <avr/io.h>#include <util/delay.h>void setup(){DDRB = 0xFF; //all PINB is output modePORTB = 0x00; //all PINB is LowTWAR = 0b00000010; //salve address setting}void loop(){TWCR = 0b11000100; //start as slave(wait for address)if((TWSR & 0xF8) == 0x60){ //check statuswhile(1){TWCR = 0b11000100; //wait for datawhile(!(TWCR & (1<<TWINT)));if((TWSR & 0xF8) == 0x80){ //check statuschar data = TWDR; //receive dataPORTB = data; //insert data to PORTB}if((TWSR & 0xF8) == 0xA0){ //check stop status_delay_ms(10);break;}}}}Sunday, April 28, 13
  • neorit1000

    May. 12, 2018
  • Shubham0255

    Nov. 1, 2016
  • MohdAzlanRazali

    Oct. 16, 2016
  • TheTraveler2020

    Aug. 9, 2016
  • harvardStudent

    May. 22, 2016
  • MingChiLin1

    Jan. 6, 2016

How to program I2C with C and Arduino

Views

Total views

11,761

On Slideshare

0

From embeds

0

Number of embeds

607

Actions

Downloads

319

Shares

0

Comments

0

Likes

6

×