ccp1 with 24 hr timer

Status
Not open for further replies.

abofar

New Member
I wrote a program using ccp1.It is actually a water level meter using pic16f628A. The idea is to use capacitance of two wires in the water to measure water level. I want to measure water consumption in 24 hours. I used interrupt in picbasic pro to make 24hr clock which affected my level reading because ccp1 and 24hr clock use time. The clock, because of the interrupt, stops ccp1 momentarily, the reading is erratic. How can I solve this problem can some one help, please.

Thank you

Obaid
 
Last edited:
The 16F628A has 3 timers on it. Timer 2 is used for the CCP module. Timer 1 is a 16-bit timer and has an input for using a dedicated crystal for using it as an on chip real time clock (most use a 32.768kHz xtal for this). Use timer 1 for the 24Hr timer.
 
Last edited:
The problem no timer works with my code. I am posting my code ,please excuse me for messy unprofessional code, don' lough!. The timer I usually use is Darrel Taylor's "NewelapsedTimMod.BAS" it so good and simple to use just include the file in my code,since I had so much problems with picbasic pro interrupt. The code is working beautifully.
The code :

Code:
 define OSC 16
    PAUSE 10000                  'To allow in circuit programing
    DEFINE LCD_DREG  PORTB   	' Set LCD Data port
	DEFINE LCD_DBIT  4        	' Set starting Data bit (0 or 4) if 4-bit bus
	DEFINE LCD_RSREG PORTB     	' Set LCD Register Select port
	DEFINE LCD_RSBIT 0       	' Set LCD Register Select bit
	DEFINE LCD_EREG  PORTB   	' Set LCD Enable port
	DEFINE LCD_EBIT  1       	' Set LCD Enable bit
	DEFINE LCD_BITS  4       	' Set LCD bus size (4 or 8 bits)
	DEFINE LCD_LINES 2       	' Set number of lines on LCD
	DEFINE LCD_COMMANDUS   2000	' Set command delay time in us
	DEFINE LCD_DATAUS        50	' Set data delay time in us
    Pause 1000                  ' WAIT FOR LCD TO COME UP
' Software Defines 
' ----------------
   capture VAR PIR1.2 
' CCP1 capture flag 
   overflow VAR PIR1.0 
' Timer1 overflow flag 
   CalPeriod var word  'Caliberated Period.
   temp var word      'pereiod storage 
   period VAR WORD 
' Period value 
   TempA var word 
   TempB var Word
   Zreading var word
   cal var PORTB.2
   barnum var    byte
   h var byte
   level var word
   barsiz var    byte
   CalValue var word
   I VAR BYTE
   hight var word
   DIMswitch var PORTA.1  ' SET DIMENTION SWITCH 
   LENGTH VAR WORD
   WIDTH VAR WORD
   T VAR WORD
   REMD VAR WORD
   LVL VAR WORD
   S VAR WORD
   F VAR WORD
   UPDATE VAR BIT
   BarNum=2                   'to prevent divide by 0
 'SET DEFAULT DIMENTION 

 'FIRST, ON START UP DISPLAY HIGHT
    READ 2,HIGHT.BYTE0
    READ 3,HIGHT.BYTE1            'READ HIGHT FROM EEPROM AND DISPLAY IT.
    READ 4,LENGTH.BYTE0
    READ 5,LENGTH.BYTE1
    READ 6,WIDTH.BYTE0
    READ 7,WIDTH.BYTE1  
    Lcdout $fe,1,"HI,"," ","LEN,"," ","WID"	
    Lcdout $fe,$C0,#hight,",",#LENGTH,",",#WIDTH," ","cm"
    PAUSE 6000      'WAIT 5S TO SEE DIMENTION DISPLAY.

' Initialise Hardware 
' ------------------- 
     UPDATE=0

'CALL ENTER HIGHT SUBROUTINE
    GOSUB ENTERHIGHT
    gosub ENTERLENGTH
    GOSUB ENTERWIDTH   
    TRISA.0=1
    TRISA.2=1
    TRISA.3=0   'porta as input A0,A2,A4 input A3, output 
    TRISB.2=1       'reset swich input
    TRISB.3=1  
    
    ' prescale 1:1 AND Timer OFF 
    T1CON=%00000000
   
   ' Capture is OFF 
    CCP1CON=0 
 
   ' Main Program Loop 
' ----------------- 
 Loop: 
    CMCON = %110         ' Set comparators mode
    select case cal
    CASE 0 

'place error in CalValue nonvolitile memory (EEprom) storage
    Calvalue=LVL
    Write 0,calvalue.byte0
    write 1,calvalue.byte1
    LCDOut $FE,1,"Caliberations"
    LCDOUT $FE,$C0,"ERROR= ",#LVL
    Pause 2000
goto loop
    end select
 
 'INITALIZATION
' -------------- 
    CMCON = %110         ' Set comparators mode
    TMR1L=0 ' Reset Timer 
    TMR1H=0 ' Remember Timer is OFF 
    overflow=0
    capture=0 ' Reset Capture Flag 
    CCP1CON = %00000111  ' Capture mode, capture on every 16 rising edge
    While Capture=0
    CMCON = %110         ' Set comparators mode
    Wend 
 ' Junk 1st Result 
    T1CON.0=1           ' Enable Timer 
    TMR1L=0 ' Reset Timer low byte
    TMR1H=0 ' Reset Timer high byte 
    capture=0 ' Reset Capture Flag 
 ' Wait for 2nd Result 
    While Capture=0
    CMCON = %110         ' Set comparators mode
    Wend 
' Store the captured value in period variable     
    LEVEL.lowbyte = CCPR1L 
    LEVEL.highbyte = CCPR1H 
    LVL=LEVEL        'NON CALIBERATED LEVEL (ERROR).
    T1CON.0=0 ' Timer is OFF 
    CCP1CON=0 ' Capture is OFF 

' Display Section 
' --------------- 
    If overflow=0 then 'converted to uS(Period/2 * 25/100 1tick=.25uS
    If LEVEL<32768 then
'read stored caliberation value (ERROR) from EEprom
    read 0,calvalue.byte0
    read 1,calvalue.byte1
    select case CalValue             'Zreading
    case is > LEVEL
    LEVEL=Calvalue
    end select
    READ 2,HIGHT.BYTE0
    READ 3,HIGHT.BYTE1            'READ HIGHT FROM EEPROM AND DISPLAY IT.
    READ 4,LENGTH.BYTE0          'READ LENGTH AND WIDTH TO DISPLAY VOLUME
    READ 5,LENGTH.BYTE1
    READ 6,WIDTH.BYTE0
    READ 7,WIDTH.BYTE1  
    LEVEL=LEVEL-Calvalue 
    level=(level*24)/10     'WATER LEVEL In cm           
           S=LEVEL*LENGTH*WIDTH  'WATER VOLUME
           F=DIV32 10000
           T=F/100              'WATER VOLUME IN TONS
           REMD=F//100          'remainder in decimal fraction        
    barsiz=HIGHT/16
    barnum=level/barsiz
          
          'DISPLAY WATER LEVE IN CM WITH BAR GRAPH.
     LCDOut $FE,1,#LEVEL," ","cm"," ",#T,".",dec2 REMD," ","T"          '"LEVEL =",#level," ","cm" 
     LCDOUT $fe,$C0,rep%11111111\barnum
     pause 500
     goto DisplayErrorSkip 
     Endif
     endif
'OUT OF RANGE
     LCDOut $FE,1,"Out of Range"
     PAUSE 1000
     Overflow=0 ' Reset error flag 
     DisplayErrorSkip: 
     Pause 500 ' Allow Time for user to view 
     Goto Loop 

'SUBROUTINE TO ENTER HIGHT IN CM
ENTERHIGHT:
     CMCON=7    'COMPARATORS OFF TO ENABLE PORTA I/O.
     TRISA.1=1   'PORTA.1 INPUT
     'Pause 500    ' Wait for LCD to startup
'ENTER HIGHT NOW BY BUTTON PRESS     
     Lcdout $fe,1,"New Hight CM"
     PAUSE 3000
     HIGHT=0 'CLEAR HIGHT
InsertHight:    
     select case DIMswitch 
     case 0   
   if update=0 then
    hight = hight+10
    IF HIGHT>900 THEN
    HIGHT=0
    ENDIF
  debounceH:
    For i = 1 To 25
        Pause 10        ' 10ms at a time so no interrupts are lost
        Next i             
      UPDATE=1 
       GOTO DISPLAYH	
DISPLAYH:
if UPDATE=1 THEN
    Lcdout $fe,1,"    Hight "	
    Lcdout $fe,$C0,"    ",#hight," ","cm"	
'WRITE HIGHT TO EEPROM
    Write 2,HIGHT.BYTE0
    WRITE 3,HIGHT.BYTE1
     ENDIF
     pause 500
    UPDATE=0
    Goto inserthight
    endif
    END SELECT
    rETURN
    
    'SUBROUTINE TO ENTER length IN CM
ENTERLENGTH:
     CMCON=7    'COMPARATORS OFF TO ENABLE PORTA I/O.
     TRISA.1=1   'PORTA.1 INPUT
     'Pause 500    ' Wait for LCD to startup
'ENTER LENGTH NOW BY BUTTON PRESS     
     Lcdout $fe,1,"New Length cm"
     PAUSE 3000
     LENGTH=0 'CLEAR HIGHT
InsertLENGTH:    
     select case DIMswitch 
     case 0   
   if update=0 then
    LENGTH = LENGTH+10
    IF LENGTH>900 THEN
    LENGTH=0
    ENDIF
  debounceL:
    For i = 1 To 25
        Pause 10        ' 10ms at a time so no interrupts are lost
        Next i             
      UPDATE=1 
       GOTO DISPLAYL	
DISPLAYL:
if UPDATE=1 THEN
    Lcdout $fe,1,"    Length "	
    Lcdout $fe,$C0,"    ",#LENGTH," ","cm"	
'WRITE LENGTH TO EEPROM
    Write 4,LENGTH.BYTE0
    WRITE 5,LENGTH.BYTE1
     ENDIF
     pause 500
    UPDATE=0
    Goto insertLENGTH
    endif
    END SELECT
    rETURN

'SUBROUTINE TO ENTER WIDTH IN CM
ENTERWIDTH:
     CMCON=7    'COMPARATORS OFF TO ENABLE PORTA I/O.
     TRISA.1=1   'PORTA.1 INPUT
'ENTER WIDTH NOW BY BUTTON PRESS     
     Lcdout $fe,1,"New Width cm"
     PAUSE 3000
     WIDTH=0 'CLEAR HIGHT
InsertWIDTH:    
     select case DIMswitch 
     case 0   
   if update=0 then
   WIDTH= WIDTH+10
    IF WIDTH>900 THEN
    WIDTH=0
    ENDIF
  debounceW:
    For i = 1 To 25
        Pause 10        ' 10ms at a time so no interrupts are lost
        Next i             
      UPDATE=1 
       GOTO DISPLAYW	
DISPLAYW:
if UPDATE=1 THEN
    Lcdout $fe,1,"    Width "	
    Lcdout $fe,$C0,"    ",#WIDTH," ","cm"	
'WRITE HIGHT TO EEPROM
    Write 6,WIDTH.BYTE0
    WRITE 7,WIDTH.BYTE1
     ENDIF
     pause 400
    UPDATE=0
    Goto insertWIDTH
    endif
    END SELECT
   RETURN
End
Obaid
 
Last edited:
Can you edit your post and put code tags around your code so it keeps it's format? You can simply type [code] before and [/code] after it or use the code button.

Mike.
 
Last edited by a moderator:
Sorry but you came to a site where PIC BASIC PRO is not the dominant language here. There are a few who know it, but most here code in either assembly or Hitech C.
 
I can't see how your code ever gets to the initialization code.
Code:
   ' Main Program Loop 
' ----------------- 
 Loop: 
    CMCON = %110         ' Set comparators mode
    select case cal
    CASE 0 
 
'place error in CalValue nonvolitile memory (EEprom) storage
    Calvalue=LVL
    Write 0,calvalue.byte0
    write 1,calvalue.byte1
    LCDOut $FE,1,"Caliberations"
    LCDOUT $FE,$C0,"ERROR= ",#LVL
    Pause 2000
goto loop                      <-------------------     how does it ever get past this?
    end select
 
 'INITALIZATION
' -------------- 
    CMCON = %110         ' Set comparators mode
    TMR1L=0 ' Reset Timer

And what do you mean by "The code is working beautifully." Is it working or not?

Mike.
 
Thank you Mike for your response. The code is working . It is looping only as long as the cal switch is pressed. The problem is the 24 hour timer to measure water consumption.

Obaid
 
Last edited:
Thank you Mike for your response. The code is working . It is looping only as long as the cal switch is pressed. The problem is the 24 hour timer to measure water consumption.

Obaid

How does the initialisation code get executed?

Mike.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…