homebrew I2C voordracht 16/05/2014

homebrew I2C voordracht 16/05/2014
To do:
Theoretische uitleg serial communicatie protocollen
I2C or two wire interface
UART protocol
SPI protocol
Gebruikelijke tools om te debuggen, controleren.
Arduino en I2C gebruikmakend van de wire library
Arduino en PCF8574 io expander ic
rand informatie
bronnen
pullup weerstanden in uw circuit voorbeeld, weet wel dat de arduino internal pullup weerstanden heeft op
de sda en scl pinnen.
Timing diagram
1. Data transfer is initiated with a START bit (S) signaled by SDA being pulled low while SCL stays high.
2. SDA sets the 1st data bit level while keeping SCL low (during blue bar time.) and the data is sampled
(received) when SCL rises (green).
3. When the transfer is complete, a STOP bit (P) is sent by releasing the data line to allow it to be pulled
high while SCL is kept high continuously.
4. In order to avoid false marker detection, the level on SDA is changed on the SCL falling edge and is
sampled and captured on the rising edge of SCL.
nota pullup weerstanden:
The correct pullup resistance for the I2C bus depends on the total capacitance on the bus and the
frequency you want to operate the bus at.
The formula from the ATmega168 datasheet (which I believe comes from the official I2C spec) is -Freq<100kHz⟹Rmin=Vcc−0.4V3mA,Rmax=1000nsCbus
Freq>100kHz⟹Rmin=Vcc−0.4V3mA,Rmax=300nsCbus
The Microchip 24LC256 specifies a maximum pin capacitance of 10pF (which is fairly typical). Count up the
number of devices you have in parallel on the bus and use the formula above to calculate a range of values
that will work.
If you are powering off of batteries I would use values that are at the high end of the range. If there are no
power limits on the power source or power dissipation issues in the ICs I would use values on the lower
end of the range.
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------Logic analyzers
When developing and/or troubleshooting the I²C bus, examination of hardware signals can be very
important. Logic analyzers are tools which collect, analyze, decode, and store signals so people can view
the high-speed waveforms at their leisure. Logic analyzers display time-stamps of each signal level change,
which can help find protocol problems. Most logic analyzers have the capability to decode bus signals into
high-level protocol data and show ASCII data
.
buspirate 3.6
Protocols
1-Wire, I2C, SPI, JTAG, asynchronous serial (UART), MIDI, PC keyboard, HD44780 LCDs, and
generic 2- and 3-wire libraries for custom protocols.
Features
USB interface, USB powered
5volt tolerant pins
0-6volt measurement probe
1Hz-40MHz frequency measurement
1kHz-4MHz pulse-width modulator, frequency generator
On-board multi-voltage pull-up resistors
On-board 3.3volt and 5volt power supplies with software reset
Macros for common operations
Bus traffic sniffers (SPI, I2C)
Transparent USB to serial bridge mode
10Hz-1MHz low-speed logic analyzer
Custom support in AVRDUDE , Flashrom , OpenOCD
AVR STK500 v2 programmer clone
Scriptable from Perl, Python, etc.
A bootloader for easy USB firmware updates
Uses DP6037 standard PCB layout
Open source (CC 0/Public Domain)
-------------------------------------------------------------------------------------------------------------------------------------------------UART Universal asynchronous receiver/transmitter
Character framing
The right-most (least significant) data bit is always transmitted first. If parity is present, the parity
bit comes after the data bits but before the stop bit(s).
Bit number 1
2
3
4
5
6
7
8
9
10 11
Start bit
5–8 data bits
Stop bit(s)
Start
Data 0 Data 1 Data 2 Data 3 Data 4 Data 5 Data 6 Data 7 Stop
The idle, no data state is high-voltage, or powered. This is a historic legacy from telegraphy, in which the
line is held high to show that the line and transmitter are not damaged. Each character is sent as a logic
low start bit, a configurable number of data bits (usually 8, but users can choose 5 to 8 or 9 bits depending
on which UART is in use), an optional parity bit if the number of bits per character chosen is not 9 bits,
and one or more logic high stop bits.
The start bit signals the receiver that a new character is coming. The next five to nine bits, depending on
the code set employed, represent the character. If a parity bit is used, it would be placed after all of the
data bits. The next one or two bits are always in the mark (logic high, i.e., '1') condition and called the stop
bit(s). They signal the receiver that the character is completed. Since the start bit is logic low (0) and the
stop bit is logic high (1) there are always at least two guaranteed signal changes between characters.
If the line is held in the logic low condition for longer than a character time, this is a break condition that
can be detected by the UART.
A parity bit, or check bit is a bit added to the end of a string of binary code that indicates whether the
number of bits in the string with the value one is even or odd. Parity bits are used as the simplest form of
error detecting code.
There are two variants of parity bits: even parity bit and odd parity bit.
In the case of even parity, the number of bits whose value is 1 in a given set are counted. If that total is odd,
the parity bit value is set to 1, making the total count of 1's in the set an even number. If the count of ones
in a given set of bits is already even, the parity bit's value remains 0.
In the case of odd parity, the situation is reversed. Instead, if the sum of bits with a value of 1 is odd, the
parity bit's value is set to zero. And if the sum of bits with a value of 1 is even, the parity bit value is set to
1, making the total count of 1's in the set an odd number.
Even parity is a special case of a cyclic redundancy check (CRC), where the 1-bit CRC is generated by the
polynomial x+1.
If the parity bit is present but not used, it may be referred to as mark parity (when the parity bit is always
1) or space parity (the bit is always 0).
7 bits of data (count of 1 bits)
0000000
1010001
1101001
1111111
0
3
4
7
8 bits including parity
even
odd
00000000 00000001
10100011 10100010
11010010 11010011
11111111 11111110
-----------------------------------------------------------------------------------------------------------------------------------------------------SPI protocol
SPI bus: single master and single slave
Typical SPI bus: master and three independent slaves
Daisy-chained SPI bus: master and cooperative slaves
-------------------------------------------------------------------------------------------------------------------------------------------------------------Wire Library
This library allows you to communicate with I2C / TWI devices. On
the Arduino boards with the R3 layout (1.0 pinout), the SDA (data
line) and SCL (clock line) are on the pin headers close to the AREF
pin. The Arduino Due has two I2C / TWI interfaces SDA1 andSCL1
are near to the AREF pin and the additional one is on pins 20 and 21.
As a reference the table below shows where TWI pins are located on
various Arduino boards.
Board
I2C / TWI pins
Uno, Ethernet
A4 (SDA), A5 (SCL)
Mega2560
20 (SDA), 21 (SCL)
Leonardo
2 (SDA), 3 (SCL)
Due
20 (SDA), 21 (SCL), SDA1, SCL1
Functions
begin()
requestFrom()
beginTransmission()
endTransmission()
write()
available()
read()
onReceive()
onRequest()
As of Arduino 1.0, the library inherits from the Stream functions,
making it consistent with other read/write libraries. Because of this,
send() and receive() have been replaced with read() and write().
Note
There are both 7- and 8-bit versions of I2C addresses. 7 bits identify
the device, and the eighth bit determines if it's being written to or
read from. The Wire library uses 7 bit addresses throughout. If you
have a datasheet or sample code that uses 8 bit address, you'll want
to drop the low bit (i.e. shift the value one bit to the right), yielding an
address between 0 and 127.
Reference Home
Corrections, suggestions, and new documentation should be posted to the Forum.
The text of the Arduino reference is licensed under a Creative Commons Attribution-ShareAlike 3.0
License. Code samples in the reference are released into the public domain.
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------De Arduino en I2C
I2c is een protocol dat is uitgevonden door Philips in de jaren 80. Het was oorspronkelijk bedoeld als
bussysteem in bijvoorbeeld in een videorecorder. Je kunt over twee draden data verzenden en ontvangen.
Met een IC, de PCF8574, kun je via i2c totaal 8 uitgangen aansturen. We hebben daarvoor het volgende
schema gebouwd:
De Arduino communiceert via de analoge poorten 4 en 5 serieel met de I/O expander PCF8574 met
respectievelijk SDA en SCL op pen 15 en 14 op het IC zelf. Het hieronder gepresenteerde programma laat
P0 t/m P7 continue knipperen. Voor het gemak is in het schema maar 1 LED aangesloten. Je kunt er zelf
voor kiezen om op P1 tot en met P7 ook een LED in serie met een weerstand te plaatsen. Belangrijk is in
ieder geval dat de massa van de PCF8574 doorverbonden is met de massa (GND) van de Arduino.
arduino code aangepast on3kvv
<code>
/* i2c voorbeeld met de io expander PCF8574
* voorbeeld code uit arduino manual 1_0
* homebrew on4os - on1arf & on3kvv
* opgelet er is een verschil in adress tussen een PCF8574A & PCF8574AP zie de juiste datasheet
* pinout: PCF8574
arduino
*
15 SDA
A4
*
14 SCL
A5
*
08 GND
GND
*
01 GND
GND
*
02 GND
GND
*
03 GND
GND
*
16 VDD
5V
* pin A0,A1,A3 zijn de adresserings pinnen voor je I2C protocol
* pin P1 tot P7 zijn je I-O poorten van je expander ic
*/
#include <Wire.h>
#define expander B0111000
void setup ()
{
Wire.begin();
Serial.begin(9600);
}
void loop()
{
Serial.println("Writing B00000000.");
expanderWrite(B00000000);
Serial.print("Read: ");
Serial.println(expanderRead(), BIN);
delay(1000);
Serial.println("Writing B1111111.");
expanderWrite(B11111111);
Serial.print("Read: ");
Serial.println(expanderRead(), BIN);
delay(1000);
Serial.println("Writing B1111111.");
expanderWrite(B01010101);
Serial.print("Read: ");
Serial.println(expanderRead(), BIN);
delay(1000);
}
void expanderWrite(byte _data)
{
Wire.beginTransmission(expander);
Wire.write(_data);
Wire.endTransmission();
}
byte expanderRead()
{
byte _data;
Wire.requestFrom(expander, 1);
if(Wire.available())
{
_data = Wire.read();
}
return _data;
}
<code>
------------------------------------------------------------------------------------------------------------------------
V1.0 - ON3KVV Dit werk is valt onder een Creative Commons Naamsvermelding 4.0 Internationaal-licentie.
Bronnen: wikipedia, arduino manual 1.0, arduino.cc, dangerousprototypes.com