New domain and blog

New domain and blog
Please head over to my new domain to view my blog and current projects

Friday, 17 August 2012

C# dll for programming Microchip PIC's

In the test development environment, the need often arises where a microcontroller needs to be programmed before the testing can be completed. This can either be done with the development environment or through the command line if this method is provided for by the manufacturer. 

Programming in the IDE is nice and easy, but very slow and needs the user to press the buttons. Often in the test environment, this process needs to be automated with as little user input as possible. Here is where using the command line parameters comes in so handy. You will need this hard-to-find document which outlines the commands to use.

In my application I am programming the PIC18LF26K22 microcontroller from Microchip and using the MPLAB PM3 device programmer. To test the devices I am using LabVIEW and TestStand from National Instruments. 

There is a provision in LabVIEW where you can run command line programs, but I have found this to be a bit slow when interfacing with the PM3 programmer. This made me think of writing a C# dll to perform this task. Now I am no expert at C# programming, but am trying to learn where I can so thought this would be a good exercise. 

I started off be investigating the Process Class and found just what I was looking for. All you need to do is create a new object of the Diagnostics.Process class. Then build the string that you need to send. The make up of this string is explained in pdf attached above. 


public bool LoadImageToPm3(string pm3CmdPath, string port, string hexImagePath)
        {
            System.Diagnostics.Process proc = new System.Diagnostics.Process();
            proc.StartInfo.FileName = pm3CmdPath;
            proc.StartInfo.Arguments = "/" + port + " /P18LF26K22 /F" + '"' + hexImagePath + '"';
            proc.StartInfo.RedirectStandardOutput = true;
            proc.StartInfo.UseShellExecute = false;            
            try
            {
                proc.Start();
                strStandardOutput = proc.StandardOutput.ReadToEnd();
                proc.WaitForExit();
                int iSuccessful = strStandardOutput.IndexOf("Operation Succeeded");
                if (iSuccessful > 0)
                {
                    return true;
                }
                else 
                {
                    return false;
                }
            }
            catch (SystemException e)
            {
                return false;
            }
        }

This is an example of how to load an image onto the PM3. I build the string and then once I start the process, I read the standard output to make sure the the operation was successful.

The dll I wrote can handle loading an image to the programmer, program a device where the image is already on the programmer and erase the device. There are many other processes that can be performed but for the production testing that I am doing, this is all I need.

In the project attached, I also wrote a simple form application and a TestStand sequence to test the dll. You can find those files here.

By using a dll, I decreased my programming time from 60sec in LabVIEW to 15sec. When the device being tested only takes 30sec to test, an extra 45sec programming time is huge.

I hope this helps and if you need anything explained further, please leave a comment and I will try help where I can.

Greg