TMR0 Frequency problem

General discussion on mikroPascal.
Post Reply
Author
Message
krikitu
Posts: 66
Joined: 03 Aug 2008 15:40

TMR0 Frequency problem

#1 Post by krikitu » 08 Aug 2008 13:26

I was testing use of timers and all is working well for TMR1 and TMR2, but not for TMR0 :(

In this example, I've set TMR1 to 10Hz, Timer2 to 100Hz and Timer0 to 1000Hz.

Simulation on Proteus gives correct frequencies for Timer1&2 but always give something like 7.220 Hz for Timer0, regardless of what I put in the prescaler :(


What can be wrong in this code ? PortD pins are connected to a kind of frequency-meter.

Code: Select all

program Interrupt_Test;

var
   Cnt1, Cnt2 : Integer;

procedure interrupt;
begin
  If (INTCON.TMR0IF) then    // Interrupt from Timer0
    Begin
      INTCON.TMR0IF := 0;
      INTCON.TMR0IE := 1;
      TMR0 := 6;
      SetBit(PortD,3);
    End;
  If (PIR1.TMR1IF) then       // Interrupt from Timer1
    Begin
      Inc(cnt1);
      PIR1.TMR1IF := 0;
      TMR1H := $3C;
      TMR1L := $B0;
      SetBit(PortD,1);
    End;
  If (PIR1.TMR2IF) then    // Interrupt from Timer2
    Begin
      Inc(cnt2);
      PR2 := 250;
      PIR1.TMR2IF := 0;
      SetBit(PortD,2);
    End;
  ClearBit(PortD,2);
  ClearBit(PortD,3);
  ClearBit(PortD,1);
end;


begin

// Timer0 Registers: Prescaler=1:8; TMR0 Preset=6; Freq=1 000,00Hz; Period=1 000,00 µs
OPTION_REG.T0CS := 0;  // bit 5 TMR0 Clock Source Select bit:0=Internal Clock (CLKO) / 1=Transition on T0CKI pin
OPTION_REG.T0SE := 0;  // bit 4 TMR0 Source Edge Select bit: 0=low/high / 1=high/low
OPTION_REG.PSA  := 1;  // bit 3 Prescaler Assignment bit: 0=Prescaler is assigned to the WDT
OPTION_REG.PS2  := 0;  // bits 2-0  PS2:PS0: Prescaler Rate Select bits
OPTION_REG.PS1  := 1;
OPTION_REG.PS0  := 0;
TMR0 := 6;             // preset for timer register

// Timer1 Registers: Prescaler=1:8; TMR1 Preset=3036; Freq=10,00Hz; Period=100,00 ms
T1CON.T1CKPS1 := 1;  // bits 5-4  Prescaler Rate Select bits
T1CON.T1CKPS0 := 0;  // bit 4
T1CON.T1OSCEN := 1;  // bit 3 Timer1 Oscillator Enable Control: bit 1=on
T1CON.T1SYNC  := 1;  // bit 2 Timer1 External Clock Input Synchronization Control bit:1=Do not synchronize external clock input
T1CON.TMR1CS  := 0;  // bit 1 Timer1 Clock Source Select bit:0=Internal clock (FOSC/4) / 1 = External clock from pin T1CKI (on the rising edge)
T1CON.TMR1ON  := 1;  // bit 0 enables timer
TMR1H := $3C;         // preset for timer1 MSB register
TMR1L := $B0;         // preset for timer1 LSB register

// Timer2 Registers: Prescaler=1:16; TMR2 PostScaler=1:5; PR2=250 - Freq = 100,00Hz - Period = 10,00 ms
T2CON := %00100110;
PR2 := 250;            // PR2 (Timer2 Match value)


PIE1.TMR1IE  := 1;           // enable Timer1 interrupt
PIE1.TMR2IE  := 1;           // enable Timer2 interrupt
INTCON.TMR0IE := 1;              // enable TMRO interrupt

  ADCON0 := %00000011;
  TrisA := %00000001;
  TrisB := %00000001;
  PortB := 0;
  TRISD := 0;
  PORTD := %00000000;
  Cnt1 := 0;
  Cnt2 := 0;

  INTCON.GIE := 1;
  INTCON.PEIE := 1;

  SetBit(PortD,7);
  while TRUE do
    begin
    end;

End.

Schematic :

Image


Thanks in advance for any help.

krikitu
Posts: 66
Joined: 03 Aug 2008 15:40

#2 Post by krikitu » 08 Aug 2008 13:58

While re-reading the 16F877 datasheet, I've found that OPTION_REG.PSA was not set as it should :(

The datasheet states
bit 3 PSA: Prescaler Assignment bit
1 = Prescaler is assigned to the WDT
0 = Prescaler is assigned to the Timer0 module
Which is (was) not the case in my code...

I have used the Pic Timer program to help for calculating correct prescaler values but this gives me this error : it sets incorrectly the PSA bit. I will contact the author to modify this behaviour.

Kia
Posts: 4
Joined: 16 May 2007 14:18
Location: Spain, Alicante

#3 Post by Kia » 26 Sep 2008 22:39

@krikitu,
I have used the Pic Timer program to help for calculating correct prescaler values but this gives me this error : it sets incorrectly the PSA bit. I will contact the author to modify this behaviour.
the bug in the programm is fixed, hope it will give now the right values.
Greetings
EasyPIC4 & microBasic Owner

Post Reply

Return to “mikroPascal General”