ChipKIT - Blink LED Tutorial

of 13/13
Getting Started with the LabVIEW Interface for chipKIT TM LabVIEW Interface for chipKIT TM Getting Started Blink LED Overview This document will explain how to use LabVIEW and chipKIT TM Uno32 TM to blink a LED Required Hardware Components 1 chipKIT TM Uno32 TM prototyping platform and USB cable 1 220Ω or 330Ω resistor (optional) 1 basic LED (optional) 1 basic solder-less breadboard (optional) *By default, the chipKIT device will output to LED4 of the device, which is connected to pin 13. So it is not necessary to have an external LED to complete this tutorial. Required Software Components - LabVIEW 2009 or newer - NI VISA - JKI VI Package Manager - LabVIEW Interface for chipKIT Note: refer to Getting Started Installing Software for more information Breadboard Connection Diagram Circuit Schematic
  • date post

    28-Oct-2015
  • Category

    Documents

  • view

    83
  • download

    4

Embed Size (px)

description

AADF

Transcript of ChipKIT - Blink LED Tutorial

  • Getting Started with the LabVIEW Interface for chipKITTM

    LabVIEW Interface for chipKITTM Getting Started Blink LED

    Overview

    This document will explain how to use LabVIEW and chipKITTM Uno32TM to blink a LED

    Required Hardware Components 1 chipKITTM Uno32TM prototyping platform and USB cable

    1 220 or 330 resistor (optional)

    1 basic LED (optional)

    1 basic solder-less breadboard (optional)

    *By default, the chipKIT device will output to LED4 of the device, which is connected to pin 13. So it is not

    necessary to have an external LED to complete this tutorial.

    Required Software Components - LabVIEW 2009 or newer

    - NI VISA

    - JKI VI Package Manager

    - LabVIEW Interface for chipKIT

    Note: refer to Getting Started Installing Software for more information

    Breadboard Connection Diagram Circuit Schematic

  • Getting Started with the LabVIEW Interface for chipKITTM

    LabVIEW Programming

    Open LabVIEW 2012 by navigating to Start All Programs National Instruments LabVIEW 2012. You will see

    a screen similar to the following while LabVIEW is loading.

    Once loaded, you are ready to create a Virtual Instrument (VI). Select File New VI.

    You are now ready to begin programming in LabVIEW. A LabVIEW VI consists of a front panel and a block

    diagram. The front panel is the grey window contains the graphical user interface (GUI). The block diagram is

    the white window and contains the graphical programming code. You can navigate between the two by

    selecting Window Show Front Panel/Block Diagram or by simply pressing . Or if you like to have

    each side by side, you can tile the two windows by pressing .

  • Getting Started with the LabVIEW Interface for chipKITTM

    Lets start on the block diagram and focus on the code to control the chipKIT to begin. First, an Application

    Programming Interface (API), is a collection/library of VIs created to communicate with a piece of hardware.

    The API written for LabVIEW to control the chipKIT device is similar in nature to all hardware APIs in LabVIEW.

    It follows the same Open/Configure Read/Write Close/Clear paradigm that powerful industry tools leverage

    such as NI DAQ devices, NI PXI RF devices, and NI Modular Instruments, so your skills will easily scale into

    industry.

    To open and configure the chipKIT Uno32 device, right-click the block diagram to bring up the functions palette

    and then navigate to chipKIT Init. This is the Initialize function. Select the function and then left-click to place

    it onto your block diagram. Now press and hover over the newly placed function with your mouse.

    This is context help and it will tell you more about the function, such as what the inputs and outputs are.

  • Getting Started with the LabVIEW Interface for chipKITTM

    The Init function is configured to auto-detect your chipKIT device, but you can hover over the VISA resource

    input terminal and right-click this terminal and select Create Control. This will create a cluster control on the

    front panel that you can configure to select your ChipKIT device and COM port. For this tutorial we will specify

    chipKIT uC32 for the device and the board is configured as COM4, but this might be different for your device.

    Leave the other settings as-is.

    Now that you have initialized the device, you need to configure Digital IO Pin 2 of the chipKIT device to be an

    output pin so that it can turn the LED on and off. Right-click the block diagram and navigate to chipKIT Low

    Level Set Digital Pin Mode. If you hover close enough to the Init function on the block diagram while the

    cursor has the function in-hand, LabVIEW will auto-wire the two functions together.

    Right-clicking the input or output terminal of a function will automatically create and input

    control/constant or indicator with the correct data type. This is a recommended practice.

  • Getting Started with the LabVIEW Interface for chipKITTM

    If you hover over the new function you can see that it has an input for Digital IO Pin and the Pin Mode,

    provided that you have Context Help turned on. Right-click each one of the input terminals, and select Create

    Constant for each. For the Digital IO Pin constant, enter 13 for the value. For the Pin Mode constant, select

    Output. After you create both, press to automatically clean-up the block diagram code.

    The next step is to actually write a value to the digital pin to be output from the chipKIT device now that we

    have initialized the device and set the digital pin mode. To do this, place a Digital Write Pin function, which is

    also located in the chipKIT Low Level functions palette. You will need to branch off of the wire for the

    existing Digital IO Pin constant that is connected to the Set Digital Pin Mode function and connect this to the

    Digital IO Pin input terminal of the Digital Write Pin.

    If you look at the context help for the Set Digital Pin Mode function, you will notice that the Pin

    Mode input terminal is not bold, and it has (Input) after it. This means that it is not a required

    input and LabVIEW will still run/execute without anything connected to it. And if you do not

    connect anything to it as an input, it will use the value inside the parenthesis as the default

    value. In this case, the default selection is to configure the pin as Input

    To easily access the parent class functions palette of a placed function, just right-click the

    function and then you will see the parent class in the shortcut menu. Just navigate to the

    function you want from there.

  • Getting Started with the LabVIEW Interface for chipKITTM

    Is anything missing for this function? Thats right! You want to be able to control the value that is sent out to

    the LED by the chipKIT device. So you need to right-click the Value input terminal and select Create Control.

    This will place a control on the front panel that allows the user to control the value.

    Double-click on the Value control that you just created to navigate to the front panel to see the control. A

    FALSE will be a digital low, or OFF, and a TRUE will be a digital high, or ON. This is known as a

    Boolean control. By default, it is using a simple LED control. But wouldnt it be better for a user to interface

    with the control if it were a button?

    Right-click the LED on the front panel and select Replace Silver Boolean Blank Button. Now right-click the

    button and select Properties. In the Appearance tab, select Show Boolean Text and then select Multiple

    strings. Now enter ON for the On text and OFF for the Off text and then select OK to exit.

    Now click the Value button to toggle it on and off to see how it appears to the user.

  • Getting Started with the LabVIEW Interface for chipKITTM

    The final function in the chipKIT API is to close and clear the reference. Right-click the block diagram and

    select chipKIT Close. This completes the programming paradigm for the chipKIT device, and technically, the

    VI is ready-to-run right now.

    Go ahead and press the Run button or navigate to Operate Run VI. This will execute the code a single

    time and send the value that you had entered (before running) into the Value control to the digital pin, and

    ultimately the LED. Try setting this value to ON and then run again. You should see the LED turn on and

    stay on until you set the value back to OFF.

    Running Continuously You have now written a VI to pass a single value out to the digital pin, one time. But what if you want to be

    able to toggle the value without pressing the Run button each time? Well with just a quick addition of a while

    loop to your VI, you can do just that!

    Return to the block diagram and make some room around the Digital Write Pin function and control. This is

    because you will next draw a loop around this section of code only, as it is just the writing of the digital value

    that needs to be repeated once the device is initialized and configured.

    A while loop is an execution structure that will continue to iterate a section of code until a

    condition is met. A typical condition to be met is: Exit while loop if Stop button is pressed. This is

    accomplished by creating a Boolean control in the form of a Stop button on the front panel.

  • Getting Started with the LabVIEW Interface for chipKITTM

    To create a while loop, right-click the block diagram and select Programming Structures While Loop (If you

    cannot see the Programming palette, click on the at the bottom of the functions palette). This will convert

    your mouse cursor into a loop-drawing tool. Left-click-and-hold above the upper-left corner of the Value control

    and then drag until you have encompassed the Digital Write Pin function.

    Release and you will have successfully created a while loop. But notice that a Loop Condition terminal has

    been created in the lower right-hand corner of the loop.

    Also notice that the Run button is now broken . This means that the VI is not executable. Click-on the

    broken run arrow to list the errors. You will see that the error is the fact that conditional terminal of the while

    loop is not wired. You can select Show Error and LabVIEW will highlight the problem area.

    You need to create and connect a Stop button to remedy this error. Can you guess how to create a Stop

    button control for this terminal? Thats right, just right-click the terminal and select Create Control. Return to

    the front panel to view the Stop button. You might consider rearranging your front panel to make is easier.

  • Getting Started with the LabVIEW Interface for chipKITTM

    Now run the VI and notice that you can toggle the value between ON and OFF of the Value control and

    notice that you can see the LED turn on and off. To stop the VI, just press the Stop button.

    A final best practice is handle any errors that might be encountered and present this to the user. This is done

    using the Simple Error Handler. Right-click the block diagram and select Programming Dialog & User

    Interface Simple Error Handler and place it immediately to the right of the Close function. Connect the Error

    Out of the Close function to the Error In of the Simple Error Handler.

    Besides handling the error, you will also want to exit the while loop if an error is detected, such as the board

    not being detected by the VI if it gets disconnected from your computer.

    To realize this, you will need to place an Unbundle by Name function to extract the Error Status Boolean value

    from the error cluster. Right-click the block diagram to bring up the functions palette and click on the Search

    button in the upper right-hand corner. Type unbundle in this search field and double-click Unbundle

    by Name from the results. Place the function just below and to the right of the Write Digital Line function

    within the while loop.

    By default LabVIEW will select the Status item from the cluster, but you can left-click on the function to view

    the other items within, but leave the selection as Status.

  • Getting Started with the LabVIEW Interface for chipKITTM

    The next step is to create an OR function that will exit the loop if the stop button is pressed or if an error is

    detected. Delete the wire that connects the Stop button to the conditional terminal.

    Right-click the block diagram and select Programming Boolean OR and place this function to the left of the

    conditional terminal and wire the output of the OR function into the input of the conditional terminal. Now wire

    the output of the Unbundle by name and Stop button into the inputs of the OR function.

    Bonus exercise Auto-blinking LED

    Lets say that perhaps you want the LED to cycle on and off automatically. Can LabVIEW do that? LabVIEW

    can do that, in fact, in several ways it can do that. We will use a very simple way to accomplish the task of

    toggling a LED on and off, automatically.

    First, lets walk through how to get the value to change from one iteration to the next iteration. As it stands,

    the program will just keep passing the value that is entered into the Value control on the front panel until it is

    changed by the user. But instead, we want it to go from ON to OFF and then back to ON and so on,

    When you delete a control, indicator, constant, or function from your code, it will leave a broken

    wire. It can be tough to select and delete each wire. To easily clean up all broken wires,

    just press and let LabVIEW remove them for you!

  • Getting Started with the LabVIEW Interface for chipKITTM

    until we press the stop button. If you were to graph the state of the LED in comparison to time, it would look

    like this.

    Make more room within the while loop and delete the Value control as we are now going to programmatically

    determine the value of the output pin. You can hold while left-clicking and dragging a box to create

    more space within the loop. Give it a try.

    Now right-click on the block diagram and select Programming Numeric Quotient & Remainder. Remember

    that you can turn on context help and hover over a function to find out more about it.

    Place this in the white space that you just created. Now wire the loop iteration terminal to the x input of

    the Quotient & Remainder function. Right-click the y input terminal and select Create Constant and enter 2

    for the value.

    Now place an Equal to 0? function by right-clicking the block diagram and selecting Programming

    Comparison Equal to 0? Then wire the x = 0? output terminal of this function into the Value input of the

    Digital Write Pin function.

  • Getting Started with the LabVIEW Interface for chipKITTM

    The end result of this function will divide the loop iteration value, which increments by one each iteration, by

    two and then compare the number to see if it is equal to zero before outputting the value to the digital of the

    chipKIT device. The only possible values are TRUE or FALSE for this and it will change each iteration.

    So go ahead and run the VI and observe the result. It might seem like the LED never turns off but is only

    about half as bright. That is because the loop is running very fast and is functioning like Pulse Width

    Modulation (PWM), but it is not using a hardware timing engine; instead, it is using the software clock of your

    computer to time each loop iteration.

    In order to slow down the execution of the loop, we need to put a Wait function within the loop. Right-click

    the block diagram and navigate to Programming Timing Wait (ms). Place this within the while loop and right-

    click the input terminal and create a constant with a value of 250 for 250 milliseconds; this is equivalent to 4

    Hz, or 4 cycles per second.

  • Getting Started with the LabVIEW Interface for chipKITTM

    The Wait (ms) function has no data dependency on the Digital Write Pin, meaning it can run completely

    parallel of the other code. This will ensure that each loop iteration will take at least 250 milliseconds, but could

    take longer if for some reason the other code in the loop took longer than the specified time to complete.

    Now run the VI and see if you can notice the automatic blinking of the LED. Try changing the Wait (ms)

    constant to a control so that you can toggle the value while it is running.

    Sometimes using software timing can be problematic if you need a deterministic loop rate, or something that

    can cycle faster that the operating system clock (~100Hz). In that case, try using the PWM functionality of the

    device to achieve more deterministic results.

    Next Steps

    There are examples for using PWM, along with many other functions that install with the device API. Within

    LabVIEW, navigate to Help Find Examples Search for chipKIT to display the available examples.

    Join the LabVIEW Interface for chipKIT community to see more examples and showcase your project.

    LabVIEW is by nature a parallel programming language. This means that it automatically multi-

    thread and splits tasks onto different cores and processors if there is no data dependency. And

    the best part is that you do not have to write any multi-threading code, it is done for you!

    OverviewRequired Hardware ComponentsRequired Software ComponentsLabVIEW ProgrammingRunning Continuously/Bonus exercise Auto-blinking LEDNext Steps