I love hardware work as much as I love working on software. These two are naturally intertwined, and I feel strongly that I need a good understanding of the two to have a complete knowledge of modern computing systems. As well as working with software, I also have a small lab set up on my desk so I can piece together little projects here or there. In this article, I will give a basic rundown of microcontrollers, as well as explain how to get them working on a Linux OS.
Recently, I’ve gained interest in making projects based on PIC microcontrollers, so I purchased the PICKIT1 form Microchip. If you haven’t heard of PIC microcontrolers yet, they’re a specific, and very good type of microcontroller produced by the company Microchip. Like any microcontroller, PIC microcontrollers are self contained computers (with around a 30hz clock and 1-2kb of RAM) that are infinitely useful in tons of applications. Take a look here if you don’t believe me on this one. The PICKIT1 Is a great started kit, and for $40 you can get 2 microcontrollers (the 12f675 and the 16f874), a programmer board to hook up to your computer, and a CD with some good PIC development tools. The problem with the the CD’s tools are that they Windows only, so they are essentially useless to me. Not wanting to have to deal with Windows, I scrounged around the internet to find tools that will allow me to stick with linux for PIC programming. Luckily, (as I expected) there are tools out there for developing on the PIC microcontroller. Piklab is a great IDE for developing, and it is capable of using tons of toolchains for assembling or compiling your code, as well as it has capability of simulating your code without burning it to the chip and then it can write to the chip upon your request. Setting up a compiler toolchain is always a tricky task, but I managed to get SDCC (Small Device C Compiler) up and running [eventually]. Today though, I’m going to clue you in on how to get a basic assembler and burning system running so you can make your first circuit and make use of your first assembly code! Here’s a rundown of how I eventually got everything running on Arch Linux.
- Buy the USB Pickit1 from Microtrend! You can pick it up at Digikey, a great online shop for pretty much any electronics part you could want. For 36 bucks (USD), this programmer a steal. If you are feeling intrepid, you can try getting the USB Pickit2, but I don’t know if it will work, and its a bit more expensive.
- Install the assembler on your computer. You need to get gputils, a collection of software for assembling code for PIC microcontrollers. I had to run
./configure && make && sudo make install
on the untarred source files to get it installed onto my Arch Desktop.
- Install usb-pickit, the programmer interface for Linux. I kinda forgot where I picked this file up, but I did have to patch it to play nice with HAL. Here’s the tarred up source for you. You could probably just be able to run the executable I accidentally left in there, but if that doesn’t work, use the same commands in step one to build it yourself. This must be run as root (or at least thats what I’ve always done, never tried it otherwise…). Note: I only know that this will work with the Pickit1 programmer. If you have a different programmer, you may need to find different software to program the microcontroller.
- Start programming assembly! This topic would make this post ungainly long, but here’s an assembly file I wrote that can make two led’s alternate blinking. Like any other architecture, you’re going to need to learn how assembly for the PIC microcontrollers, but it doesn’t take too long to do. This site will prove helpful in learning PIC assembly.
- Assemble the file! This is easy, just run
This will produce a hex file called file.hex that contains the actual ‘binary’ that the microcontroller will read and run your program.
***Note: gpasm warns you about not being in the right bank when assembling the part that turns the IO pins to output. IMHO this is a retarded warning, but its by no means fatal, so don’t worry.
- Download the hex file into the chip. With the programmer turned on and the 12f675 chip plugged in, run
sudo usb_pickit file.hex
This will ‘burn’ the hex file into the chip (not permanently, don’t worry 🙂 )
- Make the actual circuit! The programmer itself has a few led’s on it, but I never really traced out how exactly the wires snaked to the chip, so I just put the chip on a breadboard and made a circuit. Here’s the diagram. All you need is 2 1k resistors, 2 LEDs , the microcontroller, and a 5V DC source. Here’s the diagram.
Hook it up accordingly, and the two LED’s should blink alternatively! Pat yourself on the back, you’ve done a great job! Plenty of creative hardware projects await, so happy hacking! The freely available datasheets (and errata) from Microtrend are absolutely invaluable as you get further along. There’s still a lot more to learn about MCU’s and about circuits though, but you’ve broken the ice! Congratulations!