please dont rip this site

Update: 3 May 2003

As part of a class project I've taken it upon myself to convert these i2c
routines into a general purpose I2C template for devices containing the
I2C module. I expect to produce a general purpose template within the next few days using John Sands original code.

--Matt Krutar

PIC Microcontoller Input / Output Methods for I2C on 16F87x

by John Sand [jsand at PIXIE.CO.ZA]

I cribbed, borrowed and stole whatever I could, then went back to the 16f87x manual and wound up with a set of master transmit <> receive routines that are *working* - perfectly well as far as I can tell.

This app. has two Philips PCF8574 I2C general purpose byte-wide IO port chips. One (IO1) is dedicated to inputs only, for switch states, etc., while IO2 is output only, to drive a collection of panel LEDs. I have used private macros like :-

        bank0        ;use ram bank0
        bank1        ;use ram bank1
        movlf           ;load literal to freg
        movff            ;load freg1 to freg2

etc., these should need no introduction, but if they do, let me know.. Setup the ports by :-


Invoke a IO1 port read by:-

        read_switches      ;byte-wide result in freg`latest_switch_state'

Invoke a IO2 port write by:-

                                                ;put desired byte into freg
;****************** I2C macros begin
start_i2c         MACRO              ;[S]
     bank0                                     ;bank0 (bank0? names) be sure
     bcf             PIR1,SSPIF
     bank1                                           ;bank1
     bsf             SSPCON2,SEN     ;send i2c START [S] bit
     bank0                                       ;bank0
     btfsS         PIR1,SSPIF         ;start bit cycle complete?
     goto         $-1

enable_receive_i2c     MACRO
     bank1                                           ;bank1
     bsf             SSPCON2,RCEN     ;enable receiving at master 16f877
     bank0                                           ;bank0
     bcf             PIR1,SSPIF
     btfsS         PIR1,SSPIF             ;sspbuf buffer full byte?
     goto             $-1

stop_i2c         MACRO                  ;[P]
     bank0                                           ;bank0
     bcf             PIR1,SSPIF
     bank1                                           ;bank1
     bsf             SSPCON2,PEN         ;send i2c STOP [P] bit
     bank0                                           ;bank0
     btfsS         PIR1,SSPIF             ;stop bit cycle completed?
     goto         $-1

wait_for_ack_i2c         MACRO
     bank0                                           ;bank0
     bcf             PIR1,SSPIF
     btfsS         PIR1,SSPIF                 ;ACK received?
     goto         $-1

setup_i2c         MACRO                          ;setup mssp for i2c
     movlf         00101000b,SSPCON     ;=0x028
     movlf         10000000b,SSPSTAT
     movlf         01100000b,SSPCON2
     movlf         00001010b,SSPADD

read_switches     MACRO                   ;io1 is inputs only
     movlf             01000011b,SSPBUF     ;send out io1 pcf8574 addr
     movff             SSPBUF,latest_switch_state

write_leds MACRO                                   ;io2 is output only
     movlf             01000100b,SSPBUF                 ;sned out io2
address, writing
     movff             led_state,SSPBUF
;***************** I2C macros end

file: /Techref/microchip/i2c87xjs.htm, 4KB, , updated: 2009/7/25 06:52, local time: 2024/7/18 04:27, owner: MAK-PPP-J84,

 ©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?
Please DO link to this page! Digg it! / MAKE!

<A HREF=""> PIC Microcontoller Input / Ouput Methods for I2C on 16F87x by John Sand </A>

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type a nice message (short messages are blocked as spam) in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.

Link? Put it here: 
if you want a response, please enter your email address: 
Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
Did you find what you needed?


Welcome to!

Site supported by
sales, advertizing,
& kind contributors
just like you!

Please don't rip/copy
(here's why

Copies of the site on CD
are available at minimal cost.

Welcome to!