Main Site Documentation

Using Counter of the LPC2478


#1

Hi, I have modified the code from following thread (http://www.tinyclr.com/forum/10/1458/#/2/)

My Problem is now that I want to use the Counter of the Timer1, Timer2 works without any problems.

May any body know which register is set wrong.

For Timer1 I want to use IO16 and for Timer2 IO0.

I have written an Abstract Class to use the counter.


using GHIElectronics.NETMF.Hardware.LowLevel;

    public abstract class S0
    {
        public static Config Timer2 = new Config()
        {
            PCONP = 0xE01FC0C4,
            PCONP_mask = (1 << 22), // enable timer2 
            PINSEL = 0xE002C000,
            PINSEL_mask = (3 << 8), // Select IO0 on EMX CAP2.0
            CCR = 0xE0070028,
            CCR_mask = 0x07, // should be 0 for a counter
            CTCR = 0xE0070070,
            CTCR_mask = (2 << 0 | 0 << 2), // count on falling edge and use CAPn.0, (2<<0)|(0<<2)
            MCR = 0xE0070014,
            MCR_mask = 0, // Don't do anything on match
            PR = 0xE007000C,
            PR_mask = 0, // set prescale to 0
            TC = 0xE0070008,
            TCR = 0xE0070004,
            TCR_mask = 1 // To enable timer/counter
        };

        public static Config Timer1 = new Config()
        {
            PCONP = 0xE01FC0C4,
            PCONP_mask = (1 << 2), // enable timer1
            PINSEL = 0xE002C00C,
            PINSEL_mask = (3 << 7), // Select IO16 on EMX CAP1.1
            CCR = 0xE0008028,
            CCR_mask = 0x38, // should be 0 for a counter
            CTCR = 0xE0008070,
            CTCR_mask = (2 << 0 | 1 << 2),
            MCR = 0xE0008014,
            MCR_mask = 0,
            PR = 0xE000800C,
            PR_mask = 0,
            TC = 0xE0008008,
            TCR = 0xE0008004,
            TCR_mask = 1
        };

        private Config configuration;

        public S0(Config configuration)
        {
            this.configuration = configuration;

            this.InitCounter();
        }

        protected void ResetCounter()
        {
            Register tcr = new Register(this.configuration.TCR);

            // To reset the counter
            tcr.SetBits((1 << 1));
            tcr.ClearBits((1 << 1));
        }

        protected uint GetCount()
        {
            Register tc = new Register(this.configuration.TC);

            return tc.Read();
        }

        private void InitCounter()
        {
            Register pconp = new Register(this.configuration.PCONP);
            pconp.SetBits(this.configuration.PCONP_mask);

            Register pinsel = new Register(this.configuration.PINSEL);
            pinsel.SetBits(this.configuration.PINSEL_mask);

            Register tcr = new Register(this.configuration.TCR);
            tcr.Write(this.configuration.TCR_mask);

            Register pr = new Register(this.configuration.PR);
            pr.Write(this.configuration.PR_mask);

            Register ctcr = new Register(this.configuration.CTCR);
            ctcr.Write(this.configuration.CTCR_mask);

            Register ccr = new Register(this.configuration.CCR);
            ccr.ClearBits(this.configuration.CCR_mask);

            Register mcr = new Register(this.configuration.MCR);
            mcr.Write(this.configuration.MCR_mask);

            this.ResetCounter();
        }

        public struct Config
        {
            public uint PCONP;
            public uint PCONP_mask;
            public uint PINSEL;
            public uint PINSEL_mask;
            public uint TCR;
            public uint TCR_mask;
            public uint PR;
            public uint PR_mask;
            public uint CTCR;
            public uint CTCR_mask;
            public uint CCR;
            public uint CCR_mask;
            public uint MCR;
            public uint MCR_mask;
            public uint TC;
        }
    }


#2

IO16 is P1.19.
Function selection is controlled by bits 7 and 6.
You have to shift by 6 not 7.

This is the first error that I have noticed. Let me know if it fixes the problem. If not I will look further.

Cheers,
Valentin


#3

Just curious. Does it work?


#4

The Code works for timer2 without any problem, but I had no time to test timer1 with our advice.

I will check it at the weekend.


#5

@ Architect

Thanks man it worked!

Best Regards


#6

Great!

I am glad it works for you.


#7

Wow this is a great class. Is it possible to make the IO a variable? So you could use any input for counting?


#8

no it isn’t possible to do that, because the bin is hardwired to a pin at the cpu, and you can only use this special pin.

Have a look at the manuell of the cpu and of the pin mapping of the emx or cobra.

The samble is written for the emx dev board, so I’am not sure if it would work with the cobra board.

best regards


#9

It works for Cobra,
I just succeeded using 3 pulse counters (using Timer 1,2 and 3), and 1 timer ( using PWM0, timing milliseconds in my case) on a FEZ Cobra.
The project also uses the touchscreen and network. So it took some time finding the free I/O ports for capturing the pulses.

For now I use a dispatch timer in MF to get the results. Since I also take the milliseconds from the hardware timer I have the right results anyway, without depending on the MF dispatcher timer to work correctly. I looks like the MF Dispatch timer is off by ± 5 msec, and from time to time even 100ms. By catching the hardware timer , I can see this.

Now I started working on RLP to catch the counters at exactly 1000ms; send a RLP event to managed code, and get the catched counters from managed code.


#10

This is at the edge of my C# knowledge. How do I instantiate the S0 timer class? Do I have to derive a class from it? I would to instantiate an instance of Timer1. I can’t get this class as it is to allow me to do this. I can’t figure out how to specify Timer1 and get access to the ResetCounter() and GetCounter() functions.