Initializing a timer

General discussion on mikroC PRO for PIC32.
Post Reply
Author
Message
reip
Posts: 3
Joined: 30 Sep 2020 16:08

Initializing a timer

#1 Post by reip » 20 Oct 2020 13:42

Hello!

I am trying to use the timer 1 of a PIC32MX250F128B MCU so that it would rise an interrupt flag after every 2 seconds. I'm using the Fast RC Osc with PLL which is working at 8 MHz, and Pb_Clk = Sys_Clk.

So I examined some examples here and some tutorials and created such code which compiles without any errors:

Code: Select all

void Timer1Interrupt() iv IVT_TIMER_1 ilevel 7 ics ICS_SOFT {
  T1IF_bit = 0;
  LATB.B2 = ~ PORTB.B2;
}

void main() {
  JTAGEN_bit = 0;
  ANSELA = 0;
  ANSELB = 0;
  TRISA = 0;
  TRISB = 0;
  LATA = 0;
  LATB = 0;
  
  SYSKEY = 0xAA996655;      //unlock clock
  SYSKEY = 0x556699AA;

  T1CON = 0;       //clear the control register
  TMR1 = 0;        //set timer 1 to zero
  TCKPS0_bit = 1;           // Set Timer Input Clock
  TCKPS1_bit = 1;           // Prescale value to 1:256
  
  T1IP0_bit = 1;        //set interrupt priority level to 7
  T1IP1_bit = 1;
  T1IP2_bit = 1;
  
  T1IE_bit = 1;       //enable the timer 1 interrupt
  T1IF_bit = 0;       //clear the interrupt flag of timer 1
  
  PR1 = 62500;     //MCU 8 MHz -> cycle = 125 ns; timer tick = 256 * 125 ns = 0.032 ms; 2 s/0.032 ms = 62500

  ON__T1CON_bit = 1;        // Enable timer 1
  EnableInterrupts();
}
But the MCU is not working, the targeted pin B2 is not switched after every 2 seconds (it is not switched at all, MCU is not doing anything visible). Does somebody know what is wrong or missing? Btw, even if the MCU clock frequency is wrong and is actually not 8 MHz, the timer should still work, just with some other interval?

I added the project folder, too. Thanks for any help!
Attachments
TimerTry1.zip
(127.97 KiB) Downloaded 44 times

Post Reply

Return to “mikroC PRO for PIC32 General”