Counter problem


Closed Thread
Results 1 to 7 of 7

Thread: Counter problem

Hybrid View

  1. #1
    Join Date
    Sep 2006
    Posts
    747

    Default Counter problem

    Hello,

    I am having difficulties troubleshooting this problem. I have a countdown timer and when it comes to 59:51 the next number it turns to is 59:44:
    the calculations is good , I did a simulation in excel and the formula does work, what the heck is the problem:
    I am using the DS 1337 and pic16F887

    Code:
    OSCCON = %01110000 '8 Mhz
    DEFINE OSC 8
    ANSELH = 0
    
    '///////////////////////////
    '// Interrupt section //
    '///////////////////////////
    
    
    '/////////////////////////
    '// LCD configuration //
    '/////////////////////////
    
    DEFINE LCD_DREG PORTB 	' Set LCD Data port
    DEFINE LCD_DBIT 4 		' Set starting Data bit (0 or 4) if 4-bit bus  RB.4, RB.5, RB.6, RB.7
    DEFINE LCD_RSREG PORTB 	' Set LCD Register Select port
    DEFINE LCD_RSBIT 1 		' Set LCD Register Select bit
    DEFINE LCD_EREG PORTB	' Set LCD Enable port
    DEFINE LCD_EBIT 0 		' Set LCD Enable bit
    DEFINE LCD_BITS 4 		' Set LCD bus size (4 or 8 bits) '4 therefore put wire at 4, 5, 6 and 7 of LCD
    DEFINE LCD_LINES 2 		' Set number of lines on LCD
    DEFINE LCD_COMMANDUS 2500
    DEFINE LCD_DATAUS 250
    DEFINE CHAR_PACING 2000
    pause 1000
    
    '/////////////////////////
    '// PIN configuration //
    '/////////////////////////
    
    TRISB = %00000000 ' Set PORTB to all output
    TRISA = %11111111 ' Set PORTA to all input
    TRISC = %11000000 ' Set PORTC to all output
    
    
    '///////////////////////////////////////////////
    '// Variable Declaration and initialization //
    '///////////////////////////////////////////////
    
    SDApin var PORTC.4                     ' RTC data 
    SCLpin var PORTC.3                     ' RTC clock
    
    Total_time var word
    RTCSec var byte :RTCMin var byte :RTCHour var byte :RTCWDay var byte :RTCDay var byte :RTCMonth var byte :RTCYear var byte :RTCCtrl var byte 
    
    lcdout $FE,1, "Bienvenue"
    lcdout $FE,$C0, "Welcome" 
    pause 1000
    
    '////////////////////////////////////////////////////
    '////////////////// PROGRAM /////////////////////////
    '////////////////////////////////////////////////////
    
    Mainloop:
    		if PORTC.6 =0 then 
    		Gosub CountDown
    				lcdout $FE,1, "Out of Countdown"
    				lcdout $FE,$C0, " Press C.6 to Start "
    				Pause 150
    		Endif
    		
    goto Mainloop
    End
    
    CountDown:
    I2CWRITE SDApin,SCLpin,$D0,$00,[$00,$00,$00,$00,$00,$00,$00,$00] ' Write to DS1307 to start counter at ZERO
    Pause 20
    	while RTCHour != 1
    
    			I2CRead SDApin,SCLpin,$D0,$00,[RTCSec,RTCMin,RTCHour,RTCWDay,RTCDay,RTCMonth,RTCYear,RTCCtrl]  
    			Pause 20
    				'Will get the time from timer chip and do a 60 min count down
    				'Also add interrupt if someone presses start to view the charge/discharge cycle
    				 Total_time = (RTCMin * 60) + RTCSec
    				 Total_time = 3600 - Total_time
    				lcdout $FE,1, "Time left"
    				lcdout $FE,$C0, " Diff: ", dec2 RTCHour, ":", dec2 Total_time/60 ,":", dec2 Total_time//60
    				Pause 150
    	Wend
    return

  2. #2
    Join Date
    Sep 2006
    Posts
    747


    Did you find this post helpful? Yes | No

    Default

    Hi,

    I also tried a clock program with the DS1337, works good, just that the seconds do not stop at 59, it continues to 80....or 89, thats was last night. How can the chip gives me this kinf of values ????

    K

  3. #3
    Join Date
    Sep 2005
    Location
    Campbell, CA
    Posts
    1,107


    Did you find this post helpful? Yes | No

    Default

    The clock setting and ouput values are in BCD

    There are lots of DS1307 examples on the forum that explain the conversion.
    Charles Linquist

  4. #4
    Join Date
    Sep 2006
    Posts
    747


    Did you find this post helpful? Yes | No

    Default

    I think it might the mod mode, I will try this tonight, basic basic basic:

    Code:
    while RTCHour != 1
    
    			I2CRead SDApin,SCLpin,$D0,$00,[RTCSec,RTCMin,RTCHour,RTCWDay,RTCDay,RTCMonth,RTCYear,RTCCtrl]  
    			Pause 20
    				'Will get the time from timer chip and do a 60 min count down
    				'Also add interrupt if someone presses start to view the charge/discharge cycle
    				 Rest_seconds = 59 - RTCSec
    				 Rest_min = 59 - RTCMin 
    				lcdout $FE,1, "Time: ", dec2 RTCHour, ":", dec2 RTCMin, ":", dec2 RTCSec
    				lcdout $FE,$C0, " Dif: ", dec2 RTCHour, ":", dec2 Rest_min ,":", dec2 Rest_seconds 
    				Pause 150
    	Wend

  5. #5
    Join Date
    Sep 2006
    Posts
    747


    Did you find this post helpful? Yes | No

    Default

    BCD... o crap, yes would explain why it went from 9 to 16 !!!, thanks. Missed that on the spec sheet.

  6. #6
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    Code:
    sec VAR BYTE ' seconds
    mins VAR BYTE ' minutes
    hr VAR BYTE ' hours
    day VAR BYTE ' day
    date VAR BYTE ' date
    mon VAR BYTE ' month
    yr VAR BYTE ' year
     'DEC VARS
    SEC_O   VAR BYTE
    SEC_T   VAR BYTE
    MIN_O   VAR BYTE
    MIN_T   VAR BYTE
    HR_O    VAR BYTE
    HR_T    VAR BYTE
    MON_O   VAR BYTE
    MON_T   VAR BYTE
    DATE_O  VAR BYTE
    DATE_T  VAR BYTE
    YR_O    VAR BYTE
    YR_T    VAR BYTE
    
        '#################################################
    READ_RTC:
    I2CREAD DS_SDA, DS_SCL, RTC, SecReg, [sec,mins,hr,day,date,mon,yr]
    
    SEC_T = sec & $70
    SEC_T = SEC_T>>4
    SEC_O = sec & $0F
    
    MIN_T = mins & $70
    MIN_T = MIN_T>>4
    MIN_O = MINs & $0F
    
    HR_T = hr & $70
    HR_T = HR_T>>4
    HR_O = hr & $0F
    
    MON_T = mon & $70
    MON_T = MON_T>>4
    MON_O = mon & $0F
    
    DATE_T = date & $70
    DATE_T = DATE_T>>4
    DATE_O = date & $0F
    
    YR_T = yr & $70
    YR_T = YR_T>>4
    YR_O = yr & $0F
    Dave
    Always wear safety glasses while programming.

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts