Referencing the EMX Hardware

Hi Guys,

I’m trying to get started with my EMX Dev board. I have it all installed and with latest firmware all loaded etc. I can run a couple of example code snippets ok. So far so good, but now I’ve hit the wall. I want to do stuff like blink the LED, play some notes on the Piezo etc to just do baisc tests to get confident with the system.

However I can’t seem to find the basic info of how to reference the various components. Let’s take the Piezo for example, I see it is IO10 or PZ1 as labelled on the EMX dev board, but for the life of me I don’t know how to from the reference to it in my code. I can see the various code examples, but they mostly refer to the Fez. Ok, so I think to myself I should just use that code and make the small changes to refer to the EMX Dev instead, but I don’t know how to do that. Intuitively I would expect to just look at the Fez code and replace the FEZ string with EMX, but that doesn’t work, it seems the reference is formed in a different manner, why not have a consistent way to code all the modules, just changing the device string?

I look at the GHI Electronics NETMF Library but I find it hard to understand - if only there was an example usage at the bottom of each page to show you how it is actually used. I guess this makes sense to an experienced .NET programmer, but for me it’s hard to understand without any example usage to understand.

Can anyone help with an example of how to reference the LEDs at IO48/IO49 and the Piezo at IO10.

In a Fez example fromthe forum the line is…

FEZ_Components.Piezo myPiezo = new FEZ_Components.Piezo(FEZ_Pin.PWM.Di5);

or from the Melodies Turorial it uses…

static PWM MyPWM = new PWM((PWM.Pin)FEZ_Pin.PWM.Di5

How do I do the same for the EMX Dev? If yuo have any other examples of referencng the ports and pins in EMX they would be very welcome indeed.

Thanks for your help.

Philip

Welcome to the club!

Look at GHIElectronics.NETMF.Hardware.EMX.dll



public static class EMX
{
    // Nested Types
    public class Pin
    {
        // Fields
        public const Cpu.Pin GPIO_NONE = Cpu.Pin.GPIO_NONE;
        public const Cpu.Pin IO0 = Cpu.Pin.GPIO_Pin0;
        public const Cpu.Pin IO1 = Cpu.Pin.GPIO_Pin1;
        public const Cpu.Pin IO10 = Cpu.Pin.GPIO_Pin10;
        public const Cpu.Pin IO11 = Cpu.Pin.GPIO_Pin11;

        ...

Hi,

Thanks for taking the time to reply. I’m still scratching my head though. I want to know how to create the proper handler reference so I can use those various pins. Without a code example I just can’t seem to work it out. I see IO10 in the library reference, but how do you then form the handler to reference the Piezo in EMX which is mapped to IO10? Here is the full code snippet for that Piezo example fromt he tutorial projects. I just want to change it to work with my EMX Dev system.

using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
 
using GHIElectronics.NETMF.Hardware;
using GHIElectronics.NETMF.FEZ;
 
public class Program
{
    static PWM MyPWM = new PWM((PWM.Pin)FEZ_Pin.PWM.Di5);
    const int NOTE_C = 261;
    const int NOTE_D = 294;
    const int NOTE_E = 330;
    const int NOTE_F = 349;
    const int NOTE_G = 392;
 
    const int WHOLE_DURATION = 1000;
    const int EIGHTH = WHOLE_DURATION / 8;
    const int QUARTER = WHOLE_DURATION / 4;
    const int QUARTERDOT = WHOLE_DURATION / 3;
    const int HALF = WHOLE_DURATION / 2;
    const int WHOLE = WHOLE_DURATION;
 
    static int[] note = { NOTE_E, NOTE_E, NOTE_F, NOTE_G, NOTE_G, NOTE_F, NOTE_E, NOTE_D,
            NOTE_C, NOTE_C, NOTE_D, NOTE_E, NOTE_E, NOTE_D, NOTE_D, NOTE_E, NOTE_E, NOTE_F, NOTE_G,
            NOTE_G, NOTE_F, NOTE_E, NOTE_D, NOTE_C, NOTE_C, NOTE_D, NOTE_E, NOTE_D, NOTE_C, NOTE_C};
 
    static int[] duration = { QUARTER, QUARTER, QUARTER, QUARTER, QUARTER, QUARTER, QUARTER,
        QUARTER, QUARTER, QUARTER, QUARTER, QUARTER, QUARTERDOT, EIGHTH, HALF, QUARTER, QUARTER,
        QUARTER, QUARTER, QUARTER, QUARTER, QUARTER, QUARTER, QUARTER, QUARTER, QUARTER, QUARTER,
        QUARTERDOT, EIGHTH, WHOLE};
 
    public static void Main()
    {
        while (true)
        {
            for (int i = 0; i < note.Length; i++)
            {
                MyPWM.Set(note[i], 50);
                Thread.Sleep(duration[i]);
            }
            Thread.Sleep(100);
        }
    }
}

Question is, what should

static PWM MyPWM = new PWM((PWM.Pin)FEZ_Pin.PWM.Di5);

be changed to? I can’t see any list of how to define the various handlers for the EMX system. Or at least it is not apparent to me from the info given in the library reference, which doesn’t have any code examples.

Thanks, Philip

ok, think I am a step closer. I have now found the EMX.Pin format, so I have used that to flash an LED with code

namespace MFConsoleApplication1
{
    public class Program
    {
        public static void Main()
        {
            OutputPort LED;
            LED= new OutputPort(EMX.Pin.IO48, true);

            while (true)
            {
                LED.Write(!LED.Read());

                Thread.Sleep(200);
            }
        }

    }
}

So now I just want to refer to the Piezo and PWM the music to it. So I try and use the code in the following way…

static PWM MyPWM = new PWM((PWM.Pin)EMX.Pin.IO10);

this compiles, but causes a debug error when it tried to run on the board. It says ‘An unhandled exception of type ‘System.ArgumentException’ occurred in GHIElectronics.NETMF.Hardware.dll’. I guess I’m not using it right?

My full code is

using System;
using Microsoft.SPOT;
using System.Threading;
using Microsoft.SPOT.Hardware;
using GHIElectronics.NETMF.Hardware;

namespace MFConsoleApplication1
{
    public class Program
    {
        static PWM MyPWM = new PWM((PWM.Pin)EMX.Pin.IO10);
        const int NOTE_C = 261;
        const int NOTE_D = 294;
        const int NOTE_E = 330;
        const int NOTE_F = 349;
        const int NOTE_G = 392;

        const int WHOLE_DURATION = 1000;
        const int EIGHTH = WHOLE_DURATION / 8;
        const int QUARTER = WHOLE_DURATION / 4;
        const int QUARTERDOT = WHOLE_DURATION / 3;
        const int HALF = WHOLE_DURATION / 2;
        const int WHOLE = WHOLE_DURATION;

        static int[] note = { NOTE_E, NOTE_E, NOTE_F, NOTE_G, NOTE_G, NOTE_F, NOTE_E, NOTE_D,
            NOTE_C, NOTE_C, NOTE_D, NOTE_E, NOTE_E, NOTE_D, NOTE_D, NOTE_E, NOTE_E, NOTE_F, NOTE_G,
            NOTE_G, NOTE_F, NOTE_E, NOTE_D, NOTE_C, NOTE_C, NOTE_D, NOTE_E, NOTE_D, NOTE_C, NOTE_C};

        static int[] duration = { QUARTER, QUARTER, QUARTER, QUARTER, QUARTER, QUARTER, QUARTER,
        QUARTER, QUARTER, QUARTER, QUARTER, QUARTER, QUARTERDOT, EIGHTH, HALF, QUARTER, QUARTER,
        QUARTER, QUARTER, QUARTER, QUARTER, QUARTER, QUARTER, QUARTER, QUARTER, QUARTER, QUARTER,
        QUARTERDOT, EIGHTH, WHOLE};

        public static void Main()
        {
            while (true)
            {
                for (int i = 0; i < note.Length; i++)
                {
                    MyPWM.Set(note[i], 50);
                    Thread.Sleep(duration[i]);
                }
                Thread.Sleep(100);
            }
        }
    }
}

ok, found out that there is a specific function for the Piezo to make the sounds…Well I also found out about the usage of EMX.Pin.IOxx for cntrolling pins along the way. Hope the post is at least useful to another newbie…

Utility.Piezo(1000, 200);

Here is the modified code of the Melody Tutorial which runs on the EMX Dev.

using System;
using Microsoft.SPOT;
using System.Threading;
using Microsoft.SPOT.Hardware;
using GHIElectronics.NETMF.Hardware;

namespace MFConsoleApplication1
{
    public class Program
    {
        const uint NOTE_C = 261;
        const uint NOTE_D = 294;
        const uint NOTE_E = 330;
        const uint NOTE_F = 349;
        const uint NOTE_G = 392;

        const int WHOLE_DURATION = 1000;
        const int EIGHTH = WHOLE_DURATION / 8;
        const int QUARTER = WHOLE_DURATION / 4;
        const int QUARTERDOT = WHOLE_DURATION / 3;
        const int HALF = WHOLE_DURATION / 2;
        const int WHOLE = WHOLE_DURATION;

        static uint[] note = { NOTE_E, NOTE_E, NOTE_F, NOTE_G, NOTE_G, NOTE_F, NOTE_E, NOTE_D,
            NOTE_C, NOTE_C, NOTE_D, NOTE_E, NOTE_E, NOTE_D, NOTE_D, NOTE_E, NOTE_E, NOTE_F, NOTE_G,
            NOTE_G, NOTE_F, NOTE_E, NOTE_D, NOTE_C, NOTE_C, NOTE_D, NOTE_E, NOTE_D, NOTE_C, NOTE_C};

        static int[] duration = { QUARTER, QUARTER, QUARTER, QUARTER, QUARTER, QUARTER, QUARTER,
        QUARTER, QUARTER, QUARTER, QUARTER, QUARTER, QUARTERDOT, EIGHTH, HALF, QUARTER, QUARTER,
        QUARTER, QUARTER, QUARTER, QUARTER, QUARTER, QUARTER, QUARTER, QUARTER, QUARTER, QUARTER,
        QUARTERDOT, EIGHTH, WHOLE};

        public static void Main()
        {
            while (true)
            {
                for (int i = 0; i < note.Length; i++)
                {
                    Utility.Piezo(note[i], 50);
                    Thread.Sleep(duration[i]);
                }
                Thread.Sleep(100);
            }
        }
    }
}

This is wrong

static PWM MyPWM = new PWM((PWM.Pin)EMX.Pin.IO10);

You need to open the EMX manual and see where PWM pins are and use the PWM enumeration not the IOs (on FEZ we make this easier as it is targeted for beginners, on EMX this is done the proper way)

Hi Gus,

I did try using all the available documentation including the user manual. But I don’t see how to use the PWM enumeration to reference the EMX piezo buzzer. As I stated in my last post before your reply, I did find the Utility.Piezo() approach that works.

If there is also the other way using a PWM enumeration I would really like to see how that is done - I cannot find the way to do it in any EMX specific examples.

Really the crux of my post was how the information for the enumerations were used in the form of a handler definition.

If you can shed any further light, I would be grateful for a few moments of your time just to give the one line of code of how to define that handler and where that information is held in the documentation.

It seems it is probably easy once you see a couple of examples, but since there is no code examples relating to setting up the relevant handlers for the various features of the EMX in the GHI library reference or in the EMX user manual, it makes it difficult to learn the required info to get off the mark.

Regards, Philip

I think the problem is that on EMX Dev board piezo is connected to a pin that is not PWM capable.
Look at OutputCompare. Fez Mini Robot Kit has some code that shows how to use OutputCompare with a piezo.

http://www.ghielectronics.com/downloads/FEZ/Mini/FEZMini_Robot.cs

Hi everyone

I am still creating a custom PCB for our recent EMX module. If I want to play melody with a piezo, should I route the piezo on IO10 as shown in EMX module PINOUT or should route it to a PWM capable IO? This is not clear to me.

Please use new thread to ask new questions

There is a special “peizo” class built in NETMF that you can use or you can use any PWM pin.
If it is up to me, I would use a PWM pin.

Hello Gus

This does not seems to me a new question since it talks about IO10 of EMX, the piezo class and PWM… Sorry for this misunderstanding.

So let’s create a new thread about this since I have also other questions that are related with