Blinking LED using Registers

This is some basic stuffs, regarding Registers class, that I am learning a few days ago.
Using the LPC23xx datasheet as the reference.

I like to post it on Fezzer, but want to make sure that this is the right way of coding it.
And to avoid any mistake before someone could come and say that this code were not corrected, and to prevent that the code might do some damage to the microcontroller.

So, if anyone, who has the experience on the Registers, can tell me that this is OK code, or
if you can give me some comment on this, I would be really appreaciated!

Thanks,
sam


using System;
using System.Threading;

using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;

using GHIElectronics.NETMF.FEZ;
using GHIElectronics.NETMF.Hardware.LowLevel;


namespace FEZ_Panda_Application1
{
    public class Program
    {
        // LED pin (IO43) or P0.16 on Panda
        private static uint LED = 0x10000;

        // Port0 Direction Base Address
        private static Register FIO0DIR = new Register(0x3FFFC000);
        // Port0 Mask Base Address
        private static Register FIO0MASK = new Register(0x3FFFC010);
        // Port0 Pin Base Address
        private static Register FIO0PIN = new Register(0x3FFFC014);

        public static void Main()
        {
            // read Direction, Mask, and Pin values from Panda
            uint dirValue = FIO0DIR.Read();
            uint maskValue = FIO0MASK.Read();
            uint pinValue = FIO0PIN.Read();

            // set direction for the LED pin to OUTPUT
            FIO0DIR.Write(dirValue | LED);

            //set Mask to allow only LED pin (P0.16) on PORT0 to be actived
            // 1111 1111 1111 1110 1111 1111 1111 1111
            FIO0MASK.Write(0xFFFEFFFF);

            // To turn off LED, do:
            //FIO0PIN.ClearBits(LED);

            while (true)
            {
                // turn On LED
                FIO0PIN.ToggleBits(LED);

                // Sleep for 500 milliseconds
                Thread.Sleep(500);
            }
        }

    }
}


here is the video, of the blinking LED using Registers

That looks right but the question is why would anyone want to blink LED that way? Maybe only to demonstrate how register access works

Awesome! Very easy way to follow how to do register access (though as Gus says maybe not the easiest way to blink an LED.)

Yep, that is one (short) correct answer.

Here is an ( my personal) answers to the question “Why would anyone want to blink LED that way?”

Things that led me to do the blinking LED with Register were:

  • That is the first thing I usually learned to do (blink the LED) with another type of microController (i.e, PIC, ATmel’s ATMEGA, etc)
  • There should have more than one way to do thing (doesn’t matter if there is a better way to do the same thing)
  • To learn how to use GHI’s NETMF Drivers/ibralies, specifically on the Registers Class.
  • It is the ‘cheapest way’ to learn about pin configuration of USBizi/LPC23xx microprocesser, with the provided LED on board.

And here, what I gained

  • So I know that GPIO stands for General Purpose Input/Output ports
  • So I learned that LED (AKA IO43) is on pin 16 of Port 0 (P0.16) on Usbizi 100 Chipset.
  • So I learned that each Port of USBizi/LPC23xx consists of 32 pin, but some of the pin might not be utilized)
  • I learned more about the HEX, Binary, and Decimal equivalent in 32-bit term.
  • I learned and understand more on how to use the 2’s compliment bit to configure the USBizi/LPC23xx
  • And I learn that this is not the only way to switch on and off the led, I could also do this,
    IO0PIN = (IO0PIN && 0xFFFF7FFF) || 0x8000
  • Then, I have some confident in the Registers Class, and got the OK confirmation from you.
    I can step up to control/manipulate more than one pin (LED) at the time.
  • etc.

With regards to my post on switching speed vs Arduino.
[url]http://www.tinyclr.com/forum/8/1744/#/1/msg18111[/url]

It would be interesting to see what speed gain there is by using registers.

Geir:

I don’t know if this is only me, but the link in your post lead me back to this page! ???

And I could not fin any thing when I used the ‘Search’ tool, could you give me the full thread name or new link.
Thanks

Sorry Sam, this is the correct link
[url]http://www.tinyclr.com/forum/1/846/[/url]

I read through this page again the other day: [url]http://informatix.miloush.net/microframework/Articles/MeepMeep.aspx[/url], towards the bottom of the page he describes an interesting approach to quickly toggling pins. The rest of the page about tips for speeding up NETMF programs is far more valuable though, IMHO.

Thanks for link. Good article indeed.

Jeff,
Thanks for the link. But it kept gaving me the Server Error,
until I found out that there is a comma(,) added to the url!

And I thought that I should do something similar to compare between using Registers and OutputPort method,

And here is the results

here is the code:


using System;
using System.Threading;

using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;

using GHIElectronics.NETMF.FEZ;
using GHIElectronics.NETMF.Hardware.LowLevel;

namespace FEZ_Panda_Application1
{
    public class Program
    {
        static bool ledState = false;
        static OutputPort led = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.LED, ledState);

        static uint LED = 0x10000;

        static Register FIO0DIR = new Register(0x3FFFC000);
        static Register FIO0MASK = new Register(0x3FFFC010);
        static Register FIO0PIN = new Register(0x3FFFC014);

        private static void registerLED()
        {
            uint dirValue = FIO0DIR.Read();

            // Set Direction for the LED pin to output
            FIO0DIR.Write(dirValue | LED);
            // Set Mask to allow LED pin to be actived
            FIO0MASK.Write(0xFFFEFFFF);

            for (int i = 0; i < 10000; i++)
            {
                FIO0PIN.ToggleBits(LED);
                Thread.Sleep(100);
            }

        }

        private static void outputPortLED()
        {
            // Blink board LED
            for (int i = 0; i < 10000; i++)
            {
                // Sleep for 500 milliseconds
                Thread.Sleep(100);

                // toggle LED state
                ledState = !ledState;
                led.Write(ledState);
            }
        }

        public static void Main()
        {
            DateTime start;
            TimeSpan end1, end2, end3;

            //OutputPort method
            start = DateTime.Now;
            //registerLED();
            outputPortLED();
            end1 = DateTime.Now - start;

            start = DateTime.Now;
            //registerLED();
            outputPortLED();
            end2 = DateTime.Now - start;

            start = DateTime.Now;
            //registerLED();
            outputPortLED();
            end3 = DateTime.Now - start;

            Debug.Print("Test1: " + end1 + ", " + end2 + ", " + end3);
        }

    }
}