cmicrocontrollerpicpic18xc8

Timer 0 interruption on PIC18F


I am trying to make a program, that use a interruption for timer 0. The problem is I have to add a function with 2 variables. Timer configuration will be performed by defining a function with the following prototype: void int_tmr0 (int conf_int, int conf_T0), which I did it in that form:

void conf(int p1, int p2)
    {
        T0CON=p1;
        INTCON=p2;
    }

I try to put the records: T0CON, INTCON in these 2 variables: p1 and p2. I am not sure if I can call these 2 variables in the main function by:

    void main()
    {
        WDTCONbits.ADSHR = 1;
        MEMCONbits.EBDIS = 1;
        TRISD = 0x0;
        INTCONbits.GIE = 1;
        p1=0b10001000;
        INTCONbits.TMR0IE = 0;
    }

Here is the whole code:

#include <xc.h>

unsigned char counter;

void interrupt f1()
{
    if(INTCONbits.TMR0IE && INTCONbits.TMR0IF)
    {
        counter++;
        INTCONbits.TMROIF=0     
    }

void conf(int p1, int p2)
    {
        T0CON=p1;
        INTCON=p2;
    }

    void main()
    {
        WDTCONbits.ADSHR = 1;
        MEMCONbits.EBDIS = 1;
        TRISD = 0x0;
        INTCONbits.GIE = 1;
        p1=0b10001000;
        INTCONbits.TMR0IE = 0;
    }
    while(1){
        LATD= counter;
    }
}

Solution

  • If I understood it right,

    A solution would be to create the function as the SDK requires and store the parameters in global variables. Then, you can call your function from inside the 'callback' function using these variables as parameters:

    int volatile param_1 = 0, param_2 = 0;
    
    void int_tmr0(int conf_int, int conf_T0)
    {
      conf(param_1, param_2);
    }
    
    void conf(int p1, int p2)
    {
      T0CON=p1;
      INTCON=p2;
    }
    
    int main()
    {
      WDTCONbits.ADSHR = 1;
      MEMCONbits.EBDIS = 1;
      TRISD = 0x0;
      INTCONbits.GIE = 1;
      param_1=0b10001000;
      INTCONbits.TMR0IE = 0;
    
      while(1){
          LATD= counter;
      }
    }