Problem with interrupt handle timer1

Beta Testing discussion on mikroBasic PRO for dsPIC30/33 and PIC24.
Post Reply
Author
Message
5willow5
Posts: 27
Joined: 19 Sep 2008 08:14

Problem with interrupt handle timer1

#1 Post by 5willow5 » 08 Oct 2010 10:58

Hello.

I have a problem with my code. I need to configure the timer 1 of my pic (PIC24F16KA102) to make a interruption each second, but I can't achieve it.

This is my example code

Code: Select all


program Temporizador

' Declarations section

dim willow as byte

sub procedure Timer1Int()  iv IVT_ADDR_T1INTERRUPT
PR1         = 0xF0C1
  LATA.1 = not LATA.1          ' Invert PORTB
  Inc(willow)
  if (willow mod 10) = 10 then
     LATA.0 = not LATA.0
  end if
  T1IF_bit = 0                  ' Clear T1IF

end sub

main:
'   Main program 
  AD1PCFG = 0xFFFF
willow = 0
  TRISA = 0            ' Initialize PORTA as output
  LATA.0 = 0             ' Set PORTA to zero
  LATA.1 = 0             ' Set PORTA to zero


  IPC0   = IPC0 or 0x1000   ' Interrupt priority level = 1
TON_bit = 1
TCKPS_1_bit = 0
TCKPS_0_bit = 0
TCS_bit     = 0
TGATE_bit   = 0
TSYNC_bit   = 0
'PR1         = 0xFF80
PR1         = 0xF0C1
T1IE_bit    = 1
T1IP_2_bit  = 1
T1IP_1_bit  = 1
T1IP_0_bit  = 1

LATB.14 = 1
end.
This code is very simple, but I can't achieve to handle the interrupt each second. I have tried to change the value of PIR1, but always the ouput RA.1 changes its value each 38.2 ms, and I don't know why.

Can somebody help me?

Thanks for all.

Willow

GIGI
Posts: 1
Joined: 13 May 2010 15:04

Re: Problem with interrupt handle timer1

#2 Post by GIGI » 17 Oct 2010 16:50

I had the same problem with the dsPIC30F4013, there must be some bug
I solved this way:
I used a 4MHz quartz with x16 PLL (64 MHz clock) and I managed to get to within four hundredths of a second on 38 minutes

sub procedure Timer1_Inizia()
Sec = 0
Dsec = 0
Csec = 0
IPC0 = IPC0 or $1000 'priority level 1
IEC0 = IEC0 or $0008 'Timer1 interrupt abilitato
PR1 = 20000 'Interrupt periodo cicli clock (1,28 microsec ? )
T1CON = $8010 '1:8 'Timer1 abilitato
end sub
Sub procedure Timer1_Stop()
T1CON = 0
end sub

Note with a quartz 10MHz and PLL x8 with a PR1 8025 is there is a reasonable accuracy,but not for long time

samic
Posts: 6
Joined: 27 Sep 2009 17:46

Re: Problem with interrupt handle timer1

#3 Post by samic » 27 Oct 2010 18:48

Hi, I don't know this PIC but I think that the problem is on set the prescaler value

Code: Select all

TCKPS_1_bit = 0
TCKPS_0_bit = 0
It's equal at a prescaler of 1:1 and with

Code: Select all

TCS_bit = 0
you are using the internal clock.

For example with 10Mhz of Quartz you have a period of 4/10*10^6 = 0.4us and if the timer went on overflow every FFFF (counting between 0000 and FFFF) the resulting time is more or less 26ms (65535*0.4us).

I hope it's right.
Bye

Post Reply

Return to “mikroBasic PRO for dsPIC30/33 and PIC24 Beta Testing”