Anda di halaman 1dari 5

Design Laboratory #2 LED Flash

EGR 100 Due on Friday, September 28 at the beginning of Lab In this project you will work with a partner and Learn to use the Propeller Chip and Program Editor the Propeller Tool Learn the basics of the SPIN programming language Practice building a simple circuit on the breadboard Understand the Propeller Chips clock Turn an LE on and off automaticall! Learn to use pulse"#idth"modulation to control de$ices

Powering the Propeller Chip First you will need to build a circuit to apply power to the Propeller chip. It is very important to do this correctly applying power to the wrong pins on the Propeller chip can burn it out. Unfortunately we do not have spare chips so losses will be a serious detriment to the class. !oth partners should check the wiring carefully. "hen for this first lab ask the instructor or "# check your wiring before connecting the battery.

Figure $. #t left is a drawing of the Propeller chip on its breadboard with power applied. !y convention red wires are used for positive %&' and black wires are used for negative %' which is also called ground. #t right is an e(panded view of the Propeller chip showing )ss %ground battery ' )in %battery &' and input*output %I*+' pins ,-.$.

Testing the Propeller Chip /onnect the U0! cable first to the Propeller chip and then to a P/ computer. +n the P/1s 0tart menu at the bottom left of the screen select Programs*2ngineering*Propeller "ool. "est your Propeller chip and connection by selecting 3un*Identify 4ardware on the top pull-down menu. # bo( will appear saying either 56o Propeller /hip Found7 or 5Propeller chip version $ found on /+8( 7 where the 9(1 is the number of the /+8 port to which your chip is connected. If the identifier bo( does not appear check that the wiring and battery and try again. :ou can also try a different U0! port. B il!ing a LED Cir" it In order to see the Propeller chip work you will need to connect a circuit to one of its I*+ pins. "he ;ight 2mitting <iode %;2<' circuit shown in Fig. = is ideal for this first test. ;2<s are highly efficient light sources commonly used on electronic devices and increasingly for home and commercial lighting as well.

Figure =. #t left is a circuit diagram showing the ;2< circuit with a series resistor. #t right is the same circuit on the breadboard. 6ote that the long leg of the ;2< must be connected to positive %Pin , in this case' and the short leg to ground %here through a resistor'. If the ;2< is connected backwards it will not light. "he ;2< circuit is most easily understood using the circuit diagram in Fig. = %left'. "he ;2< is turned on when Pin , is held at ... ) and off when it is at ground potential %, )'. 6ote that the Propeller has an onboard regulator and operates on a steady ... ) even though the battery voltage is considerably higher %>? )'. "ogether the series ;2< and resistor therefore have ... )olts across them %Pin , at ... ) at one end and ground at , ) at the other end'. 3ecall that the ;2< will develop a voltage of appro(imately =., )olts over a wide range of currents %it does not obey +hm1s law@'. #lso note that when components are in series %as they are here' voltages add up to the source voltage %... )' and current is the same through every element.

Prelab Exercise - alculate the follo!ing" #otal $oltage applied across the circuit %from abo$e&" 'oltage across the LED %from abo$e&" 'oltage across the 22( resistor" urrent through the 22( resistor %use )hm*s La!&" urrent through the LED" urrent through the LED if a +(( resistor !ere used instead" #fter completing the e(ercise above you should be able to see how the siAe of the resistor controls the current running through the ;2<. "he smaller the resistance the larger the current and the brighter the ;2< will be. If the resistor is too small %or if you forget to put it in' the ;2< will become very bright and burn out almost immediately. !e sure you completely understand the ;2< circuit and how it is translated from the circuit diagram %left in Fig. =' into the breadboard layout %right in Fig. ='. "his simple circuit will teach you important skills that you will need to for more comple( projects later in the course. #riting a $P%& Progra' "o make the ;2< flash the Propeller chip needs to command Pin , to change between , ) %;2< off' and ... ) %;2< on' at a regular interval. "his will reBuire that we write a short program %on the P/' and download it onto the Propeller chip. :ou will first use the program e(actly as listed below. ;ater you will modify the program to complete the assignments for this lab. '' '' '' '' '' '' File name: stick_led.spin Description: This program flashes an led connected to pin 0 Author: <your name> Date: <date written>, <dates modified> Notes:

PUB Main dira[0]:=1 repeat outa[0] := 1 waitcnt(clkfreq + cnt) outa[0] := 0 waitcnt(clkfreq*2 + cnt)

' ' '

pin 0 on wait 1 second pin 0 off ' wait 1 second

"he simple program above introduces a number of important ideas in the 0PI6 language. "he first lines are comments and are not actually part of the program they tell us only the name of the program information about the creation of and modifications to the program and briefly what it does. In fact all te(t proceeded by apostrophes %C or CC' is interpreted as a comment. In general comments are used to help you remember what different parts of the

program are doing. #s your code gets more comple( well written comments will become critical to success. "he ne(t line %PU! 8ain' identifies a block of code %called a method function or subrouting' that accomplishes a specific task. In this case there is only one method. It is called 8ain and is a public method %PU!'. "he functioning of the program is contained within the 8ain method. First the direction of Pin , %dira%&'' is set eBual to $. "his establishes that Pin , is an output that will be controlled by the Propeller chip. "he direction of all other pins can be set by simply substituting a different number into the sBuare brackets. 0PI6 uses () to set the value of a variable. In this case setting the direction to $ designates an output while , designates an input %we will use pins as inputs later in the course' 6e(t the repeat statement serves as an endless loop. #ll code properly indented after the repeat is e(ecuted and then repeated forever. If you want to repeat only a fi(ed number of times put a number immediately after the repeat. For e(ample repeat *+ would flash the ;2< e(actly $D times. #s the code is currently written above the ;2< will flash forever. Eithin the repeat loop is the code that turns Pin , on and off. First Pin , is set to the on or $ state using the command outa%&'()*. Ehen this statement e(ecutes the voltage on Pin , will immediately rise from , ) to ... ) and the ;2< will light. 6e(t with the ;2< on the Propeller waits for $ second using the #aitcnt command. "his command waits until the internal clock counter on the Propeller chip reaches the value in the parentheses in this case %clkfre, - cnt'. "he variable cnt is the value of the counter when #aitcnt is first called and clkfre, is the freBuency of the Propeller1s clock. For e(ample if initially cntF= ,,, GH, and clkfre,).&/&&&/&&& %=, 8egahertA or eBuivalently =, ,,, ,,, cycles per second' waitcnt will cause the program to pause until cnt)../&&&/01&. "his will occur $ second after #aitcnt is first e(ecuted. #fter the program has paused for $ second the ;2< is turned off using the command outa%&'()&. Eith the ;2< off the program the waits again %this time for = seconds' using the command #aitcnt2clkfre,3. - cnt4. "he multiplier . causes the program to pause for I, ,,, ,,, clock cycles which reBuires = seconds. It is simple to see how other delays can be achieved. For e(ample #aitcnt2clkfre,5*&& - cnt4 would pause for $*$,,th of a second. 0ince the #aitcnt2clkfre,3. - cnt4 is the last statement in the repeat loop the program immediately returns to the first line of the repeat loop once the wait is finished. "his first line outa%&'()* turns the ;2< back on again. "he process repeats and the ;2< flashes $ second on = seconds off as long as power is applied to the Propeller chip. R nning a $P%& Progra' +nce you have carefully typed in the program above you should save it to your 4 drive folder. Ehen you save the program it will automatically acBuire the .spin e(tension necessary to make it run. "o run the program select 3un*/ompile /urrent*;oad 3#8 from the pull down menu or press the shortcut F$, key. "his will store the program in the Propeller chip1s 3andom #ccess 8emory %3#8' and begin e(ecution. "he ;2< should start flashing almost immediately.

:ou will notice that if power is removed from the Propeller chip the program will cease running and not restart until you download the program again from the P/. # more permanent way to store the program is to select 3un*/ompile /urrent*;oad 22P3+8 or press the F$$ key. "his loads your program into the Propeller1s 2lectrically 2rasable Programmable 3ead +nly 8emory %22P3+8'. Programs in 22P3+8 are not lost on power down. 2very time the Propeller chip is powered up it will begin running any program stored in its 22P3+8. If you want to update or change the program in 22P3+8 simply download another program from the P/. ;ater in the course when you are using the Propeller chip to run e(periments outdoors it will be necessary to store programs permanently in 22P3+8. P lse #i!th (o! lation Pulse-width modulation %PE8' is a simple and powerful control strategy that you will use again later in the course. PE8 allows a simple on-off switch %like Pin ,' to produce a variable output %like dimming an ;2<'. 0imply stated PE8 rapidly turns a load on and off perhaps at $,, or $,,, times per second. "he percentage of time that the load is on %the pulse width' determines how bright we perceive the ;2< to be. For e(ample if the ;2< is on for ?*$,,, of a second and off for $*$,,, of a second it will be on ?,J of the time and will appear Buite bright. #lternatively if the ;2< is turned on for .*$,,, of a second and off for K*$,,, of a second it will be on only .,J of the time and will appear dim. In both cases the cycle perio! %sum of on and off duration' is $*$,, of a second. "he percentage of time that the ;2< is on is known as the ! ty "y"le. "hus while the ;2< will appear brighter or dimmer as the duty cycle is changed its flashing will not be visible to the eye. PE8 is useful for more than controlling ;2<s. It can be used to vary motor speed and power adjust heat output from furnaces and control industrial processes. In this lab/ !ou should e6periment #ith dut! c!cle and period to control LE brightness7 ;ater in the course you will use PE8 to control fans and heating systems in model solar buildings. Lab )ssign'ent "o complete this lab you should do all the e(ercises above and e(periment with the Propeller chip until you thoroughly understand %and know' the material. !e sure to record your observations including une(pected as well as e(pected results. 2(periment %remembering to be careful when powering the chip' and have fun. :ou and your partner should then write a 0PI6 program to vary the duty cycle of an ;2< from ,J to $,,J changing in steps of =,J every second. "hus the seBuence of ,J =,J I,J H,J G,J $,,J should take si( seconds to e(ecute. :our code which you will hand in should be neat contain clear comments and list both partners1 names at the top. E6tra Credit( 8ake the ;2< seBuence repeat continuously by nesting duty-cycle loop%s' within a larger loop that repeats continuously. Lab #rite* p Follow the ;ab 3eport Luidelines posted on the web page. 6ote that you will need to do a more e(tensive write-up for this lab than you did for the car push lab. !e sure to follow the correct guidelines.

Anda mungkin juga menyukai