Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] [moved] facing problems to implement a timer interrupt in P33FJ12GP202

Status
Not open for further replies.

ahmad2005

Member level 1
Joined
Mar 30, 2011
Messages
40
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,288
Location
Mirpur,Dhaka,Bangladesh
Activity points
1,618
I have tried several ways to implement a timer interrupt in P33FJ12GP202 using mikroC pro for dsPIC. Unfortunately, non of the configurations has been worked. Can someone let me know, where did I make mistake.

1st one: This one was implemented by reading application notes
Code:
//void Timer1Int() org IVT_ADDR_T1INTERRUPT { // Timer1 interrupt handler
void Interrupt() {
if(T1IF == 1) {
 if (DONE_bit){                          // If ADC is not busy
    SAMP_bit  = 1;                       // Start new sample
  }
  PORTA.b1 ^= 1; // ~ PORTA.b1;                  // You can put oscilloscope on PORTD
                                         //  to measure sampling frequency
  T1IF_bit    = 0;
  }
}

void main() {
  TRISB = 0;
  TRISA.b1=0;


  // Interrupts setup
  IFS0    = 0;                           // Clear interrupt flags
  IFS1    = 0;                           // Clear interrupt flags
 // IFS2    = 0;                           // Clear interrupt flags
  NSTDIS_bit = 1;                        // Nested interrupts DISABLED
  INTCON2    = 0;                        // Other interrupt settings
  T1IE_bit   = 1;                        // Timer1 and
  AD1IE_bit   = 1;                        // ADC interrupts ENABLED
  T1IP0_bit  = 1;                        // Timer1 interrupt priority level = 1
  //ADIP1_bit  = 1;                        // ADC interrupt priority level = 1
 

 //TON_bit    = 1;                        // Start Timer1, internal clock FCY, prescaler 1:1
 TCKPS_1_bit =0;
  TCKPS_0_bit = 0;
  TGATE_bit = 0;
  TSYNC_bit = 0;
  TCS_bit = 0;
  PR1        = 0x03E8;                   // Sampling ~= 80 kHz. The value of 250 is calculated for 80MHz clock.


  


  while (1);                             // Infinite loop,
                                         //   wait for interrupts
}


2nd on: implemented by using MikroC Timer Calculator tool. https://libstock.mikroe.com/projects/view/398/timer-calculator

Code:
//Timer1
//Prescaler 1:64; PR1 Preload = 31250; Actual Interrupt Time = 100 ms

//Place/Copy this part in declaration section
void InitTimer1(){
  T1CON         = 0x8010;
  T1IE_bit         = 1;
  T1IF_bit         = 0;
  IPC0                 = IPC0 | 0x1000;
  PR1                 = 62500;
  TON_bit    = 1;
}

void main() {
  TRISB = 0;
  TRISA=0;
  PORTA=0;
   // Interrupts setup
  IFS0    = 0;                           // Clear interrupt flags
  IFS1    = 0;                           // Clear interrupt flags
 // IFS2    = 0;                           // Clear interrupt flags
  NSTDIS_bit = 1;                        // Nested interrupts DISABLED
  INTCON2    = 0;                        // Other interrupt settings
  T1IE_bit   = 1;                        // Timer1 and
  AD1IE_bit   = 1;                        // ADC interrupts ENABLED
  T1IP0_bit  = 1;                        // Timer1 interrupt priority level = 1
  //ADIP1_bit  = 1;                        // ADC interrupt priority level = 1

InitTimer1();

}

void Timer1Interrupt() iv IVT_ADDR_T1INTERRUPT{
  T1IF_bit         = 0;
  PORTA.b1 ^= 1; // ~ PORTA.b1;
  //Enter your code here
}

I'm using protues for simulation.
 

Re: facing problems to implement a timer interrupt in P33FJ12GP202

Hi,

Unfortunately, non of the configurations has been worked.
And what exactly did not work?

-->
What did you expect?
How did you verify it?
What was the result?

Klaus
 

Re: facing problems to implement a timer interrupt in P33FJ12GP202

I expected PIN A1 of PORTA would be toggled by each timer interrupt. I'm going to upload my project here.

- - - Updated - - -

Moderator, can you please move this post from Power Electronics section to Microcontroller section. This might be placed here mistakenly.
 

Attachments

  • Code_proteus.zip
    82.6 KB · Views: 123

This is how I solved the problem.
You can watch the solving process here:

The problem was in Interrupt Service Routine function. In the if-statement, I wrote "T1IF == 1" but it should have written with its full abbreviation like "IFS0bits.T1IF == 1". Rest of the things were almost okay.

Code:
void Timer1Interrupt() iv IVT_ADDR_T1INTERRUPT {
if(IFS0bits.T1IF == 1) {
  PORTA.b4 ^= 1; // ~ PORTA.b4;                  // You can put oscilloscope on PORTA.b4
                                                //  to measure sampling frequency
  IFS0bits.T1IF = 0;
  }
}

void main() {
  TRISA = 0;
  PORTA = 0;
  TRISB = 0;
  PORTB = 0;


  // Interrupts setup
  IFS0    = 0;                           // Clear interrupt flags
  IFS1    = 0;                           // Clear interrupt flags
 // IFS2    = 0;                           // Clear interrupt flags
  NSTDIS_bit = 1;                        // Nested interrupts DISABLED
  INTCON2    = 0;                        // Other interrupt settings
  T1IE_bit   = 1;                        // Timer1 and
  AD1IE_bit   = 1;                        // ADC interrupts ENABLED
  T1IP0_bit  = 1;                        // Timer1 interrupt priority level = 1
  //ADIP1_bit  = 1;                        // ADC interrupt priority level = 1


  
    T1CONbits.TON = 0; // Disable Timer
    T1CONbits.TCS = 0; // Select internal instruction cycle clock
    T1CONbits.TGATE = 0; // Disable Gated Timer mode
    T1CONbits.TCKPS = 0b00; // Select 1:1 Prescaler
    TMR1 = 0x0000; // Clear timer register
    PR1 = 5000; // Load the period value  //
    IPC0bits.T1IP = 0x01; // Set Timer1 Interrupt Priority Level
    IFS0bits.T1IF = 0; // Clear Timer1 Interrupt Flag
    IEC0bits.T1IE = 1; // Enable Timer1 interrupt
    T1CONbits.TON = 1; // Start Timer





  while (1) {
  PORTB = TMR1;
  }
}
 

You could have written

Code:
if(T1IF_bit) {

}
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top