Arduino接口-week3-Lesson2
来源:互联网 发布:独立开发者 知乎 编辑:程序博客网 时间:2024/06/09 17:32
This series of articles are the study notes of "Interfacing with the Arduino", by Prof. Harris, Department of Computer Science, University of California, Irvine. This article is the notes of week 3, lessen2.
2. Lesson 2
2.1 Lecture 2.1 - I2C Communication
Now we're going to look at a different library. The I2C library, actually it's called the wire library. But it supports what's called I2C communication.
2.1.1 I2C Communication Protocol
So one of the things that you often want to do in a system when you have a micro controller. You want toconnect it to another integrated circuit. Some other chip. Maybe it's a memory chip, maybe it's a digital analog converter, whatever it is. You want to connect your micro controller to other chips. So you can talk to those chips and tell them what to do, grab data from them and so forth. So these chips, when they communicate there are several different protocols. Communication protocols which they can use. The one we're going to talk about right here iscalled I2C.
(1) Synchronous, serial protocol
- serial
I2C communication protocol is a serial protocol. Serial means it sends data over only one wire. Data is only traveling over one wire. This is good,serial protocols are good because they save bits, they save pins rather. You don't need many pins, you just need the one or in this case you need two total pins, but data is only going across one. So you need very few pins. The downside of that is that since it's serial it's sending one bit at a time. So it takes longer, it's slower. If you send eight bits in parallel that's a lot quicker than sending one at a time. So, it's a serial protocol. Which saves pins, which are important.
- synchronous
I2C communication protocol is a synchronous protocol. What that means is that if there are two communicating entities,two integrated circuits, talking about I2C, they need to share the same clock. So they need to get the same clock. They need to share it so they can synchronize with each other. So, based on the clock, the receiver knows when the sender's going to be sending data. The receiver knows when to read the bus through, sender knows when to write. So it's a synchronous protocol.
(2) Multiple master, multiple slaves
And multiple masters and multiple slaves. So we're going to talk about this concept of master and slave. But basically, the master is the one who starts all the communications and the slave is the one who waits for communications, roughly. And you can have multiple masters and multiple slaves in this particular bus protocol.
(3) Bitwise is fixed, independent of number of slaves
Bit width is fixed. So when I say bit width is fixed,the number of bits traveling between these two, between any numbers of devices on an I2C bus is fixed. It's always two. There is the data signal and the clock signal, just two. And it's independent of the number of slaves. There are other protocols where every slave you have to add more bits. But I2C you can have as many slaves as you want, or up to the limit that they enforce. And you can, and it's the same number of bits, two bits.
(4) Two wires SDA (serial data) and SCL (serial clock)
Two wires SDA and SCL. These are the common names.SDA is the data, that's the line where the data's going to travel. And SCL is a clock that's used to synchronize all the devices.
(5) Both lines, SDA and SCL, are open-drain
- Pulled up to high by default
- State of bus is always known
Both lines, SDA and SCL, are open-drain, so what that means is that they are connected to power through a big resistor.
And so what happens is they're pulled up high by default. That means if let's say there are lots of different I2C communicating entities on this bus. They're wired to the SDA and SCL lines. But nobody is talking on the bus. So none of these components are driving those two wires high or low. Because they're connected to power through a big resistor, they will automatically go high. They will be high. Now, if any one of these communicating entities wants to force a zero into the line. They can pull it down to a zero, because it's a very big resistor. So it's a weak pull up. So you can pull it down, but if nobody's talking on the bus, the state of the bus is still defined. You always know that it's going to be high by default. And that's actually useful in the protocol to know, that the state is always either zero or one. It's not some random intermediate value.
2.1.2 Terminology
(1) Master- Initiates and terminates transmission; generates SCL
So some of the terms we're going to be talking about are Bus Protocol. One is the master and slave. So a master is a node on the bus that initiates and terminates transmission. So when transmission starts the master starts it. When it stops the master stops it. Also it generates the clock SCL, so the clock is generated by the master.
(2) Slave- Addressed by the Master
Now the Slave is addressed by the Master. So the Slave basically waits until the Master starts communication and until the Master stops communication. And does whatever the Master says. So these transmissions are either write transmissions or read. Sending to or from. And the slave just essentially obeys the master. If the master says I'm doing a write then the slave knows okay I better listen. I better read. I better read whatever's on the bus.
(3) Transmitter- Placing data on the bus
Now, in addition to that, there's also transmitter and receiver which are orthogonal to master and slave. So the transmitter is whatever node is putting data onto the bus.
(4) Receiver- Reading data from the bus
And the receiver is whatever date known as receiving data from the bus.
Now the master can be the transmitter, it can be the master that starts transmissions and puts data on the bus. Because they're doing a write. They want to send data to the slave,but the master can be the receiver. The master can start a transmission and say, okay slave, this is a reinteract, a retransmission. So it effectively orders the slave to give it information. So the master can also be the receiver, and the slave can be the transmitter or receiver also. So every node is either a master or a slave, and can also be either a receiver or a transmitter, and they can be mixed and matched.
2.1.3 I2C Network
SDA and SCL are bidirectional
This is a picture of what the bus network will typically look like. Each one of those blue blocks is labeled master, slave. Think of that as probably an integrated circuit, or some component that is talking on this bus. And they're labeled master and slave to say what their roles are on the bus. And there are a couple of masters and three slaves. You can mix and match. Each one of those connect to the same set of two lines SDA and SCL. So SDA and SCL are common to all the masters and slaves, all the nodes on the bus. And they share it, and they're bidirectional. So you can talk in two directions, you can talk, data can go either way along those wires. From left to right, right to left, whatever. Also notice that the two lines SDA and SCL are wired to Vcc, meaning power through big resistors. Those resistors aren't labeled, but they are presumably big. 10kΩ or something like that. We'll be talking about networks with just two nodes, one master and one slave, but you can have much larger networks.
2.2 Lecture 2.2 - I2C Transactions
So an I2C transaction, structure of a transaction, means the sequence of zeros and ones that you expect to see on the two lines. The data line and the clock line. In order to send data, to write data or read data. I'm going to go over what the transaction looks like in detail over time.2.2.1 I2C Transaction Structure
So what ones and zeros sequence is expected on those two lines? So one thing to note is that in I2C, every transaction is either a write transaction, meaning the master is writing to the slave, sending data to the slave.Or it's a re-transaction, meaning the master is going to receive data from the slave.(1) Start Condition
Indicates the beginning of a transaction.So, every transaction starts off with a start condition. A start condition is a condition on the two signals, the SDA and SCL that indicates the beginning of the transaction. I'll show you that on the next slide. So the start condition is meeting, once the start condition happens the master creates the start condition on the bus. And then the slaves are all listening to the bus. And they look for the start condition, when they see the start condition they sensibly perk up their ears, They start listening because they know a transaction is about to start.
(2) Address/ Direction Byte
- Specifies slave for communication
So the master, when it's sending data to a particular slave, starting a transaction with a particular slave. It has to send that seven bit long address on the wires, one clock cycle at a time. On an SDA line, say the address is 0000001. So it puts that sequence on the SDA line. One clock cycle at a time, one SCL. So the first SCL clock, you'll have a zero. Next one, you'll have a zero, and so on. It puts them on one at a time.
- Specifies read vs. write transaction
(3) Data Byte(s)
- Transmitted by either master or slave
(4) Stop Condition
- Indicates the end of a transaction
2.2.2 Start and Stop Conditions
(1) Start Condition:
SDA = 1->0SCL = 1
- Falling transition on SDA while SCL=1
(2) Stop Condition:
SDA = 0->1SCL = 1
- Rising transition on SDA while SCL=1
2.3 Lecture 2.3 - Sending Bits
This whole protocol is about sending one bit at a time. First you're sending the address bits, the direction bits. Then you're sending the data bits and so on. So you have to be able to send one bit at a time on these lines.2.3.1 Sending a Bit
(1) SDA and SCL
There are two wires, SDA and SCL. And the data, whatever bit you're going to send, that's going to go on the SDA line. But while the SDA line is going,the SCL line, it's a clock, so its going up and down. So this is showing how sending a bit how a might look. Now you can see at the bottom, the SCL that's low then high then low, that's one clock pulse. The SDA line however, that can change from zero to one, one to zero. The SDA, depending on what bit you're sending, you might be sending a zero or a one.So, notice that in the time diagram picture here for the SDA, you can see that the SDA, you have it on both low and high. That's representing the fact that it could be zero or one.
(2) SDA is sampled by receiver on the rising edge of SCL
But you'll notice there are two places where it crosses, where it starts out low high then it crosses and then it's low high again, then it crosses again. So those crosses what they mean in this diagram is that those are the times when SDA can change value. So all this diagram is trying to say is that, say you want to send a bit, right? A zero or a one, so you put that on SDA. But,SDA is sampled by the receiver on the rising edge of SCL. So, the value of SDA that matters is the value of SDA that SDA has right when the clock edge is going hot. So, you see where that SCL line goes from zero to one, the value at SDA at that time is what counts. So whoever is doing the transmission, they have to apply a value on SDA before the SCL goes high and hold it while SCL is going high.(3) SDA must be constant where SCL is high
Also, there's a rule about this protocol that, SDA, the data line has to be held constant as long as the clock is high. And you can see that in this picture. The SDA line, it can change values but notice that, the changes in value happen either before the clock goes high, or after the clock goes high.Always when the clock is low, that's when the change happens. There can't be a change during the time when the clock is high. That's just a rule. They only time you can break that rule is if you're doing a start condition or a stop condition. Those conditions explicitly violate those rules.
- Exception is Start/Stop Condition
2.3.2 Acknowledge Bit
An acknowledge bit. What this means is the way this protocol's organized. It's organized around sending bites. All data is sent one byte at a time. So you do a start condition. Then you send a byte. The first seven bits of the byte are the address. And then the next one bit is the direction. But all together that's a chuck of eight bytes. Then after you send that byte, let's say it's a right transaction, then you start sending data bytes. One bit at a time, but you send them in chunks of eight. Now, what happens is, after a chunk of eight is sent, after a sequence of eight bits is sent, the receiver has to acknowledge that it received the eight bits.Now this is for error checking essentially, because you never know what kind of error might have happened in communication. So if it didn't receive the eight bits, then you need to restart your communication. So after every chunk of eight, the receiver has to transmit what's called an acknowledge bit.
(1) After each byte is sent, the receiver must acknowledge
Now what that means is, let's say we've got the SDA line, SCL line. SCL's pulsing high low and SDA, somebody, say the master, is transmitting to the slave. So,it transmits eight bits and we've numbered them up there on the SDA line 1, 2, 3, ..., to bit 8. Soonce it sends out all eight of these bits, at the point, for one SCL clock pulse, the master does not drive the SDA line. It's up to the receiver, the receiver at that point to pull the SDA line low.That's called acknowledge. See where it says ACK, notice that the SDA line is low when it says ACK. So for that SCL clock pulse, the receiver has to pull that SCL line low and acknowledge. If it doesn't do that then the sender has to assume that something went wrong and restart the communication.(2) Transmitter releases SDA, receiver must pull SDA low
- Must be low for one pulse of SCL
- If SDA is not pulled low, transmission is aborted
2.3.3 Typical I2C Transaction
- Each slave has a unique 7-bit address
- Direction bit: 0 indicates write, 1 indicates read
0 0
- Arduino接口-week3-Lesson2
- Arduino接口-week1-Lesson2
- Arduino接口-week2-Lesson2
- Arduino接口-week4-Lesson2
- Arduino接口-week3-Lesson1
- Arduino接口-week3-Lesson3
- Arduino 平台与C语言程序设计-week3-Arduino Programs-Lesson2
- Arduino 平台与C语言程序设计-week1-Arduino Environment-Lesson2
- Arduino 平台与C语言程序设计-week4-Arduino Programs-Lesson2
- Arduino 平台与C语言程序设计-week3-Arduino Programs-Lesson1
- Arduino 平台与C语言程序设计-week3-Arduino Programs-Lesson3
- lesson2
- lesson2
- lesson2
- lesson2
- Arduino 平台与C语言程序设计-week2-C Programming-Lesson2
- 物联网与嵌入式系统概论-week3- Hardware and Software-Lesson2: Microcontrollers and Software
- Week3
- HDOJ 5616-Jam's balance【模拟】
- RadioGroup方法总结
- Eclipse下通过Maven的Jetty插件运行Web工程的配置
- 2015杂谈
- Leetcode 91. Decode Ways
- Arduino接口-week3-Lesson2
- 内存管理
- Web之旅第四站——CSS(Cascading style Sheet) 层叠样式表
- KM算法
- 开发者必备的网站
- 自定义旋转的ProgressBar
- addslashes()、stripslashes()和magic_quotes_gpc
- Linux Is Not Matrix——Linux下的Oracle 11g安装
- 【高斯消元】[SGU275]To xor or not to xor