SlideShare ist ein Scribd-Unternehmen logo
1 von 17
Downloaden Sie, um offline zu lesen
How to use I2C with AVR
Sunday, April 28, 13
I2C abstract(1)
AVR1 AVR2
SCL
SDA
Vdd
I2C is one of communication systems for Microcomputer.
I2C uses 2 lines, SCL(clock), SDA(data).
Sunday, April 28, 13
I2C abstract(2)
AVR1 AVR2
SCL
SDA
SCL(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 0
Sunday, April 28, 13
I2C abstract(3)
Master
You 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 Slave4
SDA
address1 address2 address3 address4
SCL
Sunday, April 28, 13
I2C abstract(4)
Master
For example, if Master wants to send data to Slave2, Master
sends address2 to SDA line.All Slaves receives the address.
Only Slave2 replies.
Slave1 Slave2 Slave3 Slave4
SDA
address1 address2 address3 address4
SCL
address2
reply
The 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
I2C abstract(5)
Master
Next Master sends data to SDA line. Only Slave2 receives
the data and reply.
Slave1 Slave2 Slave3 Slave4
SDA
address1 address2 address3 address4
SCL
data
reply
I’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 discard
Sunday, April 28, 13
Program I2C with AVR
In this instance, you use arduino uno.
Arduino uno has Atmega168.
I am going to explain how to program
Atmega168 with C, without
Arduino library.
Sunday, April 28, 13
Connection
SDA pin and SCL pin are fixed. On Arduino Uno, analog4 = SDA, analog 5 = SCL.
Connect A4 and A5 of each Arduino.
Vcc
Sunday, April 28, 13
C programming on Arduino IDE
Arduino 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() and
loop()”. Initialize process must be inside setup() and loop process must be inside loop().
#include<avr.io.h>
void main(int){
//initialize process
while(1){
//loop process
}
}
#include<avr.io.h>
void setup(){
//initialize process
}
void loop(){
//loop process
}
Normal C C on Arduino IDE
Sunday, April 28, 13
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 TWGCETWAR
TWBR7 TWBR6 TWBR5 TWBR4 TWBR3 TWBR2 TWBR1 TWBR0
TWINT TWEA TWSTA TWSTO TWWC TWEN - TWIE
TWD7 TWD6 TWD5 TWD4 TWD3 TWD2 TWD1 TWD0
TWS7 TWS6 TWS5 TWS4 TWS3 - TWPS1 TWPS0
TWBR
TWCR
TWDR
TWSR
Sunday, April 28, 13
The simplest I2C sequence
This 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)Stop
Sunday, April 28, 13
How to use TWBR and TWSR
First, 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 TWBR0
TWS7 TWS6 TWS5 TWS4 TWS3 - TWPS1 TWPS0
TWBR
TWSR
Prescaler parameter
(ex)
TWBR =0b00000001=1
TWBR =0b00000011=3
TWBR =0b11111111 = 255
(ex)
CPU clock = 1Mhz
TWBR=255
Prescaler=1
SCL = 1000000/(16+2(255)x1)=2kHz
(ex)
TWBR =(0<<TWPS1)|(0<<TWPS0) : prescaler is 1
TWBR =(0<<TWPS1)|(1<<TWPS0) : prescaler is 4
TWBR =(1<<TWPS1)|(1<<TWPS0) : prescaler is 64
Sunday, April 28, 13
How to use TWCR
You can control I2C message with TWCR.
TWINT TWEA TWSTA TWSTO TWWC TWEN - TWIETWCR
Send start condition
TWINT TWEA TWSTA TWSTO TWWC TWEN - TWIETWCR
Clear TWINT flag after sending message
TWINT TWEA TWSTA TWSTO TWWC TWEN - TWIETWCR
Send stop condition
TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN); //Start condition
while(!(TWCR & 1<<TWINT)) {} //After TWCR, always you have to check whether TWINT is updated
TWCR = 0b10000100; //flag clear
while(!(TWCR & 1<<TWINT)) {}
TWCR = 0b10010100; //finish translation
while(!(TWCR & 1<<TWINT)) {}
Source code sample
Sunday, April 28, 13
How to use TWSR
You can check status of I2C sequence from TWSR.
TWS7 TWS6 TWS5 TWS4 TWS3 - TWPS1 TWPS0
TWSR
Source code sample
//check condition
if((TWSR & 0xF8) == 0x08){
TWDR = 0b00000010; //set slave address
TWCR = 0b10000100; //flag clear
while(!(TWCR & 1<<TWINT)) {}
_delay_ms(1);
if((TWSR & 0xF8) == 0x18){
TWDR = 0b10101010; //write data(1byte)
TWCR = 0b10000100; //send data
while(!(TWCR & (1<<TWINT)));
_delay_ms(1);
}
}
Sunday, April 28, 13
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 page
Vcc
Master
Slave
LEDGND
This sample is to turn on LED connected to Slave
using data from Master
Sunday, April 28, 13
Sample code(master)
#include <avr/io.h>
#include <util/delay.h>
void setup(){
TWSR = (0<<TWPS1)|(0<<TWPS0); //Prescaler
TWBR = 0xFF; //TWBR=255
}
void loop(){
TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN); //Start condition
while(!(TWCR & 1<<TWINT)) {} //wait for flag update
_delay_ms(1);
if((TWSR & 0xF8) == 0x08){ //check condition
TWDR = 0b00000010; //send slave address
TWCR = 0b10000100; //flag clear
while(!(TWCR & 1<<TWINT)) {}
_delay_ms(1);
if((TWSR & 0xF8) == 0x18){ //check condition
TWDR = 0b10101010; //write data(1byte)
TWCR = 0b10000100; //send data
while(!(TWCR & (1<<TWINT)));
_delay_ms(1);
}
}
TWCR = 0b10010100; //finish translation
_delay_ms(2);
}
Sunday, April 28, 13
Sample code(slave)
#include <avr/io.h>
#include <util/delay.h>
void setup(){
DDRB = 0xFF; //all PINB is output mode
PORTB = 0x00; //all PINB is Low
TWAR = 0b00000010; //salve address setting
}
void loop(){
TWCR = 0b11000100; //start as slave(wait for address)
if((TWSR & 0xF8) == 0x60){ //check status
while(1){
TWCR = 0b11000100; //wait for data
while(!(TWCR & (1<<TWINT)));
if((TWSR & 0xF8) == 0x80){ //check status
char data = TWDR; //receive data
PORTB = data; //insert data to PORTB
}
if((TWSR & 0xF8) == 0xA0){ //check stop status
_delay_ms(10);
break;
}
}
}
}
Sunday, April 28, 13

Weitere ähnliche Inhalte

Was ist angesagt?

PIC 16F877A by PARTHIBAN. S.
PIC 16F877A   by PARTHIBAN. S.PIC 16F877A   by PARTHIBAN. S.
PIC 16F877A by PARTHIBAN. S.parthi_arjun
 
Microcontroller 8051
Microcontroller 8051Microcontroller 8051
Microcontroller 8051Rashmi
 
I2C And SPI Part-23
I2C And  SPI Part-23I2C And  SPI Part-23
I2C And SPI Part-23Techvilla
 
INTERFACING ANALAOG TO DIGITAL CONVERTER (ADC0808/09) TO 8051 MICROCONTROLLER
 INTERFACING ANALAOG TO DIGITAL CONVERTER (ADC0808/09) TO 8051 MICROCONTROLLER   INTERFACING ANALAOG TO DIGITAL CONVERTER (ADC0808/09) TO 8051 MICROCONTROLLER
INTERFACING ANALAOG TO DIGITAL CONVERTER (ADC0808/09) TO 8051 MICROCONTROLLER SIRILsam
 
RTC Interfacing and Programming
RTC Interfacing and ProgrammingRTC Interfacing and Programming
RTC Interfacing and ProgrammingDevashish Raval
 
8051 microcontroller introduction
8051 microcontroller introduction8051 microcontroller introduction
8051 microcontroller introductionANJUSHA R
 
Introduction to Arduino
Introduction to ArduinoIntroduction to Arduino
Introduction to ArduinoRichard Rixham
 
Introduction to Arduino
Introduction to ArduinoIntroduction to Arduino
Introduction to ArduinoYong Heui Cho
 
UNIT III PROGRAMMABLE PERIPHERAL INTERFACE
UNIT III PROGRAMMABLE PERIPHERAL INTERFACE UNIT III PROGRAMMABLE PERIPHERAL INTERFACE
UNIT III PROGRAMMABLE PERIPHERAL INTERFACE ravis205084
 
Programmable peripheral interface 8255
Programmable peripheral interface 8255Programmable peripheral interface 8255
Programmable peripheral interface 8255Marajulislam3
 
MICROCONTROLLER 8051- Architecture & Pin Configuration
MICROCONTROLLER 8051- Architecture & Pin Configuration MICROCONTROLLER 8051- Architecture & Pin Configuration
MICROCONTROLLER 8051- Architecture & Pin Configuration AKHIL MADANKAR
 
microcontroller basics
microcontroller basicsmicrocontroller basics
microcontroller basicssagar Ramdev
 
Lesson sample introduction to arduino
Lesson sample   introduction to arduinoLesson sample   introduction to arduino
Lesson sample introduction to arduinoBetsy Eng
 
Interfacing adc
Interfacing adcInterfacing adc
Interfacing adcPRADEEP
 

Was ist angesagt? (20)

Introduction to arduino
Introduction to arduinoIntroduction to arduino
Introduction to arduino
 
PIC 16F877A by PARTHIBAN. S.
PIC 16F877A   by PARTHIBAN. S.PIC 16F877A   by PARTHIBAN. S.
PIC 16F877A by PARTHIBAN. S.
 
Microcontroller 8051
Microcontroller 8051Microcontroller 8051
Microcontroller 8051
 
I2C And SPI Part-23
I2C And  SPI Part-23I2C And  SPI Part-23
I2C And SPI Part-23
 
Arduino- Serial communication
Arduino-  Serial communicationArduino-  Serial communication
Arduino- Serial communication
 
PIC Microcontrollers
PIC MicrocontrollersPIC Microcontrollers
PIC Microcontrollers
 
Lab2ppt
Lab2pptLab2ppt
Lab2ppt
 
STM32 MCU Family
STM32 MCU FamilySTM32 MCU Family
STM32 MCU Family
 
Different Arduino Boards
Different Arduino BoardsDifferent Arduino Boards
Different Arduino Boards
 
INTERFACING ANALAOG TO DIGITAL CONVERTER (ADC0808/09) TO 8051 MICROCONTROLLER
 INTERFACING ANALAOG TO DIGITAL CONVERTER (ADC0808/09) TO 8051 MICROCONTROLLER   INTERFACING ANALAOG TO DIGITAL CONVERTER (ADC0808/09) TO 8051 MICROCONTROLLER
INTERFACING ANALAOG TO DIGITAL CONVERTER (ADC0808/09) TO 8051 MICROCONTROLLER
 
RTC Interfacing and Programming
RTC Interfacing and ProgrammingRTC Interfacing and Programming
RTC Interfacing and Programming
 
8051 microcontroller introduction
8051 microcontroller introduction8051 microcontroller introduction
8051 microcontroller introduction
 
Introduction to Arduino
Introduction to ArduinoIntroduction to Arduino
Introduction to Arduino
 
Introduction to Arduino
Introduction to ArduinoIntroduction to Arduino
Introduction to Arduino
 
UNIT III PROGRAMMABLE PERIPHERAL INTERFACE
UNIT III PROGRAMMABLE PERIPHERAL INTERFACE UNIT III PROGRAMMABLE PERIPHERAL INTERFACE
UNIT III PROGRAMMABLE PERIPHERAL INTERFACE
 
Programmable peripheral interface 8255
Programmable peripheral interface 8255Programmable peripheral interface 8255
Programmable peripheral interface 8255
 
MICROCONTROLLER 8051- Architecture & Pin Configuration
MICROCONTROLLER 8051- Architecture & Pin Configuration MICROCONTROLLER 8051- Architecture & Pin Configuration
MICROCONTROLLER 8051- Architecture & Pin Configuration
 
microcontroller basics
microcontroller basicsmicrocontroller basics
microcontroller basics
 
Lesson sample introduction to arduino
Lesson sample   introduction to arduinoLesson sample   introduction to arduino
Lesson sample introduction to arduino
 
Interfacing adc
Interfacing adcInterfacing adc
Interfacing adc
 

Andere mochten auch

Andere mochten auch (19)

SPI Protocol
SPI ProtocolSPI Protocol
SPI Protocol
 
Protols used in bluetooth
Protols used in bluetoothProtols used in bluetooth
Protols used in bluetooth
 
Multiple Inheritance
Multiple InheritanceMultiple Inheritance
Multiple Inheritance
 
Arm processor
Arm processorArm processor
Arm processor
 
Inheritance in OOPS
Inheritance in OOPSInheritance in OOPS
Inheritance in OOPS
 
Embedded C - Optimization techniques
Embedded C - Optimization techniquesEmbedded C - Optimization techniques
Embedded C - Optimization techniques
 
Embedded C
Embedded CEmbedded C
Embedded C
 
Compiler in System Programming/Code Optimization techniques in System Program...
Compiler in System Programming/Code Optimization techniques in System Program...Compiler in System Programming/Code Optimization techniques in System Program...
Compiler in System Programming/Code Optimization techniques in System Program...
 
Introduction to Embedded System
Introduction to Embedded SystemIntroduction to Embedded System
Introduction to Embedded System
 
I2C Protocol
I2C ProtocolI2C Protocol
I2C Protocol
 
Object-Oriented Design: Multiple inheritance (C++ and C#)
Object-Oriented Design: Multiple inheritance (C++ and C#)Object-Oriented Design: Multiple inheritance (C++ and C#)
Object-Oriented Design: Multiple inheritance (C++ and C#)
 
Arm developement
Arm developementArm developement
Arm developement
 
Serial Peripheral Interface
Serial Peripheral InterfaceSerial Peripheral Interface
Serial Peripheral Interface
 
I2C Bus (Inter-Integrated Circuit)
I2C Bus (Inter-Integrated Circuit)I2C Bus (Inter-Integrated Circuit)
I2C Bus (Inter-Integrated Circuit)
 
Embedded C - Lecture 1
Embedded C - Lecture 1Embedded C - Lecture 1
Embedded C - Lecture 1
 
Serial peripheral interface
Serial peripheral interfaceSerial peripheral interface
Serial peripheral interface
 
Serial Peripheral Interface(SPI)
Serial Peripheral Interface(SPI)Serial Peripheral Interface(SPI)
Serial Peripheral Interface(SPI)
 
Code Optimization
Code OptimizationCode Optimization
Code Optimization
 
Iot
IotIot
Iot
 

Ähnlich wie I2C programming with C and Arduino

basics of temperature data logger (with energia and stellaris)
basics of  temperature data logger (with energia and stellaris)basics of  temperature data logger (with energia and stellaris)
basics of temperature data logger (with energia and stellaris)Zafer Genc
 
Part-2: Mastering microcontroller with embedded driver development
Part-2: Mastering microcontroller with embedded driver developmentPart-2: Mastering microcontroller with embedded driver development
Part-2: Mastering microcontroller with embedded driver developmentFastBit Embedded Brain Academy
 
m.tech esd lab manual for record
m.tech esd lab manual for recordm.tech esd lab manual for record
m.tech esd lab manual for recordG Lemuel George
 
Real Time Clock Interfacing with FPGA
Real Time Clock Interfacing with FPGAReal Time Clock Interfacing with FPGA
Real Time Clock Interfacing with FPGAMafaz Ahmed
 
ARDUINO AND ITS PIN CONFIGURATION
 ARDUINO AND ITS PIN  CONFIGURATION ARDUINO AND ITS PIN  CONFIGURATION
ARDUINO AND ITS PIN CONFIGURATIONsoma saikiran
 
I2 c communication between nodemcu and aeduino with dht11 (1)
I2 c communication between nodemcu and aeduino with dht11 (1)I2 c communication between nodemcu and aeduino with dht11 (1)
I2 c communication between nodemcu and aeduino with dht11 (1)Kuntala Das
 
ARDUINO BASED TIME AND TEMPERATURE DISPLAY
ARDUINO BASED TIME AND TEMPERATURE DISPLAY ARDUINO BASED TIME AND TEMPERATURE DISPLAY
ARDUINO BASED TIME AND TEMPERATURE DISPLAY ajit kumar singh
 
Keypad interfacing 8051 -NANOCDAC
Keypad interfacing 8051 -NANOCDACKeypad interfacing 8051 -NANOCDAC
Keypad interfacing 8051 -NANOCDACnanocdac
 
Analog To Digital Conversion (ADC) Programming in LPC2148
Analog To Digital Conversion (ADC) Programming in LPC2148Analog To Digital Conversion (ADC) Programming in LPC2148
Analog To Digital Conversion (ADC) Programming in LPC2148Omkar Rane
 
Logic gate tester for IC's ( Digital Electronics and Logic deisgn EE3114 )
Logic gate tester for IC's ( Digital Electronics and Logic deisgn EE3114 )Logic gate tester for IC's ( Digital Electronics and Logic deisgn EE3114 )
Logic gate tester for IC's ( Digital Electronics and Logic deisgn EE3114 )Jikrul Sayeed
 
Real Time Clock Interfacing for Arduino
Real Time Clock Interfacing for ArduinoReal Time Clock Interfacing for Arduino
Real Time Clock Interfacing for Arduinohandson28
 
Arduino experimenters guide hq
Arduino experimenters guide hqArduino experimenters guide hq
Arduino experimenters guide hqAndreis Santos
 
Arduino experimenters guide ARDX
Arduino experimenters guide ARDXArduino experimenters guide ARDX
Arduino experimenters guide ARDXJohnny Parrales
 

Ähnlich wie I2C programming with C and Arduino (20)

basics of temperature data logger (with energia and stellaris)
basics of  temperature data logger (with energia and stellaris)basics of  temperature data logger (with energia and stellaris)
basics of temperature data logger (with energia and stellaris)
 
Part-2: Mastering microcontroller with embedded driver development
Part-2: Mastering microcontroller with embedded driver developmentPart-2: Mastering microcontroller with embedded driver development
Part-2: Mastering microcontroller with embedded driver development
 
m.tech esd lab manual for record
m.tech esd lab manual for recordm.tech esd lab manual for record
m.tech esd lab manual for record
 
Real Time Clock Interfacing with FPGA
Real Time Clock Interfacing with FPGAReal Time Clock Interfacing with FPGA
Real Time Clock Interfacing with FPGA
 
EEE UNIT-2 PPT.pdf
EEE UNIT-2 PPT.pdfEEE UNIT-2 PPT.pdf
EEE UNIT-2 PPT.pdf
 
ARDUINO AND ITS PIN CONFIGURATION
 ARDUINO AND ITS PIN  CONFIGURATION ARDUINO AND ITS PIN  CONFIGURATION
ARDUINO AND ITS PIN CONFIGURATION
 
020419.pdf
020419.pdf020419.pdf
020419.pdf
 
I2 c communication between nodemcu and aeduino with dht11 (1)
I2 c communication between nodemcu and aeduino with dht11 (1)I2 c communication between nodemcu and aeduino with dht11 (1)
I2 c communication between nodemcu and aeduino with dht11 (1)
 
Project
ProjectProject
Project
 
ARDUINO BASED TIME AND TEMPERATURE DISPLAY
ARDUINO BASED TIME AND TEMPERATURE DISPLAY ARDUINO BASED TIME AND TEMPERATURE DISPLAY
ARDUINO BASED TIME AND TEMPERATURE DISPLAY
 
project 3 full report
project 3 full reportproject 3 full report
project 3 full report
 
Keypad interfacing 8051 -NANOCDAC
Keypad interfacing 8051 -NANOCDACKeypad interfacing 8051 -NANOCDAC
Keypad interfacing 8051 -NANOCDAC
 
Analog To Digital Conversion (ADC) Programming in LPC2148
Analog To Digital Conversion (ADC) Programming in LPC2148Analog To Digital Conversion (ADC) Programming in LPC2148
Analog To Digital Conversion (ADC) Programming in LPC2148
 
Em s7 plc
Em s7 plcEm s7 plc
Em s7 plc
 
Logic gate tester for IC's ( Digital Electronics and Logic deisgn EE3114 )
Logic gate tester for IC's ( Digital Electronics and Logic deisgn EE3114 )Logic gate tester for IC's ( Digital Electronics and Logic deisgn EE3114 )
Logic gate tester for IC's ( Digital Electronics and Logic deisgn EE3114 )
 
Real Time Clock Interfacing for Arduino
Real Time Clock Interfacing for ArduinoReal Time Clock Interfacing for Arduino
Real Time Clock Interfacing for Arduino
 
Arduino experimenters guide hq
Arduino experimenters guide hqArduino experimenters guide hq
Arduino experimenters guide hq
 
Introduction to Arduino
Introduction to ArduinoIntroduction to Arduino
Introduction to Arduino
 
Arduino experimenters guide ARDX
Arduino experimenters guide ARDXArduino experimenters guide ARDX
Arduino experimenters guide ARDX
 
Bidirect visitor counter
Bidirect visitor counterBidirect visitor counter
Bidirect visitor counter
 

Kürzlich hochgeladen

Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Jeffrey Haguewood
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...apidays
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...apidays
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Zilliz
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Angeliki Cooney
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Victor Rentea
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024The Digital Insurer
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKJago de Vreede
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxRustici Software
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistandanishmna97
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfOverkill Security
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 

Kürzlich hochgeladen (20)

Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 

I2C programming with C and Arduino

  • 1. How to use I2C with AVR Sunday, April 28, 13
  • 2. I2C abstract(1) AVR1 AVR2 SCL SDA Vdd I2C is one of communication systems for Microcomputer. I2C uses 2 lines, SCL(clock), SDA(data). Sunday, April 28, 13
  • 3. I2C abstract(2) AVR1 AVR2 SCL SDA SCL(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 0 Sunday, April 28, 13
  • 4. I2C abstract(3) Master You 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 Slave4 SDA address1 address2 address3 address4 SCL Sunday, April 28, 13
  • 5. I2C abstract(4) Master For example, if Master wants to send data to Slave2, Master sends address2 to SDA line.All Slaves receives the address. Only Slave2 replies. Slave1 Slave2 Slave3 Slave4 SDA address1 address2 address3 address4 SCL address2 reply The 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. I2C abstract(5) Master Next Master sends data to SDA line. Only Slave2 receives the data and reply. Slave1 Slave2 Slave3 Slave4 SDA address1 address2 address3 address4 SCL data reply I’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 discard Sunday, April 28, 13
  • 7. Program I2C with AVR In this instance, you use arduino uno. Arduino uno has Atmega168. I am going to explain how to program Atmega168 with C, without Arduino library. Sunday, April 28, 13
  • 8. Connection SDA pin and SCL pin are fixed. On Arduino Uno, analog4 = SDA, analog 5 = SCL. Connect A4 and A5 of each Arduino. Vcc Sunday, April 28, 13
  • 9. C programming on Arduino IDE Arduino 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() and loop()”. Initialize process must be inside setup() and loop process must be inside loop(). #include<avr.io.h> void main(int){ //initialize process while(1){ //loop process } } #include<avr.io.h> void setup(){ //initialize process } void loop(){ //loop process } Normal C C on Arduino IDE Sunday, April 28, 13
  • 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 TWGCETWAR TWBR7 TWBR6 TWBR5 TWBR4 TWBR3 TWBR2 TWBR1 TWBR0 TWINT TWEA TWSTA TWSTO TWWC TWEN - TWIE TWD7 TWD6 TWD5 TWD4 TWD3 TWD2 TWD1 TWD0 TWS7 TWS6 TWS5 TWS4 TWS3 - TWPS1 TWPS0 TWBR TWCR TWDR TWSR Sunday, April 28, 13
  • 11. The simplest I2C sequence This 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)Stop Sunday, April 28, 13
  • 12. How to use TWBR and TWSR First, 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 TWBR0 TWS7 TWS6 TWS5 TWS4 TWS3 - TWPS1 TWPS0 TWBR TWSR Prescaler parameter (ex) TWBR =0b00000001=1 TWBR =0b00000011=3 TWBR =0b11111111 = 255 (ex) CPU clock = 1Mhz TWBR=255 Prescaler=1 SCL = 1000000/(16+2(255)x1)=2kHz (ex) TWBR =(0<<TWPS1)|(0<<TWPS0) : prescaler is 1 TWBR =(0<<TWPS1)|(1<<TWPS0) : prescaler is 4 TWBR =(1<<TWPS1)|(1<<TWPS0) : prescaler is 64 Sunday, April 28, 13
  • 13. How to use TWCR You can control I2C message with TWCR. TWINT TWEA TWSTA TWSTO TWWC TWEN - TWIETWCR Send start condition TWINT TWEA TWSTA TWSTO TWWC TWEN - TWIETWCR Clear TWINT flag after sending message TWINT TWEA TWSTA TWSTO TWWC TWEN - TWIETWCR Send stop condition TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN); //Start condition while(!(TWCR & 1<<TWINT)) {} //After TWCR, always you have to check whether TWINT is updated TWCR = 0b10000100; //flag clear while(!(TWCR & 1<<TWINT)) {} TWCR = 0b10010100; //finish translation while(!(TWCR & 1<<TWINT)) {} Source code sample Sunday, April 28, 13
  • 14. How to use TWSR You can check status of I2C sequence from TWSR. TWS7 TWS6 TWS5 TWS4 TWS3 - TWPS1 TWPS0 TWSR Source code sample //check condition if((TWSR & 0xF8) == 0x08){ TWDR = 0b00000010; //set slave address TWCR = 0b10000100; //flag clear while(!(TWCR & 1<<TWINT)) {} _delay_ms(1); if((TWSR & 0xF8) == 0x18){ TWDR = 0b10101010; //write data(1byte) TWCR = 0b10000100; //send data while(!(TWCR & (1<<TWINT))); _delay_ms(1); } } Sunday, April 28, 13
  • 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 page Vcc Master Slave LEDGND This sample is to turn on LED connected to Slave using data from Master Sunday, April 28, 13
  • 16. Sample code(master) #include <avr/io.h> #include <util/delay.h> void setup(){ TWSR = (0<<TWPS1)|(0<<TWPS0); //Prescaler TWBR = 0xFF; //TWBR=255 } void loop(){ TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN); //Start condition while(!(TWCR & 1<<TWINT)) {} //wait for flag update _delay_ms(1); if((TWSR & 0xF8) == 0x08){ //check condition TWDR = 0b00000010; //send slave address TWCR = 0b10000100; //flag clear while(!(TWCR & 1<<TWINT)) {} _delay_ms(1); if((TWSR & 0xF8) == 0x18){ //check condition TWDR = 0b10101010; //write data(1byte) TWCR = 0b10000100; //send data while(!(TWCR & (1<<TWINT))); _delay_ms(1); } } TWCR = 0b10010100; //finish translation _delay_ms(2); } Sunday, April 28, 13
  • 17. Sample code(slave) #include <avr/io.h> #include <util/delay.h> void setup(){ DDRB = 0xFF; //all PINB is output mode PORTB = 0x00; //all PINB is Low TWAR = 0b00000010; //salve address setting } void loop(){ TWCR = 0b11000100; //start as slave(wait for address) if((TWSR & 0xF8) == 0x60){ //check status while(1){ TWCR = 0b11000100; //wait for data while(!(TWCR & (1<<TWINT))); if((TWSR & 0xF8) == 0x80){ //check status char data = TWDR; //receive data PORTB = data; //insert data to PORTB } if((TWSR & 0xF8) == 0xA0){ //check stop status _delay_ms(10); break; } } } } Sunday, April 28, 13