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

LPC2294 How to program a counter

Hello everybody,

I have a Philips LPC2294/01 MC.

I would like to count the rise or falling edge from an external signal.

I have 2 questions and need your help!

1. Do my MC already have an external counter? I think so but I donÂ't reallyfound somethink about that.
2. And if not? How can I count an rise or falling edge with the internal timer/counter?

I hope somebody can help me? Maybe with some code to?

Thx!!!

  • To my knowledge, your processor has timers but not counters and it do not allow the timers to count external events.

    The capture pins can be used to react to rising and falling signals - but they will not step up any counter. Instead, they will latch the current counter value, to allow you to measure the time since the previously captured event or since you manually resetted the timer.

    Is the signal you need to count of high frequency? Is the events of random duration so two events may sometimes be very closely together?

    I think you will have to use a normal interrupt input (EINT0 .. EINT3). They can be set to generate interrupts on raising or falling edge. But the events can't arrive faster than your interrupt routine has time to trig, count and return.

    For very slow events, it will be possible to just poll a processor pin to detect any changes.

  • Hi Per,

    first, thanks for your help!

    We worry, that your are right...this is nearly the same what we got from our manuels or the internet.

    Our problem is, that we have a very fast changing input signal. We what to measure an incremental dealer.

    So is there any chance for us to get the inkrements?

    We think, that the normal interrupt inputs are to slow and the MC could stop.

    PS: sorry for my bad english...IÂ'm from germany

  • No problem with the english. Most people on the different programming forums do not have english as their native language. I'm from Sweden by the way :)

    You might possibly abuse an SPI interface as a divide-by-8 prescaler by connecting the external signal to the SPI clock signal and run the SPI in slave mode. This also requires that you keep the slave-select line always asserted. You will waste extra pins, and the granularity of the counting will be 8 ticks, since the SPI controller only supports 8-bit transfers and you can't see the internal bit counter. Not a perfect solution, but it might be enough to get an acceptable frequency for an internal interrupt handler. The LPC2294 does not have a DMA or FIFO to abuse as further "prescaling" of the input signal.

    Your next alternative would be to use an external prescaler - possibly one that you can also query to get the current value. With too short external prescaler, it will be hard to capture the external prescaler witout the manually stepped internal counter also ticking.

    Is it the four CAN controllers that made you select this chip? If two CAN controllers had been enough, then the LPC23xx series might have been an alternative.

  • Hi Per,

    this is me again.

    We can not really handle what you proposed us.

    But we try to catch our interrupts with an external interrupt input. We get the increments from a magnetic sensor on a north/south magnetic line. (hope that is the right translation!!)
    We want to try the counting with a slow speed. But our external interrupt doesnÂ't work.

    here is the code

    #include <stdio.h>
    #include <LPC22XX.H>
    
    #define VPBDIV_VAL  0x02
    
    __irq void eint3 (void) {
    
      IOCLR0 = 0x00000100;
      EXTINT = 0x00000003;
    
      VICVectAddr = 0x0000000;
    }
    void init_extern(void)
    {
            IODIR0 = 0x00000100;
    
      VPBDIV=0;
      EXTMODE = (1<<3);
      VPBDIV=VPBDIV_VAL;
      VPBDIV=0;
      EXTPOLAR = ~(1<<3);
      VPBDIV=VPBDIV_VAL;
      VICIntEnable = ( 1 << 17 );
      VICVectCntl2 = (0x20 | (1 <<17)) ;
      VICVectAddr2 = (unsigned)eint3;
      VICIntEnable = ( 1 << 17 );
    }
    
    int main (void)
    {
    
            PINSEL1 |= (1<<29);
            PINSEL1 &=~ (1<<28);
            init_extern();
    
    while (1)
            {
    
            }
    }
    

    this code shall only turn a LED on when an interrupt was detected on input P30.

    What is wrong?

    Maybe you or someone else can help us with it?

    Thanks a lot!