{"id":3597,"date":"2014-11-26T10:37:55","date_gmt":"2014-11-26T18:37:55","guid":{"rendered":"https:\/\/blog.digilentinc.com\/?p=3597"},"modified":"2021-06-17T08:29:08","modified_gmt":"2021-06-17T15:29:08","slug":"direct-register-manipulation-on-your-microcontroller","status":"publish","type":"post","link":"https:\/\/digilent.com\/blog\/direct-register-manipulation-on-your-microcontroller\/","title":{"rendered":"Direct Register Manipulation on your Microcontroller"},"content":{"rendered":"<p>I really enjoy what I do here at Digilent. I get to work with some of the best tools available for students, professionals, and hobbyists alike. One of the things I like most is how easy it is to get your hands on a good quality microcontroller board, like the chipKIT Uno32.<\/p>\n<p>&nbsp;<\/p>\n<figure id=\"attachment_3614\" aria-describedby=\"caption-attachment-3614\" style=\"width: 600px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/11\/chipKIT-Uno32-obl-600.png\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-3614 size-full\" src=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/11\/chipKIT-Uno32-obl-600.png\" alt=\"chipKIT Uno32-obl-600\" width=\"600\" height=\"483\" srcset=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/11\/chipKIT-Uno32-obl-600.png 600w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/11\/chipKIT-Uno32-obl-600-225x181.png 225w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><figcaption id=\"caption-attachment-3614\" class=\"wp-caption-text\">The popular chipKIT Uno32 development board with the PIC32mx320F128H microcontroller on board.<\/figcaption><\/figure>\n<p>&nbsp;<\/p>\n<p>But even once you get a good board,\u00a0it will still need to be programmed. Now, if you&#8217;ve ever programmed a microcontroller in Assembly language, you know how elegant it can be to deal directly with the register values. It&#8217;s simple and easy to do and makes for some pretty fast code if you do it right. The problem is that the popular IDE formats, like MPIDE, are designed to be very user-friendly, so the coding language is high level (C\/C++). That means that when the chip is programmed, there is some conversion that happens in the compiler to re-write your sketch into Assembly so the chip can process it. Along with the higher level language comes the ease of having more user-friendly pre-built functions, like digitalWrite(), but they have to be converted to Assembly and loaded along with your sketch. Anytime you use that function, the processor looks it up in its memory and executes it accordingly. This takes space in memory and a lot of processing time, which can be a problem if space or speed are an issue.<\/p>\n<p>&nbsp;<\/p>\n<p>So how do we fix that? Is there a way to still use the user-friendly IDE and still have the speed and space of direct register manipulation? Why, yes! Yes, there is. And it is surprisingly simple.<\/p>\n<p>&nbsp;<\/p>\n<p>The Microchip PIC32 chips use three registers on every I\/O pin for reading and writing data. (Pins that pull multiple duty like PWM, I2C, or UART have more registers.) The first is TRISx, which sets the I\/O direction of the pin and refers to the tri-state capability\u00a0that allows the pin to be either output, input, or high-impedance (neither in nor out). Setting a &#8216;1&#8217; in TRISx will set the corresponding pin to be an input and a &#8216;0&#8217; will set it as an output. The next register is LATx and acts as a latch. If the pin has been set as an output in TRISx, you can then write a &#8216;1&#8217; or &#8216;0&#8217; as needed to the same bit position in LATx. This will set the logic state on that pin as either HIGH or LOW accordingly. The last register is PORTx, which is the input status of a pin set as an input using TRISx. You can read the data from here and process it as needed in your sketch.\u00a0The standard IDE functions that map to these registers are pinMode() (TRISx), digitalWrite() (LATx) and digitalRead() (PORTx).<\/p>\n<p>&nbsp;<\/p>\n<p>For a more in-depth explanation and some example code, check out my <a title=\"Instructable\" href=\"http:\/\/www.instructables.com\/id\/Microcontroller-Register-Manipulation\/\" target=\"_blank\" rel=\"noopener\">Instructable<\/a>!<\/p>\n<p>&nbsp;<\/p>\n<div class='watch-action'><div class='watch-position align-left'><div class='action-like'><a class='lbg-style6 like-3597 jlk' data-task='like' data-post_id='3597' data-nonce='ac068a413b' rel='nofollow'><img src='https:\/\/digilent.com\/blog\/wp-content\/plugins\/wti-like-post-pro\/images\/pixel.gif' title='Like' \/><span class='lc-3597 lc'>0<\/span><\/a><\/div><div class='action-unlike'><a class='unlbg-style6 unlike-3597 jlk' data-task='unlike' data-post_id='3597' data-nonce='ac068a413b' rel='nofollow'><img src='https:\/\/digilent.com\/blog\/wp-content\/plugins\/wti-like-post-pro\/images\/pixel.gif' title='Unlike' \/><span class='unlc-3597 unlc'>0<\/span><\/a><\/div><\/div> <div class='status-3597 status align-left'>Be the 1st to vote.<\/div><\/div><div class='wti-clear'><\/div>","protected":false},"excerpt":{"rendered":"<p>I really enjoy what I do here at Digilent. I get to work with some of the best tools available for students, professionals, and hobbyists alike. One of the things I like most is how easy it is to get your hands on a good quality microcontroller board, like the chipKIT Uno32. But even once you get a good board, it will still need to be programmed.<\/p>\n","protected":false},"author":22,"featured_media":3614,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4327],"tags":[],"ppma_author":[4471],"class_list":["post-3597","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-projects"],"jetpack_featured_media_url":"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/11\/chipKIT-Uno32-obl-600.png","jetpack_sharing_enabled":true,"authors":[{"term_id":4471,"user_id":22,"is_guest":0,"slug":"brmarcum","display_name":"Brandon Marcum","avatar_url":"https:\/\/secure.gravatar.com\/avatar\/3f3481ac36f3c92f08a1a8266d60111ca31c5031c4428d551991aa080d9d529b?s=96&d=mm&r=g","1":"","2":"","3":"","4":"","5":"","6":"","7":"","8":"","9":"","10":""}],"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/posts\/3597","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/users\/22"}],"replies":[{"embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/comments?post=3597"}],"version-history":[{"count":0,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/posts\/3597\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/media\/3614"}],"wp:attachment":[{"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/media?parent=3597"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/categories?post=3597"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/tags?post=3597"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/ppma_author?post=3597"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}