• AVR Freaks

AnsweredPIC16F18326 I2C

Author
Theo
New Member
  • Total Posts : 9
  • Reward points : 0
  • Joined: 2019/05/04 18:51:24
  • Location: 0
  • Status: offline
2019/12/04 06:47:24 (permalink)
0

PIC16F18326 I2C

Hi,
 
I noticed my PIC16F18326 is not behaving like it should. This is what the datasheet says:
 
After the write to the SSPxBUF, each bit of the address
will be shifted out on the falling edge of SCL until all
seven address bits and the R/W bit are completed. On
the falling edge of the eighth clock, the master will
release the SDA pin, allowing the slave to respond with
an Acknowledge. On the falling edge of the ninth clock,
the master will sample the SDA pin to see if the address
was recognized by a slave. The status of the ACK bit is
loaded into the ACKSTAT Status bit of the SSPxCON2
register. Following the falling edge of the ninth clock
transmission of the address, the SSPxIF is set, the BF
flag is cleared and the Baud Rate Generator is turned
off until another write to the SSPxBUF takes place,
holding SCL low and allowing SDA to float.
 
But the SSPxIF is set much earlier, the BF flag is still high.
I'm trying to scan the I2Cbus to see if a temperature device is present. I'm trying all possible addresses (0x48 to 0x4f) to see if I get an acknowledge. This didn't work untill I inserted the two lines in orange.
 
BANKSEL Bank0
MOVLW 0x48 ; start with 0x48
MOVWF Address ; store in Address
MOVLW 0x08 ; max 8 addresses to check
MOVWF TMCOUNT ; store in counter
GetAddress:
BANKSEL Bank0
BCF I2CFlag ; clear this flag
CLRF PIR1
BANKSEL Bank4
BSF I2CEnable ; enable I2C module
BSF Start ; set START condition
BANKSEL Bank0
BTFSS I2CFlag ; done with start?
GOTO $-1 ; skip if false
LSLF Address,0 ; sensor address <<1 in W
BCF WREG,0 ; read from device
BANKSEL Bank4
MOVWF Buffer ; send address
BANKSEL Bank0
BTFSS I2CFlag ; done with address?
GOTO $-1 ; skip if false
BCF I2CFlag ; clear the flag
BANKSEL Bank4
BTFSC Read_Write ; still busy?
GOTO $-1 ; try again
BTFSS Acknowledge ; acknowledged?
GOTO GetAddress1 ; done
BSF Stop ; send stop condition
BANKSEL Bank0
BTFSS I2CFlag ; done with stop?
GOTO $-1 ; skip if false
BCF I2CFlag ; clear the flag
INCF Address,1 ; try next address
DECFSZ TMCOUNT,1 ; decrement TMCOUNT (skip if zero)
GOTO GetAddress ; try again
GOTO TempError ; address not found. Continue with TempError
GetAddress1:
BSF Stop ; send stop condition
BANKSEL Bank0
BTFSS I2CFlag ; done with stop?
GOTO $-1 ; skip if false
BCF I2CFlag ; clear the flag
BANKSEL Bank4
BCF I2CEnable ; stop I2C module
 
I defined some names used (like I2CFlag) in some other part of the program.
 
Is there something wrong in the datasheet or is there something wrong with me?
#1
ric
Super Member
  • Total Posts : 24579
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: PIC16F18326 I2C 2019/12/04 16:17:38 (permalink) ☼ Best Answerby Theo 2019/12/04 16:32:33
0
You forgot to clear SSPIF after sending the START condition, so it's still set when you check if the write to SSPBUF has completed.
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#2
ric
Super Member
  • Total Posts : 24579
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: PIC16F18326 I2C 2019/12/04 16:27:48 (permalink)
+1 (1)
n.b. I think your "Bank#" defines actually make your code MORE error prone.
BANKSEL PIR1
rather than
BANKSEL BANK0
 
and
BANKSEL SSPCON1
rather than
BANKSEL BANK4
make it much more obvious what you're trying to do.
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#3
Theo
New Member
  • Total Posts : 9
  • Reward points : 0
  • Joined: 2019/05/04 18:51:24
  • Location: 0
  • Status: offline
Re: PIC16F18326 I2C 2019/12/04 16:52:33 (permalink)
0
So obviously the conclusion should be: there is something wrong with me!
 
I don''t know why I didn't see this myself.
 
About the BANKSEL statements: Maybe you're wright, but I'm used to doing it this way. Furthermore Microchip uses
Bank0 to Bankxx and this way it's easy to see if I'm in the correct bank when more registers are addressed in the same bank.
And my defines look like this:
                     #define I2CEnable SSP1CON1,SSPEN ;bank4
                     #define Read_Write SSP1STAT,R_NOT_W ;bank4
so I already have the banknumber. Probably saves some BANKSEL statements.
 
But I agree it's arbitrary.
 
#4
Jump to:
© 2019 APG vNext Commercial Version 4.5