Vous êtes sur la page 1sur 13

LabVIEW Interface for chipKIT

Getting Started Blink LED
This document will explain how to use LabVIEW and chipKIT
to blin a LE!
Required Hardware Components
" # chipKIT
protot$pin% platform and U&' cable
" # 22() or 33() resistor *optional+
" # basic LE! *optional+
" # basic solder,less breadboard *optional+
-'$ default. the chipKIT de/ice will output to LE!0 of the de/ice. which is connected to pin "31 &o it is not
necessar$ to ha/e an external LE! to complete this tutorial1
Required Software Components
, LabVIEW 2((2 or newer
, 3I VI&4
, 5KI VI 6aca%e Mana%er
, LabVIEW Interface for chipKIT
Note: refer to 7ettin% &tarted # Installin% &oftware for more information
Breadboard Connection Diagram Circuit Schematic
Getting Started with the LabVIEW Interface for chipKIT
Lab!"# $rogramming
8pen LabVIEW 2("2 b$ na/i%atin% to Start% &'' $rograms% Nationa' !nstruments% Lab!"# ()*(1 9ou will
see a screen similar to the followin% while LabVIEW is loadin%1
8nce loaded. $ou are read$ to create a Virtual Instrument *VI+1 &elect +i'e% New !1
9ou are now read$ to be%in pro%rammin% in LabVIEW1 4 LabVIEW VI consists of a front panel and a bloc
dia%ram1 The front panel is the %re$ window contains the %raphical user interface *7UI+1 The bloc dia%ram is
the white window and contains the %raphical pro%rammin% code1 9ou can na/i%ate between the two b$
selectin% #indow% Show +ront $ane',B'oc- Diagram or b$ simpl$ pressin% .Ctr' / "01 8r if $ou lie to ha/e
each side b$ side. $ou can tile the two windows b$ pressin% .Ctr' / 1 01
Getting Started with the LabVIEW Interface for chipKIT
Let:s start on the bloc dia%ram and focus on the code to control the chipKIT to be%in1 ;irst. an 4pplication
6ro%rammin% Interface *46I+. is a collection<librar$ of VIs created to communicate with a piece of hardware1
The 46I written for LabVIEW to control the chipKIT de/ice is similar in nature to all hardware 46Is in LabVIEW1
It follows the same 8pen<=onfi%ure> ?ead<Write> =lose<=lear paradi%m that powerful industr$ tools le/era%e
such as 3I !4@ de/ices. 3I 6AI ?; de/ices. and 3I Modular Instruments. so $our sills will easil$ scale into
To open and confi%ure the chipKIT Uno32 de/ice. ri%ht,clic the bloc dia%ram to brin% up the functions palette
and then na/i%ate to chip2!1% !nit1 This is the InitialiBe function1 &elect the function and then left,clic to place
it onto $our bloc dia%ram1 3ow press .Ctr' / H0 and ho/er o/er the newl$ placed function with $our mouse1
This is context help and it will tell $ou more about the function. such as what the inputs and outputs are1
Getting Started with the LabVIEW Interface for chipKIT
The !nit function is confi%ured to auto,detect $our chipKIT de/ice. but $ou can ho/er o/er the !S& resource
input terminal and ri%ht,clic this terminal and select Create% Contro'1 This will create a cluster control on the
front panel that $ou can confi%ure to select $our =hipKIT de/ice and =8M port1 ;or this tutorial we will specif$
chip2!1 uC3( for the de/ice and the board is confi%ured as CO45. but this mi%ht be different for $our de/ice1
Lea/e the other settin%s as,is1
3ow that $ou ha/e initialiBed the de/ice. $ou need to confi%ure !i%ital I8 6in 2 of the chipKIT de/ice to be an
output pin so that it can turn the LE! on and off1 ?i%ht,clic the bloc dia%ram and na/i%ate to chip2!1% Low
Leve'% Set Digita' $in 4ode1 If $ou ho/er close enou%h to the !nit function on the bloc dia%ram while the
cursor has the function in,hand. LabVIEW will auto,wire the two functions to%ether1
Getting Started with the LabVIEW Interface for chipKIT
?i%ht,clicin% the input or output terminal of a function will automaticall$ create and input
control<constant or indicator with the correct data t$pe1 This is a recommended practice1
If $ou ho/er o/er the new function $ou can see that it has an input for Digita' !O $in and the $in 4ode.
pro/ided that $ou ha/e =ontext Celp turned on1 ?i%ht,clic each one of the input terminals. and select Create%
Constant for each1 ;or the Digita' !O $in constant. enter D"3E for the /alue1 ;or the $in 4ode constant. select
D8utput1E 4fter $ou create both. press .Ctr' / 60 to automaticall$ clean,up the bloc dia%ram code1
The next step is to actuall$ write a /alue to the di%ital pin to be output from the chipKIT de/ice now that we
ha/e initialiBed the de/ice and set the di%ital pin mode1 To do this. place a Digita' #rite $in function. which is
also located in the chip2!1% Low Leve' functions palette1 9ou will need to branch off of the wire for the
existin% Digita' !O $in constant that is connected to the Set Digita' $in 4ode function and connect this to the
Digita' !O $in input terminal of the Digita' #rite $in1
Getting Started with the LabVIEW Interface for chipKIT
To easil$ access the parent class functions palette of a placed function. Fust ri%ht,clic the
function and then $ou will see the parent class in the shortcut menu1 5ust na/i%ate to the function
$ou want from there1
If $ou loo at the context help for the Set Digita' $in 4ode function. $ou will notice that the $in
4ode input terminal is not bo'd. and it has *Input+ after it1 This means that it is not a GreHuired:
input and LabVIEW will still run<execute without an$thin% connected to it1 4nd if $ou do not
connect an$thin% to it as an input. it will use the /alue inside the parenthesis as the default /alue1
In this case. the default selection is to confi%ure the pin as DInputE
Is an$thin% missin% for this functionI That:s ri%htJ 9ou want to be able to control the /alue that is sent out to the
LE! b$ the chipKIT de/ice1 &o $ou need to ri%ht,clic the a'ue input terminal and select Create% Contro'1
This will place a control on the front panel that allows the user to control the /alue1
!ouble,clic on the a'ue control that $ou Fust created to na/i%ate to the front panel to see the control1 4
D;4L&EE will be a di%ital low. or D8;;.E and a DT?UEE will be a di%ital hi%h. or D831E This is nown as a 'oolean
control1 '$ default. it is usin% a simple LE! control1 'ut wouldn:t it be better for a user to interface with the
control if it were a buttonI
?i%ht,clic the LE! on the front panel and select Rep'ace% Si'ver% Boo'ean% B'an- Button1 3ow ri%ht,clic
the button and select $roperties7 In the &ppearance tab. select D&how 'oolean TextE and then select
DMultiple strin%s1E 3ow enter ON for the D8n textE and O++ for the D8ff textE and then select O2 to exit1
3ow clic the a'ue button to to%%le it on and off to see how it appears to the user1
The final function in the chipKIT 46I is to close and clear the reference1 ?i%ht,clic the bloc dia%ram and
select chip2!1% C'ose1 This completes the pro%rammin% paradi%m for the chipKIT de/ice. and technicall$. the
VI is read$,to,run ri%ht now1
Getting Started with the LabVIEW Interface for chipKIT
7o ahead and press the Run button or na/i%ate to Operate% Run !1 This will execute the code a sin%le
time and send the /alue that $ou had entered *before runnin%+ into the a'ue control to the di%ital pin. and
ultimatel$ the LE!1 Tr$ settin% this /alue to D83E and then run a%ain1 9ou should see the LE! turn on and sta$
on until $ou set the /alue bac to D8;;1E
Running Continuous'8
9ou ha/e now written a VI to pass a sin%le /alue out to the di%ital pin. one time1 'ut what if $ou want to be able
to to%%le the /alue without pressin% the ?un button each timeI Well with Fust a Huic addition of a while loop to
$our VI. $ou can do Fust thatJ
?eturn to the bloc dia%ram and mae some room around the Digita' #rite $in function and control1 This is
because $ou will next draw a loop around this section of code onl$. as it is Fust the writin% of the di%ital /alue
that needs to be repeated once the de/ice is initialiBed and confi%ured1
To create a while loop. ri%ht,clic the bloc dia%ram and select $rogramming% Structures% #hi'e Loop *If
$ou cannot see the $rogramming palette. clic on the at the bottom of the functions palette+1 This will
con/ert $our mouse cursor into a loop,drawin% tool1 Left,clic,and,hold abo/e the upper,left corner of the
a'ue control and then dra% until $ou ha/e encompassed the Digita' #rite $in function1
Getting Started with the LabVIEW Interface for chipKIT
4 while loop is an execution structure that will continue to iterate a section of code until a
condition is met1 4 t$pical condition to be met isK Exit while loop if Stop button is pressed1 This is
accomplished b$ creatin% a 'oolean control in the form of a &top button on the front panel1
?elease and $ou will ha/e successfull$ created a while loop1 'ut notice that a Loop Condition terminal has
been created in the lower ri%ht,hand corner of the loop1
4lso notice that the Run button is now broen 1 This means that the VI is not executable1 =lic,on the
broen run arrow to list the errors1 9ou will see that the error is the fact that conditional terminal of the while
loop is not wired1 9ou can select Show "rror and LabVIEW will hi%hli%ht the problem area1
9ou need to create and connect a &top button to remed$ this error1 =an $ou %uess how to create a &top button
control for this terminalI That:s ri%ht. Fust ri%ht,clic the terminal and select Create% Contro'1 ?eturn to the
front panel to /iew the &top button1 9ou mi%ht consider rearran%in% $our front panel to mae is easier1
Getting Started with the LabVIEW Interface for chipKIT
3ow run the VI and notice that $ou can to%%le the /alue between D83E and D8;;E of the a'ue control and
notice that $ou can see the LE! turn on and off1 To stop the VI. Fust press the Stop button1
4 final best practice is handle an$ errors that mi%ht be encountered and present this to the user1 This is done
usin% the Simp'e "rror Hand'er7 ?i%ht,clic the bloc dia%ram and select $rogramming% Dia'og 9 6ser
!nterface% Simp'e "rror Hand'er and place it immediatel$ to the ri%ht of the C'ose function1 =onnect the
"rror Out of the C'ose function to the "rror !n of the Simp'e "rror Hand'er1
'esides handlin% the error. $ou will also want to exit the while loop if an error is detected. such as the board
not bein% detected b$ the VI if it %ets disconnected from $our computer1
To realiBe this. $ou will need to place an 6nbund'e b8 Name function to extract the "rror Status 'oolean
/alue from the error cluster1 ?i%ht,clic the bloc dia%ram to brin% up the functions palette and clic on the
Search button in the upper ri%ht,hand corner1 T$pe DunbundleE in this search field and double,clic
6nbund'e b8 Name from the results1 6lace the function Fust below and to the ri%ht of the #rite Digita' Line
function within the while loop1
'$ default LabVIEW will select the Status item from the cluster. but $ou can left,clic on the function to /iew
the other items within. but lea/e the selection as Status1
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
detected1 !elete the wire that connects the Stop button to the conditional terminal1
Getting Started with the LabVIEW Interface for chipKIT
?i%ht,clic the bloc dia%ram and select $rogramming% Boo'ean% 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 terminal1 3ow wire
the output of the 6nbund'e b8 name and Stop button into the inputs of the OR function1
Bonus e:ercise ; &uto<b'in-ing L"D
Let:s sa$ that perhaps $ou want the LE! to c$cle on and off automaticall$1 =an LabVIEW do thatI LabVIEW
can do that. in fact. in se/eral wa$s it can do that1 We will use a /er$ simple wa$ to accomplish the tas of
to%%lin% a LE! on and off. automaticall$1
;irst. let:s wal throu%h how to %et the /alue to chan%e from one iteration to the next iteration1 4s it stands. the
pro%ram will Fust eep passin% the /alue that is entered into the a'ue control on the front panel until it is
chan%ed b$ the user1 'ut instead. we want it to %o from D83E to D8;;E and then bac to D83E and so on. until
we press the stop button1 If $ou were to %raph the state of the LE! in comparison to time. it would loo lie
Getting Started with the LabVIEW Interface for chipKIT
When $ou delete a control. indicator. constant. or function from $our code. it will lea/e a broen
wire1 It can be tou%h to select and delete each wire1 To easil$ clean up all broen wires. Fust
press .Ctr' / B0 and let LabVIEW remo/e them for $ouJ
Mae more room within the while loop and delete the a'ue control as we are now %oin% to pro%rammaticall$
determine the /alue of the output pin1 9ou can hold .Ctr'0 while left,clicin% and dra%%in% a box to create
more space within the loop1 7i/e it a tr$1
3ow ri%ht,clic on the bloc dia%ram and select $rogramming% Numeric% =uotient 9 Remainder1
?emember that $ou can turn on context help and ho/er o/er a function to find out more about it1
6lace this in the white space that $ou Fust created1 3ow wire the 'oop iteration terminal to the : input of the
=uotient 9 Remainder function1 ?i%ht,clic the 8 input terminal and select Create% Constant and enter D2E
for the /alue1
3ow place an "qua' to )> function b$ ri%ht,clicin% the bloc dia%ram and selectin% $rogramming%
Comparison% "qua' to )> Then wire the : ? )> output terminal of this function into the a'ue input of the
Digita' #rite $in function1
The end result of this function will di/ide the loop iteration /alue. which increments b$ one each iteration. b$
two and then compare the number to see if it is eHual to Bero before outputtin% the /alue to the di%ital of the
chipKIT de/ice1 The onl$ possible /alues are DT?UEE or D;4L&EE for this and it will chan%e each iteration1
Getting Started with the LabVIEW Interface for chipKIT
&o %o ahead and run the VI and obser/e the result1 It mi%ht seem lie the LE! ne/er turns off but is onl$ about
half as bri%ht1 That is because the loop is runnin% /er$ fast and is functionin% lie 6ulse Width Modulation
*6WM+. but it is not usin% a hardware timin% en%ineL instead. it is usin% the software cloc of $our computer to
time each loop iteration1
In order to slow down the execution of the loop. we need to put a #ait function within the loop1 ?i%ht,clic the
bloc dia%ram and na/i%ate to $rogramming% 1iming% #ait @msA1 6lace this within the while loop and ri%ht,
clic the input terminal and create a constant with a /alue of D2M(E for 2M( millisecondsL this is eHui/alent to 0
CB. or 0 c$cles per second1
The #ait @msA function has no data dependenc$ on the Digita' #rite $in. meanin% it can run completel$
parallel of the other code1 This will ensure that each loop iteration will tae at least 2M( milliseconds. but could
tae lon%er if for some reason the other code in the loop too lon%er than the specified time to complete1
3ow run the VI and see if $ou can notice the automatic blinin% of the LE!1 Tr$ chan%in% the #ait @msA
constant to a control so that $ou can to%%le the /alue while it is runnin%1
&ometimes usin% software timin% can be problematic if $ou need a deterministic loop rate. or somethin% that
can c$cle faster that the operatin% s$stem cloc *N"((CB+1 In that case. tr$ usin% the 6WM functionalit$ of the
de/ice to achie/e more deterministic results1
Getting Started with the LabVIEW Interface for chipKIT
LabVIEW is b$ nature a parallel pro%rammin% lan%ua%e1 This means that it automaticall$ multi,
thread and splits tass onto different cores and processors if there is no data dependenc$1 4nd
the best part is that $ou do not ha/e to write an$ multi,threadin% code. it is done for $ouJ
Ne:t Steps
There are examples for usin% 6WM. alon% with man$ other functions that install with the de/ice 46I1 Within
LabVIEW. na/i%ate to He'p% +ind ":amp'es% Search for DchipKITE to displa$ the a/ailable examples1
5oin the LabVIEW Interface for chipKIT communit$ to see more examples and showcase $our proFect1
Getting Started with the LabVIEW Interface for chipKIT