EmbeddedRelated.com
Forums

Sorry for newbe question. Can't get timer interrupt to work at all... :-(

Started by anda...@hotmail.com September 26, 2008
Hello all,
I use the HCS12 T-board with the mc9s12dp512. I also use the trial version of ICC12 compiler and the TWINpeeks monitor.
My purpose of the program is to toggle the T-board LED bar, so I can see when the timer interrupt works.
#include

void SetTimer(unsigned int Ticks)
{
asm("sei");
TSCR1 = 0x80; //Set TEN = 1 (enable)
TSCR2 = 0x04; //Timer Prescaler select = 0x0100 => 2^4 = 16MHz
TIOS = 0x04; //Set as output
TIE = 0x04; //Timer interrupt enable
TCTL2 &= ~(0x20 | 0x10);
// Clear Timer Flag
TFLG1 = 0x81;
asm("cli");
TC2 = Ticks - 1;
}

// ISR for timer 0 this is the same for timer 7
#pragma interrupt_handler Timer0
void Timer2(void)
{
PORTB ^= 0x00; // Pulse LED
// Clear timer interrupt flag
TFLG1 = 0x01;
}

void main(void)
{
DDRB = 0xFF;
PORTB = 0xFF;

*((unsigned char *)0x3FE2) = 0x06; // JMP opcode
*((void (**)(void))0x3FE3) = Timer2;

SetTimer(200);

while(1)
{
//Toggle LED bar
}
}

Many issues with your code. interrupt_handler pragma for Timer0 function,
while ISR is called Timer2. In SetTimer you enable interrupt for TC2, clear
flags of TC0 and TC7, while Timer2 ISR is clearing TC0 flag. Also make sure
$3FE2 is right interrupt jump table entry for TC2.

Edward

----- Original Message -----
From:
To: <6...>
Sent: Friday, September 26, 2008 15:58
Subject: [68HC12] Sorry for newbe question. Can't get timer interrupt to
work at all... :-(
> Hello all,
> I use the HCS12 T-board with the mc9s12dp512. I also use the trial version
> of ICC12 compiler and the TWINpeeks monitor.
> My purpose of the program is to toggle the T-board LED bar, so I can see
> when the timer interrupt works.
> #include void SetTimer(unsigned int Ticks)
> {
> asm("sei");
> TSCR1 = 0x80; //Set TEN = 1 (enable)
> TSCR2 = 0x04; //Timer Prescaler select = 0x0100 => 2^4 = 16MHz
> TIOS = 0x04; //Set as output
> TIE = 0x04; //Timer interrupt enable
> TCTL2 &= ~(0x20 | 0x10);
> // Clear Timer Flag
> TFLG1 = 0x81;
> asm("cli");
> TC2 = Ticks - 1;
> }
>
> // ISR for timer 0 this is the same for timer 7
> #pragma interrupt_handler Timer0
> void Timer2(void)
> {
> PORTB ^= 0x00; // Pulse LED
> // Clear timer interrupt flag
> TFLG1 = 0x01;
> }
>
> void main(void)
> {
> DDRB = 0xFF;
> PORTB = 0xFF;
>
> *((unsigned char *)0x3FE2) = 0x06; // JMP opcode
> *((void (**)(void))0x3FE3) = Timer2;
>
> SetTimer(200);
>
> while(1)
> {
> //Toggle LED bar
> }
> }
>
>
Thanks for the update info, but can't still get it to work.
I think that the fault are in the following code lines:

*((unsigned char *)0x3FE2) = 0x06; // JMP opcode
*((void (**)(void))0x3FE3) = Timer2;

The manual I got with the T-board , it says 0x3FE2 for the mc9s12dp512 timer. But it also seems like some examples in the manual also are for the mc9s12dp256. :-/

So after done the small changes in the code, it's like following:
#include

void SetTimer(unsigned int Ticks)
{
asm("sei");
TSCR1 = 0x80; //Set TEN = 1 (enable)
TSCR2 = 0x04; //Timer Prescaler select = 0x0100 => 2^4 = 16MHz
TIOS = 0x04; //Set as output
TIE = 0x04; //Timer interrupt enable
TCTL2 &= ~(0x20 | 0x10);
// Clear Timer Flag
TFLG1 = 0x04;
asm("cli");
TC2 = Ticks - 1;
}

// ISR for timer 0 this is the same for timer 7
#pragma interrupt_handler Timer2
void Timer2(void)
{
PORTB ^= 0x00; // Pulse LED
// Clear timer interrupt flag
TFLG1 = 0x04;
}

void main(void)
{
DDRB = 0xFF;
PORTB = 0xFF;

*((unsigned char *)0x3FE2) = 0x06; // JMP opcode
*((void (**)(void))0x3FE3) = Timer2;

SetTimer(200);

while(1)
{
//Toggle LED bar
}
}

Thanks
/Andreas

Hello all,
>I use the HCS12 T-board with the mc9s12dp512. I also use the trial version of ICC12 compiler and the TWINpeeks monitor.
>My purpose of the program is to toggle the T-board LED bar, so I can see when the timer interrupt works.
>#include
>
>void SetTimer(unsigned int Ticks)
>{
> asm("sei");
> TSCR1 = 0x80; //Set TEN = 1 (enable)
> TSCR2 = 0x04; //Timer Prescaler select = 0x0100 => 2^4 = 16MHz
> TIOS = 0x04; //Set as output
> TIE = 0x04; //Timer interrupt enable
> TCTL2 &= ~(0x20 | 0x10);
> // Clear Timer Flag
> TFLG1 = 0x81;
> asm("cli");
> TC2 = Ticks - 1;
>}
>
>// ISR for timer 0 this is the same for timer 7
>#pragma interrupt_handler Timer0
>void Timer2(void)
>{
> PORTB ^= 0x00; // Pulse LED
> // Clear timer interrupt flag
> TFLG1 = 0x01;
>}
>
>void main(void)
>{
> DDRB = 0xFF;
> PORTB = 0xFF;
>
> *((unsigned char *)0x3FE2) = 0x06; // JMP opcode
> *((void (**)(void))0x3FE3) = Timer2;
>
> SetTimer(200);
>
> while(1)
> {
> //Toggle LED bar
> }
>}
>
>

a...@hotmail.com wrote:
>
> // ISR for timer 0 this is the same for timer 7
> #pragma interrupt_handler Timer2
> void Timer2(void)
> {
> PORTB ^= 0x00; // Pulse LED

Maybe should be PORTB ^= 0xff; rather than 0x00, as exclusive or with 0
will not toggle any bits.

David