Time Out ?


Closed Thread
Results 1 to 13 of 13

Thread: Time Out ?

  1. #1

    Default Time Out ?

    Hello,
    Awhile back Bruce had an article about a simple remote control, here's the link:
    http://www.picbasic.co.uk/forum/showthread.php?t=12554

    I do not understand ASM that why I purchase PBP, but within the example it appears to timeout if no serial data is received in a certain amount of time. However with the RXM-418-LR series the output is always noisy with 1's and 0's as everyone knows unless a squelch circuit is used. Of course then range is compromised.

    My question is how can I modify the ASM code to timeout on RA.0 (PortA.0, my serin port on a 16F628A) but not clearing any other port, or shall I say outputs?

    Also, I am using an external 4MHZ oscillator, how can I change OSCCON = %01100000 ' Internal 4MHz select, to be external?

    I see the area that say "clrf GPIO ; Clear outputs on button release (or timeout)"
    which looking over the 16F628A datasheet I could go :
    "clrf PORTA ; Clear outputs on button release (or timeout)"
    "clrf PORTB ; Clear outputs on button release (or timeout)"
    but all I am trying to accomplish is to jump away from receiving junk if

    By the way my TRISA = %00000001 TRISB= %00000000

    Thanks

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


    Did you find this post helpful? Yes | No

    Default

    The 628A does not have an OSCCON , the internal if used is a fixed 4MHz.
    You need to modify the chip's *.inc file to change the OSC.
    http://www.picbasic.co.uk/forum/cont...o-your-Program

    Bruce's code...
    Read this part
    Code:
     ' Fire up Timer1 before entry to serial input routine
        T1CON.0 = 1
        
        ' @4MHz Timer1 overflows in 65536 * 1uS (~65.5mS) if no Synch byte
        ' and serial data arrive on time. SERIN2 timeout & label options
        ' are useless with a noisy RF receiver output - as noise continually
        ' resets the timeout period causing it to hang forever.
        
        ' Wait for Synch byte, then get new inbound data & checksum
        SERIN2 D_IN,BAUD,[WAIT(Synch),DAT_IN1,DAT_IN2,CHK_SUM]
        
        T1CON.0 = 0    ' Stop Timer1 once we've received data
        TMR1L = 0      ' Clear low byte
        TMR1H = 0      ' Clear high byte
    Basically if the "WAIT(SYNCH)" does not happen the interrupt timer kicks in to reset/clear what ever.

    If "WAIT(SYNCH)" condition is met then the code falls through to stop the timer. So it does not matter what pin you receive on.

    If you do not want to clear anything just remove that portion of the ASM.
    Dave
    Always wear safety glasses while programming.

  3. #3


    Did you find this post helpful? Yes | No

    Default

    Thanks for the info, but still not sure what to delete or keep. Here is what I have so far:
    Code:
    INCLUDE "MODEDEFS.BAS"
    @ DEVICE PIC16F628a,XT_OSC
    @ DEVICE pic16F628a, WDT_OFF
    @ DEVICE pic16F628a, PWRT_ON
    @ DEVICE pic16F628a, MCLR_ON
    @ DEVICE pic16F628a, BOD_ON
    @ DEVICE pic16F628a, LVP_OFF
    @ DEVICE pic16F628a, CPD_OFF
    @ DEVICE pic16F628a, PROTECT_OFF
    
    DEFINE OSC 4
    DEFINE NO_CLRWDT 1      ' Watchdog timer is disabled, so we don't need to reset it
    DEFINE INTHAND RESET_VT ' Interrrupt on Timer1 overflow to reset outputs in 65.5mS
                                ' if no serial data received in this time period                           
    'SYMBOL  D_IN = porta.0   ' Encoder data input pin
    SYMBOL  serpin = porta.0   ' Encoder data input pin
    SYMBOL  TMR1IF = PIR1.0 ' Timer1 overflow interrupt flag (reset in int handler)
    SYMBOL  TMR1IE = PIE1.0 ' Timer1 interrupt enable bit
    CMCON=%00000111
    vrcon=0
    trisa = %00000001
    trisb = %00000000
    PORTA = 0
    PORTB = 0               
    'serpin VAR porta.0 'serial input pin
    address con %0000000000000000 
    add1 VAR WORD   
    address1 var byte
    address2 var byte
    address3 var byte
    address4 var byte
    addA var   byte '0-7 
    addB var   byte '8-15
    addC var   byte '0-7 
    addD var   byte '8-15
    chkcrc var byte
    crcA var byte
    crcB var byte
    chksum2 var byte bank0 system '
    chksum3 var byte
    flag var byte 'latching feature
    i var byte
    latch var byte
    mydata var byte 
    mydata1 VAR byte
    mydata2 var byte
    mydata3 var byte
    mydata4 var byte
    rf var porta.4
    
        'Variables for saving state in interrupt handler
        wsave	VAR	BYTE $70 system		' Saves W
        ssave	VAR	BYTE bank0 system	' Saves STATUS
        psave	VAR	BYTE bank0 system	' Saves PCLATH
        fsave	VAR	BYTE bank0 system	' Saves FSR
        
        ' Setup Timer1 for resets after ~65.5mS
        T1CON = %00000000   ' Internal clock, 1:1 prescale, Timer1 off for now
        TMR1L = 0 
        TMR1H = 0           ' Timer1 low & high bytes cleared
        TMR1IF = 0          ' Clear Timer1 overflow flag before enabling interrupt
        TMR1IE = 1          ' Enable Timer1 overflow interrupt
        INTCON = %11000000  ' Global & peripheral ints enabled
        chksum2 = 0           ' Clear match count
        GOTO cycle        ' Jump over int handler
        
    ASM
    RESET_VT
        movwf	wsave			; Save W
        swapf	STATUS, W		; Swap STATUS to W (swap avoids changing STATUS)
        clrf	STATUS			; Clear STATUS
        movwf	ssave			; Save swapped STATUS
        movf	PCLATH, W		; Move PCLATH to W
        movwf	psave			; Save PCLATH
        movf	FSR, W			; Move FSR to W
        movwf	fsave			; Save FSR
        
        ; Do interrupt stuff here
        bcf     T1CON,TMR1ON    ; Stop Timer1
        clrf    TMR1L           ; Clear low byte
        clrf    TMR1H           ; Clear high byte
        bcf     PIR1,TMR1IF     ; Clear Timer1 interrupt flag bit
        ;clrf    GPIO            ; Clear outputs on button release (or timeout)    
        clrf    chksum2           ; Clear match variable
        
        ; Restore FSR, PCLATH, STATUS and W registers
        movf    fsave, W		; retrieve FSR value
        movwf	FSR				; Restore it to FSR
        movf	psave, W		; Retrieve PCLATH value
        movwf	PCLATH			; Restore it to PCLATH
        swapf	ssave, W		; Get swapped STATUS value (swap to avoid changing STATUS)
        movwf	STATUS			; Restore it to STATUS
        swapf	wsave, F		; Swap the stored W value
        swapf	wsave, W		; Restore it to W (swap to avoid changing STATUS)
        bsf     T1CON,TMR1ON    ; Re-enable Timer1 before exiting interrupt handler
        retfie					; Return from the interrupt
    ENDASM
    
    PAUSE 20 'SETTLE INPUTS
    
    
    start:
    low rf
    'ZERO VARIABLES
    ADD1=0
    adda = 0
    addb = 0
    addc = 0
    addd = 0
    address1 = 0
    address2 = 0
    address3 = 0
    address4 = 0
    mydata=0
    chkcrc = 0
    i = 0 
    flag = 0
    latch = %01010000
    mydata = 0
    mydata1 = 0
    mydata2 = 0
    mydata3 = 0
    mydata4 = 0
    porta.1 = 0                                    
    porta.2 = 0
    porta.3 = 0
    portb = 0 
    pause 10
    
    cycle:
    low rf
    repeat
    ADD1=0
    adda = 0
    addb = 0
    addc = 0
    addd = 0
    address1 = 0
    address2 = 0
    address3 = 0
    address4 = 0
    chkcrc = 0
    i = 0 
    mydata = 0
    mydata1 = 0
    mydata2 = 0
    mydata3 = 0
    mydata4 = 0
    
    '******************************************************************************
        ' Fire up Timer1 before entry to serial input routine
        T1CON.0 = 1
        
        ' at 4MHz Timer1 overflows in 65536 * 1uS (~65.5mS) if no Synch byte
        ' and serial data arrive on time. SERIN2 timeout & label options
        ' are useless with a noisy RF receiver output - as noise continually
        ' resets the timeout period causing it to hang forever.
        
        ' Wait for Synch byte, then get new inbound data & checksum
    SERIN2 serpin,16468,[wait(254),address1,address2,address3,address4,_
    mydata1,mydata2,crca,crcb]
        
        T1CON.0 = 0    ' Stop Timer1 once we've received data
        TMR1L = 0      ' Clear low byte
        TMR1H = 0      ' Clear high byte
        
        ' / **** Begin data validation **** /
    From here I am off to decode using my own version so I am not using anything in the orignal example except for the timeout, do you have any suggestions on what to keep, delete or change?
    Last edited by mackrackit; - 3rd August 2010 at 15:32. Reason: Added Code Tags

  4. #4


    Did you find this post helpful? Yes | No

    Default

    By the way, don't laugh to hard, I have been working hard at trying to learn and build. If you did not notice, my serin was origninally:
    serpin VAR porta.0 'serial input pin
    but I commented it out because I thought it has to be:
    SYMBOL serpin = porta.0 ' Encoder data input pin.


    Please correct me if I am wrong.

    Thank you
    Last edited by tazntex; - 3rd August 2010 at 14:43.

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


    Did you find this post helpful? Yes | No

    Default

    No one is laughing. You are trying to learn and that is what we like around here.

    When using SERIN2/SEROUT2
    INCLUDE "MODEDEFS.BAS"
    is not needed. Does not cause any problems if it is there though. It is a hold over from the BS1 days.

    Same goes for "SYMBOL", another BS1 hold over.
    serpin VAR porta.0 'serial input pin
    is fine.

    Unless I am missing something your code looks like it will work for what you want.
    You did what I would do...
    ;clrf GPIO ; Clear outputs on button release (or timeout)
    Dave
    Always wear safety glasses while programming.

  6. #6


    Did you find this post helpful? Yes | No

    Default

    Thank you for checking this for me, I appreciate your help.

  7. #7


    Did you find this post helpful? Yes | No

    Default One more question,...

    I've compiled and ran this but it still hangs meaning the output is still on then a few seconds later goes off. As I noted PORTA needs to be cleared but with the exception of PORTA.0 ( my serin pin). By adding clrf PORTB for a split second they come on and go off immediately while still transmitting. I wonder if if by timing out after receiving is causing PORTB to now act that way. Is this to fast "at 4MHz Timer1 overflows in 65536 * 1uS (~65.5mS)" ? and how could I adjust this?


    @ DEVICE PIC16F628a,XT_OSC
    @ DEVICE pic16F628a, WDT_OFF
    @ DEVICE pic16F628a, PWRT_ON
    @ DEVICE pic16F628a, MCLR_ON
    @ DEVICE pic16F628a, BOD_ON
    @ DEVICE pic16F628a, LVP_OFF
    @ DEVICE pic16F628a, CPD_OFF
    @ DEVICE pic16F628a, PROTECT_OFF

    DEFINE OSC 4
    DEFINE NO_CLRWDT 1 ' Watchdog timer is disabled, so we don't need to reset it
    DEFINE INTHAND RESET_VT ' Interrrupt on Timer1 overflow to reset outputs in 65.5mS
    ' if no serial data received in this time period

    TMR1IF var PIR1.0 ' Timer1 overflow interrupt flag (reset in int handler)
    TMR1IE var PIE1.0 ' Timer1 interrupt enable bit
    CMCON=%00000111
    vrcon=0
    trisa = %00000001
    trisb = %00000000
    PORTA = 0
    PORTB = 0
    serpin VAR porta.0 'serial input pin
    address con %0010000000000001
    add1 VAR WORD
    address1 var byte
    address2 var byte
    address3 var byte
    address4 var byte
    addA var byte '0-7
    addB var byte '8-15
    addC var byte '0-7
    addD var byte '8-15
    chkcrc var byte
    crcA var byte
    crcB var byte
    chksum2 var byte bank0 system
    chksum3 var byte
    i var byte
    mydata var byte
    mydata1 VAR byte
    mydata2 var byte
    mydata3 var byte
    mydata4 var byte
    rf var porta.4

    'Variables for saving state in interrupt handler
    wsave VAR BYTE $70 system ' Saves W
    ssave VAR BYTE bank0 system ' Saves STATUS
    psave VAR BYTE bank0 system ' Saves PCLATH
    fsave VAR BYTE bank0 system ' Saves FSR

    ' Setup Timer1 for resets after ~65.5mS
    T1CON = %00000000 ' Internal clock, 1:1 prescale, Timer1 off for now
    TMR1L = 0
    TMR1H = 0 ' Timer1 low & high bytes cleared
    TMR1IF = 0 ' Clear Timer1 overflow flag before enabling interrupt
    TMR1IE = 1 ' Enable Timer1 overflow interrupt
    INTCON = %11000000 ' Global & peripheral ints enabled
    chksum2 = 0 ' Clear match count
    GOTO cycle ' Jump over int handler

    ASM
    RESET_VT
    movwf wsave ; Save W
    swapf STATUS, W ; Swap STATUS to W (swap avoids changing STATUS)
    clrf STATUS ; Clear STATUS
    movwf ssave ; Save swapped STATUS
    movf PCLATH, W ; Move PCLATH to W
    movwf psave ; Save PCLATH
    movf FSR, W ; Move FSR to W
    movwf fsave ; Save FSR

    ; Do interrupt stuff here
    bcf T1CON,TMR1ON ; Stop Timer1
    clrf TMR1L ; Clear low byte
    clrf TMR1H ; Clear high byte
    bcf PIR1,TMR1IF ; Clear Timer1 interrupt flag bit
    ;clrf GPIO ; Clear outputs on button release (or timeout)
    ;clrf PORTA ; Clear outputs on button release (or timeout)

    From what I understand from the datasheet I can clear selected outputs by:
    bcf PORTA, 2
    bcf PORTA, 3
    bcf PORTB, 0
    bcf PORTB, 1
    bcf PORTB, 2
    bcf PORTB, 3
    bcf PORTB, 4
    bcf PORTB, 5
    bcf PORTB, 6
    bcf PORTB, 7

    ;I REALLY NEED TO CLEAR PORTA WITH THE EXCEPTION OF PORTA.O (SERIN PORT)

    clrf PORTB ; Clear outputs on button release (or timeout)
    clrf chksum2 ; Clear match variable

    ; Restore FSR, PCLATH, STATUS and W registers
    movf fsave, W ; retrieve FSR value
    movwf FSR ; Restore it to FSR
    movf psave, W ; Retrieve PCLATH value
    movwf PCLATH ; Restore it to PCLATH
    swapf ssave, W ; Get swapped STATUS value (swap to avoid changing STATUS)
    movwf STATUS ; Restore it to STATUS
    swapf wsave, F ; Swap the stored W value
    swapf wsave, W ; Restore it to W (swap to avoid changing STATUS)
    bsf T1CON,TMR1ON ; Re-enable Timer1 before exiting interrupt handler
    retfie ; Return from the interrupt
    ENDASM

    PAUSE 1000 'SETTLE INPUTS


    start:
    high rf
    'ZERO VARIABLES
    ADD1=0
    adda = 0
    addb = 0
    addc = 0
    addd = 0
    address1 = 0
    address2 = 0
    address3 = 0
    address4 = 0
    mydata=0
    chkcrc = 0
    i = 0
    mydata = 0
    mydata1 = 0
    mydata2 = 0
    mydata3 = 0
    mydata4 = 0
    porta.1 = 0
    porta.2 = 0
    porta.3 = 0
    portb = 0
    pause 50

    cycle:
    high rf
    repeat
    ADD1=0
    adda = 0
    addb = 0
    addc = 0
    addd = 0
    address1 = 0
    address2 = 0
    address3 = 0
    address4 = 0
    chkcrc = 0
    i = 0
    mydata = 0
    mydata1 = 0
    mydata2 = 0
    mydata3 = 0
    mydata4 = 0

    '************************************************* *****************************
    ' Fire up Timer1 before entry to serial input routine
    T1CON.0 = 1

    ' at 4MHz Timer1 overflows in 65536 * 1uS (~65.5mS) if no Synch byte
    ' and serial data arrive on time. SERIN2 timeout & label options
    ' are useless with a noisy RF receiver output - as noise continually
    ' resets the timeout period causing it to hang forever.

    ' Wait for Synch byte, then get new inbound data & checksum
    SERIN2 serpin,16468,[wait(254),address1,address2,address3,address4,_
    mydata1,mydata2,crca,crcb]

    T1CON.0 = 0 ' Stop Timer1 once we've received data
    TMR1L = 0 ' Clear low byte
    TMR1H = 0 ' Clear high byte

    ' / **** Begin data validation **** /

    As I mentioned earlier after making changes the output will quickly come on and go off.
    Thanks
    Last edited by tazntex; - 3rd August 2010 at 20:12.

  8. #8


    Did you find this post helpful? Yes | No

    Default

    Looking at the datasheet I just changed the timer from Internal to External :
    from this:
    ' Setup Timer1 for resets after ~65.5mS
    T1CON = %00000000'%00000000Internal clock, 1:1 prescale, Timer1 off for now

    to this:
    ' Setup Timer1 for resets after ~65.5mS
    T1CON = %00000010'%00000000Internal clock, 1:1 prescale, Timer1 off for now

    Now it works and holds the output on while transmitting but when I release the button most of the time there is a delay of several seconds before the output goes off.

    Am I on the right track?

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


    Did you find this post helpful? Yes | No

    Default

    I did not think about the external OSC change, good catch.

    For some reason I was thinking that you did not want to clear anything in the ASM routine...
    I do not see what it would hurt to clear all of PORTA in the ASM, "clrf PORTA". When the code gets to SERIN2 it will still input.

    Now it works and holds the output on while transmitting but when I release the button most of the time there is a delay of several seconds before the output goes off.
    That I am not sure about. Maybe there is something in the TX code causing things to drag?

    Maybe post both the TX and RX and someone will be able to spot the problem.
    Dave
    Always wear safety glasses while programming.

  10. #10


    Did you find this post helpful? Yes | No

    Default Timer???

    Am Using the correct timer, I am using an external 4mHz oscilliator on RA6 and RA7, I've just noticed that on the original code Bruce has the PIC is a 12F635. T1CON: TIMER1 CONTROL REGISTER bit 7 is set to 0 and that Timer counts when the gate is low, I don't have a gate, so how am I counting?


    By the way I am using a 16F628A.
    Thanks

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


    Did you find this post helpful? Yes | No

    Default

    Neither on is counting.
    On the 635, T1CON bit 6 is 0 in Bruce's code. Bit 6 being 0 just turns on the timer, no gate.
    Dave
    Always wear safety glasses while programming.

  12. #12


    Did you find this post helpful? Yes | No

    Default

    Thanks for the reply, So as it is in the previous post it seems to work, but I just was looking over the datasheets of both the PIC's this past weekend. I was just curious how it works, I am still struggling to understand the timers. I shall keep reading because it is not clear to me what makes it "tick" to jump out and continue.


    Thanks again for the many replies.
    '73

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


    Did you find this post helpful? Yes | No

    Default

    I shall keep reading because it is not clear to me what makes it "tick" to jump out and continue.
    Yup, they can be confusing at times. You may have this part figured out but we will start someplace.

    Once the timer is setup it will run in the background as if there is a second chip. This second process can be looked at as an alarm clock. When the alarm goes off, timer overflows, it will interrupt the main process.

    At this time we turn the alarm off, disable the timer, and do whatever... get up go about our day....

    Then the alarm is set again...
    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