Home |
Projects |
|
Forums |
Login |
Sitemap |
|
Blue - my programming language |
Building a Digital Drum SetAfter months of playing RockBand, I've developed a new found respect for drummers. I've played the guitar for decades, so I wasn't too keen on pretending to play songs on a tiny fake guitar. But as it turns out, I love to play the drums. Being able to control each of your limbs independantly is very difficult. The only thing that helps is (big surprise) a lot of practice. I don't own RockBand or a PS3, and I don't think my better-half would appreciate me pounding on drums all night. So I've decided to build myself a digital drum set. This brings me to perhaps the most interesting part of my story. In order to accomplish this project, I've started tinkering with the PIC18F microprocessors. I will chronicle my efforts building my drum set and working with PICs. I am a complete newbie at PICs. So at the end of my odyssey, I will either be a broken man with a lot of electronic crap strewn all over my office, or I will be a little more geeky and a have a whole lot of rhythm. Choosing a ChipStated simply, I want to make a drum that can commmunicate when and how hard someone whacked it. I've been a programmer for a long time, so once that information makes it to the computer, it's no problem. But I've never made a peripheral device before, and it's a little intimidating. I did some research and after talking with a friend who has some experience in the matter, it seemed the best course of action was to use a microprocessor to gather the necessary information and pass it on to the host computer. These are the basic microprocessor requirements:
I found three candidate processor families that looked like they met my needs: They all have tons of features and are incredibly cheap. I'm working on a Linux machine, and from what I read on various web sites, Atmel offers the best Linux support. After reading countless datasheets, I was more confused than ever. Datasheets are fantastic reference material, but they are terrible "getting started" guides. Most of the material out there is written for people that already understand microprocessor basics. But that was not me.
I just decided to pull the trigger and go with the PIC18 (the PIC18F4450 to be exact). After a few quick mouse clicks and some credit card information exchange, I had two PIC18s traveling from half-way around the world destined to be part of my drum set. I can't give a good reason why I decided to go with the PIC18. Each of the manufacturers made a chip that I could use. But I was tired of reading unintelligable datasheets and vascilating from chip to chip. I chose to go with a 40 pin DIP because you can ignore unused input/output pins if you have extra, but you can't get more, if you have to few. Stuff Other than the ChipTinkering with electronics takes a lot of stuff. And it can get pretty pricey. Luckily I have a friend who was into electronics, and he let me borrow a ton of his electronic equiptment. Some of the most important components are:
ATX Power SuppliesYou need a five volt power supply to power the microprocessor. I am using a power supply from an old computer. Some power supplies (ATX) don't seem to turn on when you plug them in. They just sit there like a brick. If there is a green wire in the wire bundle that connected to the motherboard, you may be able to start the power supply. Shorting the green wire with a black wire (ground) should "turn on" the power supply. Be careful. Remember your playing with electricity. Do this at your own risk. If you don't know what I mean when I say an ATX power supply, or you don't know what bundles were heading to the motherboard, you may want to use some batteries. What Programmer to UseHardwareThe microprocessor I chose has a UART and a USB module built into it. I figured I would be able to wire a USB or serial cable to it and simply "upload" the program. Well ... it's not that simple. In order to use your chip you have to be able to push your program onto it. There are tons of programmer schematics on the web for PIC microcontrollers. Some of them use a serial connection, some use a parallel connection, and some use USB. The programmers work in concert with programming software. The software can perform a number of programming related tasks on your microprocessor.
Some software only supports certain types of programmers. And schematics for the same type of programmer are different depending on where you find them online. With no experience or baseline to judge one programmer against another, it was just as confusing as deciding which microprocessor to use. After looking at all the options, I decided to buy a "PICkit 2" programmer from Microchip for $35. I didn't want to get hung up debugging a programmer and never get started on my drum set. I also got a small PIC16 demo board. After I recieved the PICkit 2 and demo board, I was happy with my decision. I ran through the tutorials, and played around quite a bit with the programmer and demo board. It gave me the confidence that my drum project is absolutely do-able, and provided me with a lot of great experience. SoftwareIn Linux I use the "pk2cmd" software to drive the PICkit 2 programmer. You can download the source code at the Microchip website. It builds without any problems. The options are a little confusing at first. But after some trial and error, I figured out the correct sequence of commands to get everything working. Of course once I did that I found "kpk2cmd". It is a GUI front-end for pk2cmd. I recommend installing it. It makes the development cycle a little more user friendly. What Assembler/Compiler to UseI intended to use the open source "sdcc" and "gputils" to create the peripheral software. While I was experimenting with the PIC16 board that I purchased along with my PICkit 2 programmer, I used the "gpasm" assembler from the "gputils" toolchain in linux. It worked great. I assembled all of the tutorials, and did a lot of experimentation. When I moved to the PIC18, I decided to start writing in C rather than assembly code. The "sdcc" compiler doesn't seem to support the PIC18. I decided to use the MPLAB toolchain. This is the compiler, assembler, and linker by Microchip, the makers of the PIC18. It is for Windows, and I run Linux, but Wine makes it easy to run these tools under linux. It's easy to install the MPLAB toolchain using wine. Download the latest demo version from the Microchip website unzip it and run the installer in wine. Make sure the installer adds "MCC18\mpasm" and "MCC18\bin" to the path. I assume the directories are in your path for any of the example commands. You can see if the installation was successful by typing
wine cmd /c mcc18
If you see a mcc18 usage description your installation was a success. Connecting your PIC to the ProgrammerNow we know what programmer, programmer software, and compiler toolchain we are using. The next step is to get the PIC18 hooked up to the programmer. On this page you can download the "PICkit 2 Mictrocontroller Programmer User's Guide". Figure 1-2 (PICkit 2 programmer connector pinout) describes what PIC pins should be connected to each programmer pin. Use the datasheet for your PIC to determine where those pins are located on your specific PIC. Datasheets for PIC18 can be found here. Locate the pin diagram for your chip, and make sure that you are looking at the correct package type. Most PIC18 microcontrollers come in different packages. I bought a 40-pin PDIP (plastic dual inline package) so I look at the 40-pin PDIP pin diagram.
During the course of tinkering with your PIC you will plug and unplug your programmer into your microcontroller many, many times. It slowed me down when I had to plug individual wires into the programmer. I made a connector that kept the programmer wires ordered when the programmer was not plugged in. I have a bunch of old electronics laying around the house. I searched for a small connector that had at least 5 properly spaced holes. I pulled the existing wires out. If you press on the small metal tab with a small screwdriver or paperclip the wires pop right out. I then pushed other wires through so that their stripped portion extended through the existing holes. Then I hot-glued them into place and straightened the wires as best I could. I used a small fingernail file to smooth over the ends of each pin. Make sure you mark one of the wires as wire/pin 1 (note the black Sharpie mark on the connector, and the white triangle on the programmer). Talking to your PIC for the first timeIf you've followed along the PIC18F4450 is now connected to the PICkit 2 programmer. Don't connect anything else to your microprocessor (no power supply, no oscillator, no ... ). The programmer will supply power to the PIC. Plug the PICkit 2 programmer into your Linux box. If you do a "dmesg" you should see a new USB device connected. By default only root can write to the programmer. There are a number of articles online that will describe how to configure your device to make it write-able for other users. But I just use the programmer as root. su to root so you can write to the programmer: su Use pk2cmd to ask your microprocessor to identify itself: pk2cmd -P -I If everything is working properly you should see something like this: Auto-Detect: Found part PIC18F4450. Device ID = 2400 Revision = 0002 Device Name = PIC18F4450 Operation Succeeded My First Hardware BugThe first time I tried to speak with my PIC I got this unwelcome response: Auto-Detect: No known part found. I used a solderless breadboard to connect my PIC to the programmer. It turns out that the PIC was not seated properly into the breadboard. It took me a lot of debugging to figure out this simple fact.
The pins on a DIPs are angled out slightly. This makes it difficult to push the chip into the solderless breadboard. If you gently bend the pins into a 90 degree angle, it's much easier to get the chip into the solderless breadboard. Use a table-top or other flat surface to bend all the pins on one side, then do the other. Be careful and bend them only a little at a time. When the chip is fully seated the body of the chip will rest flat on the solderless breadboard. Once I fixed this problem, the chip identified itself, and I danced around the room with delight.
... To be continued ...
|
|