Timer and ADC Interrupt

Hi everybody,

I am just a beginner in interrupts. My goal is to start the timer (16 bit cascaded timer - TMR0 and TMR1) which after a compare match enables the 12 bit ADC using S12ADIO interrupt. My code is here,

#include <rxduino.h>
#include <iodefine_gcc63n.h>
#include <intvect.h>

void Excep_S12AD0_S12ADI0(void) __INTTERUPT_FUNC ;


void init_timer();
void init_adc12();

unsigned int adc;
int s12=0;
int l=0;

main()
{
// Serial.begin(9600);
pinMode(PIN_LED0,OUTPUT);
pinMode(PIN_LED3,OUTPUT);
// delay(7000);
init_timer();
init_adc12();
digitalWrite(PIN_LED0,1);
while(1)
{
while(s12==1)
{
l=!l;
adc = (S12AD.ADDR0 & 0x0fff);
// Serial.println(adc);
digitalWrite(PIN_LED0,l);
s12=0;
}
}

}


void init_timer()
{
MSTP(TMR0) = 0;
TMR0.TCCR.BIT.CSS = 0b11;
TMR0.TCR.BIT.CCLR = 0b01;
TMR01.TCORA = 60000;
TMR1.TCCR.BYTE = 0b01101;
TMR0.TCSR.BIT.ADTE=1;
}

void init_adc12()
{
MSTP (S12AD) = 0;
S12AD.ADCSR.BYTE=0b01011110;
S12AD.ADANS0.WORD=0x0001;
S12AD.ADCER.BIT.ACE=1;
S12AD.ADCER.BIT.ADRFMT=0;
S12AD.ADSTRGR.BIT.ADSTRS=0b00001001;
IEN(S12AD0,S12ADI0)=1;
IPR(S12AD0,S12ADI0)=2;
}

void Excep_S12AD0_S12ADI0(void)
{
s12=1;
}

i don't know what is wrong here.... could anybody help me out..?

Parents
  • Well.. i found the error......  It lies in the ADC initialization.

    The ADC should function in single scan mode, but i initialized in continuous scan mode. (Since the adc scans only once, each time the compare match occurs).

    Just change the ADCSR byte to S12AD.ADCSR.BYTE=0b00011110..

    Also a little change in Timer initialisation..

    I forgot a byte is composed of 8 bits - TMR1.TCCR.BYTE = 0b01101;

    change this to TMR1.TCCR.BYTE = 0b00001101;

    that's it. problem solved.. :)

    #include <rxduino.h>

    #include <iodefine_gcc63n.h>

    #include <intvect.h>

    void Excep_S12AD0_S12ADI0(void) __INTTERUPT_FUNC ;

    void init_timer();

    void init_adc12();

    unsigned int adc;

    int s12=0;

    int l=0;

    main()

    {

    Serial.begin(9600);

    pinMode(PIN_LED0,OUTPUT);

    pinMode(PIN_LED3,OUTPUT);

    delay(7000);

    init_adc12();

    init_timer();

    digitalWrite(PIN_LED0,1);

    while(1)

    {

    if(s12==1)

    {

    l=!l;

    adc = (S12AD.ADDR0 & 0x0fff);

    Serial.println(adc);

    digitalWrite(PIN_LED0,l);

    s12=0;

    }

    }

    }

    void init_timer()

    {

    MSTP(TMR0) = 0;

    TMR0.TCCR.BIT.CSS = 0b11;  

    TMR0.TCR.BIT.CCLR = 0b01;

    TMR01.TCORA = 60000;

    TMR1.TCCR.BYTE = 0b00001101;

    TMR0.TCSR.BIT.ADTE=1;

    }

    void init_adc12()

    {

    MSTP (S12AD) = 0;

    S12AD.ADCSR.BYTE=0b00011110;

    S12AD.ADANS0.WORD=0x0001;

    S12AD.ADCER.BIT.ACE=1;

    S12AD.ADCER.BIT.ADRFMT=0;

    S12AD.ADSTRGR.BIT.ADSTRS=0b00001001;

    IEN(S12AD0,S12ADI0)=1;

    IPR(S12AD0,S12ADI0)=2;

    }

    void Excep_S12AD0_S12ADI0(void)

    {

    s12=1;

    }

Reply
  • Well.. i found the error......  It lies in the ADC initialization.

    The ADC should function in single scan mode, but i initialized in continuous scan mode. (Since the adc scans only once, each time the compare match occurs).

    Just change the ADCSR byte to S12AD.ADCSR.BYTE=0b00011110..

    Also a little change in Timer initialisation..

    I forgot a byte is composed of 8 bits - TMR1.TCCR.BYTE = 0b01101;

    change this to TMR1.TCCR.BYTE = 0b00001101;

    that's it. problem solved.. :)

    #include <rxduino.h>

    #include <iodefine_gcc63n.h>

    #include <intvect.h>

    void Excep_S12AD0_S12ADI0(void) __INTTERUPT_FUNC ;

    void init_timer();

    void init_adc12();

    unsigned int adc;

    int s12=0;

    int l=0;

    main()

    {

    Serial.begin(9600);

    pinMode(PIN_LED0,OUTPUT);

    pinMode(PIN_LED3,OUTPUT);

    delay(7000);

    init_adc12();

    init_timer();

    digitalWrite(PIN_LED0,1);

    while(1)

    {

    if(s12==1)

    {

    l=!l;

    adc = (S12AD.ADDR0 & 0x0fff);

    Serial.println(adc);

    digitalWrite(PIN_LED0,l);

    s12=0;

    }

    }

    }

    void init_timer()

    {

    MSTP(TMR0) = 0;

    TMR0.TCCR.BIT.CSS = 0b11;  

    TMR0.TCR.BIT.CCLR = 0b01;

    TMR01.TCORA = 60000;

    TMR1.TCCR.BYTE = 0b00001101;

    TMR0.TCSR.BIT.ADTE=1;

    }

    void init_adc12()

    {

    MSTP (S12AD) = 0;

    S12AD.ADCSR.BYTE=0b00011110;

    S12AD.ADANS0.WORD=0x0001;

    S12AD.ADCER.BIT.ACE=1;

    S12AD.ADCER.BIT.ADRFMT=0;

    S12AD.ADSTRGR.BIT.ADSTRS=0b00001001;

    IEN(S12AD0,S12ADI0)=1;

    IPR(S12AD0,S12ADI0)=2;

    }

    void Excep_S12AD0_S12ADI0(void)

    {

    s12=1;

    }

Children
No Data