RTC2

General discussion on mikroBasic.
Post Reply
Author
Message
koehler
Posts: 15
Joined: 20 Jun 2011 16:51

RTC2

#1 Post by koehler » 12 Jul 2011 15:12

After reading through the examples and the documentation for the RTC2, a couple things are left unclear to me :(

I am using..
Development Board: LV 24-33
MCU: PIC24FJ96GA010
RTC2 External Module

I am simply trying to read and write data to the module, but no example specific to my board and MCU was found.

Which port (pins on the right side of the board) do I connect the module to?
Are there any specific jumpers that need to be set for communication to the RTC2?
In the I2C library, do I use the I2C1 or I2C2 functions?


Code Snippet:
------------------------------------
ADPCFG = 0xFFFF
TRISD = 0

I2C1_Init(100000)
I2C1_Start()
I2C1_Write(0xD0)
I2C1_Write(0)
I2C1_Restart()
I2C1_Write(0xD1)
I2C1_Restart()
byte1 =I2C1_Read(0) 'Program Infinite loops here and cannot read data :(
byte2 =I2C1_Read(0)
byte3 =I2C1_Read(0)
byte4 =I2C1_Read(0)
byte5 =I2C1_Read(0)
byte6 =I2C1_Read(0)
byte7 =I2C1_Read(1)
I2C1_Stop()
------------------------------------

I have the RTC2 Module connected to "PORT F/G" of the LV 24-33 and I have SW16.1 and SW16.3 flipped on.

Any help would be greatly appreciated :)

User avatar
filip
mikroElektronika team
Posts: 11874
Joined: 25 Jan 2008 09:56

Re: RTC2

#2 Post by filip » 13 Jul 2011 10:22

Hi,

You should connect the RTC2 board communication lines (use board schematics for this) to the I2C lines of the MCU and use on-board pull-up resistors to pull up the I2C lines
and the example should work (this is the general procedure when use it).

Regards,
Filip.

koehler
Posts: 15
Joined: 20 Jun 2011 16:51

Re: RTC2

#3 Post by koehler » 14 Jul 2011 13:34

I have the PIC24FJ96GA010, and the datasheet says that the SCL1 and SDA1 pins are RG2 and RG3, in that order.

RG2 (SCL1) and RG3 (SDA1) are on "PORT F/G" of the LV 24-33 v6 dev board. However, the SCL and SDA pins on the RTC do not match up when placing it on the dev board. See diagram below:


..................LV 23-33 Dev Board........................................................RTC2
..................(2x5 female pin slots)...................................(2x5 male pins (top down view))
.........................PORT F/G

.......................RF8 [][] RF12............................................................o o
.....................RF13 [][] RE9........................................................SQW o o SCL
......................RG0 [][] RG1........................................................SDA o o
....SCL1 on uC --> RG2 [][] RG3 <---------SDA1 on uC....................................o o
.....................VCC3 [][] GND.......................................................VCC o o


Again, the SCL and SDA pins do not match up when I plug the RTC2 in the dev board.

Regardless, to debug my problem, I first used SparkFun's "Bus Pirate" (I2C Sniffer), and connected it to RG2 and RG3 of PORT F/G. On the terminal window, all of my data messages are displayed accurately. Therefore, I know for a fact that my data is being sent over these ports. Also, please not that I have PORT F/G jumper to VCC3, and the 7 and 8 dip switches "on".

Next, I tried two things. First, I connected the RTC2 straight to the board on PORT F/G. When running my code, it does not hang, it does not freeze, which is good. BUT, it seems as there is no actual communication to the RTC2. When reading values from the RTC2, all values return as 255. Also, when writing to the RTC2, and reading from the RTC2 again, all values remain again at 255.

Next I connected RG2 and RG3 straight up to SCL and SDA on the RTC2. I also connected VCC and GND. I left SQW disconnected (could be a problem?) I made a fancy connecter so that I could tap into the communication with my Bus Pirate. The data is being sent to the RTC2, and the Bus Pirate is telling me that all of it is accurate, however, it seems as if there are no acknowledge signals being sent back :( Reading still results in all 255 values (for sec, min, hour, etc) and writing and then reading still results in the same thing.

My code is below:

Code: Select all


program I2C1TEST

' Declarations section
dim counter as byte
dim counter_string as string[6]

dim rtc2_sec, rtc2_min, rtc2_hour, rtc2_week_day, rtc2_day, rtc2_month, rtc2_year as byte
dim rtc2_sec_str, rtc2_min_str, rtc2_hour_str, rtc2_week_day_str, rtc2_day_str, rtc2_month_str, rtc2_year_str as string[6]

' Glcd module connections
dim GLCD_D7 as sbit at RD7_bit
    GLCD_D6 as sbit at RD6_bit
    GLCD_D5 as sbit at RD5_bit
    GLCD_D4 as sbit at RD4_bit
    GLCD_D3 as sbit at RD3_bit
    GLCD_D2 as sbit at RD2_bit
    GLCD_D1 as sbit at RD1_bit
    GLCD_D0 as sbit at RD0_bit
    GLCD_D7_Direction as sbit at TRISD7_bit
    GLCD_D6_Direction as sbit at TRISD6_bit
    GLCD_D5_Direction as sbit at TRISD5_bit
    GLCD_D4_Direction as sbit at TRISD4_bit
    GLCD_D3_Direction as sbit at TRISD3_bit
    GLCD_D2_Direction as sbit at TRISD2_bit
    GLCD_D1_Direction as sbit at TRISD1_bit
    GLCD_D0_Direction as sbit at TRISD0_bit

dim GLCD_CS1 as sbit at LATB2_bit
    GLCD_CS2 as sbit at LATB3_bit
    GLCD_RS  as sbit at LATB4_bit
    GLCD_RW  as sbit at LATB5_bit
    GLCD_EN  as sbit at LATB6_bit
    GLCD_RST as sbit at LATB7_bit

dim GLCD_CS1_Direction as sbit at TRISB2_bit
    GLCD_CS2_Direction as sbit at TRISB3_bit
    GLCD_RS_Direction  as sbit at TRISB4_bit
    GLCD_RW_Direction  as sbit at TRISB5_bit
    GLCD_EN_Direction  as sbit at TRISB6_bit
    GLCD_RST_Direction as sbit at TRISB7_bit
' End Glcd module connections

sub procedure Initialize()
  ADPCFG = 0xFFFF
  delay_ms(100)
  
  Glcd_Init()                              ' Initialize GLCD
  Glcd_Set_Font(@System3x5, 3, 5, 32)      ' Change font
  Glcd_Fill(0x00)                          ' Clear GLCD
  delay_ms(100)
  
  I2C1_Init(100000)
  delay_ms(100)
  GLCD_Write_Text("I2C1 INITIALIZED",0,0,1)
  delay_ms(100)
  
  counter = 0
  
end sub

main:
  Initialize()
  
  while(1)
    'Clear GLCD
    GLCD_Write_Text("          ",0,1,1)
    GLCD_Write_Text("          ",0,2,1)
    GLCD_Write_Text("          ",0,3,1)
    GLCD_Write_Text("          ",0,4,1)
    GLCD_Write_Text("          ",0,5,1)
    GLCD_Write_Text("          ",0,6,1)
    GLCD_Write_Text("          ",0,7,1)
    
    'Write to RTC2
    I2C1_Start()             ' issue start signal
    I2C1_Write(0xD0)         ' address DS1307
    I2C1_Write(0)            ' start from word at address (REG0)
    I2C1_Write(0x80)         ' write $80 to REG0. (pause counter + 0 sec)
    I2C1_Write(0)            ' write 0 to minutes word to (REG1)
    I2C1_Write(0x17)         ' write 17 to hours word (24-hours mode)(REG2)
    I2C1_Write(0x02)         ' write 2 - Monday (REG3)
    I2C1_Write(0x29)         ' write 4 to date word (REG4)
    I2C1_Write(0x11)         ' write 5 (May) to month word (REG5)
    I2C1_Write(0x10)         ' write 09 to year word (REG6)
    I2C1_Stop()              ' issue stop signal
    
    'Write Beginning Address
    I2C1_Start()             ' issue start signal
    I2C1_Write(0xD0)         ' address DS1307
    I2C1_Write(0)            ' start from word at address 0
    I2C1_Write(0)            ' write 0 to REG0 (enable counting + 0 sec)
    I2C1_Stop()              ' issue stop signal
    
    'Read from RTC2
    I2C1_Start()
    I2C1_Write(0xD0)
    I2C1_Write(0)
    I2C1_Stop()
    I2C1_Start()
    I2C1_Write(0xD1)
    rtc2_sec      = I2C1_Read(0)
    rtc2_min      = I2C1_Read(0)
    rtc2_hour     = I2C1_Read(0)
    rtc2_week_day = I2C1_Read(0)
    rtc2_day      = I2C1_Read(0)
    rtc2_month    = I2C1_Read(0)
    rtc2_year     = I2C1_Read(1)
    I2C1_Stop()
    
    'Convert Bytes to Strings
    BytetoStr(rtc2_sec     ,rtc2_sec_str)
    BytetoStr(rtc2_min     ,rtc2_min_str)
    BytetoStr(rtc2_hour    ,rtc2_hour_str)
    BytetoStr(rtc2_week_day,rtc2_week_day_str)
    BytetoStr(rtc2_day     ,rtc2_day_str)
    BytetoStr(rtc2_month   ,rtc2_month_str)
    BytetoStr(rtc2_year    ,rtc2_year_str)
    
    'Display Strings
    GLCD_Write_Text(rtc2_sec_str     ,0,1,1)
    GLCD_Write_Text(rtc2_min_str     ,0,2,1)
    GLCD_Write_Text(rtc2_hour_str    ,0,3,1)
    GLCD_Write_Text(rtc2_week_day_str,0,4,1)
    GLCD_Write_Text(rtc2_day_str     ,0,5,1)
    GLCD_Write_Text(rtc2_month_str   ,0,6,1)
    GLCD_Write_Text(rtc2_year_str    ,0,7,1)
    
    'Increment Counter and Display
    counter = counter + 1
    BytetoStr(counter,counter_string)
    GLCD_Write_Text(counter_string,90,0,1)
    
    'Delay between cycles
    delay_ms(2000)
  wend
  
end.

Any idea what could be wrong? Any help would be tremdously appreciated!

koehler
Posts: 15
Joined: 20 Jun 2011 16:51

Re: RTC2

#4 Post by koehler » 14 Jul 2011 15:34

UPDATE**

So it turns out that the RTC2 board must be powered from 5V in order to communicate with it (read or write to registers). The DS1307 can keep its internal time running from 3.3V, but communication must be done while the VCC pin is powered with 5V.

The LV 24-33 has VCC3 (3.3V) pins, so powering the chip was not successful. I had to make a custom tap from the USB line...

Post Reply

Return to “mikroBasic General”