Clock and Dual Termometer


Closed Thread
Results 1 to 25 of 25

Hybrid View

  1. #1
    Join Date
    Dec 2008
    Location
    Ploiesti, ROMANIA
    Posts
    582


    Did you find this post helpful? Yes | No

    Default

    Ok, I think that my previous code it's wrong in his essence...so...
    I try another two version, based on codes from : Paul R. Borgmeier, PhD (see EZCLOCK1) and Mr.Bruce (see RENTRON.COM). Still not good precision ... Any ideeas ?
    Code:
    @ DEVICE pic16F628A, XT_osc, WDT_OFF, PWRT_OFF, BOD_OFF, MCLR_ON, LVP_OFF, CPD_OFF, PROTECT_OFF     ' for PBP 2.4
    DEFINE OSC 4
    INCLUDE "AllDigital.pbp"
    TRISA= %11111000                       ' RA0..3=Outputs RA4=Input
    TRISB= %00001111 			         ' RB0..RB2=Inputs, RB3..RB7=Outputs
    CMCON=7                                ' Disable comparators
    OPTION_REG=%00000111
    INTCON=0
    
    DEFINE LCD_DREG PORTB                  ' LCD on port B
    DEFINE LCD_DBIT 4                      ' Data bits B4..B7
    DEFINE LCD_RSREG PORTA                 ' RS on PORTA
    DEFINE LCD_RSBIT 1                     ' RS on A1 
    DEFINE LCD_EREG PORTA                  ' E on PORTA
    DEFINE LCD_EBIT 0                      ' E on A0
    DEFINE LCD_BITS 4                      ' LCD 4 bit mode
    DEFINE LCD_LINES 2                     ' 2 line LCD display
    
    
    HzTimer         Var     Word
    HH              Var     Byte        
    MM              Var     Byte        
    SS              Var     Byte
    ZIUA            VAR     BYTE
    LUNA            VAR     BYTE		
    X       	    Var     Byte	
    col             Var     Bit
    
    TempC           Var     Word
    Float           Var     Word
    Sign		    Var	BIT		   ' +/- sign
    DQ		    Var     PORTA.4	   ' One-wire data pin
    TempC2          Var     Word
    Float2          Var     Word
    Sign2		    Var	BIT		   ' +/- sign
    DQ2		    Var     PORTA.3	   ' One-wire data pin
    Delay           var     byte
    Mode            VAR     BYTE
    semn		    var word
    semn2           var word
    DS18B20_1_12bit CON %01111111 ; 750ms,   0.0625°C  (default)
    DS18B20_2_12bit CON %01111111 ; 750ms,   0.0625°C  (default)
    
    HzTimer = $7A12
    HH=00 : MM=00 : SS=00 : ZIUA=01 : LUNA=01 : col=0  : Mode=0
    
    Pause 500
    LCDOUT $FE, 1, $FE, $0C                 ' Clear display, cursor off
    Pause 100
    
    '====================================
    ' Init Sensor 1
     OWOUT DQ, 1, [$CC, $4E, 0, 0, DS18B20_1_12bit]
     OWOut DQ, 1, [$CC, $48]       	    ' Start temperature conversion
     OWOut DQ, 1, [$CC, $B8] 
     OWOut DQ, 1, [$CC, $BE] 
     Pause 50
     OWIn DQ, 2, [TempC.byte0, TempC.byte1]
     Pause 50 
    ' Init Sensor 2
     OWOUT DQ2, 1, [$CC, $4E, 0, 0, DS18B20_2_12bit]
     OWOut DQ2, 1, [$CC, $48]       	    ' Start temperature conversion
     OWOut DQ2, 1, [$CC, $B8] 
     OWOut DQ2, 1, [$CC, $BE] 
     Pause 50
     OWIn DQ2, 2, [TempC2.byte0, TempC2.byte1]
     Pause 50 
    
    
    OPTION_REG.7=1
    TMR0=0
    INTCON.2=0
    
    '====================================
    
    Pause 500
    LCDOUT $FE, 1, $FE, $0C                 ' Clear display, cursor off
    Pause 250
    
    LCDOUT $FE, $80,  "  TERMO-CLOCK   "
    LcdOut $FE, $C0,  "By NICULESCU DAN" 
    PAUSE 3000
    LCDOUT $FE, 1, $FE, $0C
    
    
    '====================================
        
    MainLoop: 
     If PORTB.0=0 then                      ' Mode switch pressed
      Pause 50                              ' Debounce 
    LcdOut $FE, 1
     LcdOut $FE, $C0, "         SETTING"    ' Show that command is accepted 
    PAUSE 500
     LcdOut $FE, 1
    If PORTB.0=0 then MainLoop              ' Wait until button is released  
      Mode=Mode+1                           ' Increment mode
    
    ENDIF
    
    
    If Mode=1 then                          ' SET HOUR
    LcdOut $FE, $80, dec2 HH
    LcdOut $FE, $C0, "HOUR     SETTING"
    	if portb.1=0 then
          HH=HH+1
            IF HH=24 then
              HH=0
            ENDIF
          Gosub Debounce
    	endif
    
    	if portb.2=0 then
            IF HH = 0 then
              HH=24
            ENDIF
          HH=HH-1
          Gosub Debounce
    	endif
    EndIf
    
    If Mode=2 then                          ' SET MINUTES
    LcdOut $FE, $80, dec2 HH,":",dec2 MM
    LcdOut $FE, $C0, "MINUTES  SETTING"        
    
    	if portb.1=0 then
          SS=0                        
          MM=MM+1
            IF MM=60 THEN
              MM=0
            ENDIF
          Gosub Debounce
    	endif
    
    	if portb.2=0 then
          SS=0                        
            IF MM =<0 THEN
              MM=60
            ENDIF
          MM=MM-1
    	Gosub Debounce
    	endif
    EndIf
    
    If Mode=3 then                          ' SET DAY
    LcdOut $FE, $80, dec2 HH,":",dec2 MM,":",dec2 ss,"   ",DEC2 ZIUA,"/"
    LcdOut $FE, $C0, "DAY      SETTING"
    	if portb.1=0 then
          ziua=ziua+1
                IF LUNA=2 THEN 
                      IF ZIUA > 28 THEN
                      ZIUA=1
                      ENDIF
                ENDIF
                IF LUNA=4 OR LUNA=6 OR LUNA=9 OR LUNA=11 THEN  
                      IF ZIUA > 30 THEN ZIUA=1
                      ELSE
                      IF ZIUA > 31 THEN ZIUA=1
                      ENDIF 
          Gosub Debounce
    	endif
    
    	if portb.2=0 then
          ZIUA=ZIUA-1
                IF ZIUA = 0 THEN ZIUA=31
          Gosub Debounce
    	endif
    EndIf
    
    If Mode=4 then                           ' SET MONTH 
    LcdOut $FE, $80, dec2 HH,":",dec2 MM,":",dec2 ss,"   ",DEC2 ZIUA,"/",DEC2 LUNA
    LcdOut $FE, $C0, "MONTH    SETTING"
    	if portb.1=0 then
          luna=luna+1
                if luna>12 then
                luna=1
                endif
          Gosub Debounce
    	endif
    
    	if portb.2=0 then
          luna=luna-1
                if luna<1 then
                luna=12
                endif
          Gosub Debounce
    	endif
    EndIf
    
    If Mode > 4 then 
    	LCDOUT $FE, $C0, "END      SETTING"
    	PAUSE 1000
        LCDOUT $FE, 1, $FE, $0C 
    	mode=0
    EndIf
    
    If Mode > 0 then Mainloop               
    
    '====================================
    Main :
    ClockLoop : IF INTCON.2=0 THEN ClockLoop
    INTCON.2=0
    
    
    if portb.0=0 then Gosub MainLoop
       
    HzTimer = HzTimer - $1000  'decrement timer
    IF HzTimer<$1000  THEN
        IF Col=1 THEN 'update time'
            SS=SS+1
            IF SS=60 THEN
                SS=0 
                MM=MM+1
                IF MM=60 THEN
                    MM=0
                    HH=HH+1
                    IF HH=24 THEN
                        HH=0
                        ZIUA=ZIUA+1
                             IF LUNA=2 THEN 
                                 IF ZIUA > 28 THEN
                                 ZIUA=1
                                 LUNA=3
                                 ENDIF
                             ENDIF
                             IF LUNA=4 OR LUNA=6 OR LUNA=9 OR LUNA=11 THEN  
                                    IF ZIUA > 30 THEN 
                                    ZIUA=1
                                    LUNA=LUNA+1                              
                                    ENDIF
                             ENDIF
                             IF LUNA=1 OR LUNA=3 OR LUNA=5 OR LUNA=7 OR LUNA=8 OR LUNA=10  THEN
                                    IF ZIUA > 31 THEN 
                                    ZIUA=1
                                    LUNA=LUNA+1 
                                    ENDIF                              
                             ENDIF            
    				 IF LUNA=12 THEN
                                    IF ZIUA > 31 THEN 
                                    ZIUA=1
                                    LUNA=1
                                    ENDIF
                             ENDIF           
                    ENDIF
                ENDIF
             ENDIF
    
    
    '==================================== display time with :                  
              
         LcdOut $FE, $80, dec2 HH,":",dec2 MM,":",dec2 ss,"   ",DEC2 ZIUA,"/",DEC2 LUNA
    
             ELSE
    '==================================== display time without :
         LcdOut $FE, $80, dec2 HH,":",dec2 MM," ",dec2 ss,"   ",DEC2 ZIUA,"/",DEC2 LUNA
     
        ENDIF
        Col=Col+1
        HzTimer=HzTimer+$7A12
    else
      gosub ReadTemp
    ENDIF
       
    
    goto Main
    
    
    
    ' SUBROUTINES:
    '====================================
    ReadTemp:
        OWOut DQ, 1, [$CC, $44] 
        OWOut DQ, 1, [$CC, $BE]
        OWIn  DQ, 2, [TempC.byte0, TempC.byte1]         
    
        Sign = TempC.15
        TempC = ABS(TempC)
        TempC = ((TempC >> 4)*100) + ((TempC & $F)*100 >> 4)
        IF Sign THEN TempC = -TempC
    IF TempC.15 THEN 
    Semn="-" 
    else
    Semn="+"
    endif
    
        OWOut DQ2, 1, [$CC, $44] 
        OWOut DQ2, 1, [$CC, $BE]
        OWIn  DQ2, 2, [TempC2.byte0, TempC2.byte1]         
    
        Sign2 = TempC2.15
        TempC2 = ABS(TempC2)
        TempC2 = ((TempC2 >> 4)*100) + ((TempC2 & $F)*100 >> 4)
        IF Sign2 THEN TempC2 = -TempC2
    IF TempC2.15 THEN 
    Semn2="-"
    else
    Semn2="+"
    endif
    LCDOUT $FE, $C0, semn,DEC ABS TempC/100,".", DEC1 ABS TempC/10, 223,"C ", $FE, $C0 + 9, semn2,DEC ABS TempC2/100,".", DEC1 ABS TempC2/10, 223,"C   " 
    Return
    '====================================
    Debounce :
    For Delay= 1 to 200
    Pause 1
    next Delay
    Return   
    '====================================
    END       					     ' Of program
    and
    Code:
    @ DEVICE pic16F628A, XT_osc, WDT_OFF, PWRT_OFF, BOD_OFF, MCLR_ON, LVP_OFF
    DEFINE OSC 4  				   ' using a 4MHz crystal
    
    TRISA= %11111000                       ' RA0..3=Outputs RA4=Input
    TRISB= %00001111 			         ' RB0..RB2=Inputs, RB3..RB7=Outputs
    DEFINE OSC 4  ' using a 4MHz crystal
    
    DEFINE LCD_DREG PORTB                  ' LCD on port B
    DEFINE LCD_DBIT 4                      ' Data bits B4..B7
    DEFINE LCD_RSREG PORTA                 ' RS on PORTA
    DEFINE LCD_RSBIT 1                     ' RS on A1 
    DEFINE LCD_EREG PORTA                  ' E on PORTA
    DEFINE LCD_EBIT 0                      ' E on A0
    DEFINE LCD_BITS 4                      ' LCD 4 bit mode
    DEFINE LCD_LINES 2                     ' 2 line LCD display
    DEFINE LCD_COMMANDUS 2000 ' Command delay time in us 
    DEFINE LCD_DATAUS 50      ' Data delay time in us 
    
    
    TempC           var     word
    Float           var     word
    Sign		    var	bit		   ' +/- sign
    DQ		    var     PORTA.4	   ' One-wire data pin
    TempC2          var     word
    Float2          var     word
    Sign2		    var	BIT		   ' +/- sign
    DQ2		    var     PORTA.3	   ' One-wire data pin
    Delay           var     byte
    Mode            var     byte 
    semn		    var     word
    semn2           var     word
    
    DS18B20_1_12bit CON %01111111 	   ; 750ms,   0.0625°C  (default)
    DS18B20_2_12bit CON %01111111          ; 750ms,   0.0625°C  (default)
    
    
     
    ' setup vars for clock
    Time    VAR BYTE   			  ' accumulates TMR2 to PR2 match counts
    Minutes VAR BYTE   			  ' minutes
    Hours   VAR BYTE   			  ' hours
    Seconds VAR BYTE
    Match   VAR PIR1.1 			  ' TMR2 to PR2 match interrupt flag bit
     
    CMCON = 7          			  ' disable comparators
    INTCON = 0         			  ' not using interupts. Just monitoring int flag bits
     
    Time = 0           			  ' clear TMR2 to PR2 match counter
    Hours = 00         			  ' set clock starting hour here
    Minutes = 00       			  ' set clock starting minutes here
    Seconds = 0
     
    PR2 = 249           			  ' 249 +1 extra cycle for TMR2 reset = 250*5*16*1uS=20mS
    Match = 0           			  ' clear match flag
     
    
    ' setup & start TMR2
    T2CON = %00100110   			  ' 1:5 postscale, 1:16 prescale, TMR2 on
    
     
    Main:
       ' every 20mS the TMR2IF flag is set, and this routine is entered.
       ' Plenty of time to do other stuff.
       IF Match THEN           	 	 ' has TMR2 matched PR2? (should happen every 20mS)
          Match = 0           	 	 ' yes. clear TMR2 to PR2 match flag bit
          Time = Time + 1     	 	 ' increment 20mS count     
          IF Time = 50 THEN   	 	 ' 50 x 20mS = 1 second
             Time = 0         	 	 ' show time in 1 second intervals   
    LcdOut $FE, $80, DEC2 Hours, ":",DEC2 Minutes, ":",DEC2 Seconds,13,10
    LCDOUT $FE, $C0, semn,DEC ABS TempC/100,".", DEC1 ABS TempC/10, 223,"C ", $FE, $C0 + 9, semn2,DEC ABS TempC2/100,".", DEC1 ABS TempC2/10, 223,"C   " 
    Gosub readtemp 
    	Seconds = Seconds + 1
          ENDIF    
          IF Seconds = 60 THEN     	 ' has 60 seconds passed?
             Seconds = 0
             Minutes = Minutes + 1 	 ' increment minute count
             IF Minutes = 60 THEN  	 ' have 60 minutes passed?
                Minutes = 0         	 ' yes. roll-over minutes 00
                Hours = Hours + 1  	 ' update hours
                IF Hours = 24 THEN Hours = 0 ' roll-over hours from 24 to 00
             ENDIF 			 	 ' end IF Minutes = 60                
          ENDIF    			 	 ' end IF Seconds = 60
       ENDIF       			 	 ' end IF Match
     
       GOTO Main
     
    
    ReadTemp:
        OWOut DQ, 1, [$CC, $44]
        OWOut DQ, 1, [$CC, $BE]
        OWIn  DQ, 2, [TempC.byte0, TempC.byte1]         
    
        Sign = TempC.15
        TempC = ABS(TempC)
        TempC = ((TempC >> 4)*100) + ((TempC & $F)*100 >> 4)
        IF Sign THEN TempC = -TempC
    IF TempC.15 THEN 
    Semn="-" 
    else
    Semn="+"
    endif
    
        OWOut DQ2, 1, [$CC, $44]
        OWOut DQ2, 1, [$CC, $BE]
        OWIn  DQ2, 2, [TempC2.byte0, TempC2.byte1]         
    
        Sign2 = TempC2.15
        TempC2 = ABS(TempC2)
        TempC2 = ((TempC2 >> 4)*100) + ((TempC2 & $F)*100 >> 4)
        IF Sign2 THEN TempC2 = -TempC2
    IF TempC2.15 THEN 
    Semn2="-"
    else
    Semn2="+"
    endif
    Return
    
      END
    Sorry for long post... It's a proof of hard ( useless ) work...

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


    Did you find this post helpful? Yes | No

    Default

    Dave
    Always wear safety glasses while programming.

  3. #3
    Join Date
    Aug 2010
    Location
    Maryland, USA
    Posts
    869


    Did you find this post helpful? Yes | No

    Default

    First thing I would try is take out the gosub readtemp. Just for testing purpose. Then report back, tell us exactly how it is not precision. is it losing 1 second per min. is it gaining 1? what is the output you are seeing?

    BTW, I am looking at the second program
    -Bert

    The glass is not half full or half empty, Its twice as big as needed for the job!

    http://foamcasualty.com/ - Warbird R/C scratch building with foam!

  4. #4
    Join Date
    Dec 2008
    Location
    Ploiesti, ROMANIA
    Posts
    582


    Did you find this post helpful? Yes | No

    Default

    Thanks for atention ! All three programs are - without read_temperature - verry accurate !
    I try, in all three cases, to put the "read_temp" code in other places... the results are inadequates !

  5. #5
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,653


    Did you find this post helpful? Yes | No

    Default

    @ first you can try your program with a 9 bits resolution, to have the quickest sensor conversion time ...

    THEN you will know where is something wrong ...

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  6. #6
    malc-c's Avatar
    malc-c Guest


    Did you find this post helpful? Yes | No

    Default

    Not checked the data sheet so can't confirm if this is an option, but for the sake of a couple of ££ why not use a DS1307 RTC chip ??

  7. #7
    Join Date
    Aug 2010
    Location
    Maryland, USA
    Posts
    869


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by fratello View Post
    I try, in all three cases, to put the "read_temp" code in other places... the results are inadequates !
    Still waiting to hear what the results are. Is it slow, fast, sometimes right, sometimes wrong? Is it close or all over the place?
    -Bert

    The glass is not half full or half empty, Its twice as big as needed for the job!

    http://foamcasualty.com/ - Warbird R/C scratch building with foam!

  8. #8
    Join Date
    Dec 2008
    Location
    Ploiesti, ROMANIA
    Posts
    582


    Did you find this post helpful? Yes | No

    Default

    The clock run ok without read_temp, in all cases and run faster ( strange for me ) if use this subroutine.
    I try now with 9 bits resolution of DS18B20, wait for result !
    Thanks for support !

  9. #9
    Join Date
    Dec 2008
    Location
    Ploiesti, ROMANIA
    Posts
    582


    Did you find this post helpful? Yes | No

    Default

    If DS18B20 it's in 9 bit mode, the clock run faster (!) with ~ 2 sec/hour (5 sec in 3 hours).

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