I2C Hang???

General discussion on mikroBasic.
Post Reply
Author
Message
Chris Yeager
Posts: 73
Joined: 21 Sep 2005 04:23

I2C Hang???

#1 Post by Chris Yeager » 12 May 2008 01:49

I think something in the I2C library hangs in this example...
Between the PORTC.1 = 1 and the PORTC.1 = 0 lines it seems the code never returns because the LED on PORTC bit 1 stays on.

I am building a clock. It will work fine for several minutes then it will stop updating because the call to this routine never returns.
What might I be doing wrong?

NOTE: The PIC is still running because I have an interrupt routine that mutiplexes the 7SEGs and they continue to update the last read time.

If nothing else, at the very least, can someone tell me if there is anyway the I2C_Rd routine might get in a loop and not return? This is really bugging me because it works for several (up to 15-20) minutes before it hangs.

Thanks in advance for the help.

Code: Select all

sub function DS1307_GetDateTime(dim byref ts as DS1307_DateTime) as byte
  dim temp as byte                               ' a temporary variable
                                                 '
  result = DS1307_I2C_ERROR                      ' Set default error
  if I2C_Start = 0 then                          ' Start and continue if ok
    if I2C_Wr(%11010000) = 0 then                ' Send chip address and WRITE
      if I2C_Wr(0x00) = 0 then                   ' Send pointer address
        I2C_Repeated_Start                       ' Send Repeated Start
        PORTC.1 = 1
        I2C_Wr(%11010001)                        ' Send chip address and READ
        ts.Sec = I2C_Rd(1)                       ' Get seconds -- ACK
        ts.Min = I2C_Rd(1)                       ' Get minutes -- ACK
        ts.Hr = I2C_Rd(1)                        ' Get hours -- ACK
        ts.DOW = I2C_Rd(1)                       ' Get day of week -- ACK
        ts.Day = I2C_Rd(1)                       ' Get day -- ACK
        ts.Mnth = I2C_Rd(1)                      ' Get month -- ACK
        ts.Yr = I2C_Rd(0)                        ' Get year -- NAK
        PORTC.1 = 0
        result = DS1307_OK                       ' Set result to OK
      end if                                     '
    end if                                       '
    I2C_Stop                                     ' Stop
  end if                                         '
end sub                                          '

Chris Yeager
Posts: 73
Joined: 21 Sep 2005 04:23

#2 Post by Chris Yeager » 19 May 2008 17:10

Just to put in an update on this.
Apparently when the TMR0 interrupt is called it can lock up a call to the I2C_Rd routine.
I was able to fix my issue by turning off the TMR0IE flag before calling this GetDateTime routine. Then back on after the call returns.

I'm pretty sure that my interrupt routine is so short that is wouldn'get called again within itself. Plus the prescalar is set pretty high too.

So anyway, I figured out how to fix my problem, just not sure exactaly how it caused the problem.

Dany
Posts: 3854
Joined: 18 Jun 2008 11:43
Location: Nieuwpoort, Belgium
Contact:

#3 Post by Dany » 15 Aug 2008 16:07

Apparently it is a problem with every type of interrupt. I had/have the same problem with a Usart interrupt, and the I2c blocking went away after switching off the interrupt temporary (not a very good solution but..). Of course I2c routines (in my case I2c_Rd) should not ever become blocking.

Thanks in advance. :D
Kind regards, Dany.
Forget your perfect offering. There is a crack in everything, that's how the light gets in... (L. Cohen)
Remember when we were young? We shone like the sun. (David Gilmour)

janni
Posts: 5373
Joined: 18 Feb 2006 13:17
Contact:

#4 Post by janni » 16 Aug 2008 02:25


Dany
Posts: 3854
Joined: 18 Jun 2008 11:43
Location: Nieuwpoort, Belgium
Contact:

#5 Post by Dany » 17 Aug 2008 10:04

Hi, I2c_Rd and I2c_Wr routines without the "interrupt" problem (I hope) can be found at:
http://www.mikroe.com/forum/viewtopic.p ... highlight= section "units". :D
Kind regards, Dany.
Forget your perfect offering. There is a crack in everything, that's how the light gets in... (L. Cohen)
Remember when we were young? We shone like the sun. (David Gilmour)

Post Reply

Return to “mikroBasic General”