I2C_init problem with 18F452 FYI

General discussion on mikroC.
Post Reply
Author
Message
Gustl
Posts: 1
Joined: 18 Jan 2012 04:03

I2C_init problem with 18F452 FYI

#1 Post by Gustl » 18 Jan 2012 06:37

Hi everyone!

I have registered just to share this information with you guys:

I did have a problem regarding I2C while migrating from 16F877A to 18F452 (I know it is not the latest, but I had one in lying around). Same hardware, changing only the MCU.

The same code (in MikroC) did work well on 16F and failed on the 18F. The i2c simply stop functioning (ok, it is not a big surprise while migrating). To make a long story short, I found that the i2c_init library caused a glitch on SDA line that confused the i2c devices (mainly ADS7828 A/D).

Looking in the prog 'asm' file, I noticed a diferent aprouch to initialize the i2c on both processors.

Code: Select all

; i2c_init library for 18F452
; ===============================

BCF	TRISC, 4, 0     ; PORTC, 4 as output
BCF	TRISC, 3, 0     ; PORTC, 3 as output
BCF	LATC, 4, 0      ; Latch register for PORTC, 3
BCF	LATC, 3, 0      ; Latch register for PORTC, 3
BSF	TRISC, 4, 0     ; PORTC, 3 as input
BSF	TRISC, 3, 0     ; PORTC, 3 as input
BCF	SSPSTAT, 7, 0   ; SMP bit
BCF	SSPSTAT, 6, 0   ; CKE bit:Disable SMBus specific inputs
MOVLW	56              ; 0    0    1    1   1   0   0   0 = 56 = 0x38
MOVWF	SSPCON1, 0      ;SMP, CKE, D/A,  P,  S, R/W, UA, BF
RETURN

;i2c_init library for 16F877A
;===============================
_I2C_Init:
BCF	STATUS, RP1
BSF	STATUS, RP0
BCF	SSPSTAT, 6
BCF	SSPSTAT, 7
BSF	TRISC, 4
BSF	TRISC, 3
MOVLW	56
BCF	STATUS, RP0
MOVWF	SSPCON
RETURN
Well, each comand is there for a reason, I believe. But someone is causing the port pin to swing to low, sending a START on SDA line. As can be seen bellow.

I am a newbie in PIC and MikroC altogether, so I used snippets of code where the i2c_init function comes just before the i2c_start (like the exemples in MikroC help). This caused all the trouble.
I solved (sort of) the problem moving the i2c_init to the begining of the code, just after ports initialization, and the problem dissapeared, or, it is still there but is not causing troubles..

The 16F877A behavior:
16F877A.jpg
16F877A.jpg (14.53 KiB) Viewed 1617 times
The 18F452 with the i2c_init just before the i2c_start function. A glitch can be seen 56,4microsecond before the real START signal and no ACK from the device:
18F452bug.jpg
18F452bug.jpg (13.48 KiB) Viewed 1617 times
and finally, the behavior of the 18F452 with the i2c_init far away from the i2c_start. Now we got an ACK from the slave device:
18F452ok.jpg
18F452ok.jpg (14.47 KiB) Viewed 1617 times
I hope this can help someone out...

P.S.: the address of the ADS7828 device that can be seen on the pictures is 0x90.
P.S.2: Sorry, I forgot to tell: Compiler MikroC 8.2.0.0 and the C code that produced the picture with the glitch was:

Code: Select all

  i2c_init(100000);        // the glitch was produced here
  i2c_start();                // here I got no ACK from the slave device
  i2c_wr(ADSend);        //send 1 byte peripheral address  0x90 (ADS7828 A/D)
  i2c_wr(ADSch);          //send 1 command byte 10000111
  i2c_repeated_start();   
  i2c_wr(ADSend1);       //send read command
  ADShigh=i2c_rd(1u);   // receive MSB=  0   0   0   0  d11 d10 d9  d8 + ACK
  ADSlow=i2c_rd(0u);    // receive  LSB=  d7 d6 d5 d4 d3   d2   d1 d0  + NACK
  i2c_stop();
Last edited by Gustl on 19 Jan 2012 02:29, edited 1 time in total.

User avatar
janko.kaljevic
Posts: 3565
Joined: 16 Jun 2011 13:48

Re: I2C_init problem with 18F452 FYI

#2 Post by janko.kaljevic » 18 Jan 2012 17:22

Hello,

Thanks for reporting this.
We will inspect it thoroughly and reply to you soon.

Best regards.

Post Reply

Return to “mikroC General”