{"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 &#8211; 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 &#8211; 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 &#8211; 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&#8217;s programming specifications. It has facilities for reading back the microcontroller&#8217;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&#8217;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 &#8211; a standard deviation of only about 30Hz.<\/p>\n<p>Here&#8217;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&#8217;s somewhere other than C:\\Program Files\\Arduino) and run it. To upload it to the Arduino you&#8217;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&#8217;t use the avrdude from the Arduino installation here because it won&#8217;t toggle the line to reset (that&#8217;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 &#8211; Write program from Arduino to PIC.<\/li>\n<li>O &#8211; 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 &#8211; Write program from Arduino to PIC, including OSCCAL value and backup OSCCAL value &#8211; only use if the backup OSCCAL value got screwed up somehow.<\/li>\n<li>R &#8211; Read program from PIC to Arduino.<\/li>\n<li>U &#8211; Upload program from PC to Arduino (follow this with the contents of your hex file).<\/li>\n<li>L &#8211; Download program from Arduino to PC.<\/li>\n<li>T &#8211; Start timing mode.<\/li>\n<li>Q &#8211; Stop timing mode.<\/li>\n<li>A &#8211; Perform automatic calibration.\n<\/li>\n<li>B &#8211; 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 &#8211; Success.<\/li>\n<li>EF &#8211; Error: config fuses didn&#8217;t verify.<\/li>\n<li>EV &#8211; Error: Program, OSCCAL, user IDs or backup OSCCAL didn&#8217;t verify.<\/li>\n<li>ER &#8211; Address in hex file out of range.<\/li>\n<li>EU &#8211; Unknown record type in hex file.<\/li>\n<li>EZ &#8211; Non-zero byte count in end-of-file marker in hex file.<\/li>\n<li>EC &#8211; Hex file checksum failure.<\/li>\n<li>EH &#8211; Incorrect hex digit.<\/li>\n<li>E: &#8211; 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&#8217;t overdo it with the automatic calibration &#8211; it reprograms the PIC with each possible OSCCAL value so you&#8217;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}]}}