PIC Microprocessor as 12C Slave: Correction to Misleading App Note AN734 - Prof. Erik Good, Papers of Principles of Theater Design

A correction to the misleading information in microchip's application note an734 about using a pic microprocessor as a 12c slave device using 'bit-banging' method. It explains the concept of 12c communication, the role of a master and slave device, and the steps to set up a pic microprocessor as a 12c slave device. It also discusses advanced techniques like clock stretching.

Typology: Papers

Pre 2010

Uploaded on 07/29/2009

koofers-user-5yx
koofers-user-5yx 🇺🇸

10 documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
IIJ
--
Using a PIC microprocessor as a 12Cslave device.
Matt Palmer
Design Team 10
q\ ,1r
.
pf3
pf4
pf5

Partial preview of the text

Download PIC Microprocessor as 12C Slave: Correction to Misleading App Note AN734 - Prof. Erik Good and more Papers Principles of Theater Design in PDF only on Docsity!

IIJ

Using a PIC microprocessor as a 12Cslave device. Matt Palmer Design Team 10

q \ ,1r

.

Abstract:

The microchip website application note AN734 describes a "bit- banging" method to implement 12Cslave routines on a PIC microprocessor. However this application note contains misleading and incorrect information. Microchip has it listed as a 'preliminary' document, and it has not been updated since 2002. Therefore herein this application note methods will be described to use a PIC as an 12Cslave device using 'bit-banging' with some example code. This is useful for being able to do port expansion with a PIC and to be able to have PIC based daughter devices compatible with a primary PIC master.

12C message strings begin with a 'start' condition this is followed by a series of transactions that end with an acknowledge, the first will be the slave address, and others are data to be written or fetched. This whole message is ended with a stop condition. Figure 2: f2'Ccommunication

./~.~ ~ ,,-". .. ""'"""^ (b:-=~'

ni. i~

,MS8 -fUll Ita

Using a PIC as a slave device 0IV~1IiII!IId .-oI!; The basic idea is to have the PIC be an 'always on' device. Reffering to the data sheet for your particular PIC, make sure the TRISC register is set such that SDA and SCL are inputs (usually XXXO OXXX, but your mileage may vary). For convenience sake we are going to set a seriBs of bit variables with easy names that will later be associated with the input/output 12C buffer in the PIC. This code looks something like this(note this is BASIC, but can easily be adapted to C or even to assembly using EQU commands): i2c Demand i2c_IRQACK ### var bit ### var bit ## \ ~\Ji ### ~~ ~i, ~fI/ '?' 11,,\ ';pv \{J 0 ~ -l;~ r\~cV. '" t. ## (\~ ~y< rJ~ ~ _~F_ i2c address i2c outL ### var byte ### var byte ### i2c outH var byte _r_ Notice that some of these are singe bit values and some are bytes ( bits). i2c- OutL = RxBuffer[O] i2c_OutH = $ff II assuming pic is only slave, otherwise this will be lithe address bit ### This bit of code sets the device address byte to a constant 11111111 and only looks at the low byte (internal address). Next we use a series of case statements, in this case we assume the PIC is the only slave, however if there would be multiple you would set - - "..., ### i2c_address byte = RxBuffer[1] and use a while statement for the duration the slaves device address is valid. For internal address this example uses ASCII characters to make case statements easier to follow Or if you are only outputting one byte of data set TxBuffer[1] to all $ or $FF depending on signed or unsigned. This only describes the basic functions of how to use a PIC as a slave device on an 12Cbus. More advanced would be outputting a stream of data (requires watching of the acknowledge bit. Also it is possible to perform some math or logic functions on the PIC, since this will take multiple clock cycles and the master wants the data right now, a technique called clock stretching is required. All you muse is set whatever port SCL is (again check your particular PIC's data sheet) and write a zero here, this will force the clock low, and the master will wait for a rising pulse before it expects data, this is done by writing a 1 to the SCL line at the end of the slaves routine, which releases the clock back to the masters control. ### \,'0"^ ,,11"\\-\ Ct^0 \\ \ ~ LUQ.; r0. \\jO0.° ~'\. P 0 ~~