a password para a conta sshtunnel no ccams02: 201415sshtunnel
// for accessing the configuration register StartI2C(); // begin I2C communication IdleI2C(); WriteI2C( 0x49 ); // sends address to the device (R/W = 0, we are going to write in the next device being addressed) IdleI2C(); WriteI2C( 0xAC ); // sends a control byte to the device to acces the configuration register IdleI2C(); WriteI2C( 0x02 ); // sends configuration byte – continuous conversion, active high polarity // for reading temperature RestartI2C(); // briefly restart I2C communications IdleI2C(); WriteI2C( 0x49 ); IdleI2C(); WriteI2C(0xEE); // send command to begin temperature conversions IdleI2C(); StopI2C(); // stop all I2C communications StartI2C(); // start I2C communication IdleI2C(); WriteI2C( 0x49 ); // address the chip IdleI2C(); WriteI2C( 0xAA ); // access the temperature register IdleI2C(); RestartI2C(); // Initiate a RESTART command IdleI2C(); WriteI2C( 0x49+0x01 ); // address device w/read IdleI2C(); char temperatureHI = ReadI2C(); // Returns the MSB byte AckI2C(); // Send back Acknowledge IdleI2C(); char temperatureLO = ReadI2C(); // returns the LSB of the temperature IdleI2C(); NotAckI2C(); // send a not-acknowledge to tell the device to stop sending data }
I2C can be used to connect up to 127 nodes via a bus that only requires two data wires, known as SDA and SCL. Since the devices need a common voltage reference (Ground) and a power line (referred to as Vcc or Vdd) the full bus has four wires, two for data and two for power. Sometimes additional lines are added, for example to support interrupts when the state of I2C devices change. In theory the I2C bus can support multiple masters, but most micro-controllers can’t. A master is usually a microcontroller, although it doesn’t have to be. Slaves can be ICs or microcontrollers. In the diagram above a RasPi is a master and there are several slaves: a Digital-to-Analog converter (DAC), an Analog-to-Digital converter (DC), and an Arduino. I2C can be used to control a very wide range of devices. Common examples include
When the master wishes to communicate with a slave it sends a series of pulses down the SDA and SCL lines. The data that is sent includes an address that identifies the slave with which the master needs to interact. Addresses take 7 bits out of a data byte; the remaining bit specifies whether the master wishes to read (get data from a slave) or write (send data to a slave). Some devices have an address that is entirely fixed by the manufacturer; others can be configured to take one of a range of possible addresses. When a micro-controller is used as a slave it is normally possible to configure its address by software, and for that address to take on any of the 127 possible values. The address byte may be followed by one or more byes of data, which may go from master to slave or from slave to master. When data is being sent on the SDA line, clock pulses are sent on the SCL line to keep master and slave synchronised. Since the data is sent one bit at a time, the data transfer rate is one eighth of the clock rate. The original standard specified a standard clock rate of 100KHz, and most I2C chips and micro-controllers can support this. Later updates to the standard introduced a fast speed of 400KHz and a high speed of 1.7 or 3.4 MHz. The Arduino can support standard and fast speeds; the BeagleBoard has three I2C busses, one at each speed; the RasPi and BeagleBone can both support standard and fast I2C. The fast rate corresponds to a data transfer rate of 50K bytes/sec which is too slow for some control applications. One option in that case is to use SPI instead of I2C.