{"id":1294,"date":"2011-08-09T16:00:01","date_gmt":"2011-08-09T23:00:01","guid":{"rendered":"https:\/\/www.reenigne.org\/blog\/?p=1294"},"modified":"2011-08-08T10:00:37","modified_gmt":"2011-08-08T17:00:37","slug":"a-pic12-programmer-from-an-arduino","status":"publish","type":"post","link":"https:\/\/www.reenigne.org\/blog\/a-pic12-programmer-from-an-arduino\/","title":{"rendered":"A PIC12 programmer from an Arduino"},"content":{"rendered":"<p>In order to program the microcontrollers for <a href=\"https:\/\/www.reenigne.org\/blog\/musical-toy-design\">interval bars<\/a>, I needed a programmer. This is a device which connects to the chip and to a computer, and which allows one to transfer a program from the computer to the chip. You can buy them for $40 or so but I decided to make my own - since I already have an Arduino to act as a low level computer interface, I could make it very cheaply. The only complicated bit is the 13V power supply - I used <a href=\"http:\/\/www.romanblack.com\/smps\/conv.htm\">this<\/a> little boost converter circuit.<\/p>\n<p>The <a href=\"https:\/\/github.com\/reenigne\/reenigne\/tree\/master\/intervals\/programmer\">Arduino program<\/a> is fairly straightforward - it just reads a hex file over the serial serial line, checks the checksums and converts it to the sequence of signals according to the PIC12's programming specifications. It has facilities for reading back the microcontroller's contents and passing them back to the computer as well, and also a way of calibrating the clock rate. I added this not because I accidentally overwrote the OSCCAL and backup OSCCAL values (well, I did, but I had already read them at that point so I knew what they were) but because I wanted to know what the effect of the OSCCAL value was and if the factory programmed one was optimal.<\/p>\n<p>I discovered that it wasn't quite the best (the preprogrammed value of 0x24 gave a within-spec clock speed of 996.5KHz but 0x26 gave a clock speed of 1002.0KHz). I also discovered that (with the one part I tested) the low bit of the OSCCAL was ignored and the top 7 bits interpreted as a signed quantity between -64 and 63 gave an extremely linear relationship to frequencies between 587.1KHz and 1230.1KHz with a resolution of about 5KHz. I also discovered that the frequency stability was very good - a standard deviation of only about 30Hz.<\/p>\n<p>Here's the schematic for the programmer:<\/p>\n<p align=\"center\"><img decoding=\"async\" class=\"centerwide\" src=\"http:\/\/www.reenigne.org\/misc\/pic12_programmer.png\"><\/p>\n<p>To build the programmer, edit the second line of build_programmer.bat to point to your Arduino installation (if it's somewhere other than C:\\Program Files\\Arduino) and run it. To upload it to the Arduino you'll need <a href=\"http:\/\/sourceforge.net\/projects\/winavr\/files\/WinAVR\/\">WinAVR<\/a>, modify upload.bat to point to your WinAVR installation and run it. We can't use the avrdude from the Arduino installation here because it won't toggle the line to reset (that's normally done by the Arduino software).<\/p>\n<p>To use the programmer, connect to it with a serial monitor (like the one in the Arduino IDE). There are several one-character commands:<\/p>\n<ul>\n<li>W - Write program from Arduino to PIC.<\/li>\n<li>O - Write program from Arduino to PIC, including OSCCAL value (necessary if write failed, or if we want to use a different OSCCAL value).<\/li>\n<li>P - Write program from Arduino to PIC, including OSCCAL value and backup OSCCAL value - only use if the backup OSCCAL value got screwed up somehow.<\/li>\n<li>R - Read program from PIC to Arduino.<\/li>\n<li>U - Upload program from PC to Arduino (follow this with the contents of your hex file).<\/li>\n<li>L - Download program from Arduino to PC.<\/li>\n<li>T - Start timing mode.<\/li>\n<li>Q - Stop timing mode.<\/li>\n<li>A - Perform automatic calibration.\n<\/li>\n<li>B - Output a nybble to port B (for debugging purposes.)<\/li>\n<\/ul>\n<p>The returned status codes are as follows:<\/p>\n<ul>\n<li>K - Success.<\/li>\n<li>EF - Error: config fuses didn't verify.<\/li>\n<li>EV - Error: Program, OSCCAL, user IDs or backup OSCCAL didn't verify.<\/li>\n<li>ER - Address in hex file out of range.<\/li>\n<li>EU - Unknown record type in hex file.<\/li>\n<li>EZ - Non-zero byte count in end-of-file marker in hex file.<\/li>\n<li>EC - Hex file checksum failure.<\/li>\n<li>EH - Incorrect hex digit.<\/li>\n<li>E: - colon expected but not received.<\/li>\n<\/ul>\n<p>In automatic calibration mode, the Arduino also sends calibration information as three numbers per line: OSCCAL value (3 digits), frequency in Hz (7 digits) and standard deviation of frequency in Hz (5 digits).<\/p>\n<p>Don't overdo it with the automatic calibration - it reprograms the PIC with each possible OSCCAL value so you'll wear out the flash if you do it too much.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In order to program the microcontrollers for interval bars, I needed a programmer. This is a device which connects to the chip and to a computer, and which allows one to transfer a program from the computer to the chip. You can buy them for $40 or so but I decided to make my own [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,3],"tags":[],"class_list":["post-1294","post","type-post","status-publish","format-standard","hentry","category-computer","category-hardware"],"_links":{"self":[{"href":"https:\/\/www.reenigne.org\/blog\/wp-json\/wp\/v2\/posts\/1294","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.reenigne.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.reenigne.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.reenigne.org\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.reenigne.org\/blog\/wp-json\/wp\/v2\/comments?post=1294"}],"version-history":[{"count":7,"href":"https:\/\/www.reenigne.org\/blog\/wp-json\/wp\/v2\/posts\/1294\/revisions"}],"predecessor-version":[{"id":1331,"href":"https:\/\/www.reenigne.org\/blog\/wp-json\/wp\/v2\/posts\/1294\/revisions\/1331"}],"wp:attachment":[{"href":"https:\/\/www.reenigne.org\/blog\/wp-json\/wp\/v2\/media?parent=1294"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.reenigne.org\/blog\/wp-json\/wp\/v2\/categories?post=1294"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.reenigne.org\/blog\/wp-json\/wp\/v2\/tags?post=1294"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}