News:

Let's find out together what makes a PIC Tick!

Main Menu

DS1307 clock via hardware IC2

Started by Dave-S, Jul 29, 2022, 05:10 PM

Previous topic - Next topic

Dave-S

Have used many times the DS1307 via Software IC2 using BusIn/BusOut, and now want to connect it to the MSSP Hardware IC2. I thought I could just change the BusIn Instruction to IC2In but it does not work and gives the wrong BCD values. Where am I going wrong?
The IC2 is setup on a 18F47K40 64MHz ok, I have Compass, Accelometer and LCD working via IC2.

Code shown with Busin/IC2In and output results.


Symbol SDA = PORTC.4     
Symbol SCL = PORTC.3   
Symbol clockin=%11010001        'set the 1307 to transmit data
    bcd_to_bin_byte is a byte variable
    DecValue is a byte variable
    ds1307_address is in a loop and gives 0-6

         ds1307_address = 0: GoSub read_time            'read seconds data from 1307
         second = bcd_to_bin_byte                               'convert to binary
         ds1307_address = 1: GoSub read_time            'read minute data from 1307
         minute = bcd_to_bin_byte                               'convert to binary
         ds1307_address = 2: GoSub read_time            'read hour data from 1307
         hour = bcd_to_bin_byte                                 'convert to binary
         ds1307_address = 3: GoSub read_time            'read day data from 1307
         day = bcd_to_bin_byte                                  'convert to binary
         ds1307_address = 4: GoSub read_time            'read date data from 1307
         date = bcd_to_bin_byte                                 'convert to binary
         ds1307_address = 5: GoSub read_time            'read month data from 1307
         month = bcd_to_bin_byte                                'convert to binary
         ds1307_address = 6: GoSub read_time            'read year data from 1307
         year = bcd_to_bin_byte     



read_time:

   BusIn clockin, ds1307_address, [bcd_to_bin_byte]
     DecValue = (bcd_to_bin_byte >> 4) * 10
     DecValue = DecValue + (bcd_to_bin_byte & $0F)
     bcd_to_bin_byte = DecValue

   SerOut PORTD.1, 84,  ["BCD ", Dec bcd_to_bin_byte, 13]

BCD 50
BCD 7
BCD 15
BCD 1
BCD 29
BCD 7
BCD 22
29/07/2022  15:07

____________________________________________

read_time:

  I2CIn SDA, SCL, clockin, ds1307_address, [bcd_to_bin_byte]
      DecValue = (bcd_to_bin_byte >> 4) * 10
      DecValue = DecValue + (bcd_to_bin_byte & $0F)
      bcd_to_bin_byte = DecValue
 
  SerOut PORTD.1, 84,  ["BCD ", Dec bcd_to_bin_byte, 13]

BCD 25
BCD 25
BCD 25
BCD 1
BCD 45
BCD 7
BCD 45
45/07/2045  25:25

hanks David
   

Dave-S

Think I have found the problem, I still had "Declare Slow_Bus On" in from Busin/Busout removed it and added "Declare I2C_Slow_Bus On" which I assume is required because Pic is running at 64 MHz. All working OK now.

David

Dave-S

It appears I still have a Problem which I thought was because the DS1307 was accessing the IC2 using BusIn/Busout.
The problem is the Accelometer on starting the program sometimes shows zero reading and continues with zero readings and then the only way to get it working again is to operate without accessing the DS1307 clock.  Normally the program reads it every 5 minutes so is accesing the clock prior to getting a reading, if I request a reading without accesing the clock it seems to work Ok.
Any ideas how to detect problem or any suggestions?

Thank David

John Drew

I suppose you checked there isn't an address conflict Dave.
John

Dave-S

Thanks for the reply.
Yes,
clock   1101000    68
Accelometer   A6  A7
Compass   1A   1B

Some of the internal registers are the same but I assume that once it has addressed which one to talk to that is OK.
Yesterday evening I changed the clock to software IC2 Busin/Busout and connected to two different pins but I still had the Problem Accelometer = zero on the hardware IC2.

Symbol SDA = PORTC.4        'I2C data pin.
Symbol SCL = PORTC.3        'I2C clock pin.
Symbol W_DAT      = $1A     'Used to perform a Write operation  QMC5883L compass
Symbol R_DAT      = $1B     'Used to perform a Read operation   QMC5883L Compass
Symbol Acc_W_DAT  = $A6             'write address for  ADXL345  Accelometer
Symbol Acc_R_DAT  = $A7               'read address for  ADXL345  Accelometer
Symbol CON_A      = $09     'Send Configuration A Register   QMC5883L Compass
Symbol CON_B      = $0A     'Send Configuration B Register   QMC5883L Compass
Symbol MOD_R      = $0D     'Send Operating Mode. Default = Single measurement. Must be in Continuous measurement mode. QMC5883L Compass
Symbol X_MSB      = $01     'Read Register, Output of X MSB 8-bit value. QMC5883L Compass
Symbol X_LSB      = $00     'Read Register, Output of X LSB 8-bit value. QMC5883L Compass
Symbol Z_MSB      = $05     'Read Register, Output of Z MSB 8-bit value. QMC5883L Compass
Symbol Z_LSB      = $04     'Read Register, Output of Z LSB 8-bit value. QMC5883L Compass
Symbol Y_MSB      = $03     'Read Register, Output of Y MSB 8-bit value. QMC5883L Compass
Symbol Y_LSB      = $02     'Read Register, Output of Y LSB 8-bit value. QMC5883L Compass
Symbol D_FRM = $31     'Data Format.(Full Resultion,Range,Invert)   Send continuous Measurement mode
Symbol BW_RT = $2C     'Low Power,Rate.
Symbol P_CTL = $2D     '0x2D 0x08 talk To power control register Bit D3 High For measuring Enable.
Symbol F_CTL = $38     'Send Fifo Bypass mode

'setup
I2COut SDA, SCL, Acc_W_DAT, P_CTL, [$08]     ' 0x2D 0x08 talk to power control register bit D3 high for measuring enable
DelayMS 40
I2COut SDA, SCL, Acc_W_DAT, F_CTL, [$00]       'FIFO control
DelayMS 40
I2COut SDA, SCL, Acc_W_DAT, BW_RT, [$0C]     'Data rate and power mode control
DelayMS 40
I2COut SDA, SCL, Acc_W_DAT, D_FRM, [$08]      'data format  Send continuous Output command
DelayMS 40
I2COut SDA, SCL, Acc_W_DAT, 0x20, [12]      'Z axis OFFSET
DelayMS 40


Accelometer:
I2CIn SDA, SCL, Acc_R_DAT, [$00]
DelayMS 44
I2CIn SDA, SCL, Acc_R_DAT, $32, [READX.LowByte, READX.HighByte, READY.LowByte, READY.HighByte, READZ.LowByte, READZ.HighByte]
DelayMS 40

Compass:
I2CIn SDA, SCL, R_DAT, [00]
I2CIn SDA, SCL, R_DAT, $00, [READX.LowByte, READX.HighByte, READY.LowByte, READY.HighByte, READZ.LowByte, READZ.HighByte]
DelayMS 100

Thanks David

tumbleweed

Quote... via Software IC2 using BusIn/BusOut, and now want to connect it to the MSSP Hardware IC2. I thought I could just change the BusIn Instruction to IC2In but it does not work
Just to clear up a few things, both BusIn/BusOut and I2Cin/I2Cout are software bit-banged routines. Neither of them use the hardware MSSP.

For BusIn/BusOut you define the pins using 'declare' statements...
declare SDA_Pin PORTA.0 ' default is PORTA.0
declare SCL_Pin PORTA.1 ' default is PORTA.1

You could use your symbol definitions if you like...
Symbol SDA = PORTC.4     
Symbol SCL = PORTC.3   
declare SDA_Pin SDA
declare SCL_Pin SCL

For I2Cin/I2Cout the pins are defined directly in the call like you have shown ('I2Cin SDA, SCL')
If you want to use the MSSP you would use the 'Hbus' commands.

All methods require pullups to 3.3V on the SDA/SCL pins for the chips you're using.
What are the pin connections to the ADXL345? Do you have VDDIO = 3.3V, CS = 3.3V, and SDO/ALT ADDRESS = GND?


Dave-S

QuoteAll methods require pullups to 3.3V on the SDA/SCL pins for the chips you're using.
What are the pin connections to the ADXL345? Do you have VDDIO = 3.3V, CS = 3.3V, and SDO/ALT ADDRESS = GND?

Is A GY-291 ADXL345 3-Axis Accelerometer Module


David


trastikata

"Have used many times the DS1307 via Software IC2 using BusIn/BusOut, and now want to connect it to the MSSP Hardware IC2"

Then use the same code, only change the I2C commands from BStart-BusOut-BusIn-BStop to HBStart-HBusOut-HBusIn-HBStop.

Don't forget the corresponding Declares:

Declare Hbus_Bitrate 100
Declare HSDA_Pin = PORTC.4
Declare HSCL_Pin = PORTC.3

If your code worked before with BStart-BusOut-BusIn-BStop, this will work too.

superpro

'****************************************************************
'*  Name    : UNTITLED.BAS                                      *
'*  Author  :                  *
'*  Notice  : Copyright (c) 2010 Iberica 2000                   *
'*          : All Rights Reserved                               *
'*  Date    :                                 *
'*  Version : 1.0                                               *
'*  Notes   :                                                   *
'*          :                                                   *
'****************************************************************
  Device = 16F877A        ' Define el Micro a Utilizar
   All_Digital = True      ' Configura Puertos como dig.
   XTAL = 4                ' Define velocidad resonador
   PORTB_PULLUPS = ON      ' Resistencia interna Activada
   
'DEFINICION DE VARIABLES, CONSTANTES Y PUERTOS I/O
'---------------------------------------------------------
   'TRISA = $00           ' Define el Puerto A como salidas
   TRISB = $FF           ' Define el Puerto B como Entradas
   TRISC = $FF           ' Define el Puerto C como Entradas
   TRISd = $00           ' Define el Puerto C como salidas
   
   
   
   
   '************************************ reloj
        Symbol CLK       = PORTc.3
      Symbol DTA      = PORTc.7
      Symbol RST      = PORTc.6
     
     
      symbol T1= PortA.0
        symbol T2= PortA.1
        symbol T3= PortA.2
        symbol T4= PortA.3
        symbol T5= PortA.4
        symbol T6= PortA.5
       
        Symbol Punto = Portd.7
       
        Symbol Pul1= Portb.0
        symbol Pul2= Portb.1
        Symbol Pul3= Portb.2
        symbol Pul4= Portb.3
        Symbol Pul5= Portb.4
        symbol Pul6= Portb.5
        Symbol Pul7= Portb.6
        symbol Pul8= Portb.7
     
        dim Contador  as byte
        Dim Numero    as dword
        dim Unidades  as dword
        dim Decenas   as dword
        dim Centenas  as dword
        dim Millares  as dword
        dim UniMillar as dword
        dim CenMillar as dword
       
        dim Btnvar    as byte
        dim N         As byte
       
       
       
        '****************************** reloj *************************

       Dim RTCCMD      as Byte
     
      Dim TEMP      as Byte
      Dim TEMP1       as Byte
      Dim TIMEDATA[7]   as Byte
      Dim OLDSECONDS    as Byte
     
        Dim SECONDS    as timedata#0
      Dim MINUTES    as timedata#1
      Dim HOURS      as timedata#2
      Dim DATE      as timedata#3
      Dim MONTH      as timedata#4
      Dim DAY         As Timedata#5
        Dim YEAR      as timedata#6
 
        dim Seg         as byte


 
' Clear Write Protect bit in control register of DS1302
      Temp = $10
      RTCCmd = %00111
      Gosub DS1302_WRITE
        Temp = $00
      RTCCmd = $00
      Gosub DS1302_WRITE
     
        Temp = $80
      RTCCmd = %00111
      Gosub DS1302_READ
     
     
     
AGAIN:
      Gosub DS1302_READ
       'if seconds = 16 then seconds = 10
       'if seconds = 10 then
       
       ' If oldseconds <> seconds Then  n=0'Print at 1,1, "TIME ", Hex2 Hours,":",Hex2 Minutes,":",Hex2 Seconds
                              '   at 2,1, "DATE ", Hex2 Date,"/",Hex2 Month,"/20",Hex2 Year
'                                          At 3,1,$FE,$10,$FE,$10,$FE,$10,$FE,$10,"DAY  ", hex2 Day



          if seconds > 9 then seconds = seconds - 6
          if seconds > 19 then seconds = seconds - 6
          if seconds > 29 then seconds = seconds - 6
          if seconds > 39 then seconds = seconds - 6
          if seconds > 49 then seconds = seconds - 6
         
          if Minutes > 9 then Minutes = Minutes - 6
          if Minutes > 19 then Minutes = Minutes - 6
          if Minutes > 29 then Minutes = Minutes - 6
          if Minutes > 39 then Minutes = Minutes - 6
          if Minutes > 49 then Minutes = Minutes - 6
         
          if Hours > 9 then Hours = Hours - 6
          if Hours > 19 then Hours = Hours - 6
         


         Unidades = dig  Seconds,0
         Decenas  = dig  Seconds,1
         
         centenas = dig  Minutes,0
         Millares = dig  Minutes,1
         
         UniMillar = Dig  Hours,0
         CenMillar = dig  Hours,1
         
         gosub Multiplexacion
        'gosub DiaSemana
        Gosub Pulsadores
Goto AGAIN

'******************************************* 1
Multiplexacion:

 
  numero = Unidades
  gosub Paso_a_7Segmento
  low punto
  HIGH T1
  low T2
  low T3
  low T4
  low T5
  low T6
 
 
  delayms 2
  clear portd
  delayms 1
'******************************************* 2
 
  numero = decenas
  gosub Paso_a_7Segmento
 
  low T1
  HIGH T2
  low T3
  low T4
  low T5
  low T6
  'clear portd
  delayms 2
  clear portd
  delayms 1
'******************************************* 3
  'centenas = DIG contador,2


  numero = centenas
  gosub Paso_a_7Segmento
  'low punto
  high punto
  low T1
  low T2
  HIGH T3
  low T4
  low T5
  low T6
  'clear portd
  delayms 2
  clear portd
  delayms 1
'******************************************* 4
  'Millares = DIG contador,3
  numero = Millares
  gosub Paso_a_7Segmento
  low punto
  low T1
  low T2
  low T3
  high T4
  low T5
  low T6
  'clear portd
  delayms 2
  clear portd
  delayms 1
'******************************************* 5
  numero = UniMillar
  gosub Paso_a_7Segmento
  high punto
  low T1
  low T2
  low T3
  low t4
  high t5
  low t6
  delayms 1
  'clear portd
  delayms 1
 '******************************************* 6
  numero = cenMillar
  gosub Paso_a_7Segmento
  low punto
  low T1
  low T2
  low T3
  low t4
  low t5
  high t6
  delayms 2
  clear porta
  delayms 2
  clear portd
  delayms 1
  return
  '*******************************************************************************
  '*********    transformacion a siete segmentos *********************************
  '*******************************************************************************
  Paso_a_7Segmento:
   
  select case numero
       case 0
          portd =$3F
         
       case 1
          portd =$06
         
       case 2
          portd =$5B
         
       case 3
          portd =$4F
         
       case 4
          portd =$66
         
       case 5
          portd =$6D
         
       case 6
          portd =$7D
         
       case 7
          portd =$07
         
       case 8
          portd =$7F
         
       case 9
          portd =$67
       
       case else
          portd =$49
               
     endselect
       
return


'-----------------------------------------------------------------------------------
' Write to DS1302 RTC
DS1302_WRITE:     
      High RST'______________________________5
      Shout Dta, Clk, LSBFIRST, [%0\1,RTCCmd\5,%10\2,Temp\8]
      Low RST
      Return
'-----------------------------------------------------------------------------------
' Read from the DS1302
DS1302_READ:
      High RST'__________________________6
      Shout DTA, Clk, LSBFIRST, [%111111\6,%10\2]
      oldseconds = seconds
      Shin DTA, Clk, LSBPRE, [Seconds,Minutes,Hours,Date,Month,DAY,Year]
      Low RST
      Return
     
     
Pulsadores:
 
       
        button pul1,0,255,250,Btnvar,0,Bp
        goto Setup
  Bp:     
        return
       
'Bp:
'        button Pul2,0,255,250,Btnvar,0,cP
'        decenas = decenas +1 :gosub decena
'Cp:     
'        button Pul3,0,255,250,Btnvar,0,DP
'        inc Centenas:gosub centena
'Dp:
'        button Pul4,0,255,250,Btnvar,0,Ep
'        inc Millares:gosub Millar
       
'Ep:       
'        button pul5,0,255,250,Btnvar,0,Fp
'        dec unidades :gosub unidad
'Fp:
'        button Pul6,0,255,250,Btnvar,0,GP
'        dec decenas :gosub decena: if Decenas < 0 then Decenas = 5
'Gp:     
'        button Pul7,0,255,250,Btnvar,0,HP
'        dec Centenas:gosub centena: if centenas < 0 then centenas = 9
'Hp:
'        button Pul8,0,255,250,Btnvar,0,IP
'        dec Millares:gosub Millar: if millares < 0 then Millares = 5
             
'Ip:    button star,0,255,250,Btnvar,0,Sp
'       goto Contar
'Sp:
'       return
 
Setup:
     clear porta
     
     gosub MultiMinutos
     
     button Pul2,0,255,250,Btnvar,0,Seti
     Minutes = Minutes +1 :if minutes >= 60 then minutes = 0
     centenas = dig  Minutes,0
     Millares = dig  Minutes,1
     
 Seti:
     button pul1,0,255,250,Btnvar,0,setup
     gosub DS1302_WRITE
     delayms 500
     goto AGAIN
     
     
 MultiMinutos:   
     
 numero = centenas
  gosub Paso_a_7Segmento
  'low punto
  high punto
  low T1
  low T2
  HIGH T3
  low T4
  low T5
  low T6
  'clear portd
  delayms 2
  clear portd
  delayms 1
'******************************************* 4
  'Millares = DIG contador,3
  numero = Millares
  gosub Paso_a_7Segmento
  low punto
  low T1
  low T2
  low T3
  high T4
  low T5
  low T6
  'clear portd
  delayms 2
  clear porta
  delayms 1
       
 return

Dave-S

The Problem is not the CLOCK, it is the GY-291 ADXL345 3-Axis Accelerometer Module.