{"id":239,"date":"2021-08-30T23:49:12","date_gmt":"2021-08-31T06:49:12","guid":{"rendered":"https:\/\/shmalex.com\/?p=239"},"modified":"2021-12-26T00:39:11","modified_gmt":"2021-12-26T07:39:11","slug":"assembly-4-bit-push-button-binary-counter","status":"publish","type":"post","link":"https:\/\/electronsandxploits.com\/?p=239","title":{"rendered":"Assembly 4-bit Push Button Binary Counter"},"content":{"rendered":"\n<p>     This was mostly a learning opportunity to teach myself how to use a low-level programing language in programming a microcontoller.  I have always been fascinated with early programming languages, but assembly code had always seemed to be like a complete alien language compared to all other coding languages.  Assembly is as close to the processor as one can get writing code without literally writing the ones and zeros that computers understand. Although most of us will never use it, getting just a lesson or two in it help understand how programing languages interact with the physical component of the computer.<\/p>\n\n\n\n<p>    This project was fairly simple in its nature but writing it in assembly and programming the microchip made it far more interesting.  Using a Microchip <a href=\"https:\/\/www.microchip.com\/en-us\/product\/PIC18F45K20#document-table\" data-type=\"URL\" data-id=\"https:\/\/www.microchip.com\/en-us\/product\/PIC18F45K20#document-table\">PIC18F45K20<\/a> microcontroller set on breadboard connected to a micro tact switch and 4 LEDs, assembly code was written for a 4-bit binary counter. Each LED represents a 0 or 1 by being in the off or on state, e.g. (off,off,off,off =0000= 0) and (on,on,on,on)=1111=15). To increment the binary counter the tactile switch was used as input, each push sent a signal to the microcontroller counter which would output the new binary value to the user through the LEDs. Once the 4-bit counter reached 15 or 1111 the counter would reset back to 0 to repeat the process.  <\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/electronsandxploits.com\/wp-content\/uploads\/2021\/08\/pic18f4520.jpg\" alt=\"\" class=\"wp-image-240\" width=\"537\" height=\"286\" srcset=\"https:\/\/electronsandxploits.com\/wp-content\/uploads\/2021\/08\/pic18f4520.jpg 725w, https:\/\/electronsandxploits.com\/wp-content\/uploads\/2021\/08\/pic18f4520-300x160.jpg 300w, https:\/\/electronsandxploits.com\/wp-content\/uploads\/2021\/08\/pic18f4520-600x319.jpg 600w\" sizes=\"auto, (max-width: 537px) 100vw, 537px\" \/><figcaption>PIC18F45K20 Pinout<\/figcaption><\/figure><\/div>\n\n\n\n<ul class=\"wp-block-list\"><li>Pins 11,12 and 32,31 used for supply and ground of the rail.<\/li><li>Pins 1,40 and 39 are used for communication (programming the chip using <a href=\"https:\/\/www.microchip.com\/en-us\/development-tool\/PG164140\" data-type=\"URL\" data-id=\"https:\/\/www.microchip.com\/en-us\/development-tool\/PG164140\">MPLAB PICkit4<\/a>)<\/li><li>Pin 33 was used for input, push button switch (all b ports were set to input)<\/li><li>Pin 19,20,21,22 were used for output, 4 LEDs at each pin in series with a 220\u03a9 resistor going to ground.<\/li><\/ul>\n\n\n\n<p>Below is the assembly code for this project. Note that the code can be configured for 2 buttons one to increment and decrement the binary counter, or it can be changed to remove the push button to make the 4bit binary counter cycle on its own without user input, but a longer delay (1 second) would be needed to allow the LED&#8217;s to noticeably change. <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\" style=\"font-size:12px\"><span class=\"has-inline-color has-accent-color\">List\tp=18f452, f=inhx32\t\t;Initalization\n\t#include&lt;p18f452.inc&gt;\t\t;Header file for SFRs and other parameters\n\t\t\n;Declare variables\ncount\t\tEQU\t0x30\t\t;count reg\ntimer\t\tEQU \t0x31\t\t;timer reg\nbutton\t\tEQU\t0x32\t\t;button reg\n\n;Start Binary Counter Program\n\t\torg\t0x20\t\t;reset vector\n\t\tmovlw\tb'11111111'\t;set port b as input button\n\t\tmovwf\tTRISB\t\t;set port b as input \n\t\tmovlw \tbutton\t\t;move wreg to button\n\t\tmovff\tPORTB,button\t;movw wreg to portB\n\t\tmovwf\tTRISD\t        ;set portd as output\nrset\t\tclrf\tcount\t\t;reset counter after '1111'\nLoop\t\tbtfsc\tbutton,0,0\t;bit test skip if clear\n\t\tgoto\tloop1ms\t\t;delay call can be adjusted\n\t\tmovlw\tcount\t\t;move count into wreg\n\t\tmovff\tcount,PORTD\t;movw wreg to PORTD\n\t\tcall\tloop1ms\t\t;delay call\n\t\tincf\tcount,f\t\t;increment the count by 1\n\t\tmovlw\t16h\t\t;wreg 16 for full count of 4bit 0-15\n\t\txorwf\tcount,w\t\t;check w 16 xor count (logical XOR)\n\t\tbtfss\tSTATUS,Z\t;check if the zero flag is set\n\t\tgoto\tLoop\t\t;if count is not zero go back to increment\n\t\tgoto\trset\t\t;if zero flag reset go to reset\n\t\t\n;Short Delay Subroutine (4Mhz clock) change based on need, delay for offsetting debounce\n\t\t\ndelay1ms\tmovlw\td'250'\t\t;initial value  \n\t\tnop\t\t        ;no operation\nloop1ms\t\taddlw\td'255'\t\t;dec W\n\t\tbtfss\tSTATUS,Z\t;check if zero flag is set\n\t\tgoto\tloop1ms\t\t;loop back, increasing the clock cycle = time\n\t\treturn\t\t\t;jump out of the loop when done.\n\t\tEND\t\t\t;stop assembly<\/span><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This was mostly a learning opportunity to teach myself how to use a low-level programing language in programming a microcontoller. I have always been fascinated with early programming languages, but assembly code had always seemed to be like a complete alien language compared to all other coding languages. Assembly is as close to the processor [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":240,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"image","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-239","post","type-post","status-publish","format-image","has-post-thumbnail","hentry","category-projects","post_format-post-format-image"],"_links":{"self":[{"href":"https:\/\/electronsandxploits.com\/index.php?rest_route=\/wp\/v2\/posts\/239","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/electronsandxploits.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/electronsandxploits.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/electronsandxploits.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/electronsandxploits.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=239"}],"version-history":[{"count":13,"href":"https:\/\/electronsandxploits.com\/index.php?rest_route=\/wp\/v2\/posts\/239\/revisions"}],"predecessor-version":[{"id":310,"href":"https:\/\/electronsandxploits.com\/index.php?rest_route=\/wp\/v2\/posts\/239\/revisions\/310"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/electronsandxploits.com\/index.php?rest_route=\/wp\/v2\/media\/240"}],"wp:attachment":[{"href":"https:\/\/electronsandxploits.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=239"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/electronsandxploits.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=239"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/electronsandxploits.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=239"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}