Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 130 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
130
Dung lượng
2,21 MB
Nội dung
MID-RANGE DEVICES 883 Get154Table: retlw 0x0E0 ; Minutes 0 – 15 retlw 0x0D0 ; Minutes 16 – 31 retlw 0x0B0 ; Minutes 32 – 47 retlw 0x070 ; Minutes 48 – 59 This code takes a maximum of 34 instruction cycles to execute. This code, with the code presented above, means that the vast majority of time the PIC microcontroller spends processing in this application is spent polling the T0IF flag instead of calculat- ing the new LED’s display value. This is important to avoid any possibility that the T0IF flag being set is missed (and the time is lost). xmas: CHRISTMAS DECORATION WITH BLINKING LIGHT AND MUSIC If you look through the hobbyist electronics magazines in December, you are sure to find a number of flashing-light PIC microcontroller applications. These appli- cations usually use a number of LEDs to flash on and off in a seemingly random pattern. These lights are often put in a Christmas tree decorations or a stand-alone decoration, as shown in the X’mas project (Fig. 21.16), which has 15 flashing LEDs and can play a tune. This is the third time I’ve created a lighted Christmas tree with a PIC microcontroller. In the first case, I hand-wired all the connections (which made it truly horrific to wire). Figure 21.16 The Christmas tree decoration with stickers for ornaments. Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com 884 PROJECTS The second version used a predesigned PCB card, but the design was lost in a hard file crash (before I started religiously backing up files onto CD-ROM), and the software was written for an early PIC microcontroller compiler that never worked extremely well. I ended up updating it just for the light application (and the tune generator that went with it). Also with the second version, I created an unnecessarily complex tune generator that requires two separate PC applications to run before the code could be built with the tune installed. This third version avoids the problems of the other two, and by using LEDs with leads soldered to them, the build time for the entire project is just a few hours (not includ- ing decoration paint drying time). The application supports up to 16 LED outputs, a speaker with software that can drive a 128-note tune, and a built-in power supply, so the application can be run from a wall-mounted ac/dc adapter. The schematic is shown in Fig. 21.17. The bill of materials is listed in Table 21.7. There are a couple of things to notice in this application. The first is that there is no on/off switch for power. I left this off because I assumed that once power was applied, the decoration would run continuously. Next, I do not interconnect the 74LS374s, which are used as shift registers, because there are ample pins available for the application, and randomizing can be done in software. Lastly, I used a 3.58-MHz ceramic resonator instead of my typical 4.0-MHz one because I ran out of 4-MHz ceramic resonators. This change did not result in a major problem with timing the application, as will be discussed below. The macro calculator was used for calculating delays, and a 4-MHz ceramic res- onator or crystal could be substituted in its place. The software used to run the application could be considered to be a simple multitasker with two “processes.” The critical process is the tune process, which plays a tune pro- grammed into a tape. The other process is a pseudorandom number generator using as a linear feedback shift register that runs in the foreground. The application can be modeled as main ( ) // Xmas Tree high level code { int TuneDelay = 1; int TuneIndex = 0; int Data = 0x0FFFF; PORTA = output; // Set Up Outputs OPTION = TMRO Instruction Clock | TMRO Prescaler; // Set Up Timer INTCON = GIE | TOIE; // Enable Timer Interrupt While (1 = = 1) { // Loop Forever Updating LEDs dlay(); // Delay 1 second Data = (data << 1) + (Data.15 ^ Data.12); // Create Pseudo Random Number Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com 885 Figure 21.17 Christmas decoration schematic. Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com 886 PROJECTS ShiftOut(Data); // and Shift it out to the LEDs } } // End Xmas Mainline Interrupt TMROInt() // Timer0 Interrupt Handler { TMRO = Note(TuneIndex); // Reset TMR0 to Delay Again TOIF = 0; TuneDelay = TuneDelay – 1; if (TuneDelay = = 0) { 1 // If Tune Delay Finished, Output a TuneIndex = TuneIndex – 1;// New Note if Note(TuneIndex) == 0) // At End of the Tune? Tune index = 0; // Yes, Reset and Start Again TABLE 21.7 XMAS BILL OF MATERIALS REFERENCE DESIGNATOR DESCRIPTION U1 PIC16F84–P/04 U2 7805 U3–U4 74LS374 Y1 3.58-MHz ceramic resonator with built-in capacitors D1 1N4001 silicon diode D2–D17 Multicolored LEDs with leads soldered on C1 10-F, 16-V electrolytic C2, C4–C6 0.1-F, 16=V tantalum C3 0.47 F, any type R1 10 k⍀, 1 / 4 W R2–R19 220 ⍀, 1 / 4 W S1 Single pole, single throw S1 Piezo speaker J1 2.5-mm power connector Misc PCB board, wall-mounted 8-V, 500-mA ac/dc power converter Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com MID-RANGE DEVICES 887 TuneDelay = Delay(TuneIndex); } else // Toggle Note if (Note(TuneIndex) != Pause) SpkrOutput = SpkrOutput ^ 1; } // End TMR0 int. The table (listed as the “Note” and “Delay” functions in the code above) contains the tune with notes and duration programmed in using the note add macro. This macro will allow development of the code needed for a tune very quickly—just transcribe the tune into the note range described below, and program a PIC microcontroller. After getting a PCB built, you will be able to solder the application together in less than an hour. The PCB design is shown in Figs. 21.18 through 21.20. For my application, I created the Christmas tree shown in Fig. 21.16. The PCB has two 0.125-in holes drilled in it for your use in mounting it to a display. When I mounted my board to the Christmas tree, I simply screwed it to the back of the tree using small wood screws. This is shown in Fig. 21.21. When I assembled this project, T225A Overlay Figure 21.18 Silkscreen for component placement on the Christmas decoration. Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com 888 PROJECTS T225A Top Layer Figure 21.19 The topside PCB traces for the Christmas decoration. T225A Bottom Layer Figure 21.20 The bottom PCB traces for the Christmas decoration. Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com MID-RANGE DEVICES 889 I tried to keep the LED wires away from the PCB to allow removal of the PIC micro- controller for reprogramming, as well as allowing me to fix any potential problems (there weren’t any). For previous Christmas tree projects, I laboriously soldered leads onto LEDs and then glued them into holes on the tree. For this project, I was able to find LEDs that were presoldered onto leads and glued into panel-mount plastic carriers. These parts are a bit more expensive than buying LEDs and wires, but the time saved made the project quite enjoyable rather than a particularly odious chore. For my Christmas tree, I used a piece of scrap pressboard cut into an equilateral triangle 8 in on a side and a 3 / 4 -in piece of wood for the stand, as shown in Fig. 21.22. I painted the triangle forest green and the base and stand gold. To assemble the tree, I simply glued the triangle to the stand and screwed the stand to the base. After the glue and paint had dried, I inserted the LEDs into the holes and soldered their leads to the PCB. Total time working on the tree was about 2 hours, with a total elapsed time of 3 days (mostly for paint and glue to dry). When the tree was completed, it seemed to be a bit bare. To help dress it up, I bought a few sheets of small sparkly stickers that had various toys and Christmas decorations and gave them and the tree to my 4 1 / 2 -year-old daughter to decorate. This turned out to be a lot of fun for her. As a bonus, if enamel paint is used with a smooth surface for the tree triangle, the stickers can be peeled off easily and the tree redecorated the next year. Creating tunes is a relatively simple chore. Once you have chosen the song you would like the application to play, you will have to transpose it into the notes provided in Table 21.8. Figure 21.21 The rear of the Christmas decora- tion showing the PCB and the wiring. Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com 890 PROJECTS TABLE 21.8 XMAS NOTE TABLE NOTE LABEL FREQUENCY, HZ COMMENTS B lnB 494 B below middle C C nC 523 Middle C C# nCS 554 D nD 587 D# nDS 622 E nE 659 F nF 699 F# nFS 740 G nG 784 G# nGS 831 A nA 880 A# nAS 923 B nB 988 C hnC 1047 Octave above middle C C# hnCS 1109 D hnD 1175 D# hnDS 1245 E hnE 1319 Pause nP N/A Pause a sixteenth beat Completed Tree Tree Base Stand 8" on Each Side 6" Wide 3" Deep 30 Deg. 3/4" Thick 1/4" Pressboard 3/4" Wide 3/4" Deep 6" High Drill 15 1/4" Holes Randomly in “Tree” Paint: Tree - Leaf Green Base & Stand - Gold Figure 21.22 The wood pieces for the Christmas decoration. Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com MID-RANGE DEVICES 891 When specifying the time for the note, the delay is given in 16 beats. To specify a tune, the notes are specified along with the number of sixteenth beats. For example, the first line of “Oh Christmas Tree,” which is “Oh Christ-mas tree, Christ-mas tree, how lovely are . . .” 1/8D 3/16G 1/16G 1/8A 3/16B 1/16B 3/8B 1/8B 1/8A 1/4C . . . is written out as nD,2,nG,3,nG,1,nG,6,nA,2,nB,3,nB,1,nB,6,nB,2,nA,2,nB,2,hnC,4 . . . The application is timed for 72 beats per minute, which allows me to do a pretty good rendition of “O Christmas Tree.” Other songs can be programmed in very easily; to do this, I recommend buying a cheap beginners piano book with Christmas carols (or other songs) and transcribing the notes. This may be surprising, but the most difficult aspect of this application for me was to find and transcribe a version of “O Christmas Tree” that had notes that fell in the proper range for the application. For driving the LEDs seemingly randomly, the pseudorandom formula Data = (data << 1) + (data.15 ^ data.12) shifts the current value up by 1 bit and then fills in the least significant bit with the most significant bit and another XORed together. This seems unbelievably simple (and it is), but it does a very good job of randomizing the data as a very simple linear feedback shift register. The two 74LS374s are wired as synchronous serial shift registers. I use this format for a variety of different applications to shift data serially out to a parallel I/O. To shift the data out, notice that I perform a shift on the data with the carry up from the lower byte being used as the LSB of the upper (the lower bytes LSB is the pseudorandom value discussed earlier). The xmax040.asm application code is in the code/Xmas folder. If you decide to put in your own tune, then change the name of the file so that you don’t overwrite the original application. IRTank: TV IR REMOTE-CONTROL ROBOT In this chapter I present two different robot features that can be controlled by a PIC. This section will show you how to use the PIC microcontroller with a TV remote control to send commands to a tracked vehicle. At the end of this chapter I’ll go through a pro- grammable servo controller. The genesis of this project was an article in Electronics Now. In the preceding proj- ect I made the observation that hobbyist magazines always have Christmas decorations in their November–December issues. On the other hand, you’ll notice a number of dif- ferent robot designs throughout the year. The magazine project presented a simple Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com 892 PROJECTS differentially driven robot that could be controlled by a TV remote control. After look- ing through the article, I decided to build my own robot (Fig. 21.23) because there were a few things that I could improve on. The project in the article used a single infrared (IR) receiver (typically used with TV remote controls) to control the robot. Two motors (for the robot’s left and right sides) were switched on and off by an H bridge motor control made out of discrete transis- tors. The magazine robot used a PIC 16C5x for control. I felt that by using a mid-range part, I would have the advantage of interrupts to handle the incoming commands (which I’ll refer to as data packets). Most (if not all) IR TV remotes use a space-width encoding scheme in which the data bits are embedded in the packet by varying the lengths of certain data levels. This can be seen in Fig. 21.24 from a theoretical perspective and from the practical (oscilloscope output of a TV remote control IR receiver) in Fig. 21.25. The normal signal coming from an IR receiver circuit is high when nothing is coming (line idle) and then goes low with a leader signal to indicate that data is coming in. The data consists of a bit Synch, whose value, when complete, is transmitted as the Figure 21.23 The completed IR-controlled tank. Figure 21.24 The output of a TV remote-control receiver consists of a number of pulses that are sep- arated by different-length spaces that are used to encode data. ” ” ” “ ”“ “ “ ” “ ” “ Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com [...]... simple The first receiver to transmit the leader to the PIC microcontroller is the one that is “listened” to for the remainder of the data packet The application code can be found in the code\IRTank folder The application has been updated from the first edition’s code to use a PIC1 6F84 instead of a PIC1 6C84 Irtank.asm was based on Test9A from the first edition This was the last application in the series... produced these results: 0x09F 0x09D 0x08C 0x09D 0x09D Generally, the results from this program were repeatable about 60 percent of the time This might have been acceptable except for the poor discrimination that this method had For example, the codes for 2 and 3 are 0x 081 and 0x 082 , respectively The problem lies in the fact that the two codes have the same number of ones and zeros The code may pick up the. .. not perfect (ideal)—their values are somewhat off the exact specified values The second is that you probably will see the temperature creep up if the thermistor is close to the PIC microcontroller as the PIC microcontroller warms up from use Both these problems can be overcome by setting the calibration constant in the PIC microcontroller Instead of working with a constant as part of the conversion formula,... the interrupt handler operating With the scrolling speed increased, I wanted to go on and add polling of the two buttons, and when one of the buttons is pressed, another display (either the alphabet or the first 10 numbers) is displayed, and its position could be specified by turning the potentiometer When I originally implemented the button polling, I used the PICBASIC button function and specified the. .. components and wire them on the backside of the board to get everything to work together The final mess of wires is shown in Fig 21.32 A useful strategy to use in the application development process is to make sure that each subsystem in the application is tested before the final application is created The source code for the Thermo application (thermo.asm) can be found in the code\Thermo folder The program... actually restructures the application (reading an IR transmitter) to best fit the PIC microcontroller The data read is now totally 8- bit, as opposed to the 12/16 bits that had to be handled in the original application Thermo: ELECTRONIC THERMOMETER WITH SEVEN SEGMENT LED DISPLAYS One of the most popular PIC microcontroller projects I have ever created is this electronic thermometer The basic model shown... you shut off the power to the PIC microcontroller and then when you come back later, the value will still be there and usable When I was adding the EEPROM code, I encountered the most significant problem in debugging the code Every time I would run the application in hardware, only one digit would be displayed, and only the first digit of the EEPROM data check was written by the application This was very... polling the two buttons and displaying either the alphabet or the numbers 0 through 9, with the potentiometer selecting the initial digit Implementing the initial capabilities is where I first ran into significant problems It was my intention simply to use the interrupt handler code from the original project, put the PICBASIC handler prefix and postfix that I show in the Appendix E, and add some mainline... temperature range The code displays the current temperature in degrees Celsius The thermistor was bought from Radio Shack (Part Number 271-110) This part is a 10-k thermistor with a negative temperature coefficient (NTC) of –3 .85 percent This means that for each degree Celsius the thermistor is raised, the resistance within the thermistor drops by 3 .85 percent The base temperature is 25ЊC, and the thermistor’s... PDF Merge and Split Unregistered Version - http://www.simpopdf.com MID-RANGE DEVICES 905 TABLE 21.12 RESISTANCE VERSUS TEMPERATURE FOR THE RADIO SHACK THERMISTOR TEMPERATURE ACTUAL RESISTANCE 20ЊC 12.079 k 21ЊC 11.631 k 22ЊC 11.200 k 23ЊC 10. 785 k 24ЊC 10. 385 k 25ЊC 10.000 k 26ЊC 9.615 k 27ЊC 9.245 k 28 C 8. 889 k 29ЊC 8. 547 k 30ЊC 8. 2 18 k To drive the LEDs, I control the connection through the common . triangle to the stand and screwed the stand to the base. After the glue and paint had dried, I inserted the LEDs into the holes and soldered their leads to the PCB. Total time working on the tree. equilateral triangle 8 in on a side and a 3 / 4 -in piece of wood for the stand, as shown in Fig. 21.22. I painted the triangle forest green and the base and stand gold. To assemble the tree, I simply glued the. quite simple. The first receiver to transmit the leader to the PIC microcontroller is the one that is “listened” to for the remainder of the data packet. The application code can be found in the codeIRTank