Menu

what am I doing wrong

Help
MBB
2019-09-01
2019-09-02
  • MBB

    MBB - 2019-09-01

    I'm using this RTC https://www.adafruit.com/product/3028 module.
    This is the data sheet https://datasheets.maximintegrated.com/en/ds/DS3231.pdf for the IC.

    On page 11 of the data sheet it shows the register set up. I'm having trouble with register 02h which sets up the hours. The minutes, seconds, day, and date portions are working

    I wrote this code to trouble shoot the hours portion:

    ;Chip Settings
        #chip 18F4550,48
            #config PLLDIV=1, CPUDIV=OSC1_PLL2, OSC=HSPLL_HS, LVP=OFF, MCLRE=ON
    
           ;Defines (Constants)
            #define LCD_IO 2
                #define LCD_CB PORTE.1
                    #define LCD_DB PORTE.0
    
    Dim HourSTR, Time_HourSTR, Hour_12_24_PM_AM_STR as String
    
    Do_Again:
    ' The below 2 commands should produce b01100011 which = 99 in decimal OR 3 PM in 12 hour mode
      Hour_12_24_PM_AM_STR = "011" ;Bit 7 = 0,  Bit 6 is 12 Hour = 1 & 24 Hour = 0, Bit 5 is PM = 1 & Am = 0
        Hour = 3 ; 1 - 9
    
          Hour = DecToBcd_GCB(Hour) ;Per Data sheet the time portion must be in BCD format
    
            Time_HourSTR = Hour_12_24_PM_AM_STR + Right(ByteToBin(Hour), 5)
              locate 0, 0
                print Time_HourSTR ;Should print out 01100011 and it does
    
                  Time_Hour = Val(Time_HourSTR) ;This is NOT WORKING
                    locate 1, 0
                      print Time_Hour ;This prints out 235. It should print 99.
    
    wait 1 s
    cls
    goto Do_Again
    

    I thought the line Time_Hour = Val(Time_HourSTR)
    would convert 01100011 to 99. It doesn't. I get 235..

    Any ideas?

     

    Last edit: Anobium 2019-09-01
  • Anobium

    Anobium - 2019-09-01

    Why not start with the DS3231 library? See http://gcbasic.sourceforge.net/help/_libraries_overview.html

    And, the demos in your installation? See C:\GCB@Syn\GreatCowBasic\Demos\real_time_clock_solutions. There are many demos for the DS3231 and the documentation is in the directory C:\GCB@Syn\GreatCowBasic\Demos\real_time_clock_solutions\clocks

     
  • MBB

    MBB - 2019-09-01

    Thanks! I'll try these.

     
  • MBB

    MBB - 2019-09-02

    I'm still curious why the code I wrote did not work. Any idea?

     
  • Anobium

    Anobium - 2019-09-02

    Starting with the string.... the binary string of 01100011 is a numeric value of 1100011. So, know this we need to figure out what is going on.

    This is caused by lots of little errors.

    Use Option Explicit. This will start to reveal the issues. As you are NOT using Option explicit everything is assummed to by a Byte.

    When Time_Hour as Long. As the string 1100011 is a lomg number, using the VAL32 method you get the correct result.

    Str  :01100011
    Val32: 1100011
    

    So, I would not be using a string to handle binary byte values.....

    My proof code.

        #chip 18F47K42
        #option Explicit
    
        'USART settings
        #define USART_BAUD_RATE 9600
        #define USART_TX_BLOCKING
    
        'Generated by PIC PPS Tool for Great Cow Basic
        'PPS Tool version: 0.0.5.26
        'PinManager data: v1.76
        'Generated for 18F47K42
        '
        'Template comment at the start of the config file
        '
        #startup InitPPS, 85
        #define PPSToolPart 18F47K42
    
        Sub InitPPS
    
                'Module: UART pin directions
                Dir PORTC.6 Out    ' Make TX1 pin an output
                'Module: UART1
                RC6PPS = 0x0013    'TX1 > RC6
    
        End Sub
        'Template comment at the end of the config file
    
        Dim HourSTR, Time_HourSTR, Hour_12_24_PM_AM_STR as String
        Dim Hour  as word
        Dim Time_Hour as long
    
        do
            ' The below 2 commands should produce b01100011 which = 99 in decimal OR 3 PM in 12 hour mode
            Hour_12_24_PM_AM_STR = "011" ;Bit 7 = 0,  Bit 6 is 12 Hour = 1 & 24 Hour = 0, Bit 5 is PM = 1 & Am = 0
            Hour = 3 ; 1 - 9
    
            Hour = DecToBcd_GCB(Hour) ;Per Data sheet the time portion must be in BCD format
    
            Time_HourSTR = Hour_12_24_PM_AM_STR + Right(ByteToBin(Hour), 5)
            HSerPrint "Str  :"
            hserprint Time_HourSTR ;Should print out 01100011 and it does
            HSerPrintCRLF
    
            HSerPrint "Val32: "
            Time_Hour = Val32(Time_HourSTR)
            hserprint Time_Hour
            HSerPrintCRLF
    
            wait 1 s
    
        loop
    
     

    Last edit: Anobium 2019-09-02

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.