Serin2 and Timer1


Closed Thread
Results 1 to 11 of 11

Hybrid View

  1. #1

    Default Serin2 and Timer1

    I am using a 12F683 with a Serin2 routine and timeout which works fine.

    I am now introducing a timer into my code to count 250ms. The code is base on Melanie's olympic timer stuff and other bits cribbed from here.

    Since introducing the timer code the program just does not work and seems to crash at the serin2 routine.

    Does serin2 use timer1?

  2. #2
    Join Date
    Nov 2005
    Location
    Perth, Australia
    Posts
    429


    Did you find this post helpful? Yes | No

    Default

    are you using interrupts for that timer?

    Because an interrupt will interrupt serin2 in the middle of whatever it is doing at the time and you will lose data.
    "I think fish is nice, but then I think that rain is wet, so who am I to judge?" - Douglas Adams

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


    Did you find this post helpful? Yes | No

    Default

    Try
    DISABLE
    before the SERIN2 command
    and
    ENABLE
    after the SERIN2 command
    Dave
    Always wear safety glasses while programming.

  4. #4


    Did you find this post helpful? Yes | No

    Default

    I'm not using an interrupt as such I'm polling the Timer1 flag.

    Some code extracts here below

    Code:
    Initialise:								'Initialise 250ms Timer 8mhz clock
    
        	PreLoad = 3035                      				'Preload Constant Loads Timer with 3035 to get 250ms ticks               
    	PIE1.0 = 1 							'Enable TMR1 Interrupts (Not sure about this)
    	TMR1L = PreLoad.LowByte  					'Load the Timer with preload value
    	TMR1H = PreLoad.HighByte 					'Load the Timer with preload value
    	TMR1IF = 0                					'Clear TMR1 int flag
    My Serin2 command is here in the code.

    Code:
    IF TMR1IF = 0 THEN TimerJump					'If 250ms has not elapsed then jump over below section
    
    	T1CON.0=0 							'Stop the Clock
    
    	TMR1RunOn.Highbyte = TMR1H 					'Load the Run-On (Over-Run) value (if any)
    	TMR1RunOn.Lowbyte = TMR1L					'Load the Run-On (Over-Run) value (if any)
    	TMR1RunOn = PreLoad + TMR1RunOn					'Calculate new Preload setting to include any timer overrun	
    	TMR1H = TMR1RunOn.HighByte					'Load the timer with new value
    	TMR1L = TMR1RunOn.LowByte   					'Load the timer with new value
    
    	T1CON.0=1 							'Restart the Clock
        
        	TMR1IF = 0                					'Clear TMR1 int flag 
    	'PIR1.0=0 							'Reset TMR1's Interupt Flag (Not sure about this)
    So you can see i intialise the timer for 250ms ticks. Then poll the flag. If it is set then I execute some code and then reinitialise the timer adding any overrun to the preload value.

  5. #5


    Did you find this post helpful? Yes | No

    Default Re: Serin2 and Timer1

    I'm working on this and a bit confused by the 12F683 data sheet.

    I don't want an interrupt (i.e. jump to some assembly level routine) I'm just polling the TMR1 overflow flag.

    Code:
    if PIR1.0 = 1 then T1Handler   'If Timer1 has overflowed then
    So do i need to do the stuff below?

    The Timer1 register pair (TMR1H:TMR1L) increments
    to FFFFh and rolls over to 0000h. When Timer1 rolls
    over, the Timer1 interrupt flag bit of the PIR1 register is
    set. To enable the interrupt on rollover, you must set
    these bits:
    • Timer1 interrupt enable bit of the PIE1 register
    • PEIE bit of the INTCON register
    • GIE bit of the INTCON register
    The interrupt is cleared by clearing the TMR1IF bit in

    the Interrupt Service Routine.
    I just want to clear the TMR1 overflow flag every time it overflows, load my 250ms preload value etc and carry on.

  6. #6
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,604


    Did you find this post helpful? Yes | No

    Default Re: Serin2 and Timer1

    > I just want to clear the TMR1 overflow flag every time it overflows, load my 250ms preload value etc and carry on.
    Then that's all you need to do. The roll-over event will set the interrupt flag even if the actual interrupt enable bit and/or GIE bit isn't set. Just clear it, reload and you're good to go.

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