1. Trang chủ
  2. » Giáo án - Bài giảng

AN0578 use of the SSP module in the i2c multi master environment

17 176 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 17
Dung lượng 185,18 KB

Nội dung

M AN578 Use of the SSP Module in the I2C Multi-Master Environment Author: Scott Fink Microchip Technology Inc INTRODUCTION The Inter-IC (I2C) bus is a two-wire serial interface developed by Philips/Signetics The specification supports data transmission up to 400 Kbps The I2C interface employs a comprehensive protocol to ensure reliable transmission and reception of data When the bus is active, one device is the Master (generates the clock and the handshaking signals), while all the other devices are Slaves The current bus Master can both read-from and write-to any of the Slave units by addressing them individually On a Multi-Master bus the Masters follow an arbitration scheme to ensure that the bus is not corrupted Each device attached to the I2C bus is assigned a unique address When a Master wishes to initiate a data transfer, it first transmits the address of the device that it wishes to “talk” to All devices “listen” to see if this is their address Within this address, a bit specifies whether the Master wishes to read-from or write-to the Slave device The output stages of each device on the bus, attached to the clock (SCL) and data (SDA) lines, must have an open-drain or open-collector in order to perform the wired-AND function of the bus External pull-up resistors are used to ensure a high level when no device is pulling the line down The only limitation on the number of devices that may be attached to the bus is the maximum bus loading specification For complete bus specifications, refer to Philips/Signetics document “The I2C-bus and How to Use It” (www.semiconductors.philips.com)  1997 Microchip Technology Inc INITIATING AND TERMINATING DATA TRANSFER During times of no data transfer (idle time), both the SCL and SDA lines are pulled high A Master device which wishes to take control of the bus must first generate a START condition A START is defined as a high to low transition of SDA when SCL is high When the Master has completed all data transmissions and wishes to relinquish the bus, it generates a STOP condition A STOP is defined as a low to high transition of SDA while SCL is high Because the START and STOP conditions are defined as transitions of the SDA when the SCL line is high, the SDA line can only change when SCL is low during the actual data transmission Figure shows the relationship between SCL and SDA for the various conditions FIGURE 1: START AND STOP CONDITIONS SDA SCL S Start Condition P Change of Data Allowed Change of Data Allowed Stop Condition DS00578B-page AN578 ADDRESSING I2C DEVICES TRANSFER ACKNOWLEDGE There are two address formats The simplest of these is the 7-bit address format with a R/W bit (Figure 2) The more complex is the 10-bit address with a R/W bit (Figure 3) For 10-bit addressing, two bytes must be transmitted with the first five bits specifying this to be a 10-bit address Only 7-bit addressing is used in this application note Slave as Receiver FIGURE 2: All data is transmitted as bytes, with no limit to the number of bytes transmitted per data transfer After each byte, the slave-receiver generates an acknowledge bit (ACK) by pulling the SDA line low When a slave-receiver doesn’t acknowledge the slave address or received data, the master aborts the transfer Whether the ACK bit is generated or not, the SDA line must be released by the slave so that the master can generate the STOP condition 7-BIT ADDRESS FORMAT MSb LSb Master as Receiver R/W S Slave Address S R/W ACK - ACK If the master is receiving the data, it generates an acknowledge signal for each received byte of data except for the last byte To signal the end of data to the slave-transmitter, the master does not generate an acknowledge The slave then releases the SDA line so the master can generate the STOP condition The master can also generate the STOP condition during the acknowledge pulse for valid termination of data transfer Sent by Slave Start Condition Read/Write bit Acknowledge If the slave needs to delay the transmission of the next byte, holding the SCL line low will force the master into a wait state Data transfer continues when the slave releases the SCL line This allows the slave to move the received data or fetch the data it needs to transfer before allowing the clock to start This wait state technique can also be implemented at the bit level FIGURE 3: 10-BIT ADDRESS FORMAT S 1 1 A9 A8 R/W ACK A7 A6 A5 A4 A3 A2 A1 A0 ACK Sent by Slave = for write S R/W ACK - DS00578B-page Start Condition Read/Write bit Acknowledge  1997 Microchip Technology Inc AN578 MULTI-MASTER Clock Synchronization The I2C protocol allows a system to have more than one master When two or more masters try to transfer data at the same time, arbitration and synchronization occur Clock synchronization occurs after the devices have started arbitration This is performed using a wired-AND connection to the SCL line A high to low transition on the SCL line causes the concerned devices to start counting off their low period Once a device clock has gone low, it will hold the SCL line low until its SCL high state is reached The low to high transition of this clock may not change the state of the SCL line, if another device clock is still within its low period The SCL line is held low by the device with the longest low period Devices with shorter low periods enter a high wait-state, until the SCL line comes high When the SCL line comes high, all devices start counting off their high periods The first device to complete its high period will pull the SCL line low The SCL line high time is determined by the device with the shortest high period (Figure 5) Arbitration Arbitration takes place on the SDA line while the SCL line is high The master which transmits a high when the other master transmits a low loses arbitration (Figure 4) and turns off its data output stage A master which lost arbitration can generate clock pulses until the end of the data byte where it lost arbitration When the master devices are addressing the same device, arbitration continues into the data FIGURE 4: MULTI-MASTER ARBITRATION Transmitter Loses Arbitration Data = SDA DATA DATA SDA SDL S FIGURE 5: CLOCK SYNCHRONIZATION Wait State Start Counting High Period CLK1 CLK2 Counter Reset SCL  1997 Microchip Technology Inc DS00578B-page AN578 IMPLEMENTATION IN THE PIC16CXXX This Application Note uses the PIC16CXXX in a Multi-Master I2C environment The PIC16CXXX acts as both a Master and a Slave on the bus Hardware The demonstration hardware consists of a keypad multiplexed with eight LEDs on PORTB and connections to the I2C bus through the RC3/SCL and RC4/SDA pins (Figure 6) Software The software transmits in Master mode and receives data in Slave mode DS00578B-page The transmit routine scans the keypad, debounces the keypresses, and transmits their encoded value in Master mode over the I2C bus to slave address A6 Before transmitting, the status of the Synchronous Serial Port (SSP) is checked No data is sent until the SSP status register indicates that a Stop bit was received If the transmission causes any errors, the error code will be displayed on the LEDs, and transmission will again be attempted Data received in Slave mode at address A2 from the bus is displayed on the LEDs Slave reception is interrupt driven When a complete word is received with the proper address, the processor is interrupted, and the program verifies that an SSP interrupt was received Once it has been verified that the interrupt was caused by the SSP module, and that the buffer is full, the data word is read and output to the LEDs  1997 Microchip Technology Inc AN578 FIGURE 6: HARDWARE +5V C9 8.1 11 32 MCLR 13 MHz 14 10 15 16 17 I2C SCL SDA BUS 18 23 24 VDD VSS MCLR/VPP OSC1/CLKIN OSC2/CLKOUT RA0 RA1 RA2 RA3 RA4 RA4 RE0 RE1 RE2 RC0 RC1 RC2/CCP1 RC3/SCK/SCL RC4/SDI/SDA RC5/SDO U1 RB7 RB6 RB5 RB4 RB3 RB2 RB1 RB0 RD7 RD6 RD5 RD4 RD3 RD2 RD1 RD0 RC7 RC6 VSS VSS 40 39 38 37 36 35 34 33 30 29 28 23 22 21 20 19 26 25 12 31 PIC16CXXX C 3 D E F B A 8x470  1997 Microchip Technology Inc DS00578B-page AN578 Please check the Microchip BBS for the latest version of the source code Microchip’s Worldwide Web Address: www.microchip.com; Bulletin Board Support: MCHIPBBS using CompuServe® (CompuServe membership not required) APPENDIX A: IICMULT.ASM MPASM 01.40 Released LOC OBJECT CODE VALUE 00000020 00000021 00000022 00000023 00000024 00000025 00000026 00000027 00000028 00000029 0000002A 0000002B 0000002C 0000002D 0000002E 0000002F 00000030 00000031 DS00578B-page IICMULT.ASM 1-16-1997 17:01:20 PAGE LINE SOURCE TEXT 00001 00002 00003 00004 00005 00006 00007 00008 00009 00010 00011 00012 00013 00014 00015 00016 00017 00018 00019 00020 00021 00022 00023 00024 00025 00026 00001 00002 00238 00027 00028 00029 00030 00031 00032 00033 00034 00035 00036 00037 00038 00039 00040 00041 00042 00043 00044 00045 00046 00047 00048 00049 00050 00051 TITLE " Demonstration of I2C MultiMaster mode" ; LIST P=16C64 ERRORLEVEL -302 ; ;************************************************************************ ;** Two wire/I2C Bus MultiMaster sample routines for Microchip's ;** PIC16C64 8-bit CMOS single chip microcomputer ;** Revised Version (3/06/94) ; Program: IICMULT.ASM ; Revision Date: ; 1-15-97 Compatibility with MPASMWIN 1.40 ; ;** ;** Part used = PIC16C64 ;**Note: 1)All timings are based on a reference crystal frequency of ;** 4MHz which is equivalent to an instruction cycle time of usec ;** 2) Address and literal values are read in decimal unless ;** otherwise specified ;************************************************************************ ; ; -; File Register Assignment ; -; include LIST ;P16C64.INC Standard Header File, Version 1.01 Microchip Technology LIST ; FLAG EQU 20h ; Common flag bits register EEPROM EQU 21h ; Bit buffer ERCODE EQU 22h ; Error code (to indicate bus status) ADDR EQU 23h ; Address register DATAI EQU 24h ; Stored data input register DATAO EQU 25h ; Stored data output register SLAVE EQU 26h ; Device address (1010xxx0) TXBUF EQU 27h ; TX buffer RXBUF EQU 28h ; RX buffer COUNT EQU 29h ; Bit counter TEMP EQU 2Ah ; Temporary storage ROW EQU 2Bh ; Keypad row NEW_KEY EQU 2Ch ; Storage for latest key OLD_KEY EQU 2Dh ; Storage for last key pressed DISPVAL EQU 2Eh ; Value displayed on LEDs TEMP1 EQU 2Fh ; Scratchpad register TEMP_W EQU 30h ; Storage for W register TEMP_STAT EQU 31h ; Storage for STATUS register ; ; -; Bit Assignments ; -; FLAG Bits  1997 Microchip Technology Inc AN578 00000000 00000007 00000006 00000004 00000003 0000 0000 2810 0004 0004 28EB 0010 0010 0010 0011 0012 0013 0014 0015 0016 0017 0018 0019 001A 001B 001C 001D 001E 001F 0020 0021 0022 0023 0023 0024 0025 0026 0027 0028 0029 002A 002B 002C 002D 002E 002F 002F 0030 0031 0032 0033 0033 0034 0035 0036 01AE 01A0 0187 3008 00AB 303E 0094 1683 0186 3008 008C 30A2 0093 1587 1607 1283 118C 30C0 008B 01AA 216B 2102 1283 082C 39FF 1903 2823 00A5 30A6 00A6 1683 1A14 2833 1994 282F 1283 138B 203F 1C20 00052 00053 00054 00055 00056 00057 00058 00059 00060 00061 00062 00063 00064 00065 00066 00067 00068 00069 00070 00071 00072 00073 00074 00075 00076 00077 00078 00079 00080 00081 00082 00083 00084 00085 00086 00087 00088 00089 00090 00091 00092 00093 00094 00095 00096 00097 00098 00099 00100 00101 00102 00103 00104 00105 00106 00107 00108 00109 00110 00111 00112 00113 00114 00115 00116 00117  1997 Microchip Technology Inc ERR_1 EQU ; Error flag ; EEPROM input ; EEPROM output ; EEPROM Bits DI DO EQU EQU ; I2C Device Bits SDA SCL EQU EQU ; RB7, data in/out ; RB6, serial clock ;End of files/bits equate ORG goto 00h starting ; Reset Vector ORG goto 04h service_int ; Interrupt Vector ORG 10h ; Begining of Program space clrf clrf clrf movlw movwf movlw movwf bsf clrf movlw movwf movlw movwf bsf bsf bcf bcf movlw movwf DISPVAL FLAG PORTC 08h ROW B'00111110' SSPCON STATUS,RP0 TRISB B'00001000' PIE1 b'10100010' SSPADD TRISC, TRISC, STATUS,RP0 PIR1,3 B'11000000' INTCON ; Blank out LEDs ; Clear error register ; Set SDA, SCL low when not tri-stated clrf call call bcf movf andlw btfsc goto movwf movlw movwf bsf CheckAgain btfsc goto btfsc goto Goxmit bcf bcf call btfss TEMP SetupDelay ScanKbd STATUS,RP0 NEW_KEY,W 0FFh STATUS,Z KbdWait DATAO B'10100110' SLAVE STATUS,RP0 starting ; ; ; ; ; ; Set initial row to be strobed I2C bit slave mode with master mode enabled Select Bank1 Set PORT_B to all outputs Enable SSP interrupt ; Slave address ; ; ; ; ; Set SCL high Set SDA high Select Bank0 Clear SSP interrupt flag Enable interrupts KbdWait ; Check for key pressed ; Get latest key ; Key pressed? ; No, go check again ; Yes, output it on I2C bus ; Address of device being addressed SSPSTAT,4 Goxmit SSPSTAT,3 CheckAgain ;If STOP bit received last ; OK to transmit ;If START bit recieved last ; wait for STOP bit STATUS,RP0 INTCON,7 WRBYTE FLAG,ERR_1 ; Disable interrupts ; Output byte ; Check for error DS00578B-page AN578 0037 0038 0039 003A 003B 003C 003D 003D 003E 003F 003F 0040 0041 0042 0043 0044 0045 0046 0047 0048 0049 004A 004A 004B 004C 004D 004E 004F 0050 0051 0052 0053 0054 0055 0056 0057 0058 0059 005A 005B 005C 005D 005E 283D 0822 0086 00AE 1020 282F 178B 2823 1283 0826 00A7 20BF 2071 1283 0825 00A7 2071 20CD 0008 1283 0826 00A7 20BF 2071 1283 0823 00A7 2071 20BF 1283 0826 00A7 1427 2071 205F 20CD 1283 0828 00A4 0008 DS00578B-page 00118 00119 00120 00121 00122 00123 00124 00125 00126 00127 00128 00129 00130 00131 00132 00133 00134 00135 00136 00137 00138 00139 00140 00141 00142 00143 00144 00145 00146 00147 00148 00149 00150 00151 00152 00153 00154 00155 00156 00157 00158 00159 00160 00161 00162 00163 00164 00165 00166 00167 00168 00169 00170 00171 00172 00173 00174 00175 00176 00177 00178 00179 00180 00181 00182 00183 goto movf movwf movwf bcf goto Checkout ERCODE,W PORTB DISPVAL FLAG,ERR_1 CheckAgain ; No error, go on ; Get error code ; Put error code on LEDs INTCON,7 KbdWait ; Enable interrupts ; Clear error flag Checkout bsf goto ; -; BYTE-WRITE, write one byte to I2C (Master Mode) ; -; Input : DATAO = data to be written ; ADDR = destination address ; SLAVE = device address (1010xxx0) ; Output : Data written to EEPROM device ; -; WRBYTE bcf STATUS,RP0 movf SLAVE,W ; Send SLAVE address movwf TXBUF ; to TX buffer call BSTART ; Generate START bit call TX ; Output SLAVE data address bcf STATUS,RP0 movf DATAO,W ; Move DATA movwf TXBUF ; into transmit buffer call TX ; Output DATA and detect acknowledgement call BSTOP ; Generate STOP bit return ; -; BYTE-READ, read one byte from I2C (Master Mode) ; -; Input : ADDR = source address ; SLAVE = device address (1010xxx0) ; Output : DATAI = data read from serial EEPROM ; -RDBYTE bcf movf movwf call call bcf movf movwf call call bcf movf movwf bsf call call call bcf movf movwf return STATUS,RP0 SLAVE,W TXBUF BSTART TX STATUS,RP0 ADDR,W TXBUF TX BSTART STATUS,RP0 SLAVE,W TXBUF TXBUF,0 TX RX BSTOP STATUS,RP0 RXBUF,W DATAI ; Move SLAVE address ; into buffer (R/W = 0) ; Generate START bit ; Output SLAVE address Check ACK ; Put slave data address into ; Xmit buffer ; Output WORD address Check ACK ; START READ ; ; ; ; ; Specify READ mode (R/W = 1) Output SLAVE address READ in data and acknowledge Generate STOP bit ; Save data from buffer ; to DATAI file ; -; RECEIVE eight data bits subroutine ;  1997 Microchip Technology Inc AN578 005F 005F 0060 0061 0062 0063 0063 0064 0065 0066 0067 0068 0069 006A 006B 006C 006D 006E 006F 0070 1283 3008 00A9 01A8 0DA8 1C03 1028 1803 1428 2087 1283 1BA1 1428 0BA9 2863 1721 209E 3400 0071 0071 1283 0072 3008 0073 00A9 0074 0074 0075 0076 0077 0078 0079 007A 007B 007C 007D 007E 007F 0080 0081 0082 0083 0084 0085 0086 1321 1BA7 1721 209E 1283 0DA7 1C03 1027 1803 1427 0BA9 2874 2087 1283 3003 1BA1 20DE 1283 3400 00184 00185 00186 00187 00188 00189 00190 00191 00192 00193 00194 00195 00196 00197 00198 00199 00200 00201 00202 00203 00204 00205 00206 00207 00208 00209 00210 00211 00212 00213 00214 00215 00216 00217 00218 00219 00220 00221 00222 00223 00224 00225 00226 00227 00228 00229 00230 00231 00232 00233 00234 00235 00236 00237 00238 00239 00240 00241 00242 00243 00244 00245 00246 00247 00248 00249  1997 Microchip Technology Inc ; Input : None ; Output : RXBUF = 8-bit data received ; -RX bcf movlw movwf clrf STATUS,RP0 COUNT RXBUF rlf btfss bcf btfsc bsf call bcf btfsc bsf decfsz goto bsf call retlw RXBUF, F 3,0 RXBUF,0 3,0 RXBUF,0 BITIN STATUS,RP0 EEPROM,DI RXBUF,0 COUNT, F RXLP EEPROM,DO BITOUT ; bits of data ; RXLP ; Shift data to buffer ; carry -> f(0) ; Input bit =1 ; bits? ; Set acknowledge bit = ; to STOP further input ; -; TRANSMIT data bits subroutine ; -; Input : TXBUF ; Output : Data X'mitted to EEPROM device ; -TX bcf movlw movwf STATUS,RP0 COUNT bcf btfsc bsf call bcf rlf btfss bcf btfsc bsf decfsz goto call bcf movlw btfsc call bcf retlw EEPROM,DO TXBUF,7 EEPROM,DO BITOUT STATUS,RP0 TXBUF, F 3,0 TXBUF,0 3,0 TXBUF,0 COUNT, F TXLP BITIN STATUS,RP0 EEPROM,DI ERR STATUS,RP0 ; TXLP ; ; ; ; Shift data bit out If shifted bit = 0, data bit = Otherwise data bit = Serial data out ; ; ; ; Rotate TXBUF left f(6) -> f(7) f(7) -> carry carry -> f(0) ; bits done? ; No ; Read acknowledge bit ; Check for acknowledgement ; No acknowledge from device ; -; Single bit receive from I2C to PIC ; -; Input : None ; Output : Data bit received ; DS00578B-page AN578 0087 0087 0088 0089 008A 008B 008C 008D 008E 008F 0090 0091 0092 0093 0094 0095 0095 0096 0097 0098 0099 009A 009A 009B 009C 009D 009E 009E 009F 00A0 00A1 00A2 00A3 00A4 00A5 00A6 00A7 00A8 00A9 00AA 00AB 00AB 00AC 00AD 00AE 00AF 00B0 00B0 00B1 00B2 00B3 00B4 00B5 00B6 00B7 00B8 1683 1607 1283 13A1 1683 1587 3001 1283 1987 2895 1283 1C20 00A2 1420 1283 1E07 289A 1283 17A1 1683 0000 1187 3400 1283 1F21 28AB 1683 1607 3002 1283 1A07 28B0 1C20 00A2 1420 28B0 1683 1207 0000 0000 0000 1683 1587 3001 1283 1987 28BA 1283 1C20 00A2 DS00578B-page 10 00250 00251 00252 00253 00254 00255 00256 00257 00258 00259 00260 00261 00262 00263 00264 00265 00266 00267 00268 00269 00270 00271 00272 00273 00274 00275 00276 00277 00278 00279 00280 00281 00282 00283 00284 00285 00286 00287 00288 00289 00290 00291 00292 00293 00294 00295 00296 00297 00298 00299 00300 00301 00302 00303 00304 00305 00306 00307 00308 00309 00310 00311 00312 00313 00314 00315 BITIN bsf bsf bcf bcf bsf bsf movlw bcf btfsc goto bcf btfss movwf bsf STATUS,RP0 TRISC,SDA STATUS,RP0 EEPROM,DI STATUS,RP0 TRISC,SCL STATUS,RP0 PORTC,SCL BIT1 STATUS,RP0 FLAG,ERR_1 ERCODE FLAG,ERR_1 bcf btfss goto bcf bsf STATUS,RP0 PORTC,SDA ACKOK STATUS,RP0 EEPROM,DI bsf nop bcf retlw STATUS,RP0 ; Set SDA for input ; Clock high ; Skip if SCL is high ; Remain as first error encountered ; Save error code ; Set error flag BIT1 ; Read SDA pin, for ACK low ; DI = ACKOK TRISC,SCL ; Delay ; Return SCL to low ; -; Single bit data transmit from PIC to I2C ; -; Input : EEPROM register, bit DO ; Output : Bit transmitted over I2C ; Error bits set as necessary ; -BITOUT bcf btfss goto bsf bsf movlw bcf btfsc goto btfss movwf bsf goto STATUS,RP0 EEPROM,DO BIT0 STATUS,RP0 TRISC,SDA STATUS,RP0 PORTC,SDA CLK1 FLAG,ERR_1 ERCODE FLAG,ERR_1 CLK1 bsf bcf nop nop nop STATUS,RP0 TRISC,SDA bsf bsf movlw bcf btfsc goto bcf btfss movwf STATUS,RP0 TRISC,SCL STATUS,RP0 PORTC,SCL BIT2 STATUS,RP0 FLAG,ERR_1 ERCODE ; Output bit ; Check for error code ; ; ; ; Remain as first error encountered Save error code Set error flag SDA locked low by device ; BIT0 ; Output bit ; Delay CLK1 ; Error code ; SCL locked low? ; No ; Yes ; Save error code  1997 Microchip Technology Inc AN578 00B9 00BA 00BA 00BB 00BC 00BD 00BE 00BF 00BF 00C0 00C1 00C2 00C3 00C4 00C5 00C6 00C7 00C8 00C9 00CA 00CB 00CC 00CD 00CD 00CE 00CF 00D0 00D1 00D2 00D3 00D4 00D5 00D6 00D7 00D8 00D9 00DA 00DB 00DC 00DD 1420 0000 0000 1683 1187 3400 1683 1607 1587 3001 1283 1D87 20DE 1683 1207 0000 0000 0000 1187 3400 1683 1207 1587 0000 0000 0000 3001 1283 1D87 20DE 1683 1607 3004 1E07 20DE 1283 3400 00316 00317 00318 00319 00320 00321 00322 00323 00324 00325 00326 00327 00328 00329 00330 00331 00332 00333 00334 00335 00336 00337 00338 00339 00340 00341 00342 00343 00344 00345 00346 00347 00348 00349 00350 00351 00352 00353 00354 00355 00356 00357 00358 00359 00360 00361 00362 00363 00364 00365 00366 00367 00368 00369 00370 00371 00372 00373 00374 00375 00376 00377 00378 00379 00380 00381  1997 Microchip Technology Inc bsf FLAG,ERR_1 ; Set error flag nop nop bsf bcf retlw STATUS,RP0 TRISC,SCL ; Return SCL to low BIT2 ; -; START bit generation routine ; -; input : none ; output : initialize bus communication ; -;Generate START bit (SCL is high while SDA goes from high to low ;transition) and check status of the serial clock BSTART bsf STATUS,RP0 bsf TRISC,SDA ; Make sure SDA is high bsf TRISC,SCL ; Set clock high movlw ; Ready error status code bcf STATUS,RP0 btfss PORTC,SCL ; Locked? call ERR ; SCL locked low by device, flag error bsf STATUS,RP0 bcf TRISC,SDA ; SDA goes low during SCL high nop ; Timing adjustment, 1.5uS @2MHz nop nop bcf TRISC,SCL ; Start clock train retlw ; -; STOP bit generation routine ; -; Input : None ; Output : Bus communication, STOP condition ; -;Generate STOP bit (SDA goes from low to high during SCL high state) ;and check bus conditions BSTOP bsf bcf bsf nop nop nop movlw bcf btfss call bsf bsf movlw btfss call bcf retlw STATUS,RP0 TRISC,SDA TRISC,SCL STATUS,RP0 PORTC,SCL ERR STATUS,RP0 TRISC,SDA TRISC,SDA ERR STATUS,RP0 ; Return SDA to low ; Set SCL high ; Ready error code ; High? ; No, SCL locked low by device ; ; ; ; SDA goes low to high during SCL high Ready error code High? No, SDA bus not release for STOP ; -; Two wire/I2C - CPU communication error status table and subroutine ; -; input : W-reg = error code DS00578B-page 11 AN578 00DE 00DE 00DF 00E0 00E1 00E2 00E3 00E3 00E4 00E5 00E5 00E6 00E7 00E8 00E9 00EA 00EB 00EB 00EC 00ED 00EE 00EF 00F0 00F1 00F2 00F3 00F4 00F5 00F6 00F7 00F8 00F9 00FA 00FA 00FB 00FC 1283 1C20 00A2 1420 3400 1283 01AA 0000 0000 0000 0BAA 28E5 3400 118C 00B0 0E03 00B1 1683 1C14 28FA 1283 0813 1683 1E94 28FA 1283 0086 00AE 1683 158C 1283 DS00578B-page 12 00382 00383 00384 00385 00386 00387 00388 00389 00390 00391 00392 00393 00394 00395 00396 00397 00398 00399 00400 00401 00402 00403 00404 00405 00406 00407 00408 00409 00410 00411 00412 00413 00414 00415 00416 00417 00418 00419 00420 00421 00422 00423 00424 00425 00426 00427 00428 00429 00430 00431 00432 00433 00434 00435 00436 00437 00438 00439 00440 00441 00442 00443 00444 00445 00446 00447 ; output : ERCODE = error code ; FLAG(ERR_1) = ; ; code error status mode ; -; : SCL locked low by device (bus is still busy) ; : SDA locked low by device (bus is still busy) ; : No acknowledge from device (no handshake) ; : SDA bus not released for master to generate STOP bit ; -; ;Subroutine to identify the status of the serial clock (SCL) and serial ;(data SDA) condition according to the error status table.Codes ;generated are useful for bus/device diagnosis ERR bcf btfss movwf bsf retlw STATUS,RP0 FLAG,ERR_1 ERCODE FLAG,ERR_1 ; Keep first error reported ; Save error code ; Set error flag ; -; DELAY, Provide a 1.54mS delay ; -; Input : None ; Output : None ; -delay bcf clrf STATUS,RP0 TEMP nop nop nop decfsz goto retlw TEMP, F dly1 ;clear last location dly1 ;reduce count ;loop ; -; Interrupt service routine ; Only the SSP interrupt is enabled This routine will read the I2C ; data and output it on the LEDs ; -service_int bcf movwf swapf movwf bsf btfss goto bcf movf bsf btfss goto bcf movwf movwf IntOut bsf bsf bcf PIR1,3 TEMP_W STATUS,W TEMP_STAT STATUS,RP0 SSPSTAT,0 IntOut STATUS,RP0 SSPBUF,W STATUS,RP0 SSPSTAT,5 IntOut STATUS,RP0 PORTB DISPVAL STATUS,RP0 PIE1,3 STATUS,RP0 ; ; ; ; Clear SSP interrupt Save W register Get STATUS register Save STATUS register ; Check Buffer Full Flag ; No data received, so exit ; Get I2C data ; If Address received last ; exit without saving it ; Display received data on LEDs ; Re-enable SSP interrupt  1997 Microchip Technology Inc AN578 00FD 00FE 00FF 0100 0101 0102 0102 0103 0104 0105 0106 0107 0107 0108 0109 010A 010B 010C 010D 010D 010E 010F 0110 0111 0112 0113 0114 0115 0116 0117 0118 0119 011A 011B 011C 011C 011D 011E 011F 0120 0121 0122 0123 0124 0125 0126 0127 0128 0129 012A 012B 012C 012D 012E 012F 0130 0131 0132 0133 0E31 0083 0EB0 0E30 0009 1283 01AC 082D 1D03 291C 1003 0DAB 1C03 290D 3010 00AB 0186 1683 300F 0086 1283 082B 0086 300F 0506 1903 2933 0186 042B 00AC 00AD 0186 1683 0186 1283 082E 0086 01AA 216B 216B 0186 1683 300F 0086 1283 082B 0086 082D 0606 1903 2933 0186 01AC 01AD 00448 00449 00450 00451 00452 00453 00454 00455 00456 00457 00458 00459 00460 00461 00462 00463 00464 00465 00466 00467 00468 00469 00470 00471 00472 00473 00474 00475 00476 00477 00478 00479 00480 00481 00482 00483 00484 00485 00486 00487 00488 00489 00490 00491 00492 00493 00494 00495 00496 00497 00498 00499 00500 00501 00502 00503 00504 00505 00506 00507 00508 00509 00510 00511 00512 00513  1997 Microchip Technology Inc swapf movwf swapf swapf retfie TEMP_STAT,W STATUS TEMP_W,1 TEMP_W,W ; Restore STATUS register ; Restore W register ; -; Keyboard scan routine ; This routine scans the keypad connected to PORT_B, and ; returns the pressed key in New_Key ; -ScanKbd bcf clrf movf btfss goto STATUS,RP0 NEW_KEY OLD_KEY,w STATUS,Z Debounce bcf rlf btfss goto movlw movwf STATUS,C ROW, F STATUS,C Notdone 010h ROW clrf bsf movlw movwf bcf movf movwf movlw andwf btfsc goto clrf iorwf movwf movwf PORTB STATUS,RP0 00Fh TRISB STATUS,RP0 ROW,W PORTB 0Fh PORTB,W STATUS,Z KBDOUT PORTB ROW,W NEW_KEY OLD_KEY ; Clear key register ; If key was pressed last pass through ; goto Debounce Kbdloop ; Select next row to strobe ; Start over at first row Notdone ; Set PORT_B for keypad read ; ; ; ; Output Row Read colums Mask out rows Check for Key press ; No key pressed, exit ; Key pressed, save it Debounce clrf bsf clrf bcf movf movwf clrf call call clrf bsf movlw movwf bcf movf movwf movf xorwf btfsc goto clrf clrf clrf PORTB STATUS,RP0 TRISB STATUS,RP0 DISPVAL,W PORTB TEMP SetupDelay SetupDelay PORTB STATUS,RP0 0Fh TRISB STATUS,RP0 ROW,W PORTB OLD_KEY,W PORTB,w STATUS,Z KBDOUT PORTB NEW_KEY OLD_KEY ; Set LEDs ; Delay for key debounce ; Set PORT_B for keypad read ; Output Row ; Compare key with last key pressed ; Key released, clear registers KBDOUT DS00578B-page 13 AN578 0133 0134 0135 0136 0137 0138 0138 0139 013A 013B 013C 013D 013E 013E 013F 0140 0141 0142 0143 0144 0145 0146 0147 0148 0149 0149 014A 014B 014C 014D 014E 014F 0150 0151 0152 0153 0154 0155 0155 0156 0156 0157 0158 0159 015A 015B 015C 015D 015E 015F 0160 0161 0162 0163 0164 0165 0166 0167 0168 0169 016A 082E 0086 1683 0186 1283 300F 052C 00AA 1903 2955 30FF 3E01 0CAA 1C03 293E 00AA 0E2C 390F 1903 2955 00AF 30FF 3E01 0CAF 1C03 2949 00AF 1003 0DAF 0DAF 082F 072A 2156 00AC 0008 00AA 3001 008A 082A 0782 3446 3442 3430 3441 3445 3439 3438 3437 3444 3436 3435 3434 3443 3433 3432 3431 DS00578B-page 14 00514 00515 00516 00517 00518 00519 00520 00521 00522 00523 00524 00525 00526 00527 00528 00529 00530 00531 00532 00533 00534 00535 00536 00537 00538 00539 00540 00541 00542 00543 00544 00545 00546 00547 00548 00549 00550 00551 00552 00553 00554 00555 00556 00557 00558 00559 00560 00561 00562 00563 00564 00565 00566 00567 00568 00569 00570 00571 00572 00573 00574 00575 00576 00577 00578 00579 movf movwf bsf clrf bcf DISPVAL,W PORTB STATUS,RP0 TRISB STATUS,RP0 ; Set LEDs movlw andwf movwf btfsc goto movlw 00Fh NEW_KEY,W TEMP STATUS,Z DECOUT 0FFh addlw rrf btfss goto movwf swapf andlw btfsc goto movwf movlw 001h TEMP, F STATUS,C DECL1 TEMP NEW_KEY,W 00Fh STATUS,Z DECOUT TEMP1 0FFH ; Count column ; Rotate column until it is found addlw rrf btfss goto movwf bcf rlf rlf movf addwf call movwf 001h TEMP1, F STATUS,C DECL2 TEMP1 STATUS,C TEMP1, F TEMP1, F TEMP1,W TEMP,W DEC_TABL NEW_KEY ; Count row ; Rotate row until it is found TEMP 01h PCLATH TEMP,W PCL, F 'F' 'B' '0' 'A' 'E' '9' '8' '7' 'D' '6' '5' '4' 'C' '3' '2' '1' ; Save key value KEY_DEC ; Get column of key pressed ; If no key pressed, exit ; Initialize the W register DECL1 ; Get row of key pressed ;If no key pressed, exit DECL2 ; Move row to upper nibble ; Add column to row value ; Get ASCII value of key DECOUT return DEC_TABL movwf movlw movwf movf addwf retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw ; Setup for second page of RAM ; Jump, return with ASCII value ;***************************************************************** ;*This routine is a software delay * ;*At 4Mhz clock, the loop takes 3uS, so initialize TEMP with * ;*a value of to give 9uS, plus the move etc should result in *  1997 Microchip Technology Inc AN578 016B 016B 0BAA 016C 296B 016D 0008 00580 ;*a total time of > 10uS * 00581 ;***************************************************************** 00582 00583 SetupDelay 00584 decfsz TEMP, F 00585 goto SetupDelay 00586 return 00587 00588 END MEMORY USAGE MAP ('X' = Used, 0000 0040 0080 00C0 0100 0140 : : : : : : X -X XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX '-' = Unused) XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX All other memory blocks unused Program Memory Words Used: Program Memory Words Free: Errors : Warnings : Messages : 0 reported, reported,  1997 Microchip Technology Inc 352 1696 suppressed 29 suppressed DS00578B-page 15 Note the following details of the code protection feature on PICmicro® MCUs • • • • • • The PICmicro family meets the specifications contained in the Microchip Data Sheet Microchip believes that its family of PICmicro microcontrollers is one of the most secure products of its kind on the market today, when used in the intended manner and under normal conditions There are dishonest and possibly illegal methods used to breach the code protection feature All of these methods, to our knowledge, require using the PICmicro microcontroller in a manner outside the operating specifications contained in the data sheet The person doing so may be engaged in theft of intellectual property Microchip is willing to work with the customer who is concerned about the integrity of their code Neither Microchip nor any other semiconductor manufacturer can guarantee the security of their code Code protection does not mean that we are guaranteeing the product as “unbreakable” Code protection is constantly evolving We at Microchip are committed to continuously improving the code protection features of our product If you have any further questions about this matter, please contact the local sales office nearest to you Information contained in this publication regarding device applications and the like is intended through suggestion only and may be superseded by updates It is your responsibility to ensure that your application meets with your specifications No representation or warranty is given and no liability is assumed by Microchip Technology Incorporated with respect to the accuracy or use of such information, or infringement of patents or other intellectual property rights arising from such use or otherwise Use of Microchip’s products as critical components in life support systems is not authorized except with express written approval by Microchip No licenses are conveyed, implicitly or otherwise, under any intellectual property rights Trademarks The Microchip name and logo, the Microchip logo, FilterLab, KEELOQ, microID, MPLAB, PIC, PICmicro, PICMASTER, PICSTART, PRO MATE, SEEVAL and The Embedded Control Solutions Company are registered trademarks of Microchip Technology Incorporated in the U.S.A and other countries dsPIC, ECONOMONITOR, FanSense, FlexROM, fuzzyLAB, In-Circuit Serial Programming, ICSP, ICEPIC, microPort, Migratable Memory, MPASM, MPLIB, MPLINK, MPSIM, MXDEV, PICC, PICDEM, PICDEM.net, rfPIC, Select Mode and Total Endurance are trademarks of Microchip Technology Incorporated in the U.S.A Serialized Quick Turn Programming (SQTP) is a service mark of Microchip Technology Incorporated in the U.S.A All other trademarks mentioned herein are property of their respective companies © 2002, Microchip Technology Incorporated, Printed in the U.S.A., All Rights Reserved Printed on recycled paper Microchip received QS-9000 quality system certification for its worldwide headquarters, design and wafer fabrication facilities in Chandler and Tempe, Arizona in July 1999 The Company’s quality system processes and procedures are QS-9000 compliant for its PICmicro® 8-bit MCUs, KEELOQ® code hopping devices, Serial EEPROMs and microperipheral products In addition, Microchip’s quality system for the design and manufacture of development systems is ISO 9001 certified  2002 Microchip Technology Inc M WORLDWIDE SALES AND SERVICE AMERICAS ASIA/PACIFIC Japan Corporate Office Australia 2355 West Chandler Blvd Chandler, AZ 85224-6199 Tel: 480-792-7200 Fax: 480-792-7277 Technical Support: 480-792-7627 Web Address: http://www.microchip.com Microchip Technology Australia Pty Ltd Suite 22, 41 Rawson Street Epping 2121, NSW Australia Tel: 61-2-9868-6733 Fax: 61-2-9868-6755 Microchip Technology Japan K.K Benex S-1 6F 3-18-20, Shinyokohama Kohoku-Ku, Yokohama-shi Kanagawa, 222-0033, Japan Tel: 81-45-471- 6166 Fax: 81-45-471-6122 Rocky Mountain China - Beijing 2355 West Chandler Blvd Chandler, AZ 85224-6199 Tel: 480-792-7966 Fax: 480-792-7456 Microchip Technology Consulting (Shanghai) Co., Ltd., Beijing Liaison Office Unit 915 Bei Hai Wan Tai Bldg No Chaoyangmen Beidajie Beijing, 100027, No China Tel: 86-10-85282100 Fax: 86-10-85282104 Atlanta 500 Sugar Mill Road, Suite 200B Atlanta, GA 30350 Tel: 770-640-0034 Fax: 770-640-0307 Boston Lan Drive, Suite 120 Westford, MA 01886 Tel: 978-692-3848 Fax: 978-692-3821 Chicago 333 Pierce Road, Suite 180 Itasca, IL 60143 Tel: 630-285-0071 Fax: 630-285-0075 Dallas 4570 Westgrove Drive, Suite 160 Addison, TX 75001 Tel: 972-818-7423 Fax: 972-818-2924 Detroit Tri-Atria Office Building 32255 Northwestern Highway, Suite 190 Farmington Hills, MI 48334 Tel: 248-538-2250 Fax: 248-538-2260 Kokomo 2767 S Albright Road Kokomo, Indiana 46902 Tel: 765-864-8360 Fax: 765-864-8387 Los Angeles 18201 Von Karman, Suite 1090 Irvine, CA 92612 Tel: 949-263-1888 Fax: 949-263-1338 China - Chengdu Microchip Technology Consulting (Shanghai) Co., Ltd., Chengdu Liaison Office Rm 2401, 24th Floor, Ming Xing Financial Tower No 88 TIDU Street Chengdu 610016, China Tel: 86-28-6766200 Fax: 86-28-6766599 China - Fuzhou Microchip Technology Consulting (Shanghai) Co., Ltd., Fuzhou Liaison Office Unit 28F, World Trade Plaza No 71 Wusi Road Fuzhou 350001, China Tel: 86-591-7503506 Fax: 86-591-7503521 China - Shanghai Microchip Technology Consulting (Shanghai) Co., Ltd Room 701, Bldg B Far East International Plaza No 317 Xian Xia Road Shanghai, 200051 Tel: 86-21-6275-5700 Fax: 86-21-6275-5060 China - Shenzhen 150 Motor Parkway, Suite 202 Hauppauge, NY 11788 Tel: 631-273-5305 Fax: 631-273-5335 Microchip Technology Consulting (Shanghai) Co., Ltd., Shenzhen Liaison Office Rm 1315, 13/F, Shenzhen Kerry Centre, Renminnan Lu Shenzhen 518001, China Tel: 86-755-2350361 Fax: 86-755-2366086 San Jose Hong Kong Microchip Technology Inc 2107 North First Street, Suite 590 San Jose, CA 95131 Tel: 408-436-7950 Fax: 408-436-7955 Microchip Technology Hongkong Ltd Unit 901-6, Tower 2, Metroplaza 223 Hing Fong Road Kwai Fong, N.T., Hong Kong Tel: 852-2401-1200 Fax: 852-2401-3431 New York Toronto 6285 Northam Drive, Suite 108 Mississauga, Ontario L4V 1X5, Canada Tel: 905-673-0699 Fax: 905-673-6509 India Microchip Technology Inc India Liaison Office Divyasree Chambers Floor, Wing A (A3/A4) No 11, O’Shaugnessey Road Bangalore, 560 025, India Tel: 91-80-2290061 Fax: 91-80-2290062 Korea Microchip Technology Korea 168-1, Youngbo Bldg Floor Samsung-Dong, Kangnam-Ku Seoul, Korea 135-882 Tel: 82-2-554-7200 Fax: 82-2-558-5934 Singapore Microchip Technology Singapore Pte Ltd 200 Middle Road #07-02 Prime Centre Singapore, 188980 Tel: 65-334-8870 Fax: 65-334-8850 Taiwan Microchip Technology Taiwan 11F-3, No 207 Tung Hua North Road Taipei, 105, Taiwan Tel: 886-2-2717-7175 Fax: 886-2-2545-0139 EUROPE Denmark Microchip Technology Nordic ApS Regus Business Centre Lautrup hoj 1-3 Ballerup DK-2750 Denmark Tel: 45 4420 9895 Fax: 45 4420 9910 France Microchip Technology SARL Parc d’Activite du Moulin de Massy 43 Rue du Saule Trapu Batiment A - ler Etage 91300 Massy, France Tel: 33-1-69-53-63-20 Fax: 33-1-69-30-90-79 Germany Microchip Technology GmbH Gustav-Heinemann Ring 125 D-81739 Munich, Germany Tel: 49-89-627-144 Fax: 49-89-627-144-44 Italy Microchip Technology SRL Centro Direzionale Colleoni Palazzo Taurus V Le Colleoni 20041 Agrate Brianza Milan, Italy Tel: 39-039-65791-1 Fax: 39-039-6899883 United Kingdom Arizona Microchip Technology Ltd 505 Eskdale Road Winnersh Triangle Wokingham Berkshire, England RG41 5TU Tel: 44 118 921 5869 Fax: 44-118 921-5820 01/18/02  2002 Microchip Technology Inc [...]... family of PICmicro microcontrollers is one of the most secure products of its kind on the market today, when used in the intended manner and under normal conditions There are dishonest and possibly illegal methods used to breach the code protection feature All of these methods, to our knowledge, require using the PICmicro microcontroller in a manner outside the operating specifications contained in the. .. data sheet The person doing so may be engaged in theft of intellectual property Microchip is willing to work with the customer who is concerned about the integrity of their code Neither Microchip nor any other semiconductor manufacturer can guarantee the security of their code Code protection does not mean that we are guaranteeing the product as “unbreakable” Code protection is constantly evolving We at... Microchip Technology Incorporated with respect to the accuracy or use of such information, or infringement of patents or other intellectual property rights arising from such use or otherwise Use of Microchip’s products as critical components in life support systems is not authorized except with express written approval by Microchip No licenses are conveyed, implicitly or otherwise, under any intellectual property... MPLIB, MPLINK, MPSIM, MXDEV, PICC, PICDEM, PICDEM.net, rfPIC, Select Mode and Total Endurance are trademarks of Microchip Technology Incorporated in the U.S.A Serialized Quick Turn Programming (SQTP) is a service mark of Microchip Technology Incorporated in the U.S.A All other trademarks mentioned herein are property of their respective companies © 2002, Microchip Technology Incorporated, Printed in the. .. XXXXXXXXXXXXXXXX All other memory blocks unused Program Memory Words Used: Program Memory Words Free: Errors : Warnings : Messages : 0 0 reported, 0 reported,  1997 Microchip Technology Inc 352 1696 0 suppressed 29 suppressed DS00578B-page 15 Note the following details of the code protection feature on PICmicro® MCUs • • • • • • The PICmicro family meets the specifications contained in the Microchip Data... Code protection is constantly evolving We at Microchip are committed to continuously improving the code protection features of our product If you have any further questions about this matter, please contact the local sales office nearest to you Information contained in this publication regarding device applications and the like is intended through suggestion only and may be superseded by updates It is... property rights Trademarks The Microchip name and logo, the Microchip logo, FilterLab, KEELOQ, microID, MPLAB, PIC, PICmicro, PICMASTER, PICSTART, PRO MATE, SEEVAL and The Embedded Control Solutions Company are registered trademarks of Microchip Technology Incorporated in the U.S.A and other countries dsPIC, ECONOMONITOR, FanSense, FlexROM, fuzzyLAB, In- Circuit Serial Programming, ICSP, ICEPIC, microPort,... -; Input : None ; Output : None ; -delay bcf clrf STATUS,RP0 TEMP nop nop nop decfsz goto retlw TEMP, F dly1 0 ;clear last location dly1 ;reduce count ;loop ; -; Interrupt service routine ; Only the SSP interrupt is enabled This routine will read the I2C ; data and output it on the LEDs ; -service_int... Floor, Ming Xing Financial Tower No 88 TIDU Street Chengdu 610016, China Tel: 86-28-6766200 Fax: 86-28-6766599 China - Fuzhou Microchip Technology Consulting (Shanghai) Co., Ltd., Fuzhou Liaison Office Unit 28F, World Trade Plaza No 71 Wusi Road Fuzhou 350001, China Tel: 86-591-7503506 Fax: 86-591-7503521 China - Shanghai Microchip Technology Consulting (Shanghai) Co., Ltd Room 701, Bldg B Far East International... movwf movwf IntOut bsf bsf bcf PIR1,3 TEMP_W STATUS,W TEMP_STAT STATUS,RP0 SSPSTAT,0 IntOut STATUS,RP0 SSPBUF,W STATUS,RP0 SSPSTAT,5 IntOut STATUS,RP0 PORTB DISPVAL STATUS,RP0 PIE1,3 STATUS,RP0 ; ; ; ; Clear SSP interrupt Save W register Get STATUS register Save STATUS register ; Check Buffer Full Flag ; No data received, so exit ; Get I2C data ; If Address received last ; exit without saving it ; Display ... Technology Incorporated with respect to the accuracy or use of such information, or infringement of patents or other intellectual property rights arising from such use or otherwise Use of Microchip’s... To signal the end of data to the slave-transmitter, the master does not generate an acknowledge The slave then releases the SDA line so the master can generate the STOP condition The master can... Technology Inc DS00578B-page AN578 IMPLEMENTATION IN THE PIC16CXXX This Application Note uses the PIC16CXXX in a Multi- Master I2C environment The PIC16CXXX acts as both a Master and a Slave on the bus

Ngày đăng: 11/01/2016, 11:52

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN

w