This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Timer0 Interrupt does not update

Hi Guys,

I am simulating delayed pulses for a stepper motor, and I am using timer0 on MCB2300. I setup my interrupt timer and I want it to be updated every time a pulse is fired. When I simulated my code it does not look the timer0 gets updated at all: Timer0 retain the initial value I set it to which is 1second all the time.

Below is copy of my code, please advise on how I can get the timer updated and why it does not update.

//source code:

volatile unsigned int delay_constant;

/* Function that turns on requested LED */
void LED_On (unsigned int num) { FIO2SET = (1 << num);
}

/* Function that turns off requested LED */
void LED_Off (unsigned int num) { FIO2CLR = (1 << num);
}

/* Function that initializes LEDs */
void LED_Init(void) {

PINSEL10 = 0; /* Disable ETM interface, enable LEDs */ FIO2DIR = 0x000000FF; /* P2.0..7 defined as Outputs */ FIO2MASK = 0x00000000;
}

__irq void T0_IRQHandler (void) {

int i;

LED_On (0x07);

for( i=0; i<100; i++) { ; } LED_Off (0x07); T0IR = 1; /* Clear interrupt flag */ VICVectAddr = 0; /* Acknowledge Interrupt */

}
float fastsqrt(float val) { long tmp = *(long *)&val; tmp -= 127L<<23; /* Remove IEEE bias from exponent (-2^23) */ /* tmp is now an appoximation to logbase2(val) */ tmp = tmp >> 1; /* divide by 2 */ tmp += 127L<<23; /* restore the IEEE bias from the exponent (+2^23) */ return *(float *)&tmp;
}

void timer0_initialize (void){

T0MR0 = delay_constant; /* 1msec = 12000-1 at 12.0 MHz */
T0MCR = 3; /* Interrupt and Reset on
T0TCR = 1; /* Timer0 Enable */
VICVectAddr4 = (unsigned long ) T0_IRQHandler; /* Set Interrupt Vector */
VICVectCntl4 = 15; /* use it for Timer0 Interrupt */
VICIntEnable = (1 << 4); /* Enable Timer0 Interrupt */
}

int main (void)
{

float temp0; static float temp1, temp2, temp3; unsigned int step=0; unsigned int denom;

delay_constant=12000000;

LED_Init(); /* LED Initialization */

/* Enable and setup timer interrupt, start timer */ timer0_initialize();

/*C0 equation: C0=frequency*sqrt(2*motor_step_angle/angular_accel)*/ temp0= motor_step_angle+motor_step_angle; temp0=temp0/angular_accel; temp0=fastsqrt(temp0); temp0=temp0*frequency; step++;

do{ /*Cn equation: Cn= (Cn-1)-(2*Cn-1/(4*step+1))*/ denom=(step<<2)+1; temp1=(temp0+temp0)/denom; temp0=temp0- temp1; step++;

/* normalization so that delays are obtained in Microseconds */ temp3=temp0*1000000; temp3=ceil(temp3/frequency); delay_constant=temp3;

}while(step> 0 && step<31);

}

Parents
  • Hi Guys!

    I am still debugging the issue I discussed in my previous posting. so far I found that the interrupt does not get cleared it just keeps looping inside _IRQ function. Can anybody give any feedback as to why this is happening? Thank you all in advance. I pasted a copy of my modified code.

    /******************************************************************************/
    /* BLINKY.C: LED Flasher */
    /******************************************************************************/
    /* This file is part of the uVision/ARM development tools. */
    /* Copyright (c) 2005-2006 Keil Software. All rights reserved. */
    /* This software may only be used under the terms of a valid, current, */
    /* end user licence from KEIL for a compatible version of KEIL software */
    /* development tools. Nothing else gives you the right to use this software. */
    /******************************************* ***********************************/

    #include <stdio.h>
    #include <LPC23xx.H> /* LPC23xx definitions */
    #include "LCD.h" /* Graphic LCD function prototypes */
    #include "motor.h"
    #include <math.h>

    /* Macro Definitions */
    #define TEMT (1<<6)
    #define LINE_FEED 0x0A
    #define CARRIAGE_RET 0x0D

    unsigned int delay_constant;
    volatile static unsigned char flag;
    volatile unsigned int step=0;

    /* Function that turns on requested LED */
    void LED_On (unsigned int num) { FIO2SET = (1 << num);
    }

    /* Function that turns off requested LED */
    void LED_Off (unsigned int num) { FIO2CLR = (1 << num);
    }

    /* Function that outputs value to LEDs */
    void LED_Out(unsigned int value) { FIO2CLR = 0xFF; /* Turn off all LEDs */ FIO2SET = (value & 0xFF); /* Turn on requested LEDs */
    }

    __irq void T0_IRQHandler (void) {

    int i;

    LED_On (0x07); LED_On (0x06);

    for( i=0; i<120; i++) { ; }

    LED_Off (0x07);

    printf("test %d\n", delay_constant);

    flag=1;

    VICVectAddr = 0; /* Acknowledge Interrupt */ T0IR = 1; /* Clear interrupt flag */

    }
    float fastsqrt(float val) { long tmp = *(long *)&val; tmp -= 127L<<23; /* Remove IEEE bias from exponent (-2^23) */ /* tmp is now an appoximation to logbase2(val) */ tmp = tmp >> 1; /* divide by 2 */ tmp += 127L<<23; /* restore the IEEE bias from the exponent (+2^23) */ return *(float *)&tmp;
    }

    void UartInit()
    {

    PINSEL0 |= 0x40000000; /* Enable TxD1 in P0.15 */ PINSEL1 |= 0x00000001; /* Enable RxD1 in P0.16 */ U1LCR = 0x83; /* 8 bits, no Parity, 1 Stop bit */ U1FDR = 0; /* Fractional divider not used */ U1DLM = 0x00; U1DLL = 78; /* 9600 Baud Rate @ 12MHz Clock */ U1LCR = 0x03; /* DLAB = 0 */ U1FCR = 0x07; /* enable TX amp RX FIFO ,clears TX amp RX FIFO */
    }

    /* Implementation of putchar (also used by printf function to output data) */
    int sendchar(int ch) { /* Write character to Serial Port */

    while (!(U1LSR & 0x20));

    return (U1THR = ch);
    }

    int getkey (void) { /* Read character from Serial Port */

    while (!(U1LSR & 0x01));

    return (U1RBR);
    }

    /* Function that initializes LEDs */
    void LED_Init(void) {

    PINSEL10 = 0; /* Disable ETM interface, enable LEDs */ FIO2DIR = 0x000000FF; /* P2.0..7 defined as Outputs */ FIO2MASK = 0x00000000;
    }

    void UartTx(void)
    { int i=0;

    char c[]= "I am .. ";

    /* Keep Transmitting until Null character('\0') is reached */ while(c[i]) { U1THR=c[i]; i++; }

    U1THR=LINE_FEED; U1THR=CARRIAGE_RET;
    }

    int main (void) {

    float temp0; static float temp1, temp2, temp3; unsigned int denom; //volatile unsigned int delay_constant; //volatile int flag=0;

    LED_Init(); /* LED Initialization */ UartInit();

    //LED_On (0x06);

    /* initialize Interrupt */ T0MCR = 3; /* Interrupt and Reset on MR0 */ T0TCR = 1; /* Timer0 Enable */ VICVectAddr4 = (unsigned long ) T0_IRQHandler;/* Set Interrupt Vector */ VICVectCntl4 = 15; /* use it for Timer0 Interrupt */ VICIntEnable = (1 << 4);

    /*C0 equation: C0=frequency*sqrt(2*motor_step_angle/angular_accel)*/

    temp0=2*motor_step_angle; temp0=temp0/angular_accel; temp0=fastsqrt(temp0); temp0=temp0*frequency; step++; flag=1;

    while(flag) { flag=0;

    /*Cn equation: Cn= (Cn-1)-(2*Cn-1/(4*step+1))*/ denom=(step<<2)+1; temp1=(temp0+temp0)/denom; temp0=temp0- temp1; step++;

    if(step==4) { LED_On(0x5); }

    /* normalization so that delays are obtained in Microseconds */ temp3=temp0; temp3=ceil(temp3/12); delay_constant=temp3; T0MR0 = delay_constant;

    }
    }

Reply
  • Hi Guys!

    I am still debugging the issue I discussed in my previous posting. so far I found that the interrupt does not get cleared it just keeps looping inside _IRQ function. Can anybody give any feedback as to why this is happening? Thank you all in advance. I pasted a copy of my modified code.

    /******************************************************************************/
    /* BLINKY.C: LED Flasher */
    /******************************************************************************/
    /* This file is part of the uVision/ARM development tools. */
    /* Copyright (c) 2005-2006 Keil Software. All rights reserved. */
    /* This software may only be used under the terms of a valid, current, */
    /* end user licence from KEIL for a compatible version of KEIL software */
    /* development tools. Nothing else gives you the right to use this software. */
    /******************************************* ***********************************/

    #include <stdio.h>
    #include <LPC23xx.H> /* LPC23xx definitions */
    #include "LCD.h" /* Graphic LCD function prototypes */
    #include "motor.h"
    #include <math.h>

    /* Macro Definitions */
    #define TEMT (1<<6)
    #define LINE_FEED 0x0A
    #define CARRIAGE_RET 0x0D

    unsigned int delay_constant;
    volatile static unsigned char flag;
    volatile unsigned int step=0;

    /* Function that turns on requested LED */
    void LED_On (unsigned int num) { FIO2SET = (1 << num);
    }

    /* Function that turns off requested LED */
    void LED_Off (unsigned int num) { FIO2CLR = (1 << num);
    }

    /* Function that outputs value to LEDs */
    void LED_Out(unsigned int value) { FIO2CLR = 0xFF; /* Turn off all LEDs */ FIO2SET = (value & 0xFF); /* Turn on requested LEDs */
    }

    __irq void T0_IRQHandler (void) {

    int i;

    LED_On (0x07); LED_On (0x06);

    for( i=0; i<120; i++) { ; }

    LED_Off (0x07);

    printf("test %d\n", delay_constant);

    flag=1;

    VICVectAddr = 0; /* Acknowledge Interrupt */ T0IR = 1; /* Clear interrupt flag */

    }
    float fastsqrt(float val) { long tmp = *(long *)&val; tmp -= 127L<<23; /* Remove IEEE bias from exponent (-2^23) */ /* tmp is now an appoximation to logbase2(val) */ tmp = tmp >> 1; /* divide by 2 */ tmp += 127L<<23; /* restore the IEEE bias from the exponent (+2^23) */ return *(float *)&tmp;
    }

    void UartInit()
    {

    PINSEL0 |= 0x40000000; /* Enable TxD1 in P0.15 */ PINSEL1 |= 0x00000001; /* Enable RxD1 in P0.16 */ U1LCR = 0x83; /* 8 bits, no Parity, 1 Stop bit */ U1FDR = 0; /* Fractional divider not used */ U1DLM = 0x00; U1DLL = 78; /* 9600 Baud Rate @ 12MHz Clock */ U1LCR = 0x03; /* DLAB = 0 */ U1FCR = 0x07; /* enable TX amp RX FIFO ,clears TX amp RX FIFO */
    }

    /* Implementation of putchar (also used by printf function to output data) */
    int sendchar(int ch) { /* Write character to Serial Port */

    while (!(U1LSR & 0x20));

    return (U1THR = ch);
    }

    int getkey (void) { /* Read character from Serial Port */

    while (!(U1LSR & 0x01));

    return (U1RBR);
    }

    /* Function that initializes LEDs */
    void LED_Init(void) {

    PINSEL10 = 0; /* Disable ETM interface, enable LEDs */ FIO2DIR = 0x000000FF; /* P2.0..7 defined as Outputs */ FIO2MASK = 0x00000000;
    }

    void UartTx(void)
    { int i=0;

    char c[]= "I am .. ";

    /* Keep Transmitting until Null character('\0') is reached */ while(c[i]) { U1THR=c[i]; i++; }

    U1THR=LINE_FEED; U1THR=CARRIAGE_RET;
    }

    int main (void) {

    float temp0; static float temp1, temp2, temp3; unsigned int denom; //volatile unsigned int delay_constant; //volatile int flag=0;

    LED_Init(); /* LED Initialization */ UartInit();

    //LED_On (0x06);

    /* initialize Interrupt */ T0MCR = 3; /* Interrupt and Reset on MR0 */ T0TCR = 1; /* Timer0 Enable */ VICVectAddr4 = (unsigned long ) T0_IRQHandler;/* Set Interrupt Vector */ VICVectCntl4 = 15; /* use it for Timer0 Interrupt */ VICIntEnable = (1 << 4);

    /*C0 equation: C0=frequency*sqrt(2*motor_step_angle/angular_accel)*/

    temp0=2*motor_step_angle; temp0=temp0/angular_accel; temp0=fastsqrt(temp0); temp0=temp0*frequency; step++; flag=1;

    while(flag) { flag=0;

    /*Cn equation: Cn= (Cn-1)-(2*Cn-1/(4*step+1))*/ denom=(step<<2)+1; temp1=(temp0+temp0)/denom; temp0=temp0- temp1; step++;

    if(step==4) { LED_On(0x5); }

    /* normalization so that delays are obtained in Microseconds */ temp3=temp0; temp3=ceil(temp3/12); delay_constant=temp3; T0MR0 = delay_constant;

    }
    }

Children