Problem with Timer initialisation with 18F4685

General discussion on mikroBasic PRO for PIC.
Post Reply
Author
Message
Kalain
Posts: 1093
Joined: 11 Mar 2005 18:26
Location: Aubenas, France

Problem with Timer initialisation with 18F4685

#1 Post by Kalain » 19 Apr 2009 19:57

Hi,

Code below works correctly (EP3 / 18F4685@8MHz) :

Code: Select all

program test_tmr0

sub procedure interrupt
  if portb.0 = 1 then
    portb.0 = 0
    TMR0H = $00
    TMR0L = $10
  else
    portb.0 = 1
    TMR0H = $F0
    TMR0L = $00
  end if
  INTCON.TMR0IF = 0       ' clear T0IF
end sub

main:
  ADCON1 = $0F          ' set AN pins to Digital I/O
  T0CON  = %10001000     ' TMR0 in 16 bit, no prescaler to TMR0
  INTCON = $A0          ' enable TMRO interrupt
  CMCON = 7             'Comparator OFF
  TMR0L = $FE            ' timer0 initial value
  TMR0H = $FF
  
  TRISB = 0              ' PORTB is output
  PORTB = 0              ' initialize PORTB

  while TRUE
  nop
  nop
  wend

end.
But now, if I swap the lines which initialise TMR0L and TMR0H like below.

Code: Select all

    TMR0L = $10
    TMR0H = $00

and

    TMR0L = $00
    TMR0H = $F0
Impulsion duration on portb0 is not the same ! ! !

This is may be a PIC limitation or specification ?

For 18F4685, TMR0L and TMR0H are located respectively at : FD6h / FD7h.
Now, rather than to initialise TMR0L/TMR0H separatly, I'd like to initialise Timer0 in one time by declaring an absolute variable.

But code below doesn't work either, :

Code: Select all

program test_tmr0
dim timer0 as word absolute $FD6

sub procedure interrupt
  if portb.0 = 1 then
    portb.0 = 0
    timer0 = $0010
  else
    portb.0 = 1
    timer0 = $F000
  end if
  INTCON.TMR0IF = 0       ' clear T0IF
end sub

main:
  ADCON1 = $0F          ' set AN pins to Digital I/O
  T0CON  = %10001000     ' TMR0 in 16 bit, no prescaler to TMR0
  INTCON = $A0          ' enable TMRO interrupt
  CMCON = 7             'Comparator OFF
  TMR0L = $FE            ' timer0 initial value
  TMR0H = $FF
  
  TRISB = 0              ' PORTB is output
  PORTB = 0              ' initialize PORTB

  while TRUE
  nop
  nop
  wend

end.
NB : All these examples are well simulated in Debugger mode ! ! !

Any ideas ?
Last edited by Kalain on 20 Apr 2009 12:21, edited 1 time in total.
Alain

yo2lio
Posts: 1878
Joined: 19 Sep 2006 12:57
Location: Romania, Arad City
Contact:

Re: Problem with Timer initialisation

#2 Post by yo2lio » 19 Apr 2009 21:38

Kalain wrote:Any ideas ?
Yes, first read datasheet for TMR0 16 bit mode.

TMR0H is updated when you write to TMR0L.

So, this code is correct :

Code: Select all

    TMR0H = $00
    TMR0L = $10 
This code is not correct :

Code: Select all

    TMR0L = $10
    TMR0H = $00 
Also this code is not correct :

Code: Select all

   		timer0 = $0010

0x0022	0x0E10      	MOVLW       16
0x0024	0x6ED6      	MOVWF       TMR0L 
0x0026	0x0E00      	MOVLW       0
0x0028	0x6ED7      	MOVWF       TMR0H 
This works only in 8 bit mode.
Best regards, Florin Andrei Medrea.

http://www.microelemente.ro/
http://www.microelemente.ro/produse-si-servicii/
http://www.microelemente.ro/custom-software/

mail : florin@microelemente.ro

Kalain
Posts: 1093
Joined: 11 Mar 2005 18:26
Location: Aubenas, France

Re: Problem with Timer initialisation

#3 Post by Kalain » 20 Apr 2009 17:55

yo2lio wrote:
Kalain wrote:Any ideas ?
Yes, first read datasheet for TMR0 16 bit mode.
TMR0H is updated when you write to TMR0L.
Yes you're right.
Chapter 11.2 page 148 on DS39671B Datasheet Microchip.

I know now why this code is working and also why it doesn't work.

Thanks for your explanation.
Alain

Post Reply

Return to “mikroBasic PRO for PIC General”