Main Site Documentation

LPC2387 using Timer3 to count pulses


#1

Hi all,

I have a simple test, a 555 timer produces pusles (as I can see that with my oscilloscope). I want to count these pulses. I want to use Timer3.
I have this code, but I cannot see why it isn’t working.
Which bit(s) needs to be flipped in order for it to work?

Many thanks in advance.


public enum Bits
        {
            BIT0 = 1 << 0,
            BIT1 = 1 << 1,
            BIT2 = 1 << 2,
            BIT3 = 1 << 3,
            BIT4 = 1 << 4,
            BIT5 = 1 << 5,
            BIT6 = 1 << 6,
            BIT7 = 1 << 7,
            BIT8 = 1 << 8,
            BIT9 = 1 << 9,
            BIT10 = 1 << 10,
            BIT11 = 1 << 11,
            BIT12 = 1 << 12,
            BIT13 = 1 << 13,
            BIT14 = 1 << 14,
            BIT15 = 1 << 15,
            BIT16 = 1 << 16,
            BIT17 = 1 << 17,
            BIT18 = 1 << 18,
            BIT19 = 1 << 19,
            BIT20 = 1 << 20,
            BIT21 = 1 << 21,
            BIT22 = 1 << 22,
            BIT23 = 1 << 23,
            BIT24 = 1 << 24,
            BIT25 = 1 << 25,
            BIT26 = 1 << 26,
            BIT27 = 1 << 27,
            BIT28 = 1 << 28,
            BIT29 = 1 << 29,
            BIT30 = 1 << 30,
            BIT31 = 1 << 31,
        }

//Turn on the power of timer 3
            Register PCONP1 = new Register(0xE01FC0C4);
            PCONP1.SetBits((uint)Bits.BIT23);
            if ((PCONP1.Read() & (uint)Bits.BIT23) != (uint)Bits.BIT23)
            {
                Debug.Print("Failed to turn on power of timer 3");
            }

            //23.6.2 The Timer Control Register (TCR) is used to control the operation of the Timer/Counter.
            Register TimerControlRegister3 = new Register(0xE0074004);
            TimerControlRegister3.Write(1);
            //TimerControlRegister3.SetBits((uint)Bits.BIT1); //1: Timer Counter and Prescale Counter are enabled for counting. 0: disabled
            //Hold counter in reset: TimerControlRegister3.SetBits(1 << 1);
            //Release counter from reset: TimerControlRegister3.ClearBits(1 << 1);

            //23.6.3 The Count Control Register (CTCR) is used to select between Timer and Counter mode, 
            //and in Counter mode to select the pin and edge(s) for counting.
            //NOTE: The frequency of the CAP input can not exceed one quarter of the PCLK clock, meaning 78MHz clock / 4 = 19MH resolution counter. Thats enough to count < 200KHz.
            Register CountControlRegister3 = new Register(0xE0074070);
            //0:1 value 01: Counter Mode: TC is incremented on rising edges on the CAP input

            CountControlRegister3.SetBits((uint)Bits.BIT0);
            CountControlRegister3.ClearBits((uint)Bits.BIT1 | (uint)Bits.BIT2 | (uint)Bits.BIT3); // Selects CAP3.0 for TIMER3, this is P0.23

            //Becase CountControlRegister3 uses CAP3.0, the pin function must be selected to be CAP3.0 See tabl 107 chapter 9.5.2.1
            Register PinFunctionSelectRegister1 = new Register(0xE002C004);
            PinFunctionSelectRegister1.SetBits((uint)(Bits.BIT14 | Bits.BIT15)); //Give Pin0.23/AD0.0 the function of CAP3.0 That is Timer 3 capture mode.

            //Set counter to 0
            Register PrescaleRegister3 = new Register(0xE007400C);
            PrescaleRegister3.Write(0);

            //Match Control Register are not used, make sure all is off
            Register MatchControlRegister3 = new Register(0xE0074014);
            MatchControlRegister3.ClearBits((uint)(Bits.BIT0 |
                                                   Bits.BIT1 |
                                                   Bits.BIT2 |
                                                   Bits.BIT3 |
                                                   Bits.BIT4 |
                                                   Bits.BIT5 |
                                                   Bits.BIT6 |
                                                   Bits.BIT7 |
                                                   Bits.BIT8 |
                                                   Bits.BIT9 |
                                                   Bits.BIT10
                                                  )
                                            );
            //Not used in counter mode so set to 0
            Register CaptureControlRegister3 = new Register(0xE0074028);
            CaptureControlRegister3.ClearBits((uint)(Bits.BIT0 | Bits.BIT1 | Bits.BIT2));

            TimerControlRegister3.SetBits((uint)Bits.BIT1);
            TimerControlRegister3.ClearBits((uint)Bits.BIT1);

            //The 32-bit Timer Counter register is incremented when the prescale counter reaches its terminal count.
            //Used for reading the count
            Register TimerCounterRegister3 = new Register(0xE0074008);
            //Read the value of the counter 
            uint Counts = 0;
            while (true)
            {
                Counts = TimerCounterRegister3.Read();
                Debug.Print(Counts.ToString());
                Thread.Sleep(1000);
            }

#2

Hi, Mike. Will this Codeshare entry work for you? http://www.tinyclr.com/codeshare/entry/665


#3

@ Iggmoe - Thanks so much. I will test that code today. It looks promising and complete.
I had seen this example: http://www.tinyclr.com/codeshare/entry/298 but I coulnd’t get it working.
I will test the one you pointed me out soon.