{"id":72,"date":"2019-11-04T15:33:47","date_gmt":"2019-11-04T15:33:47","guid":{"rendered":"https:\/\/fletch.scot\/wp\/?p=72"},"modified":"2020-09-01T16:24:36","modified_gmt":"2020-09-01T16:24:36","slug":"bit-banging-on-pic12f683","status":"publish","type":"post","link":"https:\/\/fletch.scot\/wp\/?p=72","title":{"rendered":"Bit Banging on PIC12F683"},"content":{"rendered":"<p>The <a href=\"https:\/\/www.microchip.com\/wwwproducts\/en\/PIC12F683\">PIC12F683 Microcontroller<\/a> comes in a useful 8 pin DIL package. I favour this over smaller devices as it allows my ageing eyes to cope with connecting it to the rest of the circuit.<\/p>\n<p>However, this PIC does not come with a UART and so any text debug needs to be output using \u2018bit-banging\u2019. I needed to debug a PWM motor drive so I tried to find an existing implementation of bit-banging on this or similar PICs developed in C but couldn\u2019t find any. So here\u2019s my solution.<\/p>\n<p>This code allows communication at 9600 baud only. This is a reasonable speed for text debug as it allows fairly rapid output but won\u2019t be too fast for any modern computer. The output is digital high and low on the GP0 pin. So highs will be at the supply voltage (Vdd) and lows will be at 0V. For this PIC Vdd is from 2 to 5.5V. I did all my testing with a 5V supply. The communication is 8-bit, no parity and one stop bit (8N1 for short). The remote UART used in testing was a <a href=\"http:\/\/dangerousprototypes.com\/docs\/Bus_Pirate\">Dangerous Prototypes\u2019 Bus Pirate<\/a>. The text was displayed on a Mac mini terminal using GNU screen:\u00a0<span style=\"font-family: 'Courier New'; font-size: 18px;\">screen \/dev\/tty.usbserial-A9048DB4 115200,cs8,-parenb,-cstopb,-hupcl<\/span>.<\/p>\n<p>The code uses timer 1 as a counter to work out how long to keep the signal high or low. This means the timing of the code you are debugging will be way off as the code does a lot of looping to check the counter registers. But if the code being debugged is timing critical you shouldn\u2019t be using text debug anyway. In that case you are better off simulating, flashing LEDs or looking at signals with a \u2018scope or logic analyser.<\/p>\n<p>The meat of the <a href=\"..\/code\/bit-bang\/bit-bang.c\">bit-banging code<\/a> is in the putcharGP0 function. This sets or resets the GP0 port depending on the value of the bit to be transmitted. After each set or reset the code loops waiting for 1\/9600 = 104\u00b5s. This loop is in the \u2018WAIT_SYMBOL_TIME\u2019 macro. This macro takes an argument which allows the loop to be adjusted allowing for the time taken to run the code between each bit. The macro is called \u2018WAIT_SYMBOL_TIME\u2019 as in this case each symbol is a single bit. Each character has a start bit, 8 data bits and a single stop bit.\u00a0<\/p>\n<p>The code is built using this\u00a0<a href=\"..\/code\/bit-bang\/makefile\">make file<\/a>\u00a0which uses Microchip\u2019s xc8 C compiler and programs the PIC using a <a href=\"https:\/\/www.microchip.com\/DevelopmentTools\/ProductDetails\/pg164120\">PICkit2<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The PIC12F683 Microcontroller comes in a useful 8 pin DIL package. I favour this over smaller devices as it allows my ageing eyes to cope with connecting it to the rest of the circuit. However, this PIC does not come with a UART and so any text debug needs to be output using \u2018bit-banging\u2019. I &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/fletch.scot\/wp\/?p=72\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Bit Banging on PIC12F683&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48],"tags":[3,40],"class_list":["post-72","post","type-post","status-publish","format-standard","hentry","category-programming","tag-electronics","tag-firmware"],"_links":{"self":[{"href":"https:\/\/fletch.scot\/wp\/index.php?rest_route=\/wp\/v2\/posts\/72","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fletch.scot\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fletch.scot\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fletch.scot\/wp\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fletch.scot\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=72"}],"version-history":[{"count":2,"href":"https:\/\/fletch.scot\/wp\/index.php?rest_route=\/wp\/v2\/posts\/72\/revisions"}],"predecessor-version":[{"id":134,"href":"https:\/\/fletch.scot\/wp\/index.php?rest_route=\/wp\/v2\/posts\/72\/revisions\/134"}],"wp:attachment":[{"href":"https:\/\/fletch.scot\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=72"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fletch.scot\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=72"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fletch.scot\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=72"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}