<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-5869154326689252934</id><updated>2026-10-19T10:57:55.913+05:00</updated><category term="PIC micro-controller Projects"/><category term="PIC16F877 Projects"/><category term="PIC16F628A Projects"/><category term="8051 micro-controller Projects"/><category term="PIC12F675 Projects"/><category term="PIC16F84A Projects"/><category term="PIC16F877A Projects"/><category term="LCD interfacing"/><category term="Water Level Indicator Projects"/><category term="Digital Circuits"/><category term="Analog Circuits"/><category term="Power Supply Design"/><category term="AVR micro-controller projects"/><title type='text'>Saeed&#39;s Blog</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://saeedsolutions.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5869154326689252934/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://saeedsolutions.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><link rel='next' type='application/atom+xml' href='http://www.blogger.com/feeds/5869154326689252934/posts/default?start-index=26&amp;max-results=25&amp;redirect=false'/><author><name>Saeed</name><uri>http://www.blogger.com/profile/18064969138529094661</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>148</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-5869154326689252934.post-8711310630981794181</id><published>2022-07-02T19:18:00.003+05:00</published><updated>2022-07-02T19:27:11.627+05:00</updated><title type='text'>Follow Me</title><content type='html'>&lt;p&gt;Follow me on Facebook,&amp;nbsp;&lt;/p&gt;

&lt;div id=&quot;fb-root&quot;&gt;&lt;/div&gt;
&lt;script async=&quot;&quot; crossorigin=&quot;anonymous&quot; defer=&quot;&quot; nonce=&quot;OfRSEhdK&quot; src=&quot;https://connect.facebook.net/en_US/sdk.js#xfbml=1&amp;amp;version=v14.0&quot;&gt;&lt;/script&gt;

&lt;div class=&quot;fb-page&quot; data-adapt-container-width=&quot;true&quot; data-height=&quot;&quot; data-hide-cover=&quot;false&quot; data-href=&quot;https://www.facebook.com/EmbeddedSSS&quot; data-show-facepile=&quot;true&quot; data-small-header=&quot;false&quot; data-width=&quot;&quot;&gt;&lt;blockquote cite=&quot;https://www.facebook.com/EmbeddedSSS&quot; class=&quot;fb-xfbml-parse-ignore&quot;&gt;&lt;a href=&quot;https://www.facebook.com/EmbeddedSSS&quot;&gt;Electronic Beans&lt;/a&gt;&lt;/blockquote&gt;&lt;/div&gt;


&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;

&lt;a href=&quot;https://www.instagram.com/sylabofficial/&quot;&gt;
  &lt;img alt=&quot;instagram icon&quot; src=&quot;https://upload.wikimedia.org/wikipedia/commons/thumb/a/a5/Instagram_icon.png/2048px-Instagram_icon.png&quot; style=&quot;border-radius: 8px; margin: 0px; overflow: hidden; padding: 0px;&quot; width=&quot;50px&quot; /&gt;
&lt;/a&gt;&amp;nbsp;&lt;div&gt;Follow me on Instagram,&lt;br /&gt;&lt;div&gt;&lt;a href=&quot;https://www.instagram.com/sylabofficial/&quot; target=&quot;_blank&quot;&gt;https://www.instagram.com/sylabofficial/&lt;/a&gt;


&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;

&lt;br /&gt;Follow me on Twitter,&lt;br /&gt;
  &lt;a class=&quot;twitter-follow-button&quot; data-lang=&quot;en&quot; data-show-count=&quot;false&quot; data-size=&quot;large&quot; href=&quot;https://twitter.com/MSaeedYasin?ref_src=twsrc%5Etfw&quot;&gt;Follow @MSaeedYasin&lt;/a&gt;
  &lt;script async=&quot;&quot; charset=&quot;utf-8&quot; src=&quot;https://platform.twitter.com/widgets.js&quot;&gt;&lt;/script&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Some of my tweets,&lt;br /&gt;
&lt;a class=&quot;twitter-timeline&quot; data-lang=&quot;en&quot; data-width=&quot;400&quot; data-height=&quot;500&quot; data-theme=&quot;light&quot; href=&quot;https://twitter.com/MSaeedYasin?ref_src=twsrc%5Etfw&quot;&gt;Tweets by MSaeedYasin&lt;/a&gt; &lt;script async src=&quot;https://platform.twitter.com/widgets.js&quot; charset=&quot;utf-8&quot;&gt;&lt;/script&gt;
&lt;br /&gt;

&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://saeedsolutions.blogspot.com/feeds/8711310630981794181/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://saeedsolutions.blogspot.com/2022/07/follow-me.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5869154326689252934/posts/default/8711310630981794181'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5869154326689252934/posts/default/8711310630981794181'/><link rel='alternate' type='text/html' href='http://saeedsolutions.blogspot.com/2022/07/follow-me.html' title='Follow Me'/><author><name>Saeed</name><uri>http://www.blogger.com/profile/18064969138529094661</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5869154326689252934.post-1711798322585938659</id><published>2016-01-30T20:50:00.000+05:00</published><updated>2017-04-02T15:11:37.594+05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="PIC micro-controller Projects"/><category scheme="http://www.blogger.com/atom/ns#" term="PIC16F877A Projects"/><title type='text'>PIC16F877A based controllable digital clock using LCD display (Code+Proteus simulation)</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;This PIC16F877A microcontroller tutorial answers the question,&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&quot; How to implement a controllable digital clock using PIC16F877A ? &quot;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;br /&gt;Using PIC16 simulator (&lt;i&gt;Proteus&lt;/i&gt;) you can verify this digital clock code and change it according to your needs. Using three push buttons (As shown in figure below) you can adjust time as you desire. This code is written in C language using MPLAB with HI-TECH C compiler. You can download this code from the &#39;&lt;i&gt;Downloads&lt;/i&gt;&#39;&amp;nbsp;&lt;span style=&quot;font-family: inherit;&quot;&gt;section at the bottom of this page.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;In this article, it is assumed that you know,&lt;/span&gt;&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;How to make a simple digital clock using&amp;nbsp;&lt;span style=&quot;background-color: white;&quot;&gt;PIC16F877A. If you don&#39;t then please read&amp;nbsp;&lt;/span&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2015/06/pic16f877a-based-digital-clock-using.html&quot; target=&quot;_blank&quot;&gt;this page&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;background-color: white;&quot;&gt;How to interface LCD with PIC16F877A microcontroller. If you don&#39;t then please read&amp;nbsp;&lt;/span&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/10/137.html&quot; target=&quot;_blank&quot;&gt;this page&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;How to configure timer0 of&amp;nbsp;&lt;span style=&quot;background-color: white;&quot;&gt;PIC16F877A&amp;nbsp;&lt;/span&gt;microcontroller.&amp;nbsp;&amp;nbsp;&lt;span style=&quot;background-color: white;&quot;&gt;If you don&#39;t then please read&amp;nbsp;&lt;/span&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2015/06/pic16f877a-timer0-code-proteus.html&quot; target=&quot;_blank&quot;&gt;this page&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;
The following diagram (made in&amp;nbsp;&lt;i&gt;Proteus&lt;/i&gt;) shows the PIC microcontroller circuit diagram.&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh08l9kRM8R6n3MAdPvkwqFTtjnYtj1DjAVyzRtROZO2n7EVPGFPxY9BVEj3g7w9SSkJTzzJ8ozg6lt3ZQmf6drS2K6AH1oMDMVim-Z-WmuNuJf4_SrMsUseiLfl_01atxmRgeaJA3ninw/s1600/PIC16F877A+Controllable+Clock+Circuit.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;315&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh08l9kRM8R6n3MAdPvkwqFTtjnYtj1DjAVyzRtROZO2n7EVPGFPxY9BVEj3g7w9SSkJTzzJ8ozg6lt3ZQmf6drS2K6AH1oMDMVim-Z-WmuNuJf4_SrMsUseiLfl_01atxmRgeaJA3ninw/s1600/PIC16F877A+Controllable+Clock+Circuit.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;b style=&quot;font-size: 12.8px;&quot;&gt;&lt;i&gt;Figure 1&lt;/i&gt;&lt;/b&gt;&lt;span style=&quot;font-size: 12.8px;&quot;&gt;. PIC16F877A based controllable clock circuit&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
The above figure was taken after setting time to 03:59:38&lt;span style=&quot;background-color: white;&quot;&gt;, timer0 is used as the base for digital clock generation. Timer0 is used here to generate 1msec interrupts. After every 1msec a global variable named&amp;nbsp;&lt;/span&gt;&lt;i style=&quot;background-color: white;&quot;&gt;msCounter&amp;nbsp;&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;increments. When&amp;nbsp;&lt;/span&gt;&lt;i style=&quot;background-color: white;&quot;&gt;msCounter&amp;nbsp;&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;reaches a value of 1000 then another global variable named&amp;nbsp;&lt;/span&gt;&lt;i style=&quot;background-color: white;&quot;&gt;secCounter&amp;nbsp;&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;increments and this process repeats itself. Similarly, when&amp;nbsp;&lt;i&gt;secCounter&amp;nbsp;&lt;/i&gt;reaches 60, then &amp;nbsp;&lt;i&gt;minCounter&amp;nbsp;&lt;/i&gt;increments. And when&amp;nbsp;&lt;i&gt;minCounter&amp;nbsp;&lt;/i&gt;reaches 60 then&amp;nbsp;&lt;i&gt;hrCounter&amp;nbsp;&lt;/i&gt;increments. This process continues until&amp;nbsp;&lt;i&gt;hrCounter&amp;nbsp;&lt;/i&gt;reaches 24 then all of these variables reset their values.&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;LCD is updated with the new values of&amp;nbsp;&lt;/span&gt;&lt;i&gt;hrCounter&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;,&amp;nbsp;&lt;/span&gt;&lt;i&gt;minCounter&amp;nbsp;&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;and&amp;nbsp;&lt;/span&gt;&lt;i&gt;secCounter&amp;nbsp;&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;after every second.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;You can set time using three push buttons attached on &lt;i&gt;RE0&lt;/i&gt;, &lt;i&gt;RE1 &lt;/i&gt;and &lt;i&gt;RE2 &lt;/i&gt;pins (As shown in the above figure). By pressing &#39;&lt;i&gt;Set Time&lt;/i&gt;&#39; button one time, code enters in configuration state. &lt;i&gt;Hours &lt;/i&gt;value starts to blink and you can modify it using &lt;i&gt;Up &lt;/i&gt;and&amp;nbsp;&lt;i&gt;Down &lt;/i&gt;buttons. Pressing&amp;nbsp;&lt;i&gt;Up&lt;/i&gt; button increments the value and pressing &lt;i&gt;Down &lt;/i&gt;button decrements the value. When you are done setting &lt;i&gt;Hours &lt;/i&gt;value, press&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&#39;&lt;/span&gt;&lt;i&gt;Set Time&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&#39; button again, then &lt;i&gt;Minutes &lt;/i&gt;value will start to blink and you can adjust this value using &lt;i&gt;Up &lt;/i&gt;and &lt;i&gt;Down &lt;/i&gt;buttons. Similarly, after setting Minutes value, you can&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;press&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&#39;&lt;/span&gt;&lt;i&gt;Set Time&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&#39; button again, then&amp;nbsp;&lt;i&gt;Seconds&amp;nbsp;&lt;/i&gt;value will start to blink and you can adjust this value using&amp;nbsp;&lt;i&gt;Up&amp;nbsp;&lt;/i&gt;and&amp;nbsp;&lt;i&gt;Down&amp;nbsp;&lt;/i&gt;buttons. When you are done adjusting the time, then press&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&#39;&lt;/span&gt;&lt;i&gt;Set Time&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&#39; button for the last time and this clock will start to work normally.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;A crystal of 4MHz value is used in this circuit, which makes this PIC16F877A run at a speed of 1MIPS (Million of instructions per second).&lt;/span&gt;&lt;br /&gt;
&lt;h3&gt;
&lt;span style=&quot;color: #38761d; font-size: x-large;&quot;&gt;Code&lt;/span&gt;&lt;/h3&gt;
The main function code is shown below.&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjtQI2IoXosdY2-Caw1MXxRlA7bLk6IL2zVX0_4iBA5oCr7KYjji3qUrn7ToIIcmx1uZX_SfwkXV-FVjqcMAi63UmYh2icWZ4mRqrXWxlkZYATET8l6-owsfeQMUR8Fb0reEYyEuM3wG74/s1600/PIC16F877A+Controllable+Clock+Main+Code.PNG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjtQI2IoXosdY2-Caw1MXxRlA7bLk6IL2zVX0_4iBA5oCr7KYjji3qUrn7ToIIcmx1uZX_SfwkXV-FVjqcMAi63UmYh2icWZ4mRqrXWxlkZYATET8l6-owsfeQMUR8Fb0reEYyEuM3wG74/s1600/PIC16F877A+Controllable+Clock+Main+Code.PNG&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;i style=&quot;font-size: 12.8px;&quot;&gt;&lt;b&gt;Figure 2&lt;/b&gt;&lt;/i&gt;&lt;span style=&quot;font-size: 12.8px;&quot;&gt;. &amp;nbsp; Main function code for digital clock on PIC16F877A&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
In the main function, firstly PORTE pins are made input and ADC is turned off on these pins. Then LCD is initialized using&amp;nbsp;&lt;i&gt;InitLCD()&amp;nbsp;&lt;/i&gt;function. Then &lt;i&gt;Timer0 &lt;/i&gt;is initialized to generate 1msec interrupts.&lt;br /&gt;
&lt;br /&gt;
After that, in the&lt;i&gt;&amp;nbsp;while(1)&lt;/i&gt;&amp;nbsp;loop, there is a state machine running. A variable named &lt;i&gt;State &lt;/i&gt;is used to keep track of the current state of the clock. When this code starts, then State has a value of &lt;i&gt;NORMAL_STATE&lt;/i&gt;. In the normal state, clock value is incremented after every second and&amp;nbsp;whenever&amp;nbsp;&lt;i&gt;msCounter&amp;nbsp;&lt;/i&gt;reaches a value of zero&lt;sup&gt;&lt;span style=&quot;color: purple;&quot;&gt;[1]&lt;/span&gt;&lt;/sup&gt;, then new values of&amp;nbsp;&lt;i&gt;hrCounter&lt;/i&gt;,&amp;nbsp;&lt;i&gt;minCounter&amp;nbsp;&lt;/i&gt;and&amp;nbsp;&lt;i&gt;secCounter&amp;nbsp;&lt;/i&gt;are updated on the LCD.&amp;nbsp;&lt;i&gt;UpdateTimeCounters()&lt;/i&gt;&amp;nbsp;function is called every time and it corrects the values of every counter variable depending upon the value of&amp;nbsp;&lt;i&gt;msCounter&lt;/i&gt;,&amp;nbsp;which&amp;nbsp;is incremented in the ISR function of timer0.&lt;br /&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
In the normal state, code keeps checking &lt;i&gt;RE0 &lt;/i&gt;pin value, if &#39;&lt;i&gt;Set Time&lt;/i&gt;&#39; button is pressed then &lt;i&gt;RE0 &lt;/i&gt;pin value becomes zero. This is detected using &lt;i&gt;if(!RE0)&lt;/i&gt; statement in the code. When&amp;nbsp;&#39;&lt;i&gt;Set Time&lt;/i&gt;&#39; button is pressed then state is changed to &#39;&lt;i&gt;CONFIG_HR_STATE&lt;/i&gt;&#39; (i-e configure hours value state). In this state, &lt;i&gt;Up &lt;/i&gt;and &lt;i&gt;Down &lt;/i&gt;buttons are constantly monitored and &lt;i&gt;hrCounter &lt;/i&gt;value can be adjusted. Similarly, by pressing &#39;Set Time&#39; button again, state is changed to&amp;nbsp;&#39;&lt;i&gt;CONFIG_MIN_STATE&lt;/i&gt;&#39; (i-e configure minutes value state). In this state &lt;i&gt;minCounter &lt;/i&gt;value can be adjusted using &lt;i&gt;Up &lt;/i&gt;and &lt;i&gt;Down &lt;/i&gt;buttons and&amp;nbsp;by pressing &#39;&lt;i&gt;Set Time&lt;/i&gt;&#39; button again, state is changed to&amp;nbsp;&#39;&lt;i&gt;CONFIG_SEC_STATE&lt;/i&gt;&#39; (i-e configure seconds value state). In this state &lt;i&gt;secCounter &lt;/i&gt;value can be adjusted using &lt;i&gt;Up &lt;/i&gt;and &lt;i&gt;Down &lt;/i&gt;buttons.&amp;nbsp;By pressing &#39;&lt;i&gt;Set Time&lt;/i&gt;&#39; button again, state is changed to&amp;nbsp;&#39;&lt;i&gt;NORMAL_STATE&lt;/i&gt;&#39; and clock starts to run normally.&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;You can leave your comments in the comment section below.&lt;/span&gt;&lt;br /&gt;
&lt;h3&gt;
&lt;span style=&quot;color: blue; font-size: x-large;&quot;&gt;Notes and References&lt;/span&gt;&lt;/h3&gt;
&lt;sup&gt;&lt;span style=&quot;color: purple;&quot;&gt;[1] &amp;nbsp;&lt;/span&gt;&lt;/sup&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&lt;i&gt;msCounter&lt;/i&gt;&amp;nbsp;increments to a value of 1000, then resets to 0 and starts to increment again. So,&amp;nbsp;&lt;i&gt;msCounter&amp;nbsp;&lt;/i&gt;becomes zero again after every 1 second exactly. So LCD updates with new time value after every second.&lt;/span&gt;&lt;br /&gt;
&lt;div&gt;
&lt;h3&gt;
&lt;span style=&quot;color: red; font-size: x-large;&quot;&gt;Downloads&lt;/span&gt;&lt;/h3&gt;
&lt;/div&gt;
Controllable digital clock display code using PIC16F877A was compiled in&amp;nbsp;MPLAB v8.85 with HI-TECH C v9.83 compiler and simulation was made in Proteus v7.10.&amp;nbsp;&lt;span style=&quot;background-color: white;&quot;&gt;To download code and&amp;nbsp;&lt;/span&gt;&lt;i style=&quot;background-color: white;&quot;&gt;Proteus&amp;nbsp;&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;simulation&amp;nbsp;&lt;/span&gt;&lt;a href=&quot;http://bit.ly/2nNCtWh&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;click here&lt;/a&gt;&lt;span style=&quot;background-color: white;&quot;&gt;.&lt;/span&gt;&lt;br /&gt;
&lt;div&gt;
&lt;h3&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;span style=&quot;color: #274e13; font-size: large;&quot;&gt;Further Reading Suggestions&lt;/span&gt;&lt;/span&gt;&lt;/h3&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;You may also like,&lt;/span&gt;&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/08/129.html&quot;&gt;How to set PIC16F877A configuration bits in code ?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/08/130.html&quot;&gt;PIC16F877A LED blinking code&amp;nbsp;+ Proteus simulation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/10/137.html&quot;&gt;PIC16F877A LCD interfacing code (In 4bit mode)&amp;nbsp;+ Proteus simulation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2015/06/pic16f877a-based-digital-clock-using.html&quot;&gt;PIC16F877A based digital clock using LCD display (Code+Proteus simulation)&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://saeedsolutions.blogspot.com/feeds/1711798322585938659/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://saeedsolutions.blogspot.com/2016/01/pic16f877a-based-controllable-digital.html#comment-form' title='37 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5869154326689252934/posts/default/1711798322585938659'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5869154326689252934/posts/default/1711798322585938659'/><link rel='alternate' type='text/html' href='http://saeedsolutions.blogspot.com/2016/01/pic16f877a-based-controllable-digital.html' title='PIC16F877A based controllable digital clock using LCD display (Code+Proteus simulation)'/><author><name>Saeed</name><uri>http://www.blogger.com/profile/18064969138529094661</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh08l9kRM8R6n3MAdPvkwqFTtjnYtj1DjAVyzRtROZO2n7EVPGFPxY9BVEj3g7w9SSkJTzzJ8ozg6lt3ZQmf6drS2K6AH1oMDMVim-Z-WmuNuJf4_SrMsUseiLfl_01atxmRgeaJA3ninw/s72-c/PIC16F877A+Controllable+Clock+Circuit.png" height="72" width="72"/><thr:total>37</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5869154326689252934.post-1766391546407785983</id><published>2016-01-26T10:00:00.000+05:00</published><updated>2017-04-02T15:10:59.991+05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="PIC micro-controller Projects"/><category scheme="http://www.blogger.com/atom/ns#" term="PIC16F877A Projects"/><title type='text'>PIC16F877A internal EEPROM code + Proteus simulation</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;This post provides the internal EEPROM reading and writing&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;code for PIC16F877A microcontroller. As we know&lt;/span&gt;&lt;sup&gt;&lt;span style=&quot;color: purple;&quot;&gt;[1]&lt;/span&gt;&lt;/sup&gt;&lt;span style=&quot;background-color: white;&quot;&gt;, PIC16F877A microcontroller has 256 bytes of built in EEPROM data space with an address range of 0x00 to 0xFF. This code is written in C language using MPLAB with HI-TECH C compiler. You can download this code from the &#39;&lt;/span&gt;&lt;i style=&quot;background-color: white;&quot;&gt;Downloads&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&#39;&amp;nbsp;&lt;span style=&quot;font-family: inherit;&quot;&gt;section at the bottom of this page.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;It is assumed that you know how to blink an LED with PIC16F877A microcontroller. If you don&#39;t then please read&amp;nbsp;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/08/130.html&quot; target=&quot;_blank&quot;&gt;this page&lt;/a&gt;&amp;nbsp;first, before proceeding with this article.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;The result of simulating the code in&amp;nbsp;&lt;i&gt;Proteus&amp;nbsp;&lt;/i&gt;is shown below.&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEid5odiSTQ7Ot65P5FG_clP6khir2-68QmDV2j2tUTe5INZB9k04YRm_BdgOY8TeGbWvpmQuOqvj62uVl9awSBT_jG8tuUMEHgJsgMHfv6_tl0i9eYb3UXTphrsm2_t7Y-McFSFiw0w2fE/s1600/PIC16F877A+Internal+EEPROM+Circuit.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;362&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEid5odiSTQ7Ot65P5FG_clP6khir2-68QmDV2j2tUTe5INZB9k04YRm_BdgOY8TeGbWvpmQuOqvj62uVl9awSBT_jG8tuUMEHgJsgMHfv6_tl0i9eYb3UXTphrsm2_t7Y-McFSFiw0w2fE/s1600/PIC16F877A+Internal+EEPROM+Circuit.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;i style=&quot;font-size: 12.8px;&quot;&gt;&lt;b&gt;Figure 1&lt;/b&gt;&lt;/i&gt;&lt;span style=&quot;font-size: 12.8px;&quot;&gt;. &amp;nbsp; Internal EEPROM testing circuit for PIC16F877A&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
In the above circuit&lt;sup&gt;&lt;span style=&quot;color: purple;&quot;&gt;[2]&lt;/span&gt;&lt;/sup&gt;, LED attached on RB0 indicates whether a byte was successfully written in the internal EEPROM or not. In the above figure, LED is&amp;nbsp;&lt;i&gt;ON&amp;nbsp;&lt;/i&gt;which indicates that EEPROM was successfully programmed.&lt;br /&gt;
&lt;h3&gt;
&lt;span style=&quot;color: #38761d; font-size: x-large;&quot;&gt;Code&lt;/span&gt;&lt;/h3&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;The code for the main function is shown below.&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi4hkicsK98qTga-ISIz6iLnx8DBBG3XY7mAbVXj_-dT9m_vPWzeXfTUGOM_kUggHC1teLxx5UvXr27U5b5X4_YbZPTOF2hbm_zkZiXYuQ63vgsOSiUsu7WgUjj5dAxsODUbgEqTOq9zcs/s1600/PIC16F877A+EEPROM+Main+Code.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;640&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi4hkicsK98qTga-ISIz6iLnx8DBBG3XY7mAbVXj_-dT9m_vPWzeXfTUGOM_kUggHC1teLxx5UvXr27U5b5X4_YbZPTOF2hbm_zkZiXYuQ63vgsOSiUsu7WgUjj5dAxsODUbgEqTOq9zcs/s1600/PIC16F877A+EEPROM+Main+Code.png&quot; width=&quot;472&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;b style=&quot;font-size: 12.8px;&quot;&gt;&lt;i&gt;Figure 2&lt;/i&gt;&lt;/b&gt;&lt;span style=&quot;font-size: 12.8px;&quot;&gt;. Main code for eeprom testing with PIC16F877A&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;In the main function, firstly RB0 pin is made an output. After that,&amp;nbsp;&lt;/span&gt;&lt;i&gt;eeprom_write(address, value)&amp;nbsp;&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;function writes the desired data value on the given address in the internal EEPROM. For example, in the above code a value of 0xFF is being written on the 0x00 address of EEPROM. Also,&lt;/span&gt;&lt;i&gt;&amp;nbsp;eeprom_read(address)&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;function reads the value of byte present at the given address. In the above code, the data value from the 0x00 address of internal EEPROM is being compared to 0xFF, if it is a match then LED is turned on, if it is not a match then LED is turned off.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
These two functions&amp;nbsp;&lt;i&gt;eeprom_write(address, value)&amp;nbsp;&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;and&amp;nbsp;&lt;/span&gt;&lt;i&gt;eeprom_read(address)&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;are built in functions provided by the HI-TECH C compiler. You don&#39;t need to write your own functions to access internal EEPROM. Just use these functions and you are done. It is that simple.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;You can leave your comments and suggestions in the comment section below.&lt;/span&gt;&lt;br /&gt;
&lt;h3&gt;
&lt;span style=&quot;color: blue; font-size: x-large;&quot;&gt;Notes and References&lt;/span&gt;&lt;/h3&gt;
&lt;sup&gt;&lt;span style=&quot;color: purple;&quot;&gt;[1] &amp;nbsp;&lt;/span&gt;&lt;/sup&gt;&lt;span style=&quot;background-color: white;&quot;&gt;From the datasheet of&amp;nbsp;&lt;a href=&quot;http://ww1.microchip.com/downloads/en/DeviceDoc/39582C.pdf&quot;&gt;PIC16F877A&lt;/a&gt;.&lt;/span&gt;&lt;br /&gt;
&lt;sup&gt;&lt;span style=&quot;color: purple;&quot;&gt;[2] &amp;nbsp;&lt;/span&gt;&lt;/sup&gt;&lt;span style=&quot;background-color: white;&quot;&gt;In this circuit, PIC16F877A is running at 5 MIPS speed using 20 MHz crystal. You can use any crystal from 0 to 20MHz with this code.&lt;/span&gt;&lt;br /&gt;
&lt;div&gt;
&lt;h3&gt;
&lt;span style=&quot;color: red; font-size: x-large;&quot;&gt;Downloads&lt;/span&gt;&lt;/h3&gt;
&lt;/div&gt;
EEPROM code for PIC16F877A was compiled in&amp;nbsp;MPLAB v8.85 with HI-TECH C v9.83 compiler and simulation was made in Proteus v7.10.&amp;nbsp;&lt;span style=&quot;background-color: white;&quot;&gt;To download code and&amp;nbsp;&lt;/span&gt;&lt;i style=&quot;background-color: white;&quot;&gt;Proteus&amp;nbsp;&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;simulation&amp;nbsp;&lt;/span&gt;&lt;a href=&quot;http://bit.ly/2oN4avX&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;click here&lt;/a&gt;&lt;span style=&quot;background-color: white;&quot;&gt;.&lt;/span&gt;&lt;br /&gt;
&lt;div&gt;
&lt;h3&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;span style=&quot;color: #274e13; font-size: large;&quot;&gt;Further Reading Suggestions&lt;/span&gt;&lt;/span&gt;&lt;/h3&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;You may also like,&lt;/span&gt;&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/08/129.html&quot;&gt;How to set PIC16F877A configuration bits in code ?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/08/130.html&quot;&gt;PIC16F877A LED blinking code&amp;nbsp;+ Proteus simulation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/10/137.html&quot;&gt;PIC16F877A LCD interfacing code (In 4bit mode)&amp;nbsp;+ Proteus simulation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2015/06/pic16f877a-based-digital-clock-using.html&quot;&gt;PIC16F877A based digital clock using LCD display (Code+Proteus simulation)&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://saeedsolutions.blogspot.com/feeds/1766391546407785983/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://saeedsolutions.blogspot.com/2016/01/pic16f877a-internal-eeprom-code-proteus.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5869154326689252934/posts/default/1766391546407785983'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5869154326689252934/posts/default/1766391546407785983'/><link rel='alternate' type='text/html' href='http://saeedsolutions.blogspot.com/2016/01/pic16f877a-internal-eeprom-code-proteus.html' title='PIC16F877A internal EEPROM code + Proteus simulation'/><author><name>Saeed</name><uri>http://www.blogger.com/profile/18064969138529094661</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEid5odiSTQ7Ot65P5FG_clP6khir2-68QmDV2j2tUTe5INZB9k04YRm_BdgOY8TeGbWvpmQuOqvj62uVl9awSBT_jG8tuUMEHgJsgMHfv6_tl0i9eYb3UXTphrsm2_t7Y-McFSFiw0w2fE/s72-c/PIC16F877A+Internal+EEPROM+Circuit.png" height="72" width="72"/><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5869154326689252934.post-1169152897031609065</id><published>2016-01-25T10:00:00.000+05:00</published><updated>2017-04-02T15:10:13.261+05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="PIC micro-controller Projects"/><category scheme="http://www.blogger.com/atom/ns#" term="PIC16F877A Projects"/><title type='text'>PIC16F877A based simple calculator (Code+Proteus simulation)</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;This PIC microcontroller tutorial provides a simple calculator implementation for PIC16F877A microcontroller. This is a simple one digit&lt;/span&gt;&lt;sup style=&quot;background-color: white;&quot;&gt;&lt;span style=&quot;color: purple;&quot;&gt;[1]&lt;/span&gt;&lt;/sup&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;calculator which implements only 4 functions addition(+), subtraction(-), multiplication(x) and division(/).&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;The code for PIC16F877A is written in C language using MPLAB with HI-TECH C compiler. You can download this code from the &#39;&lt;/span&gt;&lt;i style=&quot;background-color: white;&quot;&gt;Downloads&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&#39;&amp;nbsp;&lt;span style=&quot;font-family: inherit;&quot;&gt;section at the bottom of this page.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;In this post, it is assumed that you know,&lt;/span&gt;&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style=&quot;background-color: white;&quot;&gt;How to interface keypad with PIC16F877A microcontroller. If you don&#39;t then please read&amp;nbsp;&lt;/span&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2016/01/how-to-interface-keypad-with-pic16f877a.html&quot; target=&quot;_blank&quot;&gt;this page&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;background-color: white;&quot;&gt;How to interface LCD with PIC16F877A microcontroller. If you don&#39;t then please read&amp;nbsp;&lt;/span&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/10/137.html&quot; target=&quot;_blank&quot;&gt;this page&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;
The following diagram (made in&amp;nbsp;&lt;i&gt;Proteus&lt;/i&gt;) shows the PIC microcontroller circuit diagram.&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj9IM4LtAjVOpMtPiKbMalymTQtK64rxkhWmVhjuSePeuSyJwhTa8ZQpaAQ-NuH4XaRqTAJsGoycpDqUj3r4hRjWCJZM358y5iZzUENqby6xTlYIW0QhzVBEdFLiaUufOpkZZ8Szatw83o/s1600/PIC16F877A+Calculator+Circuit.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;410&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj9IM4LtAjVOpMtPiKbMalymTQtK64rxkhWmVhjuSePeuSyJwhTa8ZQpaAQ-NuH4XaRqTAJsGoycpDqUj3r4hRjWCJZM358y5iZzUENqby6xTlYIW0QhzVBEdFLiaUufOpkZZ8Szatw83o/s1600/PIC16F877A+Calculator+Circuit.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;i style=&quot;font-size: 12.8px;&quot;&gt;&lt;b&gt;Figure 1&lt;/b&gt;&lt;/i&gt;&lt;span style=&quot;font-size: 12.8px;&quot;&gt;. &amp;nbsp; Circuit diagram for implementing calculator on PIC16F877A&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;In the above figure, we can see that a result of &quot;&lt;i&gt;3+5&lt;/i&gt;&lt;/span&gt;&lt;i style=&quot;background-color: white;&quot;&gt;=8&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&quot; is shown on the screen. To achieve this result, first press &#39;3&#39; from the keypad. Then press &#39;+&#39; and then press &#39;5&#39;. After that, on pressing &#39;=&#39; from the keypad the result &#39;8&#39; is automatically displayed on the screen.&lt;/span&gt;&lt;br /&gt;
&lt;h3&gt;
&lt;span style=&quot;color: #b45f06; font-size: large;&quot;&gt;Features of this calculator&lt;/span&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;You can give any single digit input from 0 to 9.&amp;nbsp;&lt;/li&gt;
&lt;li&gt;You can press &#39;&lt;i&gt;ON/C&lt;/i&gt;&#39; button at any time to reset the calculator.&amp;nbsp;&lt;/li&gt;
&lt;li&gt;4 functions are implemented i-e addition, subtraction,&amp;nbsp;multiplication&amp;nbsp;and division.&lt;/li&gt;
&lt;li&gt;Error messages are displayed if wrong input is detected. For example, if calculator is expecting a number, but a function key is pressed then &#39;&lt;i&gt;Wrong Input&lt;/i&gt;&#39; message is displayed. Similarly, &#39;&lt;i&gt;Wrong Function&lt;/i&gt;&#39; message is displayed if wrong key is pressed instead of a function key.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
&lt;span style=&quot;color: #38761d; font-size: large;&quot;&gt;Main function code&lt;/span&gt;&lt;/h3&gt;
The code for the main function is shown below.&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhOPvDSvT_OZy-72eB_vgimnSNkYpbWYVqTcTEbjA2__8iP7ko57gML5E2vxMdAcrfsTWTtoPz223nvtb9aS6KKhV7tMrHyqjhZ0ypU8K1S5KHfmHZxANZC-ynbSb4zPVdygDh-OPoEliM/s1600/PIC16F877+CALC+MAIN+CODE.bmp&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhOPvDSvT_OZy-72eB_vgimnSNkYpbWYVqTcTEbjA2__8iP7ko57gML5E2vxMdAcrfsTWTtoPz223nvtb9aS6KKhV7tMrHyqjhZ0ypU8K1S5KHfmHZxANZC-ynbSb4zPVdygDh-OPoEliM/s1600/PIC16F877+CALC+MAIN+CODE.bmp&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;i&gt;&lt;b&gt;Figure 2&lt;/b&gt;&lt;/i&gt;. &amp;nbsp; Main function code for calculator implementation on PIC16F877A&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;In the main code, firstly keypad and LCD are initialized. Then the code waits for the first number&lt;/span&gt;&lt;span style=&quot;background-color: white; color: purple;&quot;&gt;&lt;sup&gt;[1]&lt;/sup&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;from the keypad. After getting this number LCD screen is cleared. And this number is displayed on the LCD. After that, code waits for the function key&lt;/span&gt;&lt;sup style=&quot;background-color: white;&quot;&gt;&lt;span style=&quot;color: purple;&quot;&gt;[2]&lt;/span&gt;&lt;/sup&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;from the user. After getting the function key, code waits for the second number&lt;/span&gt;&lt;sup style=&quot;background-color: white;&quot;&gt;&lt;span style=&quot;color: purple;&quot;&gt;[1]&lt;/span&gt;&lt;/sup&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;and then the equal sign. After getting the equal sign, according to the desired function the result is calculated and displayed on the screen.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
You can leave your comments in the comment section below.&lt;br /&gt;
&lt;h3&gt;
&lt;span style=&quot;color: blue; font-size: x-large;&quot;&gt;Notes and References&lt;/span&gt;&lt;/h3&gt;
&lt;sup&gt;&lt;span style=&quot;color: purple;&quot;&gt;[1]&lt;/span&gt;&lt;/sup&gt;&amp;nbsp; You can give any single digit input from 0 to 9.&lt;br /&gt;
&lt;sup&gt;&lt;span style=&quot;color: purple;&quot;&gt;[2]&lt;/span&gt;&lt;/sup&gt;&amp;nbsp; You can select any function i-e &#39;+&#39; or &#39;-&#39; or &#39;x&#39; or &#39;/&#39;.&lt;br /&gt;
&lt;div&gt;
&lt;h3&gt;
&lt;span style=&quot;color: red; font-size: x-large;&quot;&gt;Downloads&lt;/span&gt;&lt;/h3&gt;
&lt;/div&gt;
The calculator code for PIC16F877A was compiled in&amp;nbsp;MPLAB v8.85 with HI-TECH C v9.83 compiler and simulation was made in Proteus v7.10.&amp;nbsp;&lt;span style=&quot;background-color: white;&quot;&gt;To download code and&amp;nbsp;&lt;/span&gt;&lt;i style=&quot;background-color: white;&quot;&gt;Proteus&amp;nbsp;&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;simulation&amp;nbsp;&lt;/span&gt;&lt;a href=&quot;http://bit.ly/2oMXZYX&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;click here&lt;/a&gt;&lt;span style=&quot;background-color: white;&quot;&gt;.&lt;/span&gt;&lt;br /&gt;
&lt;div&gt;
&lt;h3&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;span style=&quot;color: #274e13; font-size: large;&quot;&gt;Further Reading Suggestions&lt;/span&gt;&lt;/span&gt;&lt;/h3&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;You may also like,&lt;/span&gt;&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/08/129.html&quot;&gt;How to set PIC16F877A configuration bits in code ?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/08/130.html&quot;&gt;PIC16F877A LED blinking code&amp;nbsp;+ Proteus simulation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/10/137.html&quot;&gt;PIC16F877A LCD interfacing code (In 4bit mode)&amp;nbsp;+ Proteus simulation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2015/06/pic16f877a-based-digital-clock-using.html&quot;&gt;PIC16F877A based digital clock using LCD display (Code+Proteus simulation)&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://saeedsolutions.blogspot.com/feeds/1169152897031609065/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://saeedsolutions.blogspot.com/2016/01/pic16f877a-based-simple-calculator.html#comment-form' title='11 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5869154326689252934/posts/default/1169152897031609065'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5869154326689252934/posts/default/1169152897031609065'/><link rel='alternate' type='text/html' href='http://saeedsolutions.blogspot.com/2016/01/pic16f877a-based-simple-calculator.html' title='PIC16F877A based simple calculator (Code+Proteus simulation)'/><author><name>Saeed</name><uri>http://www.blogger.com/profile/18064969138529094661</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj9IM4LtAjVOpMtPiKbMalymTQtK64rxkhWmVhjuSePeuSyJwhTa8ZQpaAQ-NuH4XaRqTAJsGoycpDqUj3r4hRjWCJZM358y5iZzUENqby6xTlYIW0QhzVBEdFLiaUufOpkZZ8Szatw83o/s72-c/PIC16F877A+Calculator+Circuit.png" height="72" width="72"/><thr:total>11</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5869154326689252934.post-6111420239454866446</id><published>2016-01-23T21:35:00.000+05:00</published><updated>2017-04-02T15:09:20.752+05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="PIC micro-controller Projects"/><category scheme="http://www.blogger.com/atom/ns#" term="PIC16F877A Projects"/><title type='text'>How to interface keypad with PIC16F877A ?</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;This PIC microcontroller tutorial provides a simple method to interface any keypad (e-g 4x4 or 4x3 etc)&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;with PIC16F877A microcontroller. This code is written in C language using MPLAB with HI-TECH C compiler. You can download this code from the &#39;&lt;/span&gt;&lt;i style=&quot;background-color: white;&quot;&gt;Downloads&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&#39;&amp;nbsp;&lt;span style=&quot;font-family: inherit;&quot;&gt;section at the bottom of this page.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;In this post, it is assumed that you know, h&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;ow to interface LCD with PIC16F877A microcontroller, If you don&#39;t then please read&amp;nbsp;&lt;/span&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/10/137.html&quot; target=&quot;_blank&quot;&gt;this page&lt;/a&gt;.&lt;br /&gt;
&lt;ul&gt;&lt;/ul&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;The circuit required to interface keypad with PIC16F877A&lt;span style=&quot;color: purple;&quot;&gt;&lt;sup&gt;[1]&lt;/sup&gt;&amp;nbsp;&lt;/span&gt;is shown below.&lt;/span&gt;&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhu9NhJnUrGaPCdM5U88lIdo_ydZvWegSHmE77Z2sMsScj2l2dJzFFhC1FKlP0-8d9VJrjjAi6tGvK2kwq5oKli62XR49MNSTMXESeE6GMOvel6bnHmq920i9QOuQRTOzgwjwxoqv0Tq8g/s1600/PIC16F877A+Keypad+Circuit.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;410&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhu9NhJnUrGaPCdM5U88lIdo_ydZvWegSHmE77Z2sMsScj2l2dJzFFhC1FKlP0-8d9VJrjjAi6tGvK2kwq5oKli62XR49MNSTMXESeE6GMOvel6bnHmq920i9QOuQRTOzgwjwxoqv0Tq8g/s1600/PIC16F877A+Keypad+Circuit.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;i style=&quot;font-size: 12.8px;&quot;&gt;&lt;b&gt;Figure 1&lt;/b&gt;&lt;/i&gt;&lt;span style=&quot;font-size: 12.8px;&quot;&gt;. &amp;nbsp; Interfacing keypad with PIC16F877A circuit&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
The result of simulating the code in&amp;nbsp;&lt;span style=&quot;background-color: white;&quot;&gt;&lt;i&gt;Proteus&amp;nbsp;&lt;/i&gt;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;is shown above in the figure. This code is written in such a way that when you press any key from the keypad, then the value of that key is displayed on the LCD. For example, in the above figure LCD screen is displaying &#39;3&#39;, because this picture was taken after pressing &#39;3&#39; from the keypad.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;In the above circuit,&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;RD0,&amp;nbsp;RD1&amp;nbsp;and&amp;nbsp;RD4:7 pins are used to interface LCD with PIC16F877A&lt;/span&gt;. LCD is used here just to show the pressed key value. 8 pins of PORTB are used to interface 4x4 keypad. 4 pins are used to attach columns and 4 pins are used to attach rows and scanning algorithm code is used to check for any pressed key.&lt;br /&gt;
&lt;h3&gt;
&lt;span style=&quot;color: #38761d; font-size: x-large;&quot;&gt;Code&lt;/span&gt;&lt;/h3&gt;
The main function code is shown below.&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgOmsD45352SBXlOn3IL2C4MvSp3-k-higVQveQyzigGcybqppZBLU1bAwaHfckEOR6okS6M0InQXJEn3punPtax-AX8QDIdvGJPCW8eKCjcFTJWsPGUQW1_D5z4dlSBJ-Wgo6N-MEnDnI/s1600/PIC16F877+KEYPAD+MAIN+CODE.bmp&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgOmsD45352SBXlOn3IL2C4MvSp3-k-higVQveQyzigGcybqppZBLU1bAwaHfckEOR6okS6M0InQXJEn3punPtax-AX8QDIdvGJPCW8eKCjcFTJWsPGUQW1_D5z4dlSBJ-Wgo6N-MEnDnI/s1600/PIC16F877+KEYPAD+MAIN+CODE.bmp&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;i&gt;&lt;b&gt;Figure 2&lt;/b&gt;&lt;/i&gt;. &amp;nbsp; Main code for interfacing keypad with PIC16F877A&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
In the main function, keypad is initialized using&lt;i&gt;&amp;nbsp;InitKeypad()&lt;/i&gt;&amp;nbsp;function. Then LCD is initialized using&amp;nbsp;&lt;i&gt;InitLCD()&lt;/i&gt;&amp;nbsp;function. After that, in the&amp;nbsp;&lt;i&gt;while(1)&lt;/i&gt;&amp;nbsp;loop,&amp;nbsp;&lt;i&gt;GetKey()&lt;/i&gt;&amp;nbsp;function&amp;nbsp;is used to read any pressed key value.&amp;nbsp;&lt;i&gt;GetKey()&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;function is a blocking function, i-e code hangs in it until the user presses any key. After a key is pressed from the keypad, then LCD screen is cleared using&amp;nbsp;&lt;i&gt;ClearLCDScreen()&lt;/i&gt;&amp;nbsp;function and the value of pressed key is displayed on the LCD screen using&amp;nbsp;&lt;i&gt;WriteDataToLCD()&lt;/i&gt;&amp;nbsp;function and then again code starts to wait in the&amp;nbsp;&lt;i&gt;GetKey()&amp;nbsp;&lt;/i&gt;function for the user to press another key.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;The code used to initialize keypad is shown below.&lt;/span&gt;&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjQLQX26JITlwA2YdTn7s1Qo0Fgy49wvH0Tl0W8iEPdslICBgVXnBvKsZNlrSMipmmsWobHxarR32hw5u7KzvXrkmjFzM9wZKW8rqFlfatIVjkXSojHAMuz6DjClze5QlYChn7O2qXQ_TQ/s1600/PIC16F877+INIT+KEYPAD+CODE.bmp&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjQLQX26JITlwA2YdTn7s1Qo0Fgy49wvH0Tl0W8iEPdslICBgVXnBvKsZNlrSMipmmsWobHxarR32hw5u7KzvXrkmjFzM9wZKW8rqFlfatIVjkXSojHAMuz6DjClze5QlYChn7O2qXQ_TQ/s1600/PIC16F877+INIT+KEYPAD+CODE.bmp&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;i&gt;&lt;b&gt;Figure 3&lt;/b&gt;&lt;/i&gt;. &amp;nbsp; InitKeypad function code&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;In the&amp;nbsp;&lt;i&gt;InitKeypad()&lt;/i&gt;&amp;nbsp;function, PORTB is initialized to interface with the keypad. RB0 to RB3 pins are made output and RB4 to RB7 pins are made inputs. Then internal weak pull up is enabled on all the input pins&lt;/span&gt;&lt;sup style=&quot;background-color: white; color: purple;&quot;&gt;[2]&lt;/sup&gt;&lt;span style=&quot;background-color: white;&quot;&gt;.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;i&gt;GetKey()&lt;/i&gt;&amp;nbsp;function code is shown below.&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEglCsYucsP1CD-v2PoL-Bpu2Uh0azUEv_Qbf09KdueSmOvSOv5KpFdamQH7vKdyV9Tm8JtnXy1GslZ-AhZvQCZlDfukKpB0Ft7nruBxKbZhKTo1uhszNbZavJzMoE84wDiH-rylqVeeWhY/s1600/PIC16F877+GET+KEY+FUNC+CODE.bmp&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEglCsYucsP1CD-v2PoL-Bpu2Uh0azUEv_Qbf09KdueSmOvSOv5KpFdamQH7vKdyV9Tm8JtnXy1GslZ-AhZvQCZlDfukKpB0Ft7nruBxKbZhKTo1uhszNbZavJzMoE84wDiH-rylqVeeWhY/s1600/PIC16F877+GET+KEY+FUNC+CODE.bmp&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;i&gt;&lt;b&gt;Figure 4&lt;/b&gt;&lt;/i&gt;. &amp;nbsp; GetKey function code&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
In the&amp;nbsp;&lt;i&gt;GetKey()&lt;/i&gt;&amp;nbsp;function shown above, code hangs in the&amp;nbsp;&lt;i&gt;while(key==&#39;n&#39;)&lt;/i&gt;&amp;nbsp;loop until a key is pressed&lt;span style=&quot;background-color: white;&quot;&gt;.&amp;nbsp;&lt;i&gt;READ_SWITCHES()&amp;nbsp;&lt;/i&gt;function scans the whole keypad to detect any pressed key, if a key is pressed then the value of that key is returned from&lt;i&gt;&amp;nbsp;&lt;/i&gt;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&lt;i&gt;READ_SWITCHES()&lt;/i&gt;&amp;nbsp;function, otherwise&lt;i&gt;&amp;nbsp;&#39;n&#39;&lt;/i&gt;&amp;nbsp;is returned which means no key has been pressed&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;. In the end of function value of pressed key is returned.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
When key 1 is pressed then&amp;nbsp;&lt;i&gt;RowC&amp;nbsp;&lt;/i&gt;wire is shorted with&amp;nbsp;&lt;i&gt;C1&amp;nbsp;&lt;/i&gt;wire inside the keypad. Similarly, when key 9 is pressed then&amp;nbsp;&lt;i&gt;RowA&amp;nbsp;&lt;/i&gt;wire is shorted with&amp;nbsp;&lt;i&gt;C3&amp;nbsp;&lt;/i&gt;wire. This behavior is true for all the keys. Then the next question is, how to detect this behavior in the code ?&lt;br /&gt;
&lt;br /&gt;
We can detect pressed key value in the microcontroller using the &quot;Scanning Algorithm Code&quot;. This algorithm is written with the name of &#39;&lt;i&gt;READ_SWITCHES()&lt;/i&gt;&#39; function in the code. This function is shown below.&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEijhx0PinSNRMlchplPME434Gx2G_n9Y_CNAFZBBPmozjaRDmSSy4o7dl6hAI2B1f6J_iUwgbvJwn2Pg-Juf27Xs5NZrKG5HdspkS-scgUj0piS8BDuHgAQzS-xPDjYFZ2kWR8mjdWOH44/s1600/PIC16F877+READ+SWITCHES+CODE.bmp&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEijhx0PinSNRMlchplPME434Gx2G_n9Y_CNAFZBBPmozjaRDmSSy4o7dl6hAI2B1f6J_iUwgbvJwn2Pg-Juf27Xs5NZrKG5HdspkS-scgUj0piS8BDuHgAQzS-xPDjYFZ2kWR8mjdWOH44/s1600/PIC16F877+READ+SWITCHES+CODE.bmp&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;i&gt;&lt;b&gt;Figure 5&lt;/b&gt;&lt;/i&gt;. &amp;nbsp; Keypad scanning algorithm code&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
In the scanning algorithm ( i-e&amp;nbsp;&lt;i&gt;READ_SWITCHES()&lt;/i&gt;&amp;nbsp;function ), we test&lt;i&gt;&amp;nbsp;Row A&lt;/i&gt;&amp;nbsp;keys first by making&amp;nbsp;&lt;i&gt;RowA&amp;nbsp;&lt;/i&gt;wire equal to zero and making other rows one. Then each column is checked if it is equal to zero. Then&amp;nbsp;&lt;i&gt;RowB&amp;nbsp;&lt;/i&gt;wire is made zero, while making all other rows one. Then again each column is checked to detect if any&amp;nbsp;&lt;i&gt;RowB&amp;nbsp;&lt;/i&gt;key is pressed. This pattern is repeated for every row.&amp;nbsp;If no key is pressed, then &#39;n&#39; value is returned from the function.&lt;br /&gt;
&lt;br /&gt;
For example, if key 7 was pressed when this code was being executed then&amp;nbsp;&lt;i&gt;C1&amp;nbsp;&lt;/i&gt;will be zero ( Because keypad shorts the&amp;nbsp;corresponding&amp;nbsp;row and&amp;nbsp;column&amp;nbsp;wire ). &quot;if( C1 == 0 )&quot; condition will become true and there is a small delay of some micro seconds after which &quot;while( C1 == 0 );&quot; statement is present. This statement basically makes the controller to wait&amp;nbsp;until&amp;nbsp;the key is no longer pressed, to remove the&amp;nbsp;possibility&amp;nbsp;of reading one pressed key many times. After that, value of the key ( i-e &#39;7&#39; in this case ) is returned from the function.&lt;br /&gt;
&lt;br /&gt;
This code provides you&amp;nbsp;&lt;i&gt;Keypad.c&lt;/i&gt;&amp;nbsp;and&amp;nbsp;&lt;i&gt;Keypad.h&lt;/i&gt;&amp;nbsp;files, which you can use in any other project as you need. By simply calling&amp;nbsp;&lt;i&gt;InitKeypad()&lt;/i&gt;&amp;nbsp;and&lt;i&gt;&amp;nbsp;GetKey()&lt;/i&gt;&amp;nbsp;functions in your code, you can easily interface keypad with your PIC controller.&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;You can leave your comments in the comment section below.&lt;/span&gt;&lt;br /&gt;
&lt;h3&gt;
&lt;span style=&quot;color: blue; font-size: x-large;&quot;&gt;Notes and References&lt;/span&gt;&lt;/h3&gt;
&lt;sup&gt;&lt;span style=&quot;color: purple;&quot;&gt;[1] &amp;nbsp;&lt;/span&gt;&lt;/sup&gt;&lt;span style=&quot;background-color: white;&quot;&gt;In this circuit PIC16F877A is running on 20MHz crystal. You can change this crystal if you need. There is no particular reason for selecting 20MHz crystal. This code will work even for 4MHz crystal.&lt;/span&gt;&lt;br /&gt;
&lt;sup&gt;&lt;span style=&quot;color: purple;&quot;&gt;[2] &amp;nbsp;&lt;/span&gt;&lt;/sup&gt;&lt;span style=&quot;background-color: white;&quot;&gt;It is important to enable pull up on the input pins. Because these pins should be in high state when no key is pressed, so that scanning algorithm can work correctly. When a key is pressed, then the corresponding input pin becomes low and pressed key is detected.&lt;/span&gt;&lt;br /&gt;
&lt;div&gt;
&lt;h3&gt;
&lt;span style=&quot;color: red; font-size: x-large;&quot;&gt;Downloads&lt;/span&gt;&lt;/h3&gt;
&lt;/div&gt;
Keypad interfacing code using PIC16F877A was compiled in&amp;nbsp;MPLAB v8.85 with HI-TECH C v9.83 compiler and simulation was made in Proteus v7.10.&amp;nbsp;&lt;span style=&quot;background-color: white;&quot;&gt;To download code and&amp;nbsp;&lt;/span&gt;&lt;i style=&quot;background-color: white;&quot;&gt;Proteus&amp;nbsp;&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;simulation&amp;nbsp;&lt;/span&gt;&lt;a href=&quot;http://bit.ly/2ntcRdQ&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;click here&lt;/a&gt;&lt;span style=&quot;background-color: white;&quot;&gt;.&lt;/span&gt;&lt;br /&gt;
&lt;div&gt;
&lt;h3&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;span style=&quot;color: #274e13; font-size: large;&quot;&gt;Further Reading Suggestions&lt;/span&gt;&lt;/span&gt;&lt;/h3&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;You may also like,&lt;/span&gt;&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/08/129.html&quot;&gt;How to set PIC16F877A configuration bits in code ?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/08/130.html&quot;&gt;PIC16F877A LED blinking code&amp;nbsp;+ Proteus simulation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/10/137.html&quot;&gt;PIC16F877A LCD interfacing code (In 4bit mode)&amp;nbsp;+ Proteus simulation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2015/06/pic16f877a-based-digital-clock-using.html&quot;&gt;PIC16F877A based digital clock using LCD display (Code+Proteus simulation)&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://saeedsolutions.blogspot.com/feeds/6111420239454866446/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://saeedsolutions.blogspot.com/2016/01/how-to-interface-keypad-with-pic16f877a.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5869154326689252934/posts/default/6111420239454866446'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5869154326689252934/posts/default/6111420239454866446'/><link rel='alternate' type='text/html' href='http://saeedsolutions.blogspot.com/2016/01/how-to-interface-keypad-with-pic16f877a.html' title='How to interface keypad with PIC16F877A ?'/><author><name>Saeed</name><uri>http://www.blogger.com/profile/18064969138529094661</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhu9NhJnUrGaPCdM5U88lIdo_ydZvWegSHmE77Z2sMsScj2l2dJzFFhC1FKlP0-8d9VJrjjAi6tGvK2kwq5oKli62XR49MNSTMXESeE6GMOvel6bnHmq920i9QOuQRTOzgwjwxoqv0Tq8g/s72-c/PIC16F877A+Keypad+Circuit.png" height="72" width="72"/><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5869154326689252934.post-2839963862085193030</id><published>2016-01-23T19:55:00.000+05:00</published><updated>2017-04-02T15:05:02.204+05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="PIC micro-controller Projects"/><category scheme="http://www.blogger.com/atom/ns#" term="PIC16F877A Projects"/><title type='text'>How to implement free running counter in PIC16F877A using seven segment display?</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
This post provides the implementation of free running counter ( using c language ) for PIC16F877A microcontroller. This code is written in such a way that, the counter starts from a value of &#39;0&#39; ( displayed on the seven segment ) and then increments this value after every second. So, the seven segment display starts from &#39;0&#39; and then displays &#39;1&#39;, &#39;2&#39;, &#39;3&#39;, &#39;4&#39;, &#39;5&#39;, &#39;6&#39;, &#39;7&#39;, &#39;8&#39;, &#39;9&#39;, &#39;A&#39;, &#39;b&#39;, &#39;C&#39;, &#39;d&#39;, &#39;E&#39;, and in the end &#39;F&#39;. After displaying &#39;F&#39;, counter starts from &#39;0&#39; again and this pattern repeats forever.&lt;br /&gt;
&lt;br /&gt;
The code and&amp;nbsp;&lt;i&gt;Proteus&amp;nbsp;&lt;/i&gt;simulation is given in the &#39;&lt;i&gt;Downloads&lt;/i&gt;&#39; section at the bottom of this page. It is assumed that you know how to make an LED blink with&amp;nbsp;PIC16F877A microcontroller. If you don&#39;t, then please read&amp;nbsp;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/08/130.html&quot; target=&quot;_blank&quot;&gt;this post&lt;/a&gt;&amp;nbsp;first.&lt;br /&gt;
&lt;br /&gt;
The following figure shows the circuit of free running counter in&amp;nbsp;&lt;i&gt;Proteus&lt;/i&gt;.&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEizYB9JdjLhR4vHJiG_pcyWwKdcRtce8nVzrZ4pXfm_Vf01PfKF-4XDL7_X-eMOz6qVpRFV5G9bUPZ6HxOmURthcbcW4S2pumXUwKzXUpbyYKFxYGiZzjdfjDi-ZVr6L_5GWTsa_xJLMOo/s1600/PIC16F877A+Free+Running+Counter+Circuit.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;346&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEizYB9JdjLhR4vHJiG_pcyWwKdcRtce8nVzrZ4pXfm_Vf01PfKF-4XDL7_X-eMOz6qVpRFV5G9bUPZ6HxOmURthcbcW4S2pumXUwKzXUpbyYKFxYGiZzjdfjDi-ZVr6L_5GWTsa_xJLMOo/s1600/PIC16F877A+Free+Running+Counter+Circuit.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;i style=&quot;font-size: 12.8px;&quot;&gt;&lt;b&gt;Figure 1&lt;/b&gt;&lt;/i&gt;&lt;span style=&quot;font-size: 12.8px;&quot;&gt;. &amp;nbsp; Free running counter implementation using PIC16F877A&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
A crystal of 20 MHz is used here. You can use any crystal value from 0 to 20MHz with&amp;nbsp;PIC16F877A.&amp;nbsp;As we know that&amp;nbsp;PIC16F877A microcontroller has an architecture which executes an instruction in 4 CPU cycles&lt;span style=&quot;color: purple;&quot;&gt;&lt;sup&gt;[1]&lt;/sup&gt;&lt;/span&gt;, hence this 20MHz crystal makes this&amp;nbsp;PIC16F877A run at 5.0 MIPS (Million&amp;nbsp;of instructions per second).&lt;br /&gt;
&lt;br /&gt;
PORTB is used here to connect&amp;nbsp;PIC16F877A with the seven segment display. RB0 pin is attached with the &#39;a&#39; segment of the seven segment display. Similarly, RB1 pin is connected with &#39;b&#39; segment, RB2 pin is connected with &#39;c&#39; segment and so on.&lt;br /&gt;
&lt;br /&gt;
Common cathode&amp;nbsp;seven segment display is used in this example. You can easily modify this circuit and code for the common anode seven segment display as well.&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;color: #38761d; font-size: x-large;&quot;&gt;&lt;b&gt;Code&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
The following function is used in the code to display values on the seven segment display.&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi8eyVnXDT4fYxnC7WE3VQaQjSGkd9IhT57IA1-3w-v-9Vw2TUVetxA3xgxLW20JWu70fSbuCRskXa90-zknQ-QViqIwdHmTEV8wSjNAx3tAxg14JMgoaJWnbDMkOj5GFeTuCBJXL9jEzg/s1600/PIC16F877+7SEG+FUNC+CODE.bmp&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi8eyVnXDT4fYxnC7WE3VQaQjSGkd9IhT57IA1-3w-v-9Vw2TUVetxA3xgxLW20JWu70fSbuCRskXa90-zknQ-QViqIwdHmTEV8wSjNAx3tAxg14JMgoaJWnbDMkOj5GFeTuCBJXL9jEzg/s1600/PIC16F877+7SEG+FUNC+CODE.bmp&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;i&gt;&lt;b&gt;Figure 2.&lt;/b&gt;&lt;/i&gt; &amp;nbsp; &lt;i&gt;DisplayOn7Segment()&lt;/i&gt; function code&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
For example, to display &#39;0&#39; on the seven segment display, we can call this function in the main as&amp;nbsp;&lt;i&gt;DisplayOn7Segment(&#39;0&#39;);&lt;/i&gt;.&lt;br /&gt;
&lt;br /&gt;
The code for the main function is shown below.&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh-b-jQqDTR7zylfiMlGYyI2DsuZf0vG4fUhggzxA4xKCY-SLNWnznaS0j2a2ekuyJN3tCDi6kz7dN5SgPwojD7xZG3-r7ONwZF7gOcZ2wsRK5szHs8stZ9bGEuHZIAFwB9GW0hp4803VI/s1600/PIC16F877+7SEG+MAIN+CODE.bmp&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh-b-jQqDTR7zylfiMlGYyI2DsuZf0vG4fUhggzxA4xKCY-SLNWnznaS0j2a2ekuyJN3tCDi6kz7dN5SgPwojD7xZG3-r7ONwZF7gOcZ2wsRK5szHs8stZ9bGEuHZIAFwB9GW0hp4803VI/s1600/PIC16F877+7SEG+MAIN+CODE.bmp&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;i&gt;&lt;b&gt;Figure 3&lt;/b&gt;&lt;/i&gt;. &amp;nbsp; Main function for PIC16F877A free running counter&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
In the main function code, a variable &#39;&lt;i&gt;ch&lt;/i&gt;&#39; is displayed on the seven segment display. Then there is a delay of about 1 second. After that, a new value of&amp;nbsp;&lt;i&gt;ch&amp;nbsp;&lt;/i&gt;is selected using the switch statement. Then the new value present in&amp;nbsp;&lt;i&gt;ch&lt;/i&gt;&amp;nbsp;variable is displayed on the seven&amp;nbsp;segment. This process continues forever.&lt;br /&gt;
&lt;br /&gt;
You can leave your comments in the comment section below.&lt;br /&gt;
&lt;h3&gt;
&lt;span style=&quot;color: blue; font-size: x-large;&quot;&gt;Notes and References&lt;/span&gt;&lt;/h3&gt;
&lt;sup&gt;&lt;span style=&quot;color: purple;&quot;&gt;[1]&lt;/span&gt;&lt;/sup&gt;&amp;nbsp; From&amp;nbsp;PIC16F877A datasheet.&lt;br /&gt;
&lt;div&gt;
&lt;h3&gt;
&lt;span style=&quot;color: red; font-size: x-large;&quot;&gt;Downloads&lt;/span&gt;&lt;/h3&gt;
&lt;/div&gt;
Free running counter code using&amp;nbsp;&lt;span style=&quot;background-color: white;&quot;&gt;PIC16F877A w&lt;/span&gt;as compiled in&amp;nbsp;MPLAB v8.85 with HI-TECH C v9.83 compiler and simulation was made in Proteus v7.10.&amp;nbsp;&lt;span style=&quot;background-color: white;&quot;&gt;To download code and&amp;nbsp;&lt;/span&gt;&lt;i style=&quot;background-color: white;&quot;&gt;Proteus&amp;nbsp;&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;simulation&amp;nbsp;&lt;/span&gt;&lt;a href=&quot;http://bit.ly/2op3ZLc&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;click here&lt;/a&gt;&lt;span style=&quot;background-color: white;&quot;&gt;.&lt;/span&gt;&lt;br /&gt;
&lt;div&gt;
&lt;h3&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;span style=&quot;color: #274e13; font-size: large;&quot;&gt;Further Reading Suggestions&lt;/span&gt;&lt;/span&gt;&lt;/h3&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;You may also like,&lt;/span&gt;&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/08/129.html&quot;&gt;How to set PIC16F877A configuration bits in code ?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/08/130.html&quot;&gt;PIC16F877A LED blinking code&amp;nbsp;+ Proteus simulation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/08/132.html&quot;&gt;PIC16F877A PWM code&amp;nbsp;+ Proteus simulation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2015/06/pic16f877a-based-digital-clock-using.html&quot;&gt;PIC16F877A based digital clock using LCD display (Code+Proteus simulation)&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://saeedsolutions.blogspot.com/feeds/2839963862085193030/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://saeedsolutions.blogspot.com/2016/01/how-to-implement-free-running-counter.html#comment-form' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5869154326689252934/posts/default/2839963862085193030'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5869154326689252934/posts/default/2839963862085193030'/><link rel='alternate' type='text/html' href='http://saeedsolutions.blogspot.com/2016/01/how-to-implement-free-running-counter.html' title='How to implement free running counter in PIC16F877A using seven segment display?'/><author><name>Saeed</name><uri>http://www.blogger.com/profile/18064969138529094661</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEizYB9JdjLhR4vHJiG_pcyWwKdcRtce8nVzrZ4pXfm_Vf01PfKF-4XDL7_X-eMOz6qVpRFV5G9bUPZ6HxOmURthcbcW4S2pumXUwKzXUpbyYKFxYGiZzjdfjDi-ZVr6L_5GWTsa_xJLMOo/s72-c/PIC16F877A+Free+Running+Counter+Circuit.png" height="72" width="72"/><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5869154326689252934.post-808778658265668654</id><published>2016-01-21T21:40:00.002+05:00</published><updated>2017-04-02T15:04:12.826+05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="PIC micro-controller Projects"/><category scheme="http://www.blogger.com/atom/ns#" term="PIC16F877A Projects"/><title type='text'>How to display custom characters on LCD using PIC16F877A ?</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
This post explains the idea of creating custom characters on any LCD ( e-g on 16x2 LCD ) which has HD44780U controller in it. Almost all 16x2 or 20x2 LCDs have HD44780U controller in them&lt;sup&gt;&lt;span style=&quot;color: purple;&quot;&gt;[1]&lt;/span&gt;&lt;/sup&gt;. This controller provides the functionality of CGRAM ( Character Generator RAM ). We can write character patterns on this RAM and then they can be easily displayed on the LCD. The code for custom character generation using PIC16F877A microcontroller and&amp;nbsp;&lt;i&gt;Proteus&amp;nbsp;&lt;/i&gt;simulation can be downloaded from the &#39;&lt;i&gt;Downloads&lt;/i&gt;&#39; section at the bottom of this page.&lt;br /&gt;
&lt;br /&gt;
If you don&#39;t know how to interface LCD with PIC16F877A in 8bit mode, then you should read&amp;nbsp;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/08/136.html&quot; target=&quot;_blank&quot;&gt;this post&lt;/a&gt;&amp;nbsp;first.&amp;nbsp;The required circuit for displaying custom characters on LCD is shown below.&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjc99FZzaEC-carMU9QCNPGZM1zTkfdAxDqOTEwCuemmPjYygeUX6DoZl9EzbOiuFrEK7cD3p2rgfFa70m9VFfDQvu8kv5ixMKsyvpvg29bDimseX2HgcaoJjdFFjAsIrf_auzKOXj244Y/s1600/PIC16F877A+Custom+Code+CCT.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;293&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjc99FZzaEC-carMU9QCNPGZM1zTkfdAxDqOTEwCuemmPjYygeUX6DoZl9EzbOiuFrEK7cD3p2rgfFa70m9VFfDQvu8kv5ixMKsyvpvg29bDimseX2HgcaoJjdFFjAsIrf_auzKOXj244Y/s1600/PIC16F877A+Custom+Code+CCT.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;i&gt;&lt;b&gt;Figure 1&lt;/b&gt;&lt;/i&gt;. PIC16F877A interfacing with LCD in 8bit mode&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
PORTB is being used as data bus for the LCD. Also, RD1 pin is used as &lt;i&gt;RS &lt;/i&gt;(Register Select for LCD) and RD0 pin is used as &lt;i&gt;E &lt;/i&gt;(Enable pin for LCD).&lt;br /&gt;
&lt;br /&gt;
A crystal of 20 MHz is used here. You can use any crystal value from 3 to 20MHz in this circuit.&amp;nbsp;Close-up picture of the LCD is shown below.&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhDYqoKHUMixuMT38T9iosucEwtPgh0mIA03o2IET-9lKlP7oJ2wbha9aiqhpCPTispLrs15t-BTQDzhELXEdcHhbcvM0OSpXXruVMeaNXD-H5UZVe6qA9bjIPLgMWvcY_mElTLYYi4NK8/s1600/CUSTOM+CHARACTERS+ON+LCD.bmp&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;161&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhDYqoKHUMixuMT38T9iosucEwtPgh0mIA03o2IET-9lKlP7oJ2wbha9aiqhpCPTispLrs15t-BTQDzhELXEdcHhbcvM0OSpXXruVMeaNXD-H5UZVe6qA9bjIPLgMWvcY_mElTLYYi4NK8/s400/CUSTOM+CHARACTERS+ON+LCD.bmp&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;i&gt;&lt;b&gt;Figure 2&lt;/b&gt;&lt;/i&gt;. &amp;nbsp; Custom&amp;nbsp;characters&amp;nbsp;displayed on LCD&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;i&gt;Figure 2&lt;/i&gt; shows the custom characters displayed on the LCD by running the code.&lt;br /&gt;
&lt;h3&gt;
&lt;span style=&quot;color: purple; font-size: large;&quot;&gt;How to generate custom character ?&lt;/span&gt;&lt;/h3&gt;
I am going to&amp;nbsp;explain&amp;nbsp;custom character generation using an example. In the&lt;i&gt; figure 2&lt;/i&gt;, first character displayed on the LCD is named &#39;Curvy Object&#39; in the code. To generate this character, First make a box of 8 by 5 dots. Then fill the dots required to make the custom character you want to display. Following figure explains this concept.&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEixC6ZHu2_plb_NXtZP3yKv54EwIMiJIWjt0HFiXdh6Fm36_ACO6PoN1VfGGy9wCx3GwIynMbas51oSehWJdpNd8OHSGiVg12J6R9mRDkbSwRLInyxtGZiYafzTW7TXmDxDJMR-UlhmhjE/s1600/Custom+Character+Creation.JPG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;430&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEixC6ZHu2_plb_NXtZP3yKv54EwIMiJIWjt0HFiXdh6Fm36_ACO6PoN1VfGGy9wCx3GwIynMbas51oSehWJdpNd8OHSGiVg12J6R9mRDkbSwRLInyxtGZiYafzTW7TXmDxDJMR-UlhmhjE/s640/Custom+Character+Creation.JPG&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;i&gt;&lt;b&gt;Figure 3&lt;/b&gt;&lt;/i&gt;. &amp;nbsp; &amp;nbsp;Custom character code array generation&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
After filling the dots, find out the value of each line. For example, ( in the figure 3 for &#39;Curvy Object&#39; creation ) first line has a value of 0x01, because only first dot ( at 2&lt;sup&gt;0&lt;/sup&gt;&amp;nbsp;position ) needs to be displayed. Then second line has a value of 0x02, because only one dot at 2&lt;sup&gt;1&lt;/sup&gt;&amp;nbsp;position needs to be displayed. Similarly, third line has a value of 0x04, fourth line has a value of 0x08 and fifth line has a value of 0x10. Sixth line has the two dots to be displayed, hence 0x10 + 0x01 = 0x11 is it&#39;s value. Seventh line has all the dots to be displayed, which corresponds to a value of 0x10 + 0x08 + 0x04 + 0x02 + 0x01 = 0x1F. Eight line has no dots to be displayed, so it has a value of zero. After finding out these values make an array of these values as shown in the figure 3. This array named &#39;&lt;i&gt;CurvyObject&lt;/i&gt;&#39;, which has 8 bytes of data will be transmitted to the CGRAM of LCD.&lt;br /&gt;
&lt;br /&gt;
We can program 8 custom characters when we are using 5x8 font in the LCD settings. And we can program 4 custom characters&amp;nbsp;when we are using 5x10 font in the LCD settings. I have used 5x8 font in the code, hence at most 8 custom characters can be programmed. Using the approach to make the custom character described above, 2 custom characters were defined in the code. These custom character arrays are shown below&lt;sup&gt;&lt;span style=&quot;color: purple;&quot;&gt;[2]&lt;/span&gt;&lt;/sup&gt;.&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj_aM2QXig6Y4H6L8fkF_NknGxbqxq5e8FFWqpOZmi9GJjg-tTn-gtUJ8ZI0Xm7wbXXdSrvVD-Z-NqpfXcqmhE1Byx8UNQ665wrZ856V9k1BBJXjSIXNSg4dVzisbVg47tDJwbXVm3gCPI/s1600/CUSTOM+CHARS+DEF.bmp&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj_aM2QXig6Y4H6L8fkF_NknGxbqxq5e8FFWqpOZmi9GJjg-tTn-gtUJ8ZI0Xm7wbXXdSrvVD-Z-NqpfXcqmhE1Byx8UNQ665wrZ856V9k1BBJXjSIXNSg4dVzisbVg47tDJwbXVm3gCPI/s1600/CUSTOM+CHARS+DEF.bmp&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;i&gt;&lt;b&gt;Figure 4&lt;/b&gt;&lt;/i&gt;. &amp;nbsp; &amp;nbsp;Custom character arrays used in the code&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;color: orange; font-size: x-large;&quot;&gt;Code&lt;/span&gt;&lt;/h3&gt;
The code for the&lt;i&gt;&amp;nbsp;InitLCD()&lt;/i&gt;&amp;nbsp;function is shown below&lt;sup&gt;&lt;span style=&quot;color: purple;&quot;&gt;[3]&lt;/span&gt;&lt;/sup&gt;. This function is used to initialize the LCD properly with the custom characters.&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgRGflXt_HT2KyDz2gMXqqLFs-YM9upe4D9YJ8bkeZIilRE_36KWa3OjFTKyE_BkhI71F0anrcAMC100OhTh1NrS-2WjV9GaQzuPQrUqqgEud7Ny77Jf8A69RsMsTW9QkD2_nk7s5YbtEY/s1600/LCD+INIT+FUNC+CODE.bmp&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgRGflXt_HT2KyDz2gMXqqLFs-YM9upe4D9YJ8bkeZIilRE_36KWa3OjFTKyE_BkhI71F0anrcAMC100OhTh1NrS-2WjV9GaQzuPQrUqqgEud7Ny77Jf8A69RsMsTW9QkD2_nk7s5YbtEY/s1600/LCD+INIT+FUNC+CODE.bmp&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;i&gt;&lt;b&gt;Figure 5&lt;/b&gt;&lt;/i&gt;. &amp;nbsp; LCD Initialization code&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&#39;&lt;i&gt;LCDBuildChar()&lt;/i&gt;&#39; function is used to transfer the custom character array to the CGRAM of the LCD. This function is programming 2 custom characters in the CGRAM of the LCD. Note that &#39;&lt;i&gt;Curvy Object&lt;/i&gt;&#39; is being written at 0x00 position and &#39;&lt;i&gt;Square Box&lt;/i&gt;&#39; at 0x01 position&amp;nbsp;of CGRAM in LCD.&lt;br /&gt;
&lt;br /&gt;
&#39;&lt;i&gt;LCDBuildChar()&lt;/i&gt;&#39;&amp;nbsp;function is shown below.&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgJ9IbnMVpAB2w3LQMlN6FygRZOaGt2BE-CjIsdiA-lbFx9F0m2Loyow-2QO2nztvgb7b8qA2s4aByOaZhZtNJwoLh115LzYrmVoEtEzJNcz1u_ewlVLcAfgB0JhmGjJEtfnVR73fmCBDA/s1600/LCD+CHAR+BUILD+FUNC+CODE.bmp&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgJ9IbnMVpAB2w3LQMlN6FygRZOaGt2BE-CjIsdiA-lbFx9F0m2Loyow-2QO2nztvgb7b8qA2s4aByOaZhZtNJwoLh115LzYrmVoEtEzJNcz1u_ewlVLcAfgB0JhmGjJEtfnVR73fmCBDA/s1600/LCD+CHAR+BUILD+FUNC+CODE.bmp&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;i&gt;&lt;b&gt;Figure 6&lt;/b&gt;&lt;/i&gt;. &amp;nbsp; &amp;nbsp;Custom Character Build Function&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
For the details of this function, please refer to the &quot;CGRAM Programming Section&quot; of&amp;nbsp;HD44780U datasheet.&lt;br /&gt;
&lt;br /&gt;
After the programming of these custom characters, we can display them on the LCD using the&amp;nbsp;&lt;i&gt;WriteDataToLCD()&amp;nbsp;&lt;/i&gt;function. For example, to display &#39;&lt;i&gt;Curvy Object&lt;/i&gt;&#39; on the LCD just write&amp;nbsp;&lt;i&gt;WriteDataToLCD&lt;/i&gt;&lt;i&gt;(0x00); &lt;/i&gt;because this custom character was stored at 0x00 position in CGRAM. Similarly, to display &#39;Square Box&#39; on the LCD, just write&amp;nbsp;&lt;i&gt;WriteDataToLCD&lt;/i&gt;&lt;i&gt;(0x01);&lt;/i&gt;&amp;nbsp;in the main function. The code for the main function is displayed below.&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi5ezme5Pib12DRDXBhiQg8oyNcslq2F5P8AO7cCzdKLM_B1sm1Hvlo7FnJ8s32uz0aKY87R7armTpv9cVDNolfrg2XDVBK3I6XbslFDfH4pSajIcFKmpLALoO0tWgVoziXPUdtoVcrwps/s1600/LCD+MAIN+CODE.bmp&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi5ezme5Pib12DRDXBhiQg8oyNcslq2F5P8AO7cCzdKLM_B1sm1Hvlo7FnJ8s32uz0aKY87R7armTpv9cVDNolfrg2XDVBK3I6XbslFDfH4pSajIcFKmpLALoO0tWgVoziXPUdtoVcrwps/s1600/LCD+MAIN+CODE.bmp&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;i&gt;&lt;b&gt;Figure 7&lt;/b&gt;&lt;/i&gt;. &amp;nbsp; Main function code for calling custom characters&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
In the main function, after initializing the LCD, both custom characters are being displayed on the LCD. You can change the custom character array to change it&#39;s shape. And then&amp;nbsp;&lt;i&gt;WriteDataToLCD&lt;/i&gt;&lt;i&gt;()&amp;nbsp;&lt;/i&gt;function can be used to display that new custom character on the LCD. It is that easy.&lt;br /&gt;
&lt;br /&gt;
You can leave your comments in the comment section below.&lt;br /&gt;
&lt;h3&gt;
&lt;span style=&quot;color: blue; font-size: x-large;&quot;&gt;Notes and References&lt;/span&gt;&lt;/h3&gt;
&lt;sup&gt;&lt;span style=&quot;color: purple;&quot;&gt;[1]&lt;/span&gt;&lt;/sup&gt;&amp;nbsp;&amp;nbsp;Most 16x2 LCDs have HD44780U controller inside them. So, the datasheet for HD44780U controller explains all the commands you can give to the LCD.&amp;nbsp;You can get&amp;nbsp;&lt;i&gt;HD44780U controller datasheet&lt;/i&gt;&amp;nbsp;from Google.&lt;br /&gt;
&lt;sup&gt;&lt;span style=&quot;color: purple;&quot;&gt;[2]&lt;/span&gt;&lt;/sup&gt;&amp;nbsp;You can match these custom character array definitions with the&amp;nbsp;characters&amp;nbsp;displayed on the LCD in &lt;i&gt;Figure 2&lt;/i&gt;.&lt;br /&gt;
&lt;sup&gt;&lt;span style=&quot;color: purple;&quot;&gt;[3]&lt;/span&gt;&lt;/sup&gt;&amp;nbsp;LCD initialization procedure is also given in the HD44780U controller datasheet.&lt;br /&gt;
&lt;div&gt;
&lt;h3&gt;
&lt;span style=&quot;color: red; font-size: x-large;&quot;&gt;Downloads&lt;/span&gt;&lt;/h3&gt;
&lt;/div&gt;
Custom character display on LCD code for PIC16F877A was compiled in&amp;nbsp;MPLAB v8.85 with HI-TECH C v9.83 compiler and simulation was made in Proteus v7.10.&amp;nbsp;&lt;span style=&quot;background-color: white;&quot;&gt;To download code and&amp;nbsp;&lt;/span&gt;&lt;i style=&quot;background-color: white;&quot;&gt;Proteus&amp;nbsp;&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;simulation&amp;nbsp;&lt;/span&gt;&lt;a href=&quot;http://bit.ly/2nKKv0x&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;click here&lt;/a&gt;&lt;span style=&quot;background-color: white;&quot;&gt;.&lt;/span&gt;&lt;br /&gt;
&lt;div&gt;
&lt;h3&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;span style=&quot;color: #274e13; font-size: large;&quot;&gt;Further Reading Suggestions&lt;/span&gt;&lt;/span&gt;&lt;/h3&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;You may also like,&lt;/span&gt;&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2015/06/pic16f877a-based-digital-clock-using.html&quot;&gt;PIC16F877A based digital clock using LCD display (Code+Proteus simulation)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/08/135.html&quot;&gt;PIC16F877A UART code + Proteus simulation&lt;/a&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/12/pic16f877a-lcd-code-proteus-simulation.html&quot;&gt;PIC16F877A LCD code&amp;nbsp;+ Proteus simulation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/08/132.html&quot;&gt;PIC16F877A PWM code + Proteus simulation&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://saeedsolutions.blogspot.com/feeds/808778658265668654/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://saeedsolutions.blogspot.com/2016/01/how-to-display-custom-characters-on-lcd.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5869154326689252934/posts/default/808778658265668654'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5869154326689252934/posts/default/808778658265668654'/><link rel='alternate' type='text/html' href='http://saeedsolutions.blogspot.com/2016/01/how-to-display-custom-characters-on-lcd.html' title='How to display custom characters on LCD using PIC16F877A ?'/><author><name>Saeed</name><uri>http://www.blogger.com/profile/18064969138529094661</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjc99FZzaEC-carMU9QCNPGZM1zTkfdAxDqOTEwCuemmPjYygeUX6DoZl9EzbOiuFrEK7cD3p2rgfFa70m9VFfDQvu8kv5ixMKsyvpvg29bDimseX2HgcaoJjdFFjAsIrf_auzKOXj244Y/s72-c/PIC16F877A+Custom+Code+CCT.png" height="72" width="72"/><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5869154326689252934.post-7087040715560445935</id><published>2015-06-26T15:25:00.000+05:00</published><updated>2017-04-02T15:03:30.915+05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="PIC micro-controller Projects"/><category scheme="http://www.blogger.com/atom/ns#" term="PIC16F877A Projects"/><title type='text'>PIC16F877A based digital clock using LCD display (Code+Proteus simulation)</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;This PIC16F877A microcontroller tutorial answers the question,&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&quot; How to implement a digital clock using PIC16F877A ? &quot;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;br /&gt;Using PIC16 simulator (&lt;i&gt;Proteus&lt;/i&gt;) you can verify this digital clock code and change it according to your needs.&amp;nbsp;This code is written in C language using MPLAB with HI-TECH C compiler. You can download this code from the &#39;&lt;i&gt;Downloads&lt;/i&gt;&#39;&amp;nbsp;&lt;span style=&quot;font-family: inherit;&quot;&gt;section at the bottom of this page.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;In this article, it is assumed that you know,&lt;/span&gt;&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style=&quot;background-color: white;&quot;&gt;How to interface LCD with PIC16F877A microcontroller. If you don&#39;t then please read&amp;nbsp;&lt;/span&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/10/137.html&quot; target=&quot;_blank&quot;&gt;this page&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;How to configure timer0 of&amp;nbsp;&lt;span style=&quot;background-color: white;&quot;&gt;PIC16F877A&amp;nbsp;&lt;/span&gt;microcontroller.&amp;nbsp;&amp;nbsp;&lt;span style=&quot;background-color: white;&quot;&gt;If you don&#39;t then please read&amp;nbsp;&lt;/span&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2015/06/pic16f877a-timer0-code-proteus.html&quot; target=&quot;_blank&quot;&gt;this page&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;
The following diagram (made in&amp;nbsp;&lt;i&gt;Proteus&lt;/i&gt;) shows the PIC microcontroller circuit diagram.&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjF_uPLWkQvZGzLUOUrhz3SrcHbnSO1_PNlPDEnrMUKdK7B2nF_PLrX-CZfjpUcYx0YJv_XbiFu08R2sYbekMYESyM9oLLEGCrp9kXiv0uLNpSGS6kIkTzz-kfNgHzDmVEw0nPfekn9jqE/s1600/PIC16F877A+Digital+Clock+cct.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;300&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjF_uPLWkQvZGzLUOUrhz3SrcHbnSO1_PNlPDEnrMUKdK7B2nF_PLrX-CZfjpUcYx0YJv_XbiFu08R2sYbekMYESyM9oLLEGCrp9kXiv0uLNpSGS6kIkTzz-kfNgHzDmVEw0nPfekn9jqE/s1600/PIC16F877A+Digital+Clock+cct.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;span style=&quot;font-size: 12.8000001907349px;&quot;&gt;&lt;i&gt;Figure 1&lt;/i&gt;. &amp;nbsp; PIC16F877A based digital clock&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
The above figure was taken after 2 minute and 01 seconds of code&amp;nbsp;&lt;span style=&quot;background-color: white;&quot;&gt;simulation&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;in&amp;nbsp;&lt;/span&gt;&lt;i style=&quot;background-color: white;&quot;&gt;Proteus&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;. In the code, timer0 is used as the base for digital clock generation. Timer0 is used here to generate 1msec interrupts. After every 1msec a global variable named&amp;nbsp;&lt;/span&gt;&lt;i style=&quot;background-color: white;&quot;&gt;msCounter&amp;nbsp;&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;increments. When&amp;nbsp;&lt;/span&gt;&lt;i style=&quot;background-color: white;&quot;&gt;msCounter&amp;nbsp;&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;reaches a value of 1000 then another global variable named&amp;nbsp;&lt;/span&gt;&lt;i style=&quot;background-color: white;&quot;&gt;secCounter&amp;nbsp;&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;increments and this process repeats itself. Similarly, when&amp;nbsp;&lt;i&gt;secCounter&amp;nbsp;&lt;/i&gt;reaches 60, then &amp;nbsp;&lt;i&gt;minCounter&amp;nbsp;&lt;/i&gt;increments. And when&amp;nbsp;&lt;i&gt;minCounter&amp;nbsp;&lt;/i&gt;reaches 60 then&amp;nbsp;&lt;i&gt;hrCounter&amp;nbsp;&lt;/i&gt;increments. This process continues until&amp;nbsp;&lt;i&gt;hrCounter&amp;nbsp;&lt;/i&gt;reaches 24 then all of these variables reset their values.&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;LCD is updated with the new values of&amp;nbsp;&lt;/span&gt;&lt;i&gt;hrCounter&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;,&amp;nbsp;&lt;/span&gt;&lt;i&gt;minCounter&amp;nbsp;&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;and&amp;nbsp;&lt;/span&gt;&lt;i&gt;secCounter&amp;nbsp;&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;after every second.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;A crystal of 4MHz value is used in this circuit, which makes this PIC16F877A run at a speed of 1MIPS (Million of instructions per second).&lt;/span&gt;&lt;br /&gt;
&lt;h3&gt;
&lt;span style=&quot;color: #38761d; font-size: x-large;&quot;&gt;Code&lt;/span&gt;&lt;/h3&gt;
The main function code is shown below.&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg2189EApHwybdP-6ASJP-ybKlBAz_kVGdDrIsy8k68E4MN6mfmhd1grUVrQyD0v1qBycoYfeJ3HRkHcvQauDwSjikc2P-kAnE6_FMFLlAN7cMSVTD_rEkoyKiKBjebll8oKTsLJZxkuH0/s1600/PIC16F877A+Digital+Clock+Main+Code.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg2189EApHwybdP-6ASJP-ybKlBAz_kVGdDrIsy8k68E4MN6mfmhd1grUVrQyD0v1qBycoYfeJ3HRkHcvQauDwSjikc2P-kAnE6_FMFLlAN7cMSVTD_rEkoyKiKBjebll8oKTsLJZxkuH0/s1600/PIC16F877A+Digital+Clock+Main+Code.png&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;span style=&quot;font-size: 12.8000001907349px;&quot;&gt;&lt;i&gt;Figure 2.&lt;/i&gt; &amp;nbsp; Main function code for digital clock on PIC16F877A&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
In the main function, firstly LCD is initialized using&amp;nbsp;&lt;i&gt;InitLCD()&amp;nbsp;&lt;/i&gt;function. Then Timer0 is initialized to generate 1msec interrupts. After that, in the&lt;i&gt;&amp;nbsp;while(1)&lt;/i&gt;&amp;nbsp;loop, whenever&amp;nbsp;&lt;i&gt;msCounter&amp;nbsp;&lt;/i&gt;reaches a value of zero&lt;sup&gt;&lt;span style=&quot;color: purple;&quot;&gt;[1]&lt;/span&gt;&lt;/sup&gt;, then new values of&amp;nbsp;&lt;i&gt;hrCounter&lt;/i&gt;,&amp;nbsp;&lt;i&gt;minCounter&amp;nbsp;&lt;/i&gt;and&amp;nbsp;&lt;i&gt;secCounter&amp;nbsp;&lt;/i&gt;are updated on the LCD.&amp;nbsp;&lt;i&gt;UpdateTimeCounters()&lt;/i&gt;&amp;nbsp;function is called every time and it corrects the values of every counter variable depending upon the value of&amp;nbsp;&lt;i&gt;msCounter&lt;/i&gt;,&amp;nbsp;which&amp;nbsp;is incremented in the ISR function of timer0 (shown below).&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgd_O_HdYcPeeqqwgKFvW58yDBuzC7SEV5uy5ju3WUzarb-_FohGpC65z-vNkZk6VmEBTk5RU_g__hCJQL0pPuVqRu5k0pP5jowENBoUYBLKD_H1p9JVlmXQALjM820lr0L1wjVdAQV8JM/s1600/PIC16F877A+Digital+Clock+ISR+Code.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgd_O_HdYcPeeqqwgKFvW58yDBuzC7SEV5uy5ju3WUzarb-_FohGpC65z-vNkZk6VmEBTk5RU_g__hCJQL0pPuVqRu5k0pP5jowENBoUYBLKD_H1p9JVlmXQALjM820lr0L1wjVdAQV8JM/s1600/PIC16F877A+Digital+Clock+ISR+Code.png&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;i&gt;Figure 3.&lt;/i&gt; &amp;nbsp; Timer0 ISR code for digital clock&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
In the above figure, interrupt code for timer0 is shown. Timer0 expires after every 1msec and an interrupt is generated which executes&lt;i&gt;&amp;nbsp;interrupt ISR()&lt;/i&gt;&amp;nbsp;function shown above. In this function, timer0 register is initialized to a value of 0x08 which makes timer0 to expire after&amp;nbsp;exactly&amp;nbsp;1msec. After that, timer0 interrupt flag is cleared and&amp;nbsp;&lt;i&gt;msCounter&amp;nbsp;&lt;/i&gt;variable&amp;nbsp;is incremented. So, this means that&amp;nbsp;&lt;i&gt;msCounter&amp;nbsp;&lt;/i&gt;variable increments after every 1msec.&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;The code used to initialize Timer0 is shown below.&lt;/span&gt;&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgyAYH_gsHrWMFXfJZzO7PF3E-zC8EcKb5N9yV9Qsuif1_FBElj8C5poCdPw8B6vnCT2TW31WEpDWb1cvVgOhI91pMUAMvCpfpz82yPnPkpJVdfGhG8kYHnvpItqWQ2u7EbAbYPZzU-9aE/s1600/PIC16F877+CLOCK+TIMER+CODE.bmp&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgyAYH_gsHrWMFXfJZzO7PF3E-zC8EcKb5N9yV9Qsuif1_FBElj8C5poCdPw8B6vnCT2TW31WEpDWb1cvVgOhI91pMUAMvCpfpz82yPnPkpJVdfGhG8kYHnvpItqWQ2u7EbAbYPZzU-9aE/s1600/PIC16F877+CLOCK+TIMER+CODE.bmp&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;i&gt;Figure 4&lt;/i&gt;. &amp;nbsp; Init1msecTimerInterrupts function code&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;In the function&lt;i&gt;&amp;nbsp;Init1msecTimerInterrupts()&lt;/i&gt;, timer0 is initialized to generate an interrupt after every 1msec. Timer0 counts from 0 to 249 and a prescalar of 1:4 means that an interrupt will be generated after every 250x4 = 1msec time.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;i&gt;UpdateTimeCounters()&lt;/i&gt;&amp;nbsp;function code is shown below.&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiIGlLtL6ED51JIZq3onWQqTe_Qj80mZ7G4-p8f9ll_rnu7y3ylrH4cJYZyJx5oPoy-qsUrYa_Ffu8QtUrSqlLyQuLjy2REczTM91l4Zq2-k1O_0fPX3L7zyNHHaW4A3K1GJEQlSbmcL3s/s1600/PIC16F877A+Digital+Clock+Timer+Counter+Update+Code.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiIGlLtL6ED51JIZq3onWQqTe_Qj80mZ7G4-p8f9ll_rnu7y3ylrH4cJYZyJx5oPoy-qsUrYa_Ffu8QtUrSqlLyQuLjy2REczTM91l4Zq2-k1O_0fPX3L7zyNHHaW4A3K1GJEQlSbmcL3s/s1600/PIC16F877A+Digital+Clock+Timer+Counter+Update+Code.png&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;i&gt;Figure 5&lt;/i&gt;. &amp;nbsp; UpdateTimeCounter function code&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
In this function it is clear that w&lt;span style=&quot;background-color: white;&quot;&gt;hen&amp;nbsp;&lt;/span&gt;&lt;i style=&quot;background-color: white;&quot;&gt;msCounter&amp;nbsp;&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;reaches a value of 1000 then&amp;nbsp;&lt;/span&gt;&lt;i style=&quot;background-color: white;&quot;&gt;secCounter&amp;nbsp;&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;increments and&amp;nbsp;&lt;i&gt;msCounter&amp;nbsp;&lt;/i&gt;becomes zero. Similarly, when&amp;nbsp;&lt;i&gt;secCounter&amp;nbsp;&lt;/i&gt;reaches 60, then &amp;nbsp;&lt;i&gt;minCounter&amp;nbsp;&lt;/i&gt;increments and&amp;nbsp;&lt;/span&gt;&lt;i&gt;secCounter&amp;nbsp;&lt;/i&gt;becomes zero&lt;span style=&quot;background-color: white;&quot;&gt;. And when&amp;nbsp;&lt;/span&gt;&lt;i&gt;minCounter&amp;nbsp;&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;reaches 60 then&amp;nbsp;&lt;/span&gt;&lt;i&gt;hrCounter&amp;nbsp;&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;increments&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;and&amp;nbsp;&lt;/span&gt;&lt;i&gt;minCounter&amp;nbsp;&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;becomes zer&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;o&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;. This process continues until&amp;nbsp;&lt;/span&gt;&lt;i style=&quot;background-color: white;&quot;&gt;hrCounter&amp;nbsp;&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;reaches 24 then all of these variables reset their values.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;This code is intended to be a simple example of how PIC16F877A can be used to make a digital clock display. You can leave your comments in the comment section below.&lt;/span&gt;&lt;br /&gt;
&lt;h3&gt;
&lt;span style=&quot;color: blue; font-size: x-large;&quot;&gt;Notes and References&lt;/span&gt;&lt;/h3&gt;
&lt;sup&gt;&lt;span style=&quot;color: purple;&quot;&gt;[1] &amp;nbsp;&lt;/span&gt;&lt;/sup&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&lt;i&gt;msCounter&lt;/i&gt;&amp;nbsp;increments to a value of 1000, then resets to 0 and starts to increment again. So,&amp;nbsp;&lt;i&gt;msCounter&amp;nbsp;&lt;/i&gt;becomes zero again after every 1 second exactly. So LCD updates with new time value after every second.&lt;/span&gt;&lt;br /&gt;
&lt;div&gt;
&lt;h3&gt;
&lt;span style=&quot;color: red; font-size: x-large;&quot;&gt;Downloads&lt;/span&gt;&lt;/h3&gt;
&lt;/div&gt;
Digital clock display code using PIC16F877A was compiled in&amp;nbsp;MPLAB v8.85 with HI-TECH C v9.83 compiler and simulation was made in Proteus v7.10.&amp;nbsp;&lt;span style=&quot;background-color: white;&quot;&gt;To download code and&amp;nbsp;&lt;/span&gt;&lt;i style=&quot;background-color: white;&quot;&gt;Proteus&amp;nbsp;&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;simulation&amp;nbsp;&lt;/span&gt;&lt;a href=&quot;http://bit.ly/2nKDyMP&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;click here&lt;/a&gt;&lt;span style=&quot;background-color: white;&quot;&gt;.&lt;/span&gt;&lt;br /&gt;
&lt;div&gt;
&lt;h3&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;span style=&quot;color: #274e13; font-size: large;&quot;&gt;Further Reading Suggestions&lt;/span&gt;&lt;/span&gt;&lt;/h3&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;You may also like,&lt;/span&gt;&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/08/129.html&quot;&gt;How to set PIC16F877A configuration bits in code ?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/08/135.html&quot;&gt;PIC16F877A UART code + Proteus simulation&lt;/a&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/12/pic16f877a-lcd-code-proteus-simulation.html&quot;&gt;PIC16F877A LCD code&amp;nbsp;+ Proteus simulation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/08/132.html&quot;&gt;PIC16F877A PWM code + Proteus simulation&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://saeedsolutions.blogspot.com/feeds/7087040715560445935/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://saeedsolutions.blogspot.com/2015/06/pic16f877a-based-digital-clock-using.html#comment-form' title='25 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5869154326689252934/posts/default/7087040715560445935'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5869154326689252934/posts/default/7087040715560445935'/><link rel='alternate' type='text/html' href='http://saeedsolutions.blogspot.com/2015/06/pic16f877a-based-digital-clock-using.html' title='PIC16F877A based digital clock using LCD display (Code+Proteus simulation)'/><author><name>Saeed</name><uri>http://www.blogger.com/profile/18064969138529094661</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjF_uPLWkQvZGzLUOUrhz3SrcHbnSO1_PNlPDEnrMUKdK7B2nF_PLrX-CZfjpUcYx0YJv_XbiFu08R2sYbekMYESyM9oLLEGCrp9kXiv0uLNpSGS6kIkTzz-kfNgHzDmVEw0nPfekn9jqE/s72-c/PIC16F877A+Digital+Clock+cct.png" height="72" width="72"/><thr:total>25</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5869154326689252934.post-2289723697597268974</id><published>2015-06-24T16:20:00.000+05:00</published><updated>2017-04-02T15:02:50.805+05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="PIC micro-controller Projects"/><category scheme="http://www.blogger.com/atom/ns#" term="PIC16F877A Projects"/><title type='text'>PIC16F877A timer1 code + Proteus simulation</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;This PIC16F877A microcontroller tutorial answers the question,&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&quot; How to use timer1 of PIC16F877A and how to handle its interrupts? &quot;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;U&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;sing PIC16 simulator (&lt;/span&gt;&lt;i&gt;Proteus&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;) you can verify this PIC timer1 code and change it according to your needs.&amp;nbsp;This code is written in C language using MPLAB with HI-TECH C compiler. You can download this code from the &#39;&lt;/span&gt;&lt;i&gt;Downloads&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&#39;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;section at the bottom of this page.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;It is assumed that you know how to blink an LED with PIC16F877A microcontroller. If you don&#39;t then please read&amp;nbsp;&lt;/span&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/08/130.html&quot; target=&quot;_blank&quot;&gt;this page&lt;/a&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;first, before proceeding with this article.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;br /&gt;&lt;/span&gt;The following diagram (made in&amp;nbsp;&lt;i&gt;Proteus&lt;/i&gt;) shows the PIC microcontroller circuit diagram.&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjJntA2D2YqggpiwLEFEpWM_P42RT7D6mVGH8LelyLxx-5irmVXt008vnHhOmJj_Q2I1xXxfJE0AhFMMzj6XmjBCJza2gfIZgcgykiW6QYWKhy0CVN9zoBDEXg8LsvSSk1QKmPlUW7UZr0/s1600/PIC16F877A+Timer1+cct.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;396&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjJntA2D2YqggpiwLEFEpWM_P42RT7D6mVGH8LelyLxx-5irmVXt008vnHhOmJj_Q2I1xXxfJE0AhFMMzj6XmjBCJza2gfIZgcgykiW6QYWKhy0CVN9zoBDEXg8LsvSSk1QKmPlUW7UZr0/s1600/PIC16F877A+Timer1+cct.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;span style=&quot;font-size: 12.8000001907349px;&quot;&gt;&lt;i&gt;Figure 1.&lt;/i&gt; &amp;nbsp; PIC16F877A timer1 simulation&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
In this circuit, PIC16F877A is running on external crystal of 20MHz value. RB0 pin is toggled every time timer1 expires and executes it&#39;s ISR&lt;span style=&quot;color: purple;&quot;&gt;&lt;sup&gt;[1]&lt;/sup&gt;&amp;nbsp;&lt;/span&gt;code. In the above figure, it is clear that after approximately every 13msec, RB0 pin is toggled i-e timer1 expires. You can easily change this value in the code.&lt;br /&gt;
&lt;h3&gt;
&lt;span style=&quot;color: #38761d; font-size: x-large;&quot;&gt;Code&lt;/span&gt;&lt;/h3&gt;
The main function code is shown below.&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgGG1n9itqHdWD2WRGvcZ4kRsBKlSF2gypdFNQuYdEhmN9d_4LOIqHkp6TmtAbPqzjXx83rHrY6U7V-onigVbXebu31WrSczx4L5S74V5o0w-Ql5u1By_04FTPLYtuu8TDT9trEfJRpiPI/s1600/PIC16F877+TIMER1+MAIN+CODE.bmp&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgGG1n9itqHdWD2WRGvcZ4kRsBKlSF2gypdFNQuYdEhmN9d_4LOIqHkp6TmtAbPqzjXx83rHrY6U7V-onigVbXebu31WrSczx4L5S74V5o0w-Ql5u1By_04FTPLYtuu8TDT9trEfJRpiPI/s1600/PIC16F877+TIMER1+MAIN+CODE.bmp&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;i&gt;Figure 2&lt;/i&gt;. &amp;nbsp; Main function for timer1 of PIC16F877A&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
In the main function, firstly TRISB0 is made zero to make RB0 pin an output pin, also RB0 pin is made zero. After that,&lt;i&gt;&amp;nbsp;InitTimer1()&amp;nbsp;&lt;/i&gt;function is called which initializes timer1. Rest of the work is done in timer1 interrupt service routine. Every time timer1 expires RB0 pin is toggled.&lt;br /&gt;
&lt;br /&gt;
The code used to initialize timer1 is shown below.&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgJOwjxFM5zJlj-ngQ7FUaIWtIyYPqCfopnDv8UcThGs0k10SjCJmKI_YFzpSEkuSXv1MGtzsBxK4JvughWirkH4nN9yTee4MtiUqAkNt9P3ZpPMIY_DzM6SNzmgvNHNq2oM9tcTHHSAAE/s1600/PIC16F877+INIT+TIMER1+CODE.bmp&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgJOwjxFM5zJlj-ngQ7FUaIWtIyYPqCfopnDv8UcThGs0k10SjCJmKI_YFzpSEkuSXv1MGtzsBxK4JvughWirkH4nN9yTee4MtiUqAkNt9P3ZpPMIY_DzM6SNzmgvNHNq2oM9tcTHHSAAE/s1600/PIC16F877+INIT+TIMER1+CODE.bmp&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;i&gt;Figure 3.&lt;/i&gt; &amp;nbsp; InitTimer1 function code for PIC16F877A&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
In this function, &lt;i&gt;T1CON &lt;/i&gt;is initialized to make timer1 prescalar to be 1:1. Timer1 is an 16bit timer, so it expires after reaching a value of 65535. When timer1 prescalar is made 1:1 then it means that timer1 value will increment after every clock cycle. Since PIC16F877A is running at 5MIPS&lt;sup style=&quot;color: purple;&quot;&gt;[2]&lt;/sup&gt;&lt;span style=&quot;color: purple;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;speed, this means that timer1 will expire after every 65536/5 = 13.1 msec&lt;/span&gt;&lt;sup style=&quot;color: purple;&quot;&gt;[3]&lt;/sup&gt;&lt;span style=&quot;background-color: white;&quot;&gt;. &lt;i&gt;TMR1IE &lt;/i&gt;bit enables timer1 interrupts and GIE bit enables global interrupts.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Timer1 interrupt service routine code is shown below.&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh2tcptu8_g4lf6GK4seFvNW7fyAiMa_IQSV8hdNbS8U9zGBE-vGboGf2bkDH5-J4zeYUvAEXb4LcGRrN0Qo7nGzWsUo-4r_djKQFjDlVluFILuAVrYd_XHeDgL3oGlsfiLg08QCKbqD1g/s1600/PIC16F877+TIMER1+ISR+CODE.bmp&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh2tcptu8_g4lf6GK4seFvNW7fyAiMa_IQSV8hdNbS8U9zGBE-vGboGf2bkDH5-J4zeYUvAEXb4LcGRrN0Qo7nGzWsUo-4r_djKQFjDlVluFILuAVrYd_XHeDgL3oGlsfiLg08QCKbqD1g/s1600/PIC16F877+TIMER1+ISR+CODE.bmp&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;i&gt;Figure 4&lt;/i&gt;. &amp;nbsp; Timer1 interrupt service routine&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
Whenever timer1 expires, then an interrupt is generated which executes the function shown above in the&amp;nbsp;&lt;i&gt;Figure 4&lt;/i&gt;. In this function, RB0 pin is toggled every time and timer1 interrupt flag is cleared. This means that in&amp;nbsp;&lt;i&gt;Figure 1&lt;/i&gt;&amp;nbsp;when ever&amp;nbsp;RB0&amp;nbsp;pin toggles than timer1 interrupt has occurred.&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;This is just an example code for timer1 of PIC16F877A microcontroller. You can modify it to fulfill your circuit requirements. You can leave your comments in the comment section below.&lt;/span&gt;&lt;br /&gt;
&lt;h3&gt;
&lt;span style=&quot;color: blue; font-size: x-large;&quot;&gt;Notes and References&lt;/span&gt;&lt;/h3&gt;
&lt;sup&gt;&lt;span style=&quot;color: purple;&quot;&gt;[1] &amp;nbsp;&lt;/span&gt;&lt;/sup&gt;&lt;span style=&quot;background-color: white;&quot;&gt;Interrupt service routine i-e when ever timer1 expires, it generates an interrupt and the code which is executed due to that interrupt is called it&#39;s ISR.&lt;/span&gt;&lt;br /&gt;
&lt;sup&gt;&lt;span style=&quot;color: purple;&quot;&gt;[2] &amp;nbsp;&lt;/span&gt;&lt;/sup&gt;&lt;span style=&quot;background-color: white;&quot;&gt;I am using external crystal of 20MHz for this PIC16F877A microcontroller. Since PIC microcontrollers execute every instruction in 4CPU cycles, so effectively this&amp;nbsp;PIC16F877A is running at 20MHz/4 = 5 MIPS (million of instructions per second) speed. You can use any crystal value from 4 to 20MHz with&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;PIC16F877A in HS mode&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;sup&gt;&lt;span style=&quot;color: purple;&quot;&gt;[3] &amp;nbsp;&lt;/span&gt;&lt;/sup&gt;You can confirm this value from&amp;nbsp;&lt;i&gt;Figure 1&lt;/i&gt;.&lt;/span&gt;&lt;br /&gt;
&lt;div&gt;
&lt;h3&gt;
&lt;span style=&quot;color: red; font-size: x-large;&quot;&gt;Downloads&lt;/span&gt;&lt;/h3&gt;
&lt;/div&gt;
Timer1 code for PIC16F877A was compiled in&amp;nbsp;MPLAB v8.85 with HI-TECH C v9.83 compiler and simulation was made in Proteus v7.10.&amp;nbsp;&lt;span style=&quot;background-color: white;&quot;&gt;To download code and&amp;nbsp;&lt;/span&gt;&lt;i style=&quot;background-color: white;&quot;&gt;Proteus&amp;nbsp;&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;simulation&amp;nbsp;&lt;/span&gt;&lt;a href=&quot;http://bit.ly/2ophieO&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;click here&lt;/a&gt;&lt;span style=&quot;background-color: white;&quot;&gt;.&lt;/span&gt;&lt;br /&gt;
&lt;div&gt;
&lt;h3&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;span style=&quot;color: #274e13; font-size: large;&quot;&gt;Further Reading Suggestions&lt;/span&gt;&lt;/span&gt;&lt;/h3&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;You may also like,&lt;/span&gt;&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/08/129.html&quot;&gt;How to set PIC16F877A configuration bits in code ?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/08/135.html&quot;&gt;PIC16F877A UART code + Proteus simulation&lt;/a&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/12/pic16f877a-lcd-code-proteus-simulation.html&quot;&gt;PIC16F877A LCD code&amp;nbsp;+ Proteus simulation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/08/132.html&quot;&gt;PIC16F877A PWM code + Proteus simulation&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://saeedsolutions.blogspot.com/feeds/2289723697597268974/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://saeedsolutions.blogspot.com/2015/06/pic16f877a-timer1-code-proteus.html#comment-form' title='8 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5869154326689252934/posts/default/2289723697597268974'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5869154326689252934/posts/default/2289723697597268974'/><link rel='alternate' type='text/html' href='http://saeedsolutions.blogspot.com/2015/06/pic16f877a-timer1-code-proteus.html' title='PIC16F877A timer1 code + Proteus simulation'/><author><name>Saeed</name><uri>http://www.blogger.com/profile/18064969138529094661</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjJntA2D2YqggpiwLEFEpWM_P42RT7D6mVGH8LelyLxx-5irmVXt008vnHhOmJj_Q2I1xXxfJE0AhFMMzj6XmjBCJza2gfIZgcgykiW6QYWKhy0CVN9zoBDEXg8LsvSSk1QKmPlUW7UZr0/s72-c/PIC16F877A+Timer1+cct.png" height="72" width="72"/><thr:total>8</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5869154326689252934.post-8236399699678349995</id><published>2015-06-23T15:43:00.003+05:00</published><updated>2017-04-02T15:02:19.447+05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="PIC micro-controller Projects"/><category scheme="http://www.blogger.com/atom/ns#" term="PIC16F877A Projects"/><title type='text'>PIC16F877A timer0 code + Proteus simulation</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;This PIC16F877A microcontroller tutorial answers the question,&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&quot; How to use timer0 of PIC16F877A and how to handle its interrupts? &quot;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;U&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;sing PIC16 simulator (&lt;/span&gt;&lt;i&gt;Proteus&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;) you can verify this PIC timer0 code and change it according to your needs.&amp;nbsp;This code is written in C language using MPLAB with HI-TECH C compiler. You can download this code from the &#39;&lt;/span&gt;&lt;i&gt;Downloads&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&#39;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;section at the bottom of this page.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;It is assumed that you know how to blink an LED with PIC16F877A microcontroller. If you don&#39;t then please read&amp;nbsp;&lt;/span&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/08/130.html&quot; target=&quot;_blank&quot;&gt;this page&lt;/a&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;first, before proceeding with this article.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;br /&gt;&lt;/span&gt;The following diagram (made in&amp;nbsp;&lt;i&gt;Proteus&lt;/i&gt;) shows the PIC microcontroller circuit diagram.&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjK4aiT70L3sOEFAwKtVZV7jSgbOZQD8lsRX0LBi-n6FpU_VARpMXiy169H44wXriv2R2dwTsQAzHc0snzNqE8yOnrGbqLkzEXlVnktNXTa8nWWb6YFVoIC6VHSGY98IHQkx7KhPoA212o/s1600/PIC16F877A+Timer0+CCT.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;426&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjK4aiT70L3sOEFAwKtVZV7jSgbOZQD8lsRX0LBi-n6FpU_VARpMXiy169H44wXriv2R2dwTsQAzHc0snzNqE8yOnrGbqLkzEXlVnktNXTa8nWWb6YFVoIC6VHSGY98IHQkx7KhPoA212o/s1600/PIC16F877A+Timer0+CCT.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;i style=&quot;font-size: 12.8000001907349px;&quot;&gt;Figure 1&lt;/i&gt;&lt;span style=&quot;font-size: 12.8000001907349px;&quot;&gt;. &amp;nbsp; PIC16F877A timer0 simulation&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
In this circuit, PIC16F877A is running on external crystal of 20MHz value. RB0 pin is toggled every time timer0 expires and executes it&#39;s ISR&lt;span style=&quot;color: purple;&quot;&gt;&lt;sup&gt;[1]&lt;/sup&gt;&amp;nbsp;&lt;/span&gt;code. In the above figure, it is clear that after approximately every 100usec, RB0 pin is toggled i-e timer0 expires. You can easily change this value in the code.&lt;br /&gt;
&lt;h3&gt;
&lt;span style=&quot;color: #38761d; font-size: x-large;&quot;&gt;Code&lt;/span&gt;&lt;/h3&gt;
The main function code is shown below.&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhiC6giOdkyyuAouSA0gby6vV5LhAXzUEUU78Cj3HwDS1ScJ3pOS3ytmvYkZHuIoV7e6qVU6Ip1xow98XAlu1QfN3stKKFfZ2Ga-bwlh-ghcZZP_DhDtqRd4MOtZzpBtCaYpJly3MfvCmo/s1600/PIC16F877A+Timer0+Main+Code.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhiC6giOdkyyuAouSA0gby6vV5LhAXzUEUU78Cj3HwDS1ScJ3pOS3ytmvYkZHuIoV7e6qVU6Ip1xow98XAlu1QfN3stKKFfZ2Ga-bwlh-ghcZZP_DhDtqRd4MOtZzpBtCaYpJly3MfvCmo/s1600/PIC16F877A+Timer0+Main+Code.png&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;span style=&quot;font-size: 12.8000001907349px;&quot;&gt;&lt;i&gt;Figure 2.&lt;/i&gt; &amp;nbsp; Main function for timer0 of PIC16F877A&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
In the main function, firstly TRISB0 is made zero to make RB0 pin an output pin, also RB0 pin is made zero. After that,&lt;i&gt;&amp;nbsp;InitTimer0()&amp;nbsp;&lt;/i&gt;function is called which initializes timer0. Rest of the work is done in timer0 interrupt service routine. Every time timer0 expires RB0 pin is toggled.&lt;br /&gt;
&lt;br /&gt;
The code used to initialize timer0 is shown below.&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhLZzzxY9f66rw-HJLre_ZlhEfw7jqDdngt9CyaRWGSuHTN7nMzT85_yxEKwZ1KNRtlP-WgA4PJfccS8q2WNiZnSHuv7SQVfsiThpGUROmNUGlk0cJ6qU8O3V_EVxPB7nWfav6pjoY1XT4/s1600/PIC16F877A+Timer0+Init+Code.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhLZzzxY9f66rw-HJLre_ZlhEfw7jqDdngt9CyaRWGSuHTN7nMzT85_yxEKwZ1KNRtlP-WgA4PJfccS8q2WNiZnSHuv7SQVfsiThpGUROmNUGlk0cJ6qU8O3V_EVxPB7nWfav6pjoY1XT4/s1600/PIC16F877A+Timer0+Init+Code.png&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;span style=&quot;font-size: 12.8000001907349px;&quot;&gt;&lt;i&gt;Figure 3&lt;/i&gt;. &amp;nbsp; InitTimer0 function code for PIC16F877A&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
In this function, OPTION_REG is initialized to make timer0 prescalar to be 1:2. Timer0 is an 8bit timer, so it expires after reaching a value of 255. When timer0 prescalar is made 1:2 then it means that timer0 value will increment after every two clock cycles. Since PIC16F877 is running at 5MIPS&lt;sup style=&quot;color: purple;&quot;&gt;[2]&lt;/sup&gt;&lt;span style=&quot;color: purple;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;speed, this means that timer0 will expire after every 256*2/5 = 102 usec&lt;/span&gt;&lt;sup style=&quot;color: purple;&quot;&gt;[3]&lt;/sup&gt;&lt;span style=&quot;background-color: white;&quot;&gt;. T0IE bit enables timer0 interrupts and GIE bit enables global interrupts.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Timer0 interrupt service routine code is shown below.&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjHZktS7FpDuMLmzEmk2hZMDGBKwJ_l6_IF_iLUMt8b5Ie8XX9T2ZaMMU7ekhANInq0lrVugrNJypZ1WI73phqbFP0CAKzONhN860rHo7NyA8lU6Pe8Wnio8ROQnTzFWo_7WrDe4bp-Yzk/s1600/PIC16F877A+Timer0+ISR+Code.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjHZktS7FpDuMLmzEmk2hZMDGBKwJ_l6_IF_iLUMt8b5Ie8XX9T2ZaMMU7ekhANInq0lrVugrNJypZ1WI73phqbFP0CAKzONhN860rHo7NyA8lU6Pe8Wnio8ROQnTzFWo_7WrDe4bp-Yzk/s1600/PIC16F877A+Timer0+ISR+Code.png&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;span style=&quot;font-size: 12.8000001907349px;&quot;&gt;&lt;i&gt;Figure 4&lt;/i&gt;. &amp;nbsp; Timer0 interrupt service routine&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
Whenever timer0 expires, then an interrupt is generated which executes the function shown above in the &lt;i&gt;Figure 4&lt;/i&gt;. In this function, RB0 pin is toggled every time and timer0 interrupt flag is cleared. This means that in &lt;i&gt;Figure 1&lt;/i&gt; when ever&amp;nbsp;RB0&amp;nbsp;pin toggles than timer0 interrupt has occurred.&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;This is just an example code for timer0 of PIC16F877A microcontroller. You can modify it to fulfill your circuit requirements. You can leave your comments in the comment section below.&lt;/span&gt;&lt;br /&gt;
&lt;h3&gt;
&lt;span style=&quot;color: blue; font-size: x-large;&quot;&gt;Notes and References&lt;/span&gt;&lt;/h3&gt;
&lt;sup&gt;&lt;span style=&quot;color: purple;&quot;&gt;[1] &amp;nbsp;&lt;/span&gt;&lt;/sup&gt;&lt;span style=&quot;background-color: white;&quot;&gt;Interrupt service routine i-e when ever timer0 expires, it generates an interrupt and the code which is executed due to that interrupt is called it&#39;s ISR.&lt;/span&gt;&lt;br /&gt;
&lt;sup&gt;&lt;span style=&quot;color: purple;&quot;&gt;[2] &amp;nbsp;&lt;/span&gt;&lt;/sup&gt;&lt;span style=&quot;background-color: white;&quot;&gt;I am using external crystal of 20MHz for this PIC16F877A microcontroller. Since PIC microcontrollers execute every instruction in 4CPU cycles, so effectively this&amp;nbsp;PIC16F877A is running at 20MHz/4 = 5 MIPS (million of instructions per second) speed. You can use any crystal value from 4 to 20MHz with&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;PIC16F877A in HS mode&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;sup&gt;&lt;span style=&quot;color: purple;&quot;&gt;[3] &amp;nbsp;&lt;/span&gt;&lt;/sup&gt;You can confirm this value from &lt;i&gt;Figure 1&lt;/i&gt;.&lt;/span&gt;&lt;br /&gt;
&lt;div&gt;
&lt;h3&gt;
&lt;span style=&quot;color: red; font-size: x-large;&quot;&gt;Downloads&lt;/span&gt;&lt;/h3&gt;
&lt;/div&gt;
Timer0 code for PIC16F877A was compiled in&amp;nbsp;MPLAB v8.85 with HI-TECH C v9.83 compiler and simulation was made in Proteus v7.10.&amp;nbsp;&lt;span style=&quot;background-color: white;&quot;&gt;To download code and&amp;nbsp;&lt;/span&gt;&lt;i style=&quot;background-color: white;&quot;&gt;Proteus&amp;nbsp;&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;simulation&amp;nbsp;&lt;/span&gt;&lt;a href=&quot;http://bit.ly/2owyY4U&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;click here&lt;/a&gt;&lt;span style=&quot;background-color: white;&quot;&gt;.&lt;/span&gt;&lt;br /&gt;
&lt;div&gt;
&lt;div&gt;
&lt;h3&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;span style=&quot;color: #274e13; font-size: large;&quot;&gt;Further Reading Suggestions&lt;/span&gt;&lt;/span&gt;&lt;/h3&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;You may also like,&lt;/span&gt;&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/08/129.html&quot;&gt;How to set PIC16F877A configuration bits in code ?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/08/135.html&quot;&gt;PIC16F877A UART code + Proteus simulation&lt;/a&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/12/pic16f877a-lcd-code-proteus-simulation.html&quot;&gt;PIC16F877A LCD code&amp;nbsp;+ Proteus simulation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/08/132.html&quot;&gt;PIC16F877A PWM code + Proteus simulation&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://saeedsolutions.blogspot.com/feeds/8236399699678349995/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://saeedsolutions.blogspot.com/2015/06/pic16f877a-timer0-code-proteus.html#comment-form' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5869154326689252934/posts/default/8236399699678349995'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5869154326689252934/posts/default/8236399699678349995'/><link rel='alternate' type='text/html' href='http://saeedsolutions.blogspot.com/2015/06/pic16f877a-timer0-code-proteus.html' title='PIC16F877A timer0 code + Proteus simulation'/><author><name>Saeed</name><uri>http://www.blogger.com/profile/18064969138529094661</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjK4aiT70L3sOEFAwKtVZV7jSgbOZQD8lsRX0LBi-n6FpU_VARpMXiy169H44wXriv2R2dwTsQAzHc0snzNqE8yOnrGbqLkzEXlVnktNXTa8nWWb6YFVoIC6VHSGY98IHQkx7KhPoA212o/s72-c/PIC16F877A+Timer0+CCT.png" height="72" width="72"/><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5869154326689252934.post-8116217385361553567</id><published>2014-12-10T13:34:00.001+05:00</published><updated>2017-04-02T15:01:40.287+05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="LCD interfacing"/><category scheme="http://www.blogger.com/atom/ns#" term="PIC micro-controller Projects"/><category scheme="http://www.blogger.com/atom/ns#" term="PIC16F877A Projects"/><title type='text'>PIC16F877A LCD code + Proteus simulation</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;This PIC16F877A microcontroller tutorial provides very detailed and comprehensive LCD&lt;sup&gt;&lt;span style=&quot;color: purple;&quot;&gt;[1]&amp;nbsp;&lt;/span&gt;&lt;/sup&gt;code. U&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;sing PIC16 simulator (&lt;/span&gt;&lt;i&gt;Proteus&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;) you can verify this LCD code and change it according to your needs.&amp;nbsp;This code is written in C language using MPLAB with HI-TECH C compiler. You can download this code from the &#39;&lt;/span&gt;&lt;i&gt;Downloads&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&#39;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;section at the bottom of this page.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;It is assumed that you know how to blink an LED with PIC16F877A microcontroller. If you don&#39;t then please read&amp;nbsp;&lt;/span&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/08/130.html&quot; target=&quot;_blank&quot;&gt;this page&lt;/a&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;first, before proceeding with this article.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;br /&gt;&lt;/span&gt;The following diagram (made in&amp;nbsp;&lt;i&gt;Proteus&lt;/i&gt;) shows the PIC microcontroller circuit diagram.&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjr1ZcZH6z6Y6lTp5WqBSrtuG_zdCJO_UKCk9ToXkcgHktfgBzTN9f67OKdAcQbqld8AsI35QAMD_Lpqmgn5YfuHrgFLWPYKdwOYbvXCzbNwmfHLznoc31Mip9mkZfiTSX3xciERY3I0Hk/s1600/PIC16F877A+LCD+CCT.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;294&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjr1ZcZH6z6Y6lTp5WqBSrtuG_zdCJO_UKCk9ToXkcgHktfgBzTN9f67OKdAcQbqld8AsI35QAMD_Lpqmgn5YfuHrgFLWPYKdwOYbvXCzbNwmfHLznoc31Mip9mkZfiTSX3xciERY3I0Hk/s1600/PIC16F877A+LCD+CCT.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;i&gt;Figure 1&lt;/i&gt;. &amp;nbsp; PIC16F877A LCD interfacing circuit&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
In the above figure, RB0 pin is being used as&amp;nbsp;&lt;i&gt;RS&amp;nbsp;&lt;/i&gt;pin for LCD. RB1 pin is used as&amp;nbsp;&lt;i&gt;RW&amp;nbsp;&lt;/i&gt;pin,&amp;nbsp;RB2 pin is used as&amp;nbsp;&lt;i&gt;Enable&amp;nbsp;&lt;/i&gt;pin&amp;nbsp;and RB4 to RB7 pins are used as&amp;nbsp;&lt;i&gt;Data bus&amp;nbsp;&lt;/i&gt;for the LCD. When code starts running then &#39;&lt;i&gt;saeedsolutions.blogspot.com&lt;/i&gt;&#39;&amp;nbsp;is displayed on the first line of LCD first. Also, on the second line in the end, two custom characters are also displayed (Downward arrow and Upward arrow). Then whole screen starts to scroll towards left. Following animation shows the result of simulating the code in&amp;nbsp;&lt;i&gt;Proteus&lt;/i&gt;.&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhuQzE2dpIeLuflxrLZXdPedFdLYNofRJW89ixndlrf-jeWPdFPzMKchX1BKCoCki-7e2pmZp4s7WCX484PzGsprLRWVmzfgPMbfqpsi0_lAUh2Pz87oDGTl3lxfHbds5Halkk8L3MWyWk/s1600/Scrolling.gif&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhuQzE2dpIeLuflxrLZXdPedFdLYNofRJW89ixndlrf-jeWPdFPzMKchX1BKCoCki-7e2pmZp4s7WCX484PzGsprLRWVmzfgPMbfqpsi0_lAUh2Pz87oDGTl3lxfHbds5Halkk8L3MWyWk/s1600/Scrolling.gif&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;font-size: 13px;&quot;&gt;&lt;i&gt;Figure 2&lt;/i&gt;. &amp;nbsp; LCD close up animation (Result of simulating code in Proteus)&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;h3&gt;
&lt;span style=&quot;color: #38761d; font-size: x-large;&quot;&gt;Code&lt;/span&gt;&lt;/h3&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;The code for the main function is shown below.&lt;/span&gt;&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiMavFaRAGE4DcjA6cyiw70r4yP9bfJSk0RPMIg_A8d1ghQzmp_ob9KGTvIFoFzj5Ybqwjyd1akL67lJ5kda3x2QDGGDdd8yuNka7mcb4SLJr-pEw0yfSTeAKw-X_80f_NCWK23jmU3ZZ4/s1600/PIC16F877+LCD+MAIN+CODE.bmp&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiMavFaRAGE4DcjA6cyiw70r4yP9bfJSk0RPMIg_A8d1ghQzmp_ob9KGTvIFoFzj5Ybqwjyd1akL67lJ5kda3x2QDGGDdd8yuNka7mcb4SLJr-pEw0yfSTeAKw-X_80f_NCWK23jmU3ZZ4/s1600/PIC16F877+LCD+MAIN+CODE.bmp&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;font-size: 13px;&quot;&gt;&lt;i&gt;Figure 3.&lt;/i&gt; &amp;nbsp; Main function for interfacing LCD with PIC16F877A&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;In the main function, firstly LCD is initialized using&amp;nbsp;&lt;/span&gt;&lt;i&gt;InitLCD()&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;function. After that, &#39;&lt;i&gt;saeedsolutions.blogspot.com&#39;&lt;/i&gt;&amp;nbsp;is written on the LCD screen&lt;/span&gt;&lt;sup&gt;&lt;span style=&quot;color: purple;&quot;&gt;[2]&lt;/span&gt;&lt;/sup&gt;&lt;span style=&quot;background-color: white;&quot;&gt;.&amp;nbsp;In this way using&amp;nbsp;&lt;/span&gt;&lt;i&gt;WriteStringToLCD()&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;function, you can write any string on the LCD screen.&amp;nbsp;&lt;i&gt;GotoYXPositionOnLCD(LCD_LINE2, 38)&lt;/i&gt;&amp;nbsp;statement makes the DDRAM(Data Display RAM) pointer to go to LCD line 2 second last character position, this means that next character written to LCD will be displayed at this position.&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;Each line of the LCD can have 40 characters saved in its DDRAM at most and only 16 characters are displayed at a given time&lt;/span&gt;&lt;sup&gt;&lt;span style=&quot;color: purple;&quot;&gt;[3]&lt;/span&gt;&lt;/sup&gt;&lt;span style=&quot;background-color: white;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;There are 8 custom characters&lt;/span&gt;&lt;sup&gt;&lt;span style=&quot;color: purple;&quot;&gt;[4]&lt;/span&gt;&lt;/sup&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;defined in the code as well. At position 0x06&amp;nbsp;&lt;i&gt;Downward Arrow&lt;/i&gt;&amp;nbsp;custom character is defined and a&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;t position 0x07&amp;nbsp;&lt;/span&gt;&lt;i&gt;Upward Arrow&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;custom character is defined. These two characters are displayed at last position of LCD line 2 using&amp;nbsp;&lt;i&gt;WriteDataToLCD(0x06)&lt;/i&gt;&amp;nbsp;for&amp;nbsp;&lt;/span&gt;&lt;i&gt;Downward Arrow&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;and&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&lt;i&gt;WriteDataToLCD(0x07)&lt;/i&gt;&amp;nbsp;for&amp;nbsp;&lt;/span&gt;&lt;i&gt;Upward Arrow&lt;/i&gt;. Then there is delay of 1 second using&amp;nbsp;&lt;i&gt;__delay_ms(1000);&amp;nbsp;&lt;/i&gt;statement.&lt;br /&gt;
&lt;br /&gt;
In the&amp;nbsp;&lt;i&gt;while(1)&lt;/i&gt;&amp;nbsp;loop, LCD screen is scrolled to the left by one character after every 0.2 seconds. This scrolling is achieved using the function&amp;nbsp;&lt;i&gt;ScrollLCD(ScrollScreen, ToLeft, 1)&lt;/i&gt;, in which you can change&amp;nbsp;&lt;i&gt;ScrollScreen&amp;nbsp;&lt;/i&gt;to&amp;nbsp;&lt;i&gt;ScrollCursor&lt;/i&gt;&amp;nbsp;if you want to scroll cursor only. You can change&amp;nbsp;&lt;i&gt;ToLeft&amp;nbsp;&lt;/i&gt;parameter to&amp;nbsp;&lt;i&gt;ToRight&amp;nbsp;&lt;/i&gt;if you want to scroll towards right. You can change value 1 to any&amp;nbsp;&lt;i&gt;x&lt;/i&gt;&amp;nbsp;number, if you want to scroll LCD by&amp;nbsp;&lt;i&gt;x&lt;/i&gt;&amp;nbsp;characters instead of 1 character at a time.&lt;br /&gt;
&lt;br /&gt;
In the code, custom characters&lt;sup&gt;&lt;span style=&quot;color: purple;&quot;&gt;[4]&lt;/span&gt;&lt;/sup&gt;&amp;nbsp;defined are shown below (From&amp;nbsp;&lt;i&gt;LCD.h&lt;/i&gt;&amp;nbsp;file).&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh0SWpcX_p4SzNRwY8HWWQiMl6pXh-vLm1PUrFT8G295ykyoFIzK58b2CGkf0mPxb2QqkDcdRInt2U7xQE5CnRJ1YmHP9UbGi2uXwSzo9gQiPtcIG_OUVtBNZqZDzB958uNVDmiN67hBd4/s1600/PIC16F877+Custom+Characters.bmp&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh0SWpcX_p4SzNRwY8HWWQiMl6pXh-vLm1PUrFT8G295ykyoFIzK58b2CGkf0mPxb2QqkDcdRInt2U7xQE5CnRJ1YmHP9UbGi2uXwSzo9gQiPtcIG_OUVtBNZqZDzB958uNVDmiN67hBd4/s1600/PIC16F877+Custom+Characters.bmp&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;font-size: 13px;&quot;&gt;&lt;i&gt;Figure 4&lt;/i&gt;. &amp;nbsp; Custom characters defined in the code&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
There are 8 custom characters defined. You can learn how to create these arrays from&amp;nbsp;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2012/12/how-to-display-custom-characters-on-lcd.html&quot; style=&quot;background-color: white;&quot;&gt;this link&lt;/a&gt;&lt;span style=&quot;background-color: white;&quot;&gt;.&lt;/span&gt;&amp;nbsp;Then you can replace these arrays with your own custom character arrays and those custom characters can be called from the main function if required.&lt;br /&gt;
&lt;br /&gt;
In the code you can easily select the configuration for the LCD to be used. Following figure shows the pin selection code.&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjDzH8o-ZPgRPrF_Dul6AqJ8rRLE7e0yE63s94cTeq4-F5huC4YpYseEP6AM0UkuljJWeeMdw2XwZjr2NoUuk0zpIFB7eWUo_iJxTzT1sp0OMtcNg4VU0sxcwPl6KWvi0Jh3Y61LGC-Sps/s1600/PIC16F877+LCD+SELECTABLES.bmp&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjDzH8o-ZPgRPrF_Dul6AqJ8rRLE7e0yE63s94cTeq4-F5huC4YpYseEP6AM0UkuljJWeeMdw2XwZjr2NoUuk0zpIFB7eWUo_iJxTzT1sp0OMtcNg4VU0sxcwPl6KWvi0Jh3Y61LGC-Sps/s1600/PIC16F877+LCD+SELECTABLES.bmp&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;font-size: 13px;&quot;&gt;&lt;i&gt;Figure 5&lt;/i&gt;. &amp;nbsp; LCD configuration selection code&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
Here for example, you can change RB0 to RB3 if you want to attach&amp;nbsp;&lt;i&gt;RS&amp;nbsp;&lt;/i&gt;pin of the LCD on pin36 of PIC16F877A. You will also need to change&amp;nbsp;&lt;i&gt;LCD_RS_Dir&amp;nbsp;&lt;/i&gt;to TRISB3 for RB3. Similarly, you can change other pin definitions as well.&lt;br /&gt;
&lt;br /&gt;
In the&amp;nbsp;&lt;i&gt;Selectables&lt;/i&gt;, there are many macros defined.&amp;nbsp;You should uncomment or comment these macros as per requirement. For example, the above code shows that LCD is being configured in&amp;nbsp;&lt;i&gt;DataBus4bit&amp;nbsp;&lt;/i&gt;mode, Entry mode is set to increment with each character, 16x2 LCD is selected, character font is 5x8 and custom characters are being defined.&lt;br /&gt;
&lt;br /&gt;
There are many functions available in&lt;i&gt;&amp;nbsp;LCD.c&lt;/i&gt;&amp;nbsp;file which you can use in your code. This tutorial only covers few of these functions.&amp;nbsp;&lt;span style=&quot;background-color: white;&quot;&gt;You can leave your comments in the comment section below.&lt;/span&gt;&lt;br /&gt;
&lt;h3&gt;
&lt;span style=&quot;color: blue; font-size: x-large;&quot;&gt;Notes and References&lt;/span&gt;&lt;/h3&gt;
&lt;sup&gt;&lt;span style=&quot;color: purple;&quot;&gt;[1] &amp;nbsp;&lt;/span&gt;&lt;/sup&gt;&lt;span style=&quot;background-color: white;&quot;&gt;Here LCD means most widely used common 16x2 LCD, which has HD44780U controller in it.&lt;/span&gt;&lt;br /&gt;
&lt;sup&gt;&lt;span style=&quot;color: purple;&quot;&gt;[2] &amp;nbsp;&lt;/span&gt;&lt;/sup&gt;&lt;span style=&quot;background-color: white;&quot;&gt;As shown in Figure 1,&amp;nbsp;&lt;i&gt;&quot;saeedsolutions.blogspot.com&quot;&lt;/i&gt;&lt;i&gt;&amp;nbsp;&lt;/i&gt;is displayed in the&amp;nbsp;&lt;i&gt;Proteus&amp;nbsp;&lt;/i&gt;simulation.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;sup&gt;&lt;span style=&quot;color: purple;&quot;&gt;[3] &amp;nbsp;&lt;/span&gt;&lt;/sup&gt;This statement is valid only for 16x2 LCD. For 20x2 LCD 20 characters are displayed on LCD screen at a given time, but only 40 characters can be saved for each LCD line in DDRAM.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;sup&gt;&lt;span style=&quot;color: purple;&quot;&gt;[4] &amp;nbsp;&lt;/span&gt;&lt;/sup&gt;Custom character are special characters, which you can store in the CGRAM(Character generator RAM) of LCD. These characters can be designed to display custom made character. For details on how to make these custom characters, you can check&amp;nbsp;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2012/10/how-to-display-custom-characters-on-lcd.html&quot;&gt;this link&lt;/a&gt;.&lt;/span&gt;&lt;br /&gt;
&lt;div&gt;
&lt;div&gt;
&lt;h3&gt;
&lt;span style=&quot;color: red; font-size: x-large;&quot;&gt;Downloads&lt;/span&gt;&lt;/h3&gt;
&lt;/div&gt;
LCD interfacing code using&amp;nbsp;&lt;span style=&quot;background-color: white;&quot;&gt;PIC16F877A w&lt;/span&gt;as compiled in&amp;nbsp;MPLAB v8.85 with HI-TECH C v9.83 compiler and simulation was made in Proteus v7.10.&amp;nbsp;&lt;span style=&quot;background-color: white;&quot;&gt;To download code and&amp;nbsp;&lt;/span&gt;&lt;i style=&quot;background-color: white;&quot;&gt;Proteus&amp;nbsp;&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;simulation&amp;nbsp;&lt;/span&gt;&lt;a href=&quot;http://bit.ly/2nYJlk0&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;click here&lt;/a&gt;&lt;span style=&quot;background-color: white;&quot;&gt;.&lt;/span&gt;&lt;br /&gt;
&lt;div&gt;
&lt;h3&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;span style=&quot;color: #274e13; font-size: large;&quot;&gt;Further Reading Suggestions&lt;/span&gt;&lt;/span&gt;&lt;/h3&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;You may also like,&lt;/span&gt;&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/08/129.html&quot;&gt;How to set PIC16F877A configuration bits in code ?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/08/135.html&quot;&gt;PIC16F877A UART code + Proteus simulation&lt;/a&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/08/134.html&quot;&gt;PIC16F877A external interrupt code + Proteus simulation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/08/132.html&quot;&gt;PIC16F877A PWM code + Proteus simulation&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://saeedsolutions.blogspot.com/feeds/8116217385361553567/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://saeedsolutions.blogspot.com/2014/12/pic16f877a-lcd-code-proteus-simulation.html#comment-form' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5869154326689252934/posts/default/8116217385361553567'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5869154326689252934/posts/default/8116217385361553567'/><link rel='alternate' type='text/html' href='http://saeedsolutions.blogspot.com/2014/12/pic16f877a-lcd-code-proteus-simulation.html' title='PIC16F877A LCD code + Proteus simulation'/><author><name>Saeed</name><uri>http://www.blogger.com/profile/18064969138529094661</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjr1ZcZH6z6Y6lTp5WqBSrtuG_zdCJO_UKCk9ToXkcgHktfgBzTN9f67OKdAcQbqld8AsI35QAMD_Lpqmgn5YfuHrgFLWPYKdwOYbvXCzbNwmfHLznoc31Mip9mkZfiTSX3xciERY3I0Hk/s72-c/PIC16F877A+LCD+CCT.png" height="72" width="72"/><thr:total>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5869154326689252934.post-251849582634662341</id><published>2014-10-24T16:02:00.001+05:00</published><updated>2017-04-02T15:01:03.207+05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="PIC micro-controller Projects"/><category scheme="http://www.blogger.com/atom/ns#" term="PIC16F877A Projects"/><title type='text'>PIC16F877A LCD interfacing code (In 4bit mode) + Proteus simulation</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;This PIC16F877A microcontroller tutorial&amp;nbsp;answers the question,&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&quot;How to interface LCD&lt;/span&gt;&lt;sup&gt;&lt;span style=&quot;color: purple;&quot;&gt;[1]&lt;/span&gt;&lt;/sup&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;in 4bit mode with PIC16F877A&quot; ?&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;Also, using PIC16 simulator (&lt;i&gt;Proteus&lt;/i&gt;) you can verify this LCD code and change it according to your needs.&amp;nbsp;This code is written in C language using MPLAB with HI-TECH C compiler. You can download this code from the &#39;&lt;i&gt;Downloads&lt;/i&gt;&#39;&amp;nbsp;&lt;span style=&quot;font-family: inherit;&quot;&gt;section at the bottom of this page.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;It is assumed that you know how to blink an LED with PIC16F877A microcontroller. If you don&#39;t then please read&amp;nbsp;&lt;/span&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/08/130.html&quot; target=&quot;_blank&quot;&gt;this page&lt;/a&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;first, before proceeding with this article.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;br /&gt;&lt;/span&gt;The following diagram (made in&amp;nbsp;&lt;i&gt;Proteus&lt;/i&gt;) shows the PIC microcontroller circuit diagram.&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiLxkVPy9l06xZnIqv-7yt19apbAFAgj-lCFVHjW1rCbDfVpZhl23StJKMwWixcQIjqWMfpjn_2h6G61PY_3I7LsyhNb68r_S9gnbv9skJr1_mhhB8yrLry4ipoqt7Kr87ZfP4K2N0L2Vw/s1600/PIC16F877A+4bit+LCD+CCT.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;296&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiLxkVPy9l06xZnIqv-7yt19apbAFAgj-lCFVHjW1rCbDfVpZhl23StJKMwWixcQIjqWMfpjn_2h6G61PY_3I7LsyhNb68r_S9gnbv9skJr1_mhhB8yrLry4ipoqt7Kr87ZfP4K2N0L2Vw/s1600/PIC16F877A+4bit+LCD+CCT.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;i&gt;Figure 1&lt;/i&gt;. &amp;nbsp; PIC16F877A LCD interfacing circuit&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
In the above figure, RB0 pin is being used as&amp;nbsp;&lt;i&gt;Enable&amp;nbsp;&lt;/i&gt;pin for LCD. RB1 pin is used as&amp;nbsp;&lt;i&gt;RS&amp;nbsp;&lt;/i&gt;pin and PORTB (RB4 to RB7) pins are used as&amp;nbsp;&lt;i&gt;Data bus&amp;nbsp;&lt;/i&gt;for the LCD. When code starts running then &#39;&lt;i&gt;Hello World!&lt;/i&gt;&#39;&amp;nbsp;is displayed on the LCD.&lt;br /&gt;
&lt;h3&gt;
&lt;span style=&quot;color: #38761d; font-size: x-large;&quot;&gt;Code&lt;/span&gt;&lt;/h3&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;The code for the main function is shown below.&lt;/span&gt;&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhse3vWr6pa2dHr9_lv3jxE-uoG67Peq941sVvAXYJ7TFmK1FPatGrthBdh0dQkkVegxSRqetC_oeoZq7zoLJEXLRtNlF9HicddqZ9Pu7Zqt2gJJs30YgzYfxHOOVSrG_s_QmvlOUCtBJQ/s1600/PIC16F877+4bit+LCD+MAIN+CODE.bmp&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhse3vWr6pa2dHr9_lv3jxE-uoG67Peq941sVvAXYJ7TFmK1FPatGrthBdh0dQkkVegxSRqetC_oeoZq7zoLJEXLRtNlF9HicddqZ9Pu7Zqt2gJJs30YgzYfxHOOVSrG_s_QmvlOUCtBJQ/s1600/PIC16F877+4bit+LCD+MAIN+CODE.bmp&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;i&gt;Figure 2&lt;/i&gt;. &amp;nbsp; Main function for interfacing LCD with PIC16F877A&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;In the main function, firstly LCD is initialized in 4 bit mode using&amp;nbsp;&lt;/span&gt;&lt;i&gt;InitLCD()&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;function. After that, LCD screen is cleared using&amp;nbsp;&lt;i&gt;ClearLCDScreen()&lt;/i&gt;&amp;nbsp;function. Then &#39;&lt;i&gt;Hello World!&#39;&lt;/i&gt;&amp;nbsp;is written on the LCD screen&lt;/span&gt;&lt;sup&gt;&lt;span style=&quot;color: purple;&quot;&gt;[2]&lt;/span&gt;&lt;/sup&gt;&lt;span style=&quot;background-color: white;&quot;&gt;.&amp;nbsp;In this way using&amp;nbsp;&lt;/span&gt;&lt;i&gt;WriteStringToLCD()&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;function, you can write any string on the LCD screen.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
In the code you can easily select pins to be used for interfacing with the LCD. Following figure shows the pin selection code.&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiplq6OlllBMjrxWiIoVyJENs_Egf2Jaz3dhF5wcI1gs7jBa78PviRivB1LQJLTS7tSphPnRb6uv_3OJU1OH0_pT1PaBycQjRhsnuPMXJv7og63oASo5KsGGuXZfB7nx-vjjMrr_heUJks/s1600/PIC16F877+4bit+LCD+PIN+SETTINGS.bmp&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiplq6OlllBMjrxWiIoVyJENs_Egf2Jaz3dhF5wcI1gs7jBa78PviRivB1LQJLTS7tSphPnRb6uv_3OJU1OH0_pT1PaBycQjRhsnuPMXJv7og63oASo5KsGGuXZfB7nx-vjjMrr_heUJks/s1600/PIC16F877+4bit+LCD+PIN+SETTINGS.bmp&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;i&gt;Figure 3&lt;/i&gt;. &amp;nbsp; LCD pins selection code&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
Here for example, you can change RB0 to RD3 if you want to attach Enable pin of the LCD on pin22 of PIC16F877A. You will also need to change&amp;nbsp;&lt;i&gt;LCD_E_Dir&amp;nbsp;&lt;/i&gt;to TRISD3 for RD3.&lt;br /&gt;
&lt;br /&gt;
We can write data or command to the LCD. Following code shows the functions required for this.&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhWcumsx0h80xuc78SufVpESrQsrocbcyHLYXn2fbxTurVQSIA6zA4qFfTFAB27ys3HrtqkYPDJ_tYAj0c8kHznuRxgx7adV3EvlOiVM4cAZA45r_yUmv04Q_f64eiBnfB8Twm2rPZ4Afw/s1600/PIC16F877+4bit+LCD+Functions.bmp&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhWcumsx0h80xuc78SufVpESrQsrocbcyHLYXn2fbxTurVQSIA6zA4qFfTFAB27ys3HrtqkYPDJ_tYAj0c8kHznuRxgx7adV3EvlOiVM4cAZA45r_yUmv04Q_f64eiBnfB8Twm2rPZ4Afw/s1600/PIC16F877+4bit+LCD+Functions.bmp&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;i&gt;Figure 4&lt;/i&gt;. &amp;nbsp; Functions to write data and command to LCD&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;i&gt;WriteCommandToLCD()&lt;/i&gt;&amp;nbsp;function writes a command to LCD in 4bit mode.&amp;nbsp;&lt;i&gt;WriteDataToLCD()&lt;/i&gt;&amp;nbsp;function writes any given character on the LCD. For example, to write &#39;a&#39; on LCD, simply call&amp;nbsp;&lt;span style=&quot;background-color: white;&quot;&gt;&lt;i&gt;WriteDataToLCD(&#39;a&#39;);&lt;/i&gt;&amp;nbsp;in your main function.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;LCD&amp;nbsp;initialization&amp;nbsp;function is shown below.&lt;/span&gt;&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiImrF08YzOO_8mnv9KQoZ-P-3P-N4SLWUxJdZfZmOUyd-RI4D_KxJ-lBjm4u0Oyw8eBLXxVc5E1DxfVK74LHyCs_s1FeQNQaaOzx0RT2Fkb5E-0XH6mFAF05HufaHkzDlVxzSJ4EvpDBE/s1600/PIC16F877+4bit+INIT+LCD+Code.bmp&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiImrF08YzOO_8mnv9KQoZ-P-3P-N4SLWUxJdZfZmOUyd-RI4D_KxJ-lBjm4u0Oyw8eBLXxVc5E1DxfVK74LHyCs_s1FeQNQaaOzx0RT2Fkb5E-0XH6mFAF05HufaHkzDlVxzSJ4EvpDBE/s1600/PIC16F877+4bit+INIT+LCD+Code.bmp&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;i&gt;Figure 5&lt;/i&gt;. &amp;nbsp; InitLCD function for PIC16F877A&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;i&gt;InitLCD()&lt;/i&gt;&amp;nbsp;function (shown above) is initialising LCD in 4bit mode with cursor off, blink off and entry mode set to increment. This initialisation process is written according to the datasheet of&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;HD44780U controller, which is built in the LCD&lt;/span&gt;&lt;sup&gt;&lt;span style=&quot;color: purple;&quot;&gt;[1]&lt;/span&gt;&lt;/sup&gt;&lt;span style=&quot;background-color: white;&quot;&gt;.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;You can leave your comments in the comment section below.&lt;/span&gt;&lt;br /&gt;
&lt;h3&gt;
&lt;span style=&quot;color: blue; font-size: x-large;&quot;&gt;Notes and References&lt;/span&gt;&lt;/h3&gt;
&lt;sup&gt;&lt;span style=&quot;color: purple;&quot;&gt;[1] &amp;nbsp;&lt;/span&gt;&lt;/sup&gt;&lt;span style=&quot;background-color: white;&quot;&gt;Here LCD means most widely used common 16x2 LCD, which has HD44780U controller in it.&lt;/span&gt;&lt;br /&gt;
&lt;sup&gt;&lt;span style=&quot;color: purple;&quot;&gt;[2] &amp;nbsp;&lt;/span&gt;&lt;/sup&gt;&lt;span style=&quot;background-color: white;&quot;&gt;As shown in Figure 1,&amp;nbsp;&lt;i&gt;Hello World!&amp;nbsp;&lt;/i&gt;is displayed in the&amp;nbsp;&lt;i&gt;Proteus&amp;nbsp;&lt;/i&gt;simulation.&lt;/span&gt;&lt;br /&gt;
&lt;div&gt;
&lt;div&gt;
&lt;h3&gt;
&lt;span style=&quot;color: red; font-size: x-large;&quot;&gt;Downloads&lt;/span&gt;&lt;/h3&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
LCD interfacing code using&amp;nbsp;&lt;span style=&quot;background-color: white;&quot;&gt;PIC16F877A w&lt;/span&gt;as compiled in&amp;nbsp;MPLAB v8.85 with HI-TECH C v9.83 compiler and simulation was made in Proteus v7.10.&amp;nbsp;&lt;span style=&quot;background-color: white;&quot;&gt;To download code and&amp;nbsp;&lt;/span&gt;&lt;i style=&quot;background-color: white;&quot;&gt;Proteus&amp;nbsp;&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;simulation&amp;nbsp;&lt;/span&gt;&lt;a href=&quot;http://bit.ly/2ntc6Bk&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;click here&lt;/a&gt;&lt;span style=&quot;background-color: white;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div&gt;
&lt;table&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;div align=&quot;right&quot; right=&quot;right&quot; text-align:=&quot;text-align:&quot;&gt;
&lt;iframe allowfullscreen=&quot;allowfullscreen&quot; frameborder=&quot;0&quot; height=&quot;180&quot; scrolling=&quot;no&quot; src=&quot;http://wizpert.com/wizapi/widget?beta_key=3ba42&amp;amp;view=227&amp;amp;ep=6570&amp;amp;size=standard&quot; width=&quot;180&quot;&gt; &lt;/iframe&gt;
&lt;/div&gt;
&lt;/td&gt;&lt;td&gt;&lt;div&gt;
&lt;h3&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;span style=&quot;color: #274e13; font-size: large;&quot;&gt;Further Reading Suggestions&lt;/span&gt;&lt;/span&gt;&lt;/h3&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;You may also like,&lt;/span&gt;&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/08/133.html&quot;&gt;PIC16F877A 2 channel PWM code&amp;nbsp;+ Proteus simulation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/08/135.html&quot;&gt;PIC16F877A UART code&amp;nbsp;+ Proteus simulation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/08/134.html&quot;&gt;PIC16F877A external interrupt code&amp;nbsp;+ Proteus simulation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/08/132.html&quot;&gt;PIC16F877A PWM code&amp;nbsp;+ Proteus simulation&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://saeedsolutions.blogspot.com/feeds/251849582634662341/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://saeedsolutions.blogspot.com/2014/10/137.html#comment-form' title='17 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5869154326689252934/posts/default/251849582634662341'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5869154326689252934/posts/default/251849582634662341'/><link rel='alternate' type='text/html' href='http://saeedsolutions.blogspot.com/2014/10/137.html' title='PIC16F877A LCD interfacing code (In 4bit mode) + Proteus simulation'/><author><name>Saeed</name><uri>http://www.blogger.com/profile/18064969138529094661</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiLxkVPy9l06xZnIqv-7yt19apbAFAgj-lCFVHjW1rCbDfVpZhl23StJKMwWixcQIjqWMfpjn_2h6G61PY_3I7LsyhNb68r_S9gnbv9skJr1_mhhB8yrLry4ipoqt7Kr87ZfP4K2N0L2Vw/s72-c/PIC16F877A+4bit+LCD+CCT.png" height="72" width="72"/><thr:total>17</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5869154326689252934.post-77838622496400043</id><published>2014-08-27T13:43:00.001+05:00</published><updated>2017-04-02T15:00:21.493+05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="PIC micro-controller Projects"/><category scheme="http://www.blogger.com/atom/ns#" term="PIC16F877A Projects"/><title type='text'>PIC16F877A LCD interfacing code (In 8bit mode) + Proteus simulation</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;This PIC16F877A microcontroller tutorial&amp;nbsp;answers the question,&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&quot;How to interface LCD&lt;/span&gt;&lt;sup&gt;&lt;span style=&quot;color: purple;&quot;&gt;[1]&lt;/span&gt;&lt;/sup&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;in 8bit mode with PIC16F877A&quot; ?&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;/span&gt;

&lt;span style=&quot;background-color: white;&quot;&gt;Also, using PIC16 simulator (&lt;i&gt;Proteus&lt;/i&gt;) you can verify this LCD code and change it according to your needs.&amp;nbsp;This code is written in C language using MPLAB with HI-TECH C compiler. You can download this code from the &#39;&lt;i&gt;Downloads&lt;/i&gt;&#39;&amp;nbsp;&lt;span style=&quot;font-family: inherit;&quot;&gt;section at the bottom of this page.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;It is assumed that you know how to blink an LED with PIC16F877A microcontroller. If you don&#39;t then please read&amp;nbsp;&lt;/span&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/08/130.html&quot; target=&quot;_blank&quot;&gt;this page&lt;/a&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;first, before proceeding with this article.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;br /&gt;&lt;/span&gt;The following diagram (made in&amp;nbsp;&lt;i&gt;Proteus&lt;/i&gt;) shows the PIC microcontroller circuit diagram.&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhWvGtnHoYgpoBEEJ1LiataueE4tHvl40o_VlapkWRRBQ1Pic3k5u1vFOLxl5hn9khKxU6MLdZ5ZmdHWpSgIjfoXbbBH1Kbtsvg-FO0X5HzC12n3LwFhRjSCJVopnMqluDS209Vur82Rjw/s1600/PIC16F877A+8BIT+LCD+CCT.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;296&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhWvGtnHoYgpoBEEJ1LiataueE4tHvl40o_VlapkWRRBQ1Pic3k5u1vFOLxl5hn9khKxU6MLdZ5ZmdHWpSgIjfoXbbBH1Kbtsvg-FO0X5HzC12n3LwFhRjSCJVopnMqluDS209Vur82Rjw/s1600/PIC16F877A+8BIT+LCD+CCT.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;i&gt;Figure 1&lt;/i&gt;. &amp;nbsp; PIC16F877A LCD interfacing circuit&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
In the above figure, RD0 pin is being used as&amp;nbsp;&lt;i&gt;Enable&amp;nbsp;&lt;/i&gt;pin for LCD. RD1 pin is used as&amp;nbsp;&lt;i&gt;RS&amp;nbsp;&lt;/i&gt;pin and PORTB is used as&amp;nbsp;&lt;i&gt;Data bus&amp;nbsp;&lt;/i&gt;for the LCD. When code starts running then &#39;&lt;i&gt;Hello World!&lt;/i&gt;&#39;&amp;nbsp;is displayed on the LCD.&lt;br /&gt;
&lt;h3&gt;
&lt;span style=&quot;color: #38761d; font-size: x-large;&quot;&gt;Code&lt;/span&gt;&lt;/h3&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;The code for the main function is shown below.&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgEmj0iJ0bSvuthfLQsTfDkFCqmGHnvI95-xDMz3_hllCJW1tZ00vnhxOzXFlnvYm3o-KMZF694bkewXvpBCOBALpp0riQe_2TTIhBARU96NErh6lF76Qk2vfN0CxZ1KL009BCh4sWwJnM/s1600/PIC16F877A+8BIT+LCD+MAIN+CODE.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;209&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgEmj0iJ0bSvuthfLQsTfDkFCqmGHnvI95-xDMz3_hllCJW1tZ00vnhxOzXFlnvYm3o-KMZF694bkewXvpBCOBALpp0riQe_2TTIhBARU96NErh6lF76Qk2vfN0CxZ1KL009BCh4sWwJnM/s1600/PIC16F877A+8BIT+LCD+MAIN+CODE.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;i&gt;Figure 2.&lt;/i&gt; &amp;nbsp; Main function for interfacing LCD with PIC16F877A&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;In the main function, firstly LCD is initialised using&amp;nbsp;&lt;/span&gt;&lt;i&gt;InitLCD()&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;function. After that, LCD screen is cleared using &lt;i&gt;ClearLCDScreen()&lt;/i&gt; function. Then &#39;&lt;i&gt;Hello World!&#39;&lt;/i&gt;&amp;nbsp;is written on the LCD screen&lt;/span&gt;&lt;sup&gt;&lt;span style=&quot;color: purple;&quot;&gt;[2]&lt;/span&gt;&lt;/sup&gt;&lt;span style=&quot;background-color: white;&quot;&gt;.&amp;nbsp;In this way using&amp;nbsp;&lt;/span&gt;&lt;i&gt;WriteStringToLCD()&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;function, you can write any string on the LCD screen.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
In the code you can easily select pins to be used for interfacing with the LCD. Following figure shows the pin selection code.&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgUXmQ7YDnusI-eZezsvmcD_fZC6CRumJY1aJwLuGaSrOFbVBVp-BBTXEy2e6Mt-ArmUalrsfQSV_rBaN2mhL43qeuVHIZ7wQmU_emkUhnemhHn4TSwxZ22GQGM4-HNhnLzc5xxMppZa3w/s1600/PIC16F877A+8BIT+LCD+PINS.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;192&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgUXmQ7YDnusI-eZezsvmcD_fZC6CRumJY1aJwLuGaSrOFbVBVp-BBTXEy2e6Mt-ArmUalrsfQSV_rBaN2mhL43qeuVHIZ7wQmU_emkUhnemhHn4TSwxZ22GQGM4-HNhnLzc5xxMppZa3w/s1600/PIC16F877A+8BIT+LCD+PINS.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;i&gt;Figure 3&lt;/i&gt;. &amp;nbsp; LCD pins selection code&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
Here for example, you can change RD0 to RD3 if you want to attach Enable pin of the LCD on pin22 of PIC16F877A. You will also need to change&amp;nbsp;&lt;i&gt;LCD_E_Dir&amp;nbsp;&lt;/i&gt;to TRISD3 for RD3.&lt;br /&gt;
&lt;br /&gt;
We can write data or command to the LCD. Following code shows the functions required for this.&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj_sZ91_HBENxpaYPad7U-RVnhqNYKwyUv9oOlTRivuT9KKaVRAIrCY2gVsmwVKYfCkiEg6-N0VR75k3bne6HxAx2qw0HJH6Q9arU1ajD5TUbwyXhIRUn5S9oXwz8M_qr9bJA6ONgmjsHE/s1600/PIC16F877A+8BIT+LCD+FUNCS.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;286&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj_sZ91_HBENxpaYPad7U-RVnhqNYKwyUv9oOlTRivuT9KKaVRAIrCY2gVsmwVKYfCkiEg6-N0VR75k3bne6HxAx2qw0HJH6Q9arU1ajD5TUbwyXhIRUn5S9oXwz8M_qr9bJA6ONgmjsHE/s1600/PIC16F877A+8BIT+LCD+FUNCS.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;i&gt;Figure 4&lt;/i&gt;. &amp;nbsp; Functions to write data and command to LCD&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;i&gt;WriteCommandToLCD()&lt;/i&gt;&amp;nbsp;function writes a command to LCD in 8bit mode.&amp;nbsp;&lt;i&gt;WriteDataToLCD()&lt;/i&gt;&amp;nbsp;function writes any given character on the LCD. For example, to write &#39;a&#39; on LCD, simply call&amp;nbsp;&lt;span style=&quot;background-color: white;&quot;&gt;&lt;i&gt;WriteDataToLCD(&#39;a&#39;);&lt;/i&gt;&amp;nbsp;in your main function.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;LCD&amp;nbsp;initialisation&amp;nbsp;function is shown below.&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgtB30q5A9FNl-6iCXB0oLiOZslhc6BWAbyyIm5ZJvHd_CAZhDK8pe2wc7g8U4RZ2pJ5HbWUi26tc15nuhzYR3FfQrE1fePfY71WYnVPKYEgw-ItKp5-AJqFt_TGNw8XuMNTkJK6NJfTC0/s1600/PIC16F877A+8BIT+LCD+INIT+CODE.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;536&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgtB30q5A9FNl-6iCXB0oLiOZslhc6BWAbyyIm5ZJvHd_CAZhDK8pe2wc7g8U4RZ2pJ5HbWUi26tc15nuhzYR3FfQrE1fePfY71WYnVPKYEgw-ItKp5-AJqFt_TGNw8XuMNTkJK6NJfTC0/s1600/PIC16F877A+8BIT+LCD+INIT+CODE.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;i&gt;Figure 5&lt;/i&gt;. &amp;nbsp; InitLCD function for PIC16F877A&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;i&gt;InitLCD()&lt;/i&gt;&amp;nbsp;function (shown above) is initialising LCD in 8bit mode with cursor off, blink off and entry mode set to increment. This initialisation process is written according to the datasheet of&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;HD44780U controller, which is built in the LCD&lt;/span&gt;&lt;sup&gt;&lt;span style=&quot;color: purple;&quot;&gt;[1]&lt;/span&gt;&lt;/sup&gt;&lt;span style=&quot;background-color: white;&quot;&gt;.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;You can leave your comments in the comment section below.&lt;/span&gt;&lt;br /&gt;
&lt;h3&gt;
&lt;span style=&quot;color: blue; font-size: x-large;&quot;&gt;Notes and References&lt;/span&gt;&lt;/h3&gt;
&lt;sup&gt;&lt;span style=&quot;color: purple;&quot;&gt;[1] &amp;nbsp;&lt;/span&gt;&lt;/sup&gt;&lt;span style=&quot;background-color: white;&quot;&gt;Here LCD means most widely used common 16x2 LCD, which has HD44780U controller in it.&lt;/span&gt;&lt;br /&gt;
&lt;sup&gt;&lt;span style=&quot;color: purple;&quot;&gt;[2] &amp;nbsp;&lt;/span&gt;&lt;/sup&gt;&lt;span style=&quot;background-color: white;&quot;&gt;As shown in Figure 1,&amp;nbsp;&lt;i&gt;Hello World!&amp;nbsp;&lt;/i&gt;is displayed in the&amp;nbsp;&lt;i&gt;Proteus&amp;nbsp;&lt;/i&gt;simulation.&lt;/span&gt;&lt;br /&gt;
&lt;div&gt;
&lt;div&gt;
&lt;ul&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;h3&gt;
&lt;span style=&quot;color: red; font-size: x-large;&quot;&gt;Downloads&lt;/span&gt;&lt;/h3&gt;
&lt;div&gt;
LCD interfacing code using&amp;nbsp;&lt;span style=&quot;background-color: white;&quot;&gt;PIC16F877A w&lt;/span&gt;as compiled in&amp;nbsp;MPLAB v8.85 with HI-TECH C v9.83 compiler and simulation was made in Proteus v7.10.&amp;nbsp;&lt;span style=&quot;background-color: white;&quot;&gt;To download code and&amp;nbsp;&lt;/span&gt;&lt;i style=&quot;background-color: white;&quot;&gt;Proteus&amp;nbsp;&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;simulation&amp;nbsp;&lt;/span&gt;&lt;a href=&quot;http://bit.ly/2nKwWhs&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;click here&lt;/a&gt;&lt;span style=&quot;background-color: white;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div&gt;
&lt;table&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;div align=&quot;right&quot; right=&quot;right&quot; text-align:=&quot;text-align:&quot;&gt;
&lt;iframe allowfullscreen=&quot;allowfullscreen&quot; frameborder=&quot;0&quot; height=&quot;180&quot; scrolling=&quot;no&quot; src=&quot;http://wizpert.com/wizapi/widget?beta_key=3ba42&amp;amp;view=227&amp;amp;ep=6570&amp;amp;size=standard&quot; width=&quot;180&quot;&gt; &lt;/iframe&gt;
&lt;/div&gt;
&lt;/td&gt;&lt;td&gt;&lt;div&gt;
&lt;h3&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;span style=&quot;color: #274e13; font-size: large;&quot;&gt;Further Reading Suggestions&lt;/span&gt;&lt;/span&gt;&lt;/h3&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;You may also like,&lt;/span&gt;&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/08/133.html&quot;&gt;PIC16F877A 2 channel PWM code&amp;nbsp;+ Proteus simulation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/08/135.html&quot;&gt;PIC16F877A UART code&amp;nbsp;+ Proteus simulation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/08/134.html&quot;&gt;PIC16F877A external interrupt code&amp;nbsp;+ Proteus simulation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/08/132.html&quot;&gt;PIC16F877A PWM code&amp;nbsp;+ Proteus simulation&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://saeedsolutions.blogspot.com/feeds/77838622496400043/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://saeedsolutions.blogspot.com/2014/08/136.html#comment-form' title='6 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5869154326689252934/posts/default/77838622496400043'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5869154326689252934/posts/default/77838622496400043'/><link rel='alternate' type='text/html' href='http://saeedsolutions.blogspot.com/2014/08/136.html' title='PIC16F877A LCD interfacing code (In 8bit mode) + Proteus simulation'/><author><name>Saeed</name><uri>http://www.blogger.com/profile/18064969138529094661</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhWvGtnHoYgpoBEEJ1LiataueE4tHvl40o_VlapkWRRBQ1Pic3k5u1vFOLxl5hn9khKxU6MLdZ5ZmdHWpSgIjfoXbbBH1Kbtsvg-FO0X5HzC12n3LwFhRjSCJVopnMqluDS209Vur82Rjw/s72-c/PIC16F877A+8BIT+LCD+CCT.png" height="72" width="72"/><thr:total>6</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5869154326689252934.post-7707261775475273690</id><published>2014-08-26T14:53:00.003+05:00</published><updated>2017-04-02T14:59:27.980+05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="PIC micro-controller Projects"/><category scheme="http://www.blogger.com/atom/ns#" term="PIC16F877A Projects"/><title type='text'>PIC16F877A UART code + Proteus simulation</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;This post answers the question, &quot;How to use interrupt based UART in PIC16F877A&quot; ?&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;Also, using PIC16 simulator (&lt;i&gt;Proteus&lt;/i&gt;) you can verify this UART code and change it according to your needs.&amp;nbsp;This code is written in C language using MPLAB with HI-TECH C compiler. You can download this code from the &#39;&lt;i&gt;Downloads&lt;/i&gt;&#39;&amp;nbsp;&lt;span style=&quot;font-family: inherit;&quot;&gt;section at the bottom of this page.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;span style=&quot;background-color: transparent;&quot;&gt;This PIC16F877A microcontroller tutorial&lt;/span&gt;&amp;nbsp;provides the interrupt based UART functions which you can easily understand and use.&amp;nbsp;After reading this page you can easily use PIC16F877A serial UART interface.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;It is assumed that you know how to blink an LED with PIC16F877A microcontroller. If you don&#39;t then please read&amp;nbsp;&lt;/span&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/08/130.html&quot; target=&quot;_blank&quot;&gt;this page&lt;/a&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;first, before proceeding with this article.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;br /&gt;&lt;/span&gt;The following diagram (made in&amp;nbsp;&lt;i&gt;Proteus&lt;/i&gt;) shows the PIC microcontroller circuit diagram.&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjX-BwnwvQO6wyLQkPbSKToV96ghexik1atXTUSP_saZvUV-_lgSgb4F1GErhqe3AXDpOkqEqCyfxWrAq68g6srNIZoC-D6QcXhFk7YOuaE75_j-rgETlRFafALhzi5fAR0WMrWs8H95G0/s1600/PIC16F877A+UART+CCT.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;298&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjX-BwnwvQO6wyLQkPbSKToV96ghexik1atXTUSP_saZvUV-_lgSgb4F1GErhqe3AXDpOkqEqCyfxWrAq68g6srNIZoC-D6QcXhFk7YOuaE75_j-rgETlRFafALhzi5fAR0WMrWs8H95G0/s1600/PIC16F877A+UART+CCT.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;i&gt;Figure 1&lt;/i&gt;. &amp;nbsp; PIC16F877A UART circuit&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;In the above figure,&amp;nbsp;UART baudrate is currently set to 9600 bps, but you can change it to your desired value. RC6 pin is the TX pin and RC7 pin is the RX pin the UART. Whenever a new character is received on the UART, then PIC16F877A goes into interrupt service routine and received character is echoed back.&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;When code starts to execute then PIC16F877A sends &#39;&lt;i&gt;Hello World&lt;/i&gt;&#39; to virtual terminal. After that&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;PIC16F877A echoes back whatever character is sent to it. &#39;&lt;/span&gt;&lt;i&gt;Hi&#39;&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;was typed in the virtual terminal after the&amp;nbsp;start up&amp;nbsp;of the simulation in&amp;nbsp;&lt;/span&gt;&lt;i&gt;Proteus&amp;nbsp;&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;and PIC16F877A echoed it back.&lt;/span&gt;&lt;br /&gt;
&lt;h3&gt;
&lt;span style=&quot;color: #38761d; font-size: x-large;&quot;&gt;Code&lt;/span&gt;&lt;/h3&gt;
The code used to set different properties of UART is shown below. (From&amp;nbsp;&lt;i&gt;UART.h&lt;/i&gt;&amp;nbsp;file)&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhKlcTkDiYBvs32Hl2RHLG-YokWxYhQPSbiXnIIHutFjj6cge6scPNR46g8xrP-HVXh17V_KttAdzJTS08IK0NlJU_MBNFKTLxnLeHaiFBNE4C0Hhxqt3pOTRam_MLmcjV0sARQiR81e5w/s1600/PIC16F877A+UART+BAUDRATE+SETTINGS.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;135&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhKlcTkDiYBvs32Hl2RHLG-YokWxYhQPSbiXnIIHutFjj6cge6scPNR46g8xrP-HVXh17V_KttAdzJTS08IK0NlJU_MBNFKTLxnLeHaiFBNE4C0Hhxqt3pOTRam_MLmcjV0sARQiR81e5w/s1600/PIC16F877A+UART+BAUDRATE+SETTINGS.png&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;i&gt;Figure 2&lt;/i&gt;. &amp;nbsp; UART settings in the code&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
In the above figure,&amp;nbsp;&lt;i&gt;_XTAL_FREQ&amp;nbsp;&lt;/i&gt;is defined to be 20000000, which is the external crystal frequency used with this PIC16F877A. If you change the crystal (e-g by attaching an external crystal of different frequency) then you will need to change&amp;nbsp;&lt;span style=&quot;background-color: white;&quot;&gt;&lt;i&gt;_XTAL_FREQ&amp;nbsp;&lt;/i&gt;value as well. For example, if 12MHz external crystal is used, then&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&lt;i&gt;_XTAL_FREQ&amp;nbsp;&lt;/i&gt;should be defined as 12000000 in the above code.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;Similarly, you can define&amp;nbsp;&lt;i&gt;Baudrate&amp;nbsp;&lt;/i&gt;for the UART, in the above figure it is defined to be 9600 bps. You can change this value if you need&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;. All other settings are at default, which means this UART will use one starting bit, 8 data bits and then one stop bit. There is no parity bit and no flow control mechanism.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;The code for main function is shown below.&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiAEQ0wExfixQ7v0lcMR_dkpC5FdMAIzQHYlK1-2u4nkPdoNoYRFnBvU1C_DY8kwnHuLqCzGmMjEsZZ28uSFdwSkVPV9t5Qy3ZRUrthaaC8RbOneznkMPGKbD9LnxmmUlFP0EP8hF1pU3w/s1600/PIC16F877A+UART+MAIN+CODE.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;312&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiAEQ0wExfixQ7v0lcMR_dkpC5FdMAIzQHYlK1-2u4nkPdoNoYRFnBvU1C_DY8kwnHuLqCzGmMjEsZZ28uSFdwSkVPV9t5Qy3ZRUrthaaC8RbOneznkMPGKbD9LnxmmUlFP0EP8hF1pU3w/s1600/PIC16F877A+UART+MAIN+CODE.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;i&gt;Figure 3&lt;/i&gt;. &amp;nbsp; Main code for UART of PIC16F877A&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;In the main function, firstly&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;UART is initialised using&amp;nbsp;&lt;/span&gt;&lt;i&gt;InitUART()&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;function. After that, using&amp;nbsp;&lt;i&gt;SendStringSerially()&lt;/i&gt;&amp;nbsp;function &#39;&lt;i&gt;Hello World!&lt;/i&gt;&#39; is sent on UART. After that, global and peripheral interrupts are enabled, so that when a character is received on UART then an interrupt should be generated. In the end, i&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;n the&amp;nbsp;&lt;/span&gt;&lt;i&gt;while(1)&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;loop, code executes NULL statements.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;The code for&amp;nbsp;&lt;/span&gt;&lt;i&gt;InitUART()&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;function is shown below.&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgzDg3s0ZjdTH6HaLpvpFoCKHZtZP9temf87rFo-6fKntKEseNEsTXfNkHSjyblqsZV6Id2TSXTV24CEN4d2aC54fdPX9yI-BL75sVHaFaShsNLm6fEVdwYcuzl4GeKZu4Xv7Im-699WpQ/s1600/PIC16F877A+UART+INIT+CODE.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;336&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgzDg3s0ZjdTH6HaLpvpFoCKHZtZP9temf87rFo-6fKntKEseNEsTXfNkHSjyblqsZV6Id2TSXTV24CEN4d2aC54fdPX9yI-BL75sVHaFaShsNLm6fEVdwYcuzl4GeKZu4Xv7Im-699WpQ/s1600/PIC16F877A+UART+INIT+CODE.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;i&gt;Figure 4&lt;/i&gt;. &amp;nbsp; InitUART function code&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;From the above figure, it is clear that&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;i&gt;InitUART()&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;function initialises UART TX and RX pin direction first. Also, baud rate is set in SPBRG register according to the value of external crystal and &lt;i&gt;BAUDRATE&lt;/i&gt;. After that, various other register settings of UART are being programmed with correct values to start UART module.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;The code for the UART byte transmit and receive&amp;nbsp;functions is shown below.&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhxWTQ1b3XiCGcG42CU8bRB2Rwm-RJhM04YXbjFVUMPz8kVkU7_og_07UHkdcfZRZr29o-w3L1yUjL-R27SCR_gj8pkVNMEFj9y7mxBSsV6u6BxauMzfE5vD5VZ0mTgzDD-64A6TkFZ5-w/s1600/PIC16F877A+UART+FUNCS.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;267&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhxWTQ1b3XiCGcG42CU8bRB2Rwm-RJhM04YXbjFVUMPz8kVkU7_og_07UHkdcfZRZr29o-w3L1yUjL-R27SCR_gj8pkVNMEFj9y7mxBSsV6u6BxauMzfE5vD5VZ0mTgzDD-64A6TkFZ5-w/s1600/PIC16F877A+UART+FUNCS.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;i&gt;Figure 5&lt;/i&gt;. &amp;nbsp; UART byte transfer and receive function code&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;By using&amp;nbsp;&lt;/span&gt;&lt;i&gt;SendByteSerially()&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;function, you can send a single byte on UART. For example,&amp;nbsp;&lt;/span&gt;&lt;i&gt;SendByteSerially(&#39;H&#39;);&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;statement will send character &#39;&lt;i&gt;H&lt;/i&gt;&#39; on UART. Similarly,&amp;nbsp;&lt;/span&gt;&lt;i&gt;ReceiveByteSerially()&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;function will receive a byte from UART and return its value. You can call these function in your main to transmit or receive bytes on UART.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;Interrupt service routine function for UART reception is shown below.&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjbgwHhZheP9w9HDHFBCIRBpSN4swW91hU_DbO7FQhm30gidUZ1MYG6_JIJFg_M-GHRv9WhYAcGIUenB5lSZVufVlY3rV7FX-UCtywXwtI1AzIFpixg5qAUtjqZELKqmdk22NVR13pOL7s/s1600/PIC16F877A+UART+ISR+CODE.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;256&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjbgwHhZheP9w9HDHFBCIRBpSN4swW91hU_DbO7FQhm30gidUZ1MYG6_JIJFg_M-GHRv9WhYAcGIUenB5lSZVufVlY3rV7FX-UCtywXwtI1AzIFpixg5qAUtjqZELKqmdk22NVR13pOL7s/s1600/PIC16F877A+UART+ISR+CODE.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;i&gt;Figure 6&lt;/i&gt;. &amp;nbsp; ISR code for UART reception&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
In the ISR code shown above,&amp;nbsp;the character received from UART RX pin is stored in the &lt;i&gt;RCREG &lt;/i&gt;register and this character is echoed back using&amp;nbsp;&lt;i&gt;SendByteSerially&lt;/i&gt;&lt;i&gt;()&lt;/i&gt;&amp;nbsp;function. Whenever a character is sent to PIC16F877A, then this&lt;i&gt;&amp;nbsp;interrupt ISR()&lt;/i&gt;&amp;nbsp;function is executed automatically and in this function received character is echoed back.&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;You can leave your comments and suggestions in the comment section below.&lt;/span&gt;&lt;br /&gt;
&lt;div&gt;
&lt;ul&gt;&lt;/ul&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;ul&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;h3&gt;
&lt;span style=&quot;color: red; font-size: x-large;&quot;&gt;Downloads&lt;/span&gt;&lt;/h3&gt;
&lt;div&gt;
UART code for PIC16F877A was compiled in&amp;nbsp;MPLAB v8.85 with HI-TECH C v9.83 compiler and simulation was made in Proteus v7.10.&amp;nbsp;&lt;span style=&quot;background-color: white;&quot;&gt;To download code and&amp;nbsp;&lt;/span&gt;&lt;i style=&quot;background-color: white;&quot;&gt;Proteus&amp;nbsp;&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;simulation&amp;nbsp;&lt;/span&gt;&lt;a href=&quot;http://bit.ly/2nNz0ag&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;click here&lt;/a&gt;.&lt;span style=&quot;background-color: white;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div&gt;
&lt;table&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;div align=&quot;right&quot; right=&quot;right&quot; text-align:=&quot;text-align:&quot;&gt;
&lt;iframe allowfullscreen=&quot;allowfullscreen&quot; frameborder=&quot;0&quot; height=&quot;180&quot; scrolling=&quot;no&quot; src=&quot;https://wizpert.com/wizapi/widget?beta_key=3ba42&amp;amp;view=227&amp;amp;ep=6570&amp;amp;size=standard&quot; width=&quot;180&quot;&gt; &lt;/iframe&gt;
&lt;/div&gt;
&lt;/td&gt;&lt;td&gt;&lt;div&gt;
&lt;h3&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;span style=&quot;color: #274e13; font-size: large;&quot;&gt;Further Reading Suggestions&lt;/span&gt;&lt;/span&gt;&lt;/h3&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;You may also like,&lt;/span&gt;&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/08/133.html&quot;&gt;PIC16F877A 2 channel PWM code&amp;nbsp;+ Proteus simulation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/08/130.html&quot;&gt;PIC16F877A LED blinking code + Proteus simulation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/08/134.html&quot;&gt;PIC16F877A external interrupt code&amp;nbsp;+ Proteus simulation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/08/132.html&quot;&gt;PIC16F877A PWM code&amp;nbsp;+ Proteus simulation&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://saeedsolutions.blogspot.com/feeds/7707261775475273690/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://saeedsolutions.blogspot.com/2014/08/135.html#comment-form' title='19 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5869154326689252934/posts/default/7707261775475273690'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5869154326689252934/posts/default/7707261775475273690'/><link rel='alternate' type='text/html' href='http://saeedsolutions.blogspot.com/2014/08/135.html' title='PIC16F877A UART code + Proteus simulation'/><author><name>Saeed</name><uri>http://www.blogger.com/profile/18064969138529094661</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjX-BwnwvQO6wyLQkPbSKToV96ghexik1atXTUSP_saZvUV-_lgSgb4F1GErhqe3AXDpOkqEqCyfxWrAq68g6srNIZoC-D6QcXhFk7YOuaE75_j-rgETlRFafALhzi5fAR0WMrWs8H95G0/s72-c/PIC16F877A+UART+CCT.png" height="72" width="72"/><thr:total>19</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5869154326689252934.post-6455677492154652085</id><published>2014-08-21T18:24:00.001+05:00</published><updated>2017-04-02T14:58:32.923+05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="PIC micro-controller Projects"/><category scheme="http://www.blogger.com/atom/ns#" term="PIC16F877A Projects"/><title type='text'>PIC16F877A external interrupt code + Proteus simulation</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
This post answers the question, &quot;How to capture a pulse to generate an interrupt in PIC16F877A&quot; ?&lt;br /&gt;
Also, using PIC16 simulator (&lt;i&gt;Proteus&lt;/i&gt;) you can verify this external interrupt code and change it according to your needs.&amp;nbsp;&lt;span style=&quot;background-color: white;&quot;&gt;This code is written in C language using MPLAB with HI-TECH C compiler. You can download this code from the &#39;&lt;/span&gt;&lt;i style=&quot;background-color: white;&quot;&gt;Downloads&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&#39;&amp;nbsp;&lt;span style=&quot;font-family: inherit;&quot;&gt;section at the bottom of this page.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;span style=&quot;background-color: transparent;&quot;&gt;This PIC16F877A microcontroller tutorial provides the&lt;/span&gt;&amp;nbsp;external interrupt&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;code&lt;/span&gt;&amp;nbsp;(e-g when you need to control servo motor which has position encoder, with your PIC microcontroller you can use this code)&lt;span style=&quot;background-color: white;&quot;&gt;. As we know,&amp;nbsp;PIC16F877A microcontroller has one RB0/INT pin, this pin is used to service external interrupts in the circuit shown below.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;It is assumed that you know how to blink an LED with PIC16F877A microcontroller. If you don&#39;t then please read&amp;nbsp;&lt;/span&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/08/130.html&quot; target=&quot;_blank&quot;&gt;this page&lt;/a&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;first, before proceeding with this article.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;span style=&quot;background-color: transparent;&quot;&gt;The following diagram (made in&amp;nbsp;&lt;/span&gt;&lt;i style=&quot;background-color: transparent;&quot;&gt;Proteus&lt;/i&gt;&lt;span style=&quot;background-color: transparent;&quot;&gt;) shows the PIC microcontroller circuit diagram&lt;/span&gt;&lt;sup&gt;&lt;span style=&quot;color: purple;&quot;&gt;[1]&lt;/span&gt;&lt;/sup&gt;.&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjVaTLf_2CRepGEcSoFnBPd72X9zDYi0vdEBilLpZlG7MGaWuVB8DTvp7Gy1II-4w-sIDd1KWe7l390uIQPLtr3G9j2Y0Fgesa1SFKf2O4YdEqyhSjXwnkCZWRU_CP_z-76SK-Ivf6uPJw/s1600/PIC16F877A+EXT+INT+CCT.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;376&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjVaTLf_2CRepGEcSoFnBPd72X9zDYi0vdEBilLpZlG7MGaWuVB8DTvp7Gy1II-4w-sIDd1KWe7l390uIQPLtr3G9j2Y0Fgesa1SFKf2O4YdEqyhSjXwnkCZWRU_CP_z-76SK-Ivf6uPJw/s1600/PIC16F877A+EXT+INT+CCT.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;i&gt;Figure 1&lt;/i&gt;. &amp;nbsp; PIC16F877A external interrupt capture circuit&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
In the above circuit&lt;sup&gt;&lt;span style=&quot;color: purple;&quot;&gt;[2]&lt;/span&gt;&lt;/sup&gt;, normally a low value is present on the&amp;nbsp;&lt;span style=&quot;background-color: white;&quot;&gt;RB0/INT&lt;/span&gt;&amp;nbsp;pin because of the&amp;nbsp;pull down resistor. But when push button is pressed then a low to high pulse on the&amp;nbsp;&lt;span style=&quot;background-color: white;&quot;&gt;RB0/INT&lt;/span&gt;&amp;nbsp;pin generates the interrupt. And code is written in such a way that whenever interrupt is generated then RD0 pin is toggled.&lt;br /&gt;
&lt;h3&gt;
&lt;span style=&quot;color: #38761d; font-size: x-large;&quot;&gt;Code&lt;/span&gt;&lt;/h3&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;The code for the main function is shown below.&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhNc7eVNg8gJDbbTtI5Etub9QKzRz6-r1bpMXW_DnYZmucAtxJEu8zu-BmG_pKjUYpRO7SLPiH1VnoM2j79FQjXtDFlZ4fUw3uBAhV9VqThE_2q5TLOsEFzXCARkITwZrNpF8Vyimwha1U/s1600/PIC16F877A+EXT+INT+MAIN+CODE.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;516&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhNc7eVNg8gJDbbTtI5Etub9QKzRz6-r1bpMXW_DnYZmucAtxJEu8zu-BmG_pKjUYpRO7SLPiH1VnoM2j79FQjXtDFlZ4fUw3uBAhV9VqThE_2q5TLOsEFzXCARkITwZrNpF8Vyimwha1U/s1600/PIC16F877A+EXT+INT+MAIN+CODE.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;i&gt;Figure 2&lt;/i&gt;. &amp;nbsp; Main function code for INT pin interrupt code for PIC16F877A&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;In the main function, firstly&amp;nbsp;&lt;/span&gt;&lt;i&gt;InitExternal_INT()&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;function initialises interrupts on RB0/INT pin.&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;Then&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;RD0 pin direction is set to be an output. &lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;After that, NULL statements are executed in the&amp;nbsp;&lt;/span&gt;&lt;i&gt;while(1)&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;loop forever. Rest of the work is done in the interrupt service routine code of RB0/INT pin.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;The code for&amp;nbsp;&lt;/span&gt;&lt;i&gt;InitExternal_INT()&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;function is shown below.&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj9cVgQDr4JAcXOjYaBFqFMrvenvceQAFVgN6ivNby-t9FtgZPoDUnEpNg6Ay4iBOYOeiAVoROoUoBngL1SNOzN2a3z8StmBjigrT2Hx0CDhK7fDbEfb5RS45gVGdHFZ2G3JGVsPo3Ko0U/s1600/PIC16F877A+EXT+INT+INIT+CODE.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;123&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj9cVgQDr4JAcXOjYaBFqFMrvenvceQAFVgN6ivNby-t9FtgZPoDUnEpNg6Ay4iBOYOeiAVoROoUoBngL1SNOzN2a3z8StmBjigrT2Hx0CDhK7fDbEfb5RS45gVGdHFZ2G3JGVsPo3Ko0U/s1600/PIC16F877A+EXT+INT+INIT+CODE.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;i&gt;Figure 3.&lt;/i&gt; &amp;nbsp; InitExternal_INT function code&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;In the&amp;nbsp;&lt;i&gt;InitExternal_INT()&lt;/i&gt;&amp;nbsp;function, RB0 pin is made input to capture the push button signal. Then global and INT pin interrupts are enabled, also INT pin is made posedge triggered.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;Whenever a posedge pulse is received on the&amp;nbsp;RB0/INT pin, then following code is executed (shown below).&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhAJ2Pf-0OsQSxRg3DMSGvvcCHv0qxwl5MtfGNVY8lKX8jkiJ3Yh99YTPlbG6lWWHEECIr_H8jOhnLuifUHvzif66SsLiQHXFb8YhHtp43a02_mk-LsPNPM5KuIGXNC2Xy_BB8UxtROYs4/s1600/PIC16F877A+EXT+INT+ISR+CODE.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;192&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhAJ2Pf-0OsQSxRg3DMSGvvcCHv0qxwl5MtfGNVY8lKX8jkiJ3Yh99YTPlbG6lWWHEECIr_H8jOhnLuifUHvzif66SsLiQHXFb8YhHtp43a02_mk-LsPNPM5KuIGXNC2Xy_BB8UxtROYs4/s1600/PIC16F877A+EXT+INT+ISR+CODE.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;i&gt;Figure 4&lt;/i&gt;. &amp;nbsp; ISR code for external interrupts&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;In the&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;RB0&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;INT pin interrupt service routine code (shown above), RD0 pin is toggled. This means that whenever you press the push button then LED attached with&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;RD0&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;pin will toggle its state.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;Using the code given in this post you will be able to easily handle interrupts in the PIC microcontroller.&amp;nbsp;&lt;/span&gt;As an example, when you need to control servo motor (which has position encoder), then you can attach the output of position encoder on your PIC RB0/INT pin and capture interrupts on it. Whenever servo motor moves one position, then you will receive one interrupt. By incrementing a variable on each interrupt you can always keep track of servo motor position in real time.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;You can leave your comments and suggestions in the comment section below.&lt;/span&gt;&lt;br /&gt;
&lt;h3&gt;
&lt;span style=&quot;color: blue; font-size: x-large;&quot;&gt;Notes and References&lt;/span&gt;&lt;/h3&gt;
&lt;sup&gt;&lt;span style=&quot;color: purple;&quot;&gt;[1] &amp;nbsp;&lt;/span&gt;&lt;/sup&gt;&lt;span style=&quot;background-color: white;&quot;&gt;This is just a sample circuit and code to test external interrupts on&amp;nbsp;PIC16F877A.&amp;nbsp;You can modify this code to meet your requirements.&lt;/span&gt;&lt;br /&gt;
&lt;sup&gt;&lt;span style=&quot;color: purple;&quot;&gt;[2] &amp;nbsp;&lt;/span&gt;&lt;/sup&gt;&lt;span style=&quot;background-color: white;&quot;&gt;In this circuit, PIC16F877A is running at 5 MIPS speed using 20 MHz external crystal.&lt;/span&gt;&lt;br /&gt;
&lt;div&gt;
&lt;ul&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;h3&gt;
&lt;span style=&quot;color: red; font-size: x-large;&quot;&gt;Downloads&lt;/span&gt;&lt;/h3&gt;
&lt;div&gt;
External interrupt code for&amp;nbsp;&lt;span style=&quot;background-color: white;&quot;&gt;PIC16F877A&lt;/span&gt;&amp;nbsp;as compiled in&amp;nbsp;MPLAB v8.85 with HI-TECH C v9.83 compiler and simulation was made in Proteus v7.10.&amp;nbsp;&lt;span style=&quot;background-color: white;&quot;&gt;To download code and&amp;nbsp;&lt;/span&gt;&lt;i style=&quot;background-color: white;&quot;&gt;Proteus&amp;nbsp;&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;simulation&amp;nbsp;&lt;/span&gt;&lt;a href=&quot;http://bit.ly/2oMO7OR&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;click here&lt;/a&gt;&lt;span style=&quot;background-color: white;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div&gt;
&lt;table&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;div align=&quot;right&quot; right=&quot;right&quot; text-align:=&quot;text-align:&quot;&gt;
&lt;iframe allowfullscreen=&quot;allowfullscreen&quot; frameborder=&quot;0&quot; height=&quot;180&quot; scrolling=&quot;no&quot; src=&quot;http://wizpert.com/wizapi/widget?beta_key=3ba42&amp;amp;view=227&amp;amp;ep=6570&amp;amp;size=standard&quot; width=&quot;180&quot;&gt; &lt;/iframe&gt;
&lt;/div&gt;
&lt;/td&gt;&lt;td&gt;&lt;div&gt;
&lt;h3&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;span style=&quot;color: #274e13; font-size: large;&quot;&gt;Further Reading Suggestions&lt;/span&gt;&lt;/span&gt;&lt;/h3&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;You may also like,&lt;/span&gt;&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/08/133.html&quot;&gt;PIC16F877A 2 channel PWM code&amp;nbsp;+ Proteus simulation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/08/130.html&quot;&gt;PIC16F877A LED blinking code + Proteus simulation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/08/131.html&quot;&gt;How to use PIC16F877A pin as input ?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/08/132.html&quot;&gt;PIC16F877A PWM code&amp;nbsp;+ Proteus simulation&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://saeedsolutions.blogspot.com/feeds/6455677492154652085/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://saeedsolutions.blogspot.com/2014/08/134.html#comment-form' title='7 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5869154326689252934/posts/default/6455677492154652085'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5869154326689252934/posts/default/6455677492154652085'/><link rel='alternate' type='text/html' href='http://saeedsolutions.blogspot.com/2014/08/134.html' title='PIC16F877A external interrupt code + Proteus simulation'/><author><name>Saeed</name><uri>http://www.blogger.com/profile/18064969138529094661</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjVaTLf_2CRepGEcSoFnBPd72X9zDYi0vdEBilLpZlG7MGaWuVB8DTvp7Gy1II-4w-sIDd1KWe7l390uIQPLtr3G9j2Y0Fgesa1SFKf2O4YdEqyhSjXwnkCZWRU_CP_z-76SK-Ivf6uPJw/s72-c/PIC16F877A+EXT+INT+CCT.png" height="72" width="72"/><thr:total>7</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5869154326689252934.post-3704862076904977160</id><published>2014-08-20T13:03:00.004+05:00</published><updated>2017-04-02T14:57:33.281+05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="PIC micro-controller Projects"/><category scheme="http://www.blogger.com/atom/ns#" term="PIC16F877A Projects"/><title type='text'>PIC16F877A PWM (2 channel) code + Proteus simulation</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
This PIC16F877A microcontroller tutorial provides the 2 channel PWM code (using C language) and&amp;nbsp;&lt;i&gt;Proteus&amp;nbsp;&lt;/i&gt;simulation ( written using MPLAB and HI-TECH C compiler ).&amp;nbsp;&lt;span style=&quot;background-color: white;&quot;&gt;There are many uses for PWM signal, e-g you can control dc motor speed using PWM.&amp;nbsp;&lt;/span&gt;CCP is the name for the built in PWM module in PIC16F877A. There are 2 built in PWM modules available, namely CCP1 and CCP2. This code is using both CCP1 and CCP2 modules to generate 2&amp;nbsp;separate&amp;nbsp;PWM signals.&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;It is assumed that you know how to blink an LED with PIC16F877A microcontroller. If you don&#39;t then please read&amp;nbsp;&lt;/span&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/08/130.html&quot; target=&quot;_blank&quot;&gt;this page&lt;/a&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;first, before proceeding with this article.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
The following diagram (made in&amp;nbsp;&lt;i&gt;Proteus&lt;/i&gt;) shows the PWM generator circuit diagram.&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgfFmsC3P9oWJisZ1tkshyphenhyphen3Jm8NyU-qpMeTCPSAUqUljeNSW-jiM7xoQ1v72YiRXoYjne4cLUb1Vh2fvf47m32PtBPCTwT6hDeOEuWk3FECb4CDeYYwbKkwXrfuY1abnmUENgGPulLHSoE/s1600/PIC16F877A+2+CHANNEL+PWM+CCT.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;350&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgfFmsC3P9oWJisZ1tkshyphenhyphen3Jm8NyU-qpMeTCPSAUqUljeNSW-jiM7xoQ1v72YiRXoYjne4cLUb1Vh2fvf47m32PtBPCTwT6hDeOEuWk3FECb4CDeYYwbKkwXrfuY1abnmUENgGPulLHSoE/s1600/PIC16F877A+2+CHANNEL+PWM+CCT.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;i&gt;Figure 1&lt;/i&gt;. &amp;nbsp; PIC16F877A PWM generation circuit&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
A crystal of 20MHz is used here with PIC16F877A. As we know that any PIC micro controller has an architecture which executes an instruction in 4 CPU cycles, hence this 20Mhz crystal makes this PIC16F877A run at 5MIPS (Million&amp;nbsp;of instructions per second). In the above figure, it is clear that a PWM of 4.9kHz frequency with 50% duty cycle is being generated at&amp;nbsp;&lt;i&gt;RC2/CCP1&lt;/i&gt;&amp;nbsp;pin and&amp;nbsp;a PWM of 4.9kHz frequency with 75% duty cycle is being generated at&amp;nbsp;&lt;i&gt;RC1/CCP2&lt;/i&gt;&amp;nbsp;pin.&lt;br /&gt;
&lt;h3&gt;
&lt;span style=&quot;color: #38761d; font-size: x-large;&quot;&gt;Code&lt;/span&gt;&lt;/h3&gt;
The code for main function is shown below.&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiN1U1tVtgovpH1iu0IbT3YROMejAEjcs9-_zejCa54jaoITKbVd1VI_HHGtwR8OeQ0kpRW1BJ-1vtpTwt_w81Xu_38Y6vaCmZ-5DtylL01hwHyXvEedWm9M1ItxIJf1a0QXnAjc2Y4ifo/s1600/PIC16F877A+2+CHANNEL+PWM+MAIN+CODE.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;397&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiN1U1tVtgovpH1iu0IbT3YROMejAEjcs9-_zejCa54jaoITKbVd1VI_HHGtwR8OeQ0kpRW1BJ-1vtpTwt_w81Xu_38Y6vaCmZ-5DtylL01hwHyXvEedWm9M1ItxIJf1a0QXnAjc2Y4ifo/s1600/PIC16F877A+2+CHANNEL+PWM+MAIN+CODE.png&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;i&gt;Figure 2&lt;/i&gt;. &amp;nbsp; Main code for PIC16F877A PWM&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
In the main function, PWM is initialised using the &#39;&lt;i&gt;InitPWM()&lt;/i&gt;&#39; function. Then PWM duty cycle is set to 50% on CCP1 module and 75% on CCP2 module ( You can verify this value from&amp;nbsp;&lt;i&gt;Figure 1&lt;/i&gt;&amp;nbsp;). After that microcontroller is free and built in CCP1, CCP2 modules automatically generates desired PWM frequencies. When you need to change PWM duty cycle, then just call&amp;nbsp;&lt;i&gt;SetPWMDutyCycle()&amp;nbsp;&lt;/i&gt;function and change the duty cycle as per requirement. You can pass a value of 0(0% duty cycle) to 255(100% duty cycle) in the argument of&amp;nbsp;&lt;i&gt;SetPWMDutyCycle()&amp;nbsp;&lt;/i&gt;function.&lt;br /&gt;
&lt;br /&gt;
The code for&lt;i&gt; InitPWM()&lt;/i&gt; function is shown below.&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjMniRPlmVyLI85q-ZTvt6tBL3-SnwN-8OtzjRPfYKMXvwNw8bHVdQ-lQ7TlcyINplJghgpVrdtuwTRWByv2uiB68yVAXOGyBexLrBvOjlXi1tFTnqisOYAGHL7DbfstFwtROTcHsgQJrw/s1600/PIC16F877A+2+CHANNEL+INIT+PWM+CODE.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;232&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjMniRPlmVyLI85q-ZTvt6tBL3-SnwN-8OtzjRPfYKMXvwNw8bHVdQ-lQ7TlcyINplJghgpVrdtuwTRWByv2uiB68yVAXOGyBexLrBvOjlXi1tFTnqisOYAGHL7DbfstFwtROTcHsgQJrw/s1600/PIC16F877A+2+CHANNEL+INIT+PWM+CODE.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;i&gt;Figure 3&lt;/i&gt;. &amp;nbsp; InitPWM function code for PIC16F877A&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;i&gt;InitPWM() &lt;/i&gt;function&amp;nbsp;initialises the PWM module CCP1 and CCP2 to generate PWM signal at 4.88KHz using &#39;&lt;i&gt;Timer 2&lt;/i&gt;&#39;. RC2 ( Pin 17 ) gives the output of the CCP1 module and&amp;nbsp;RC1 ( Pin 16 ) gives the output of the CCP2 module.&lt;br /&gt;
&lt;br /&gt;
The code for&amp;nbsp;&#39;&lt;i&gt;SetPWMDutyCycle()&lt;/i&gt;&#39;&amp;nbsp;function is shown below.&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjwT_C0jKTmKx9w98XMmBgm5QHUTsDLgOi4ZQ_DRuBs3fTNB_Vq8z6NNwsrQQEEWSnrbNIc0vXL_MNeKniIXef2QbD1umj6cwjKqMt-3T35hQ1eJAVZscNGEGEsXy4K-7ENvlpZ7Al1G-I/s1600/PIC16F877A+2+CHANNEL+SETDUTYCYC+CODE.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;385&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjwT_C0jKTmKx9w98XMmBgm5QHUTsDLgOi4ZQ_DRuBs3fTNB_Vq8z6NNwsrQQEEWSnrbNIc0vXL_MNeKniIXef2QbD1umj6cwjKqMt-3T35hQ1eJAVZscNGEGEsXy4K-7ENvlpZ7Al1G-I/s1600/PIC16F877A+2+CHANNEL+SETDUTYCYC+CODE.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;i&gt;Figure 4&lt;/i&gt;. &amp;nbsp; SetPWMDutyCycle function code for PIC16F877A&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;i&gt;SetPWMDutyCycle()&lt;/i&gt;&amp;nbsp;is the function which is used to set the duty cycle value for the PWM. This function has 2 input&amp;nbsp;arguments. First is the parameter to select CCP module, i-e if you want to change PWM of CCP1 module or CCP2 module. Write 1 here for CCP1 and 2 for CCP2 module. Then second parameter is the value of PWM duty cycle. Write 0 here for 0% duty cycle and write 255 here for 100% duty cycle. For example, to generate a PWM with 50% duty cycle on CCP1 write &#39;&lt;i&gt;SetPWMDutyCycle(1, 127)&lt;/i&gt;&#39; in your code.&lt;br /&gt;
&lt;br /&gt;
You can calculate PWM frequency using the formula&amp;nbsp;&lt;i&gt;_XTAL_FREQ/(4*(PR2+1)*Prescalar)&lt;/i&gt;. Where&amp;nbsp;&lt;i&gt;_XTAL_FREQ&lt;/i&gt;&amp;nbsp;is the value of crystal frequency, in the above code it is defined to be 20000000.&amp;nbsp;&lt;i&gt;PR2&amp;nbsp;&lt;/i&gt;is&amp;nbsp;&lt;i&gt;Timer2&amp;nbsp;&lt;/i&gt;register, it&#39;s value is defined to be&amp;nbsp;&lt;i&gt;0xFF&amp;nbsp;&lt;/i&gt;( In the&lt;i&gt;&amp;nbsp;InitPWM()&lt;/i&gt;&amp;nbsp;function ). You can set&amp;nbsp;&lt;i&gt;PR2&amp;nbsp;&lt;/i&gt;value in between 0 to 0xFF only.&amp;nbsp;&lt;i&gt;Prescalar&amp;nbsp;&lt;/i&gt;value is currently set to be 4 in&amp;nbsp;&lt;i&gt;InitPWM()&lt;/i&gt;&amp;nbsp;function. Putting all these values, we get a PWM frequency of 20000000/(4*256*4) = 4.88kHz (This value can be verified from Figure1). You can modify these parameters to change PWM frequency.&lt;br /&gt;
&lt;br /&gt;
An application of PIC16F877A could be that you can use this PWM code to control dc motor with&amp;nbsp;&lt;span style=&quot;background-color: white;&quot;&gt;PIC16F877A c&lt;/span&gt;ontroller. You will need some sort of dc motor driver circuitry in between&amp;nbsp;&lt;span style=&quot;background-color: white;&quot;&gt;PIC16F877A c&lt;/span&gt;ontroller and the dc motor. Other than that, using this simple PWM controlling code, you can control dc motor speed with&amp;nbsp;&lt;span style=&quot;background-color: white;&quot;&gt;PIC16F877A&amp;nbsp;&lt;/span&gt;controller easily.&amp;nbsp;You can leave your comments in the comment section below.&lt;br /&gt;
&lt;h3&gt;
&lt;span style=&quot;color: red; font-size: x-large;&quot;&gt;Downloads&lt;/span&gt;&lt;/h3&gt;
&lt;div&gt;
2 channel PWM code using PIC16F877A was compiled in&amp;nbsp;MPLAB v8.85 with HI-TECH C v9.83 compiler and simulation was made in Proteus v7.10.&amp;nbsp;&lt;span style=&quot;background-color: white;&quot;&gt;To download code and&amp;nbsp;&lt;/span&gt;&lt;i style=&quot;background-color: white;&quot;&gt;Proteus&amp;nbsp;&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;simulation&amp;nbsp;&lt;/span&gt;&lt;a href=&quot;http://bit.ly/2nNECRI&quot; rel=&quot;nofollow&quot; style=&quot;background-color: white;&quot; target=&quot;_blank&quot;&gt;click here&lt;/a&gt;&lt;span style=&quot;background-color: white;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div&gt;
&lt;table&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;div align=&quot;right&quot; right=&quot;right&quot; text-align:=&quot;text-align:&quot;&gt;
&lt;iframe allowfullscreen=&quot;allowfullscreen&quot; frameborder=&quot;0&quot; height=&quot;180&quot; scrolling=&quot;no&quot; src=&quot;http://wizpert.com/wizapi/widget?beta_key=3ba42&amp;amp;view=227&amp;amp;ep=6570&amp;amp;size=standard&quot; width=&quot;180&quot;&gt; &lt;/iframe&gt;
&lt;/div&gt;
&lt;/td&gt;&lt;td&gt;&lt;div&gt;
&lt;h3&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;span style=&quot;color: #274e13; font-size: large;&quot;&gt;Further Reading Suggestions&lt;/span&gt;&lt;/span&gt;&lt;/h3&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;You may also like,&lt;/span&gt;&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/08/129.html&quot;&gt;How to set PIC16F877A configuration bits in code ?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/08/130.html&quot;&gt;PIC16F877A LED blinking code + Proteus simulation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/08/131.html&quot;&gt;How to use PIC16F877A pin as input ?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/08/132.html&quot;&gt;PIC16F877A PWM code&amp;nbsp;+ Proteus simulation&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://saeedsolutions.blogspot.com/feeds/3704862076904977160/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://saeedsolutions.blogspot.com/2014/08/133.html#comment-form' title='12 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5869154326689252934/posts/default/3704862076904977160'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5869154326689252934/posts/default/3704862076904977160'/><link rel='alternate' type='text/html' href='http://saeedsolutions.blogspot.com/2014/08/133.html' title='PIC16F877A PWM (2 channel) code + Proteus simulation'/><author><name>Saeed</name><uri>http://www.blogger.com/profile/18064969138529094661</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgfFmsC3P9oWJisZ1tkshyphenhyphen3Jm8NyU-qpMeTCPSAUqUljeNSW-jiM7xoQ1v72YiRXoYjne4cLUb1Vh2fvf47m32PtBPCTwT6hDeOEuWk3FECb4CDeYYwbKkwXrfuY1abnmUENgGPulLHSoE/s72-c/PIC16F877A+2+CHANNEL+PWM+CCT.png" height="72" width="72"/><thr:total>12</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5869154326689252934.post-1201618353213667817</id><published>2014-08-19T20:02:00.002+05:00</published><updated>2017-04-02T14:54:36.636+05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="PIC micro-controller Projects"/><category scheme="http://www.blogger.com/atom/ns#" term="PIC16F877A Projects"/><title type='text'>PIC16F877A PWM code + Proteus simulation</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
This post provides the PWM code (using C language) and &lt;i&gt;Proteus &lt;/i&gt;simulation for PIC16F877A microcontroller ( written using MPLAB and HI-TECH C compiler ).&amp;nbsp;&lt;span style=&quot;background-color: white;&quot;&gt;There are many uses for PWM signal, e-g you can control dc motor speed using PWM.&amp;nbsp;&lt;/span&gt;CCP is the name for the built in PWM module in PIC16F877A. There are 2 built in PWM modules available, namely CCP1 and CCP2. This code is using CCP1 module to generate PWM. You can download the complete project code and &lt;i&gt;Proteus &lt;/i&gt;simulation from the &#39;&lt;i&gt;Downloads&lt;/i&gt;&#39; section at the bottom of this page.&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;It is assumed that you know how to blink an LED with PIC16F877A microcontroller. If you don&#39;t then please read&amp;nbsp;&lt;/span&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/08/130.html&quot; target=&quot;_blank&quot;&gt;this page&lt;/a&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;first, before proceeding with this article.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
The following diagram (made in &lt;i&gt;Proteus&lt;/i&gt;) shows the circuit diagram.&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjP2zxALkLSDyvDTomi3wYbXVhsW1iRSCZ_XBzH_gnlCP8UYh6SDYaAvrP33GAXJZaGzna4ShR7ty3a8m7YwhdUJ7aq4zTWQJvcHnfzPaHxWZg24Vn7z6HNaiosxXvxRr7zyWw8mo8ZFN4/s1600/PIC16F877A+PWM+CCT.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;352&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjP2zxALkLSDyvDTomi3wYbXVhsW1iRSCZ_XBzH_gnlCP8UYh6SDYaAvrP33GAXJZaGzna4ShR7ty3a8m7YwhdUJ7aq4zTWQJvcHnfzPaHxWZg24Vn7z6HNaiosxXvxRr7zyWw8mo8ZFN4/s1600/PIC16F877A+PWM+CCT.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;i&gt;Figure 1&lt;/i&gt;. PIC16F877A PWM circuit&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
A crystal of 20MHz is used here with PIC16F877A. As we know that any PIC micro controller has an architecture which executes an instruction in 4 CPU cycles, hence this 20Mhz crystal makes this PIC16F877A run at 5MIPS (Million&amp;nbsp;of instructions per second). In the above figure, it is clear that a PWM of 4.9kHz frequency with 50% duty cycle is being generated at &lt;i&gt;RC2/CCP1&lt;/i&gt; pin.&lt;br /&gt;
&lt;h3&gt;
&lt;span style=&quot;color: #38761d; font-size: x-large;&quot;&gt;Code&lt;/span&gt;&lt;/h3&gt;
The code for main function is shown below.&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjGYH-mNO0fO72FFR6lCjNKrZrYYc0wYOHLNSmVildj8Zth7a3th7USyxKqcc52OpZsqeZrfbatWYoLLnypvZK3pQO7ttKIhd2nNqzyjMy5xOOMUiENjkwgR0l8OP7kDwZbPntZE58stf8/s1600/PIC16F877A+PWM+MAIN+CODE.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;616&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjGYH-mNO0fO72FFR6lCjNKrZrYYc0wYOHLNSmVildj8Zth7a3th7USyxKqcc52OpZsqeZrfbatWYoLLnypvZK3pQO7ttKIhd2nNqzyjMy5xOOMUiENjkwgR0l8OP7kDwZbPntZE58stf8/s1600/PIC16F877A+PWM+MAIN+CODE.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;i&gt;Figure 2&lt;/i&gt;. &amp;nbsp; Main code for PC16F877A PWM&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
In the main function, PWM is initialised using the &#39;&lt;i&gt;InitPWM()&lt;/i&gt;&#39; function. Then PWM duty cycle is set to 50% ( You can verify this value from &lt;i&gt;Figure 1&lt;/i&gt; ). After that microcontroller is free and built in CCP1 module automatically generates desired PWM frequency. When you need to change PWM duty cycle, then just call &lt;i&gt;SetPWMDutyCycle() &lt;/i&gt;function and change the duty cycle as per requirement. You can pass a value of 0(0% duty cycle) to 1023(100% duty cycle) in the arguement of&amp;nbsp;&lt;i&gt;SetPWMDutyCycle()&amp;nbsp;&lt;/i&gt;function.&lt;br /&gt;
&lt;br /&gt;
The code for PWM functions is shown below.&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgmGtrHVdrJwbAt3vXhQSzm7iaQYYAcaQa7xSsWOWFR4l6euK9uI6pIBXTI1UsSv0Dsvn8t3fFLX60i_7WerUzyDPG0OeKGIjprTfDItYe7-C2Qb_duptwOIIx_SnRTqAIhu5aIJPbpoXE/s1600/PIC16F877A+PWM+FUNC+CODE.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;458&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgmGtrHVdrJwbAt3vXhQSzm7iaQYYAcaQa7xSsWOWFR4l6euK9uI6pIBXTI1UsSv0Dsvn8t3fFLX60i_7WerUzyDPG0OeKGIjprTfDItYe7-C2Qb_duptwOIIx_SnRTqAIhu5aIJPbpoXE/s1600/PIC16F877A+PWM+FUNC+CODE.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;i&gt;Figure 3&lt;/i&gt;. &amp;nbsp; PWM function code for PIC16F877A&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
InitPWM() function&amp;nbsp;initialises the PWM module CCP1 to generate PWM signal at 4.88KHz using &#39;&lt;i&gt;Timer 2&lt;/i&gt;&#39;. RC2 ( Pin 17 ) gives the output of the CCP1 module.&amp;nbsp;CCP1 module has 10bit PWM resolution.&amp;nbsp;&#39;&lt;i&gt;SetPWMDutyCycle()&lt;/i&gt;&#39; is the function which is used to set the duty cycle value for the PWM of CCP1. This function has 1 input&amp;nbsp;argument&amp;nbsp;which is the required value of the duty cycle of PWM. Write 0 here for 0% duty cycle and write 1023 here for 100% duty cycle. For example, to generate a PWM with 50% duty cycle write &#39;&lt;i&gt;SetPWMDutyCycle( 512)&lt;/i&gt;&#39; in your code.&lt;br /&gt;
&lt;br /&gt;
You can calculate PWM frequency using the formula &lt;i&gt;_XTAL_FREQ/(4*(PR2+1)*Prescalar)&lt;/i&gt;. Where&amp;nbsp;&lt;i&gt;_XTAL_FREQ&lt;/i&gt; is the value of crystal frequency, in the above code it is defined to be 20000000. &lt;i&gt;PR2 &lt;/i&gt;is&amp;nbsp;&lt;i&gt;Timer2&amp;nbsp;&lt;/i&gt;register, it&#39;s value is defined to be &lt;i&gt;0xFF &lt;/i&gt;( In the&lt;i&gt; InitPWM()&lt;/i&gt; function ). You can set &lt;i&gt;PR2 &lt;/i&gt;value in between 0 to 0xFF only. &lt;i&gt;Prescalar &lt;/i&gt;value is currently set to be 4 in&amp;nbsp;&lt;i&gt;InitPWM()&lt;/i&gt;&amp;nbsp;function. Putting all these values, we get a PWM frequency of 20000000/(4*256*4) = 4.88kHz (This value can be verified from Figure1). You can modify these parameters to change PWM frequency.&lt;br /&gt;
&lt;br /&gt;
As an example, You can use this PWM code to control dc motor with&amp;nbsp;&lt;span style=&quot;background-color: white;&quot;&gt;PIC16F877A c&lt;/span&gt;ontroller. You will need some sort of dc motor driver circuitry in between&amp;nbsp;&lt;span style=&quot;background-color: white;&quot;&gt;PIC16F877A c&lt;/span&gt;ontroller and the dc motor. Other than that, using this simple PWM controlling code, you can control dc motor speed with&amp;nbsp;&lt;span style=&quot;background-color: white;&quot;&gt;PIC16F877A&amp;nbsp;&lt;/span&gt;controller easily.&amp;nbsp;You can leave your comments in the comment section below.&lt;br /&gt;
&lt;h3&gt;
&lt;span style=&quot;color: red; font-size: x-large;&quot;&gt;Downloads&lt;/span&gt;&lt;/h3&gt;
&lt;div&gt;
PWM code using PIC16F877A was compiled in&amp;nbsp;MPLAB v8.85 with HI-TECH C v9.83 compiler and simulation was made in Proteus v7.10.&amp;nbsp;&lt;span style=&quot;background-color: white;&quot;&gt;To download code and&amp;nbsp;&lt;/span&gt;&lt;i style=&quot;background-color: white;&quot;&gt;Proteus&amp;nbsp;&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;simulation&amp;nbsp;&lt;/span&gt;&lt;a href=&quot;http://bit.ly/2nKvUCb&quot; rel=&quot;nofollow&quot; style=&quot;background-color: white;&quot; target=&quot;_blank&quot;&gt;click here&lt;/a&gt;&lt;span style=&quot;background-color: white;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div&gt;
&lt;table&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;div align=&quot;right&quot; right=&quot;right&quot; text-align:=&quot;text-align:&quot;&gt;
&lt;iframe allowfullscreen=&quot;allowfullscreen&quot; frameborder=&quot;0&quot; height=&quot;180&quot; scrolling=&quot;no&quot; src=&quot;http://wizpert.com/wizapi/widget?beta_key=3ba42&amp;amp;view=227&amp;amp;ep=6570&amp;amp;size=standard&quot; width=&quot;180&quot;&gt; &lt;/iframe&gt;
&lt;/div&gt;
&lt;/td&gt;&lt;td&gt;&lt;div&gt;
&lt;h3&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;span style=&quot;color: #274e13; font-size: large;&quot;&gt;Further Reading Suggestions&lt;/span&gt;&lt;/span&gt;&lt;/h3&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;You may also like,&lt;/span&gt;&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/08/129.html&quot;&gt;How to set PIC16F877A configuration bits in code ?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/08/130.html&quot;&gt;PIC16F877A LED blinking code + Proteus simulation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/08/131.html&quot;&gt;How to use PIC16F877A pin as input ?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2012/05/pic18f452-led-blinking-code-proteus.html&quot;&gt;PIC18F452 LED blinking code + Proteus simulation&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://saeedsolutions.blogspot.com/feeds/1201618353213667817/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://saeedsolutions.blogspot.com/2014/08/132.html#comment-form' title='7 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5869154326689252934/posts/default/1201618353213667817'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5869154326689252934/posts/default/1201618353213667817'/><link rel='alternate' type='text/html' href='http://saeedsolutions.blogspot.com/2014/08/132.html' title='PIC16F877A PWM code + Proteus simulation'/><author><name>Saeed</name><uri>http://www.blogger.com/profile/18064969138529094661</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjP2zxALkLSDyvDTomi3wYbXVhsW1iRSCZ_XBzH_gnlCP8UYh6SDYaAvrP33GAXJZaGzna4ShR7ty3a8m7YwhdUJ7aq4zTWQJvcHnfzPaHxWZg24Vn7z6HNaiosxXvxRr7zyWw8mo8ZFN4/s72-c/PIC16F877A+PWM+CCT.png" height="72" width="72"/><thr:total>7</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5869154326689252934.post-5270117006294459544</id><published>2014-08-09T00:26:00.001+05:00</published><updated>2017-04-02T14:53:46.747+05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="PIC micro-controller Projects"/><category scheme="http://www.blogger.com/atom/ns#" term="PIC16F877A Projects"/><title type='text'>How to use PIC16F877A pin as input? (Code+Proteus simulation)</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;This post provides an example code to use PIC16F877A pins as inputs. After going through this example, you will understand how to make PIC16F877A pins as inputs and how to read their value in the code. This code is written in C language using MPLAB with HI-TECH C compiler.You can download this code from the &#39;&lt;/span&gt;&lt;i&gt;Downloads&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&#39; section at the bottom of this page.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;It is assumed that you know how to blink an LED with PIC16F877A microcontroller. If you don&#39;t then please read&amp;nbsp;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/08/130.html&quot; target=&quot;_blank&quot;&gt;this page&lt;/a&gt;&amp;nbsp;first, before proceeding with this article.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;Following figure shows the circuit used to demonstrate how to get input in PIC16F877A microcontroller.&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhF17jYa4JVJVvMgNDPcNYhZatPHgVxLcWH5kC8vvcFQYl8a1e0lS70aYKwAjuQdmTBSKusu7YJoHR5Xw0X1gqsoJab48_ycECNtgU0FLV9cu9pIOpLYVrXG4mSwIzXh2ILtpgFUNR8u-o/s1600/PIC16F877A+INPUT+PIN+CCT.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;362&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhF17jYa4JVJVvMgNDPcNYhZatPHgVxLcWH5kC8vvcFQYl8a1e0lS70aYKwAjuQdmTBSKusu7YJoHR5Xw0X1gqsoJab48_ycECNtgU0FLV9cu9pIOpLYVrXG4mSwIzXh2ILtpgFUNR8u-o/s1600/PIC16F877A+INPUT+PIN+CCT.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;i&gt;Figure 1&lt;/i&gt;. &amp;nbsp; PIC16F877A pin as input demonstrating circuit&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;In this figure,&amp;nbsp;PIC16F877A is running on external crystal of 20MHz&lt;/span&gt;&lt;sup&gt;&lt;span style=&quot;color: purple;&quot;&gt;[1]&lt;/span&gt;&lt;/sup&gt;&lt;span style=&quot;background-color: white;&quot;&gt;. RB0 pin is being used as the input pin. When the push button is in the pushed state then,&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;RB0&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;pin is high, otherwise&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;RB0&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;pin is low. Whenever RB0 pin is high, then RD0 pin (Attached with the LED) is also made high just to indicate correct reading of RB0 pin status in the microcontroller.&lt;/span&gt;&lt;br /&gt;
&lt;h3&gt;
&lt;span style=&quot;color: #38761d; font-size: x-large;&quot;&gt;Code&lt;/span&gt;&lt;/h3&gt;
The code for reading the status of RB0 pin is shown below.&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjY1uoyuREmLhiMwAEPNrUWIIH2oJXo5aMBAS-NZicumvjhsw2RNHVe1x0ODIrQsRIS-9A0NcAGQ3MMam9xbvCbv4yz3IhuswKDbXuFxFCQMKZgxrv5yMRn6Z-KaJ9JZlZYPrJeo2vsKEg/s1600/PIC16F877A+INPUT+PIN+MAIN+CODE.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;640&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjY1uoyuREmLhiMwAEPNrUWIIH2oJXo5aMBAS-NZicumvjhsw2RNHVe1x0ODIrQsRIS-9A0NcAGQ3MMam9xbvCbv4yz3IhuswKDbXuFxFCQMKZgxrv5yMRn6Z-KaJ9JZlZYPrJeo2vsKEg/s1600/PIC16F877A+INPUT+PIN+MAIN+CODE.png&quot; width=&quot;632&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;i&gt;Figure 2&lt;/i&gt;. &amp;nbsp; GPIO pins as input for PIC16F877A main code&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
In the main function, firstly RB0 pin is made an input and RD0 is made an output&lt;span style=&quot;background-color: white;&quot;&gt;. Using TRISx register&lt;/span&gt;&lt;sup&gt;&lt;span style=&quot;color: purple;&quot;&gt;[2]&lt;/span&gt;&lt;/sup&gt;&lt;span style=&quot;background-color: white;&quot;&gt;, we can set the direction of any pin i-e if it is an input or output. So,&amp;nbsp;&lt;i&gt;TRISB0 = 1;&lt;/i&gt;&amp;nbsp;makes RB0 pin an input pin. And&amp;nbsp;&lt;/span&gt;&lt;i&gt;TRISD0 = 0;&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;makes RD0 pin an output pin.&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;Also, using the statement&amp;nbsp;&lt;i&gt;RD0 = 0;&lt;/i&gt;&amp;nbsp;RD0 pin is made low.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
In the&amp;nbsp;&lt;i&gt;while(1)&lt;/i&gt;&amp;nbsp;loop, status of RB0 pin is constantly being checked, if it becomes high then RD0 is also made high. If RB0 is low, then RD0 is also made low as well.&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;You can leave your comments in the comment section below.&lt;/span&gt;&lt;br /&gt;
&lt;h3&gt;
&lt;span style=&quot;color: blue; font-size: x-large;&quot;&gt;Notes and References&lt;/span&gt;&lt;/h3&gt;
&lt;sup&gt;&lt;span style=&quot;color: purple;&quot;&gt;[1] &amp;nbsp;&lt;/span&gt;&lt;/sup&gt;&lt;span style=&quot;background-color: white;&quot;&gt;Because every PIC microcontroller has an architecture which executes an instruction in 4 CPU cycles, w&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;hen CPU frequency is defined to be 20MHz, then actual speed of this PIC microcontroller will be 5 MIPS (Million of instructions per second). You can attach any crystal from 0 to 20MHz with PIC16F877A.&lt;/span&gt;&lt;br /&gt;
&lt;sup&gt;&lt;span style=&quot;color: purple;&quot;&gt;[2] &amp;nbsp;&lt;/span&gt;&lt;/sup&gt;&lt;span style=&quot;background-color: white;&quot;&gt;For&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;details please refer to PIC16F877A datasheet.&lt;/span&gt;&lt;br /&gt;
&lt;div&gt;
&lt;h3&gt;
&lt;span style=&quot;color: red; font-size: x-large;&quot;&gt;Downloads&lt;/span&gt;&lt;/h3&gt;
&lt;/div&gt;
&lt;div&gt;
Input pin code using PIC16F877A was compiled in&amp;nbsp;MPLAB v8.85 with HI-TECH C v9.83 compiler and simulation was made in Proteus v7.10.&amp;nbsp;&lt;span style=&quot;background-color: white;&quot;&gt;To download code and&amp;nbsp;&lt;/span&gt;&lt;i style=&quot;background-color: white;&quot;&gt;Proteus&amp;nbsp;&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;simulation&amp;nbsp;&lt;/span&gt;&lt;a href=&quot;http://bit.ly/2nYQGzU&quot; rel=&quot;nofollow&quot; style=&quot;background-color: white;&quot; target=&quot;_blank&quot;&gt;click here&lt;/a&gt;&lt;span style=&quot;background-color: white;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div&gt;
&lt;table&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;div align=&quot;right&quot; right=&quot;right&quot; text-align:=&quot;text-align:&quot;&gt;
&lt;iframe allowfullscreen=&quot;allowfullscreen&quot; frameborder=&quot;0&quot; height=&quot;180&quot; scrolling=&quot;no&quot; src=&quot;http://wizpert.com/wizapi/widget?beta_key=3ba42&amp;amp;view=227&amp;amp;ep=6570&amp;amp;size=standard&quot; width=&quot;180&quot;&gt; &lt;/iframe&gt;
&lt;/div&gt;
&lt;/td&gt;&lt;td&gt;&lt;div&gt;
&lt;h3&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;span style=&quot;color: #274e13; font-size: large;&quot;&gt;Further Reading Suggestions&lt;/span&gt;&lt;/span&gt;&lt;/h3&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;You may also like,&lt;/span&gt;&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/08/129.html&quot;&gt;How to set PIC16F877A configuration bits in code ?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/08/130.html&quot;&gt;PIC16F877A LED blinking code + Proteus simulation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2012/10/pic16f84a-led-blinking-code-proteus.html&quot;&gt;PIC16F84A LED blinking code + Proteus simulation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2012/05/pic18f452-led-blinking-code-proteus.html&quot;&gt;PIC18F452 LED blinking code + Proteus simulation&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://saeedsolutions.blogspot.com/feeds/5270117006294459544/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://saeedsolutions.blogspot.com/2014/08/131.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5869154326689252934/posts/default/5270117006294459544'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5869154326689252934/posts/default/5270117006294459544'/><link rel='alternate' type='text/html' href='http://saeedsolutions.blogspot.com/2014/08/131.html' title='How to use PIC16F877A pin as input? (Code+Proteus simulation)'/><author><name>Saeed</name><uri>http://www.blogger.com/profile/18064969138529094661</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhF17jYa4JVJVvMgNDPcNYhZatPHgVxLcWH5kC8vvcFQYl8a1e0lS70aYKwAjuQdmTBSKusu7YJoHR5Xw0X1gqsoJab48_ycECNtgU0FLV9cu9pIOpLYVrXG4mSwIzXh2ILtpgFUNR8u-o/s72-c/PIC16F877A+INPUT+PIN+CCT.png" height="72" width="72"/><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5869154326689252934.post-2359384488832979709</id><published>2014-08-06T17:15:00.003+05:00</published><updated>2017-04-02T14:52:57.415+05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="PIC micro-controller Projects"/><category scheme="http://www.blogger.com/atom/ns#" term="PIC16F877A Projects"/><title type='text'>PIC16F877A LED blinking code + Proteus simulation</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;This post provides the code to make an LED blink using PIC16F877A microcontroller. This code is written in C language using MPLAB with HI-TECH C compiler. This code is intended to be the first step in learning how to use PIC16F877A microcontroller in your projects. You can download this code from the &#39;&lt;/span&gt;&lt;i&gt;Downloads&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&#39; section at the bottom of this page.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;Following figure shows the minimum circuit&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;required&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;to make an LED blink with PIC16F877A.&lt;/span&gt;&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgj5tpKM86cfoqIcu-FZLMIcCtwzDaZIBzh6hJhyjZbsK6cAwHOifc7ZSttxc9r6_90ur8DihmcoNF7ysoCDaHSUaWgMxGnMntHF2NYbtLB4FsYP2TBsNuzsm99DDDsZB6BovYIvRv59bQ/s1600/PIC16F877A+LED+BLINK+CCT.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;376&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgj5tpKM86cfoqIcu-FZLMIcCtwzDaZIBzh6hJhyjZbsK6cAwHOifc7ZSttxc9r6_90ur8DihmcoNF7ysoCDaHSUaWgMxGnMntHF2NYbtLB4FsYP2TBsNuzsm99DDDsZB6BovYIvRv59bQ/s1600/PIC16F877A+LED+BLINK+CCT.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;i&gt;Figure 1&lt;/i&gt;. &amp;nbsp; Circuit diagram for LED blinking using PIC16F877A&amp;nbsp;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;In this figure, first thing to note is that there is a crystal of 20MHz used with&amp;nbsp;PIC16F877A&lt;sup&gt;&lt;span style=&quot;color: purple;&quot;&gt;[1]&lt;/span&gt;&lt;/sup&gt;. You can use any crystal from 3 to 20MHz with&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;PIC16F877A&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;.&amp;nbsp;&lt;i&gt;MCLR&lt;/i&gt;&amp;nbsp;master reset pin is pulled high to keep&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;PIC16F877A out of reset&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;. RB0 pin is being toggled in the code.&lt;/span&gt;&lt;br /&gt;
&lt;h3&gt;
&lt;span style=&quot;color: #38761d; font-size: x-large;&quot;&gt;Code&lt;/span&gt;&lt;/h3&gt;
The code for making LED blink using&amp;nbsp;&lt;span style=&quot;background-color: white;&quot;&gt;PIC16F877A&amp;nbsp;&lt;/span&gt;is shown below.&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjuruAuzEeAGnmYXmnucz6DAZDFhglnmpg0ObRHcNUMHV19VX9qSjDDKOZZuy8YEod3DRIlxVSFyBmPGs2OlLs_I53byYVk4hntGdoH6nR5LUpERJAZPiMceL5myDRAhiAVHJnlhNSS6HE/s1600/PIC16F877A+LED+BLINK+MAIN+CODE.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;640&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjuruAuzEeAGnmYXmnucz6DAZDFhglnmpg0ObRHcNUMHV19VX9qSjDDKOZZuy8YEod3DRIlxVSFyBmPGs2OlLs_I53byYVk4hntGdoH6nR5LUpERJAZPiMceL5myDRAhiAVHJnlhNSS6HE/s1600/PIC16F877A+LED+BLINK+MAIN+CODE.png&quot; width=&quot;612&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;i&gt;Figure 2&lt;/i&gt;. &amp;nbsp; Code for making LED blink using PIC16F877A&amp;nbsp;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
Whenever you are writing code for PIC microcontrollers, then you have to include &quot;htc.h&quot; file in the code. After including &quot;htc.h&quot; file, configuration bits are being set in the code shown above. To understand the details of how these configuration bits are being programmed, you can read&amp;nbsp;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/08/129.html&quot; target=&quot;_blank&quot;&gt;this post&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
After the configuration bits, LED pin is being defined as the RB0 pin. You can replace&amp;nbsp;RB0&amp;nbsp;with any other pin name if you want (e-g RB1 etc). After LED pin definition, CPU frequency is being defined. You have to define _XTAL_FREQ macro, if you want to use built in delay functions like&amp;nbsp;&lt;i&gt;__delay_us()&amp;nbsp;&lt;/i&gt;and&amp;nbsp;&lt;i&gt;__delay_ms()&lt;/i&gt;. Here CPU frequency is defined to be 20MHz, which is the crystal oscillator frequency attached with PIC16F877A in the circuit.&lt;br /&gt;
&lt;br /&gt;
In the main function, firstly RB0 pin direction is set to be an output pin using&amp;nbsp;&lt;i&gt;TRISB0 = 0;&lt;/i&gt;&amp;nbsp;statement&lt;span style=&quot;background-color: white;&quot;&gt;. Using TRISx register&lt;/span&gt;&lt;sup&gt;&lt;span style=&quot;color: purple;&quot;&gt;[2]&lt;/span&gt;&lt;/sup&gt;&lt;span style=&quot;background-color: white;&quot;&gt;, we can set the direction of any pin i-e if it is an input or output. Then LED pin is made low using&lt;i&gt;&amp;nbsp;LED = 0;&lt;/i&gt;&amp;nbsp;statement.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;LED pin is being toggled i&lt;/span&gt;n the while loop after every half second. In this way you can easily make LED blink using&amp;nbsp;&lt;span style=&quot;background-color: white;&quot;&gt;PIC16F877A&amp;nbsp;&lt;/span&gt;microcontroller.&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;You can leave your comments in the comment section below.&lt;/span&gt;&lt;br /&gt;
&lt;h3&gt;
&lt;span style=&quot;color: blue; font-size: x-large;&quot;&gt;Notes and References&lt;/span&gt;&lt;/h3&gt;
&lt;sup&gt;&lt;span style=&quot;color: purple;&quot;&gt;[1] &amp;nbsp;&lt;/span&gt;&lt;/sup&gt;&lt;span style=&quot;background-color: white;&quot;&gt;Because every PIC microcontroller has an architecture which executes an instruction in 4 CPU cycles, w&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;hen CPU frequency is defined to be 20MHz, then actual speed of this PIC microcontroller will be 5 MIPS (Million of instructions per second).&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;sup&gt;&lt;span style=&quot;color: purple;&quot;&gt;[2] &amp;nbsp;&lt;/span&gt;&lt;/sup&gt;&lt;span style=&quot;background-color: white;&quot;&gt;For&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;details please refer to&amp;nbsp;PIC16F877A datasheet.&lt;/span&gt;&lt;br /&gt;
&lt;div&gt;
&lt;h3&gt;
&lt;span style=&quot;color: red; font-size: x-large;&quot;&gt;Downloads&lt;/span&gt;&lt;/h3&gt;
&lt;/div&gt;
&lt;div&gt;
LED blinking code using PIC16F877A was compiled in&amp;nbsp;MPLAB v8.85 with HI-TECH C v9.83 compiler and simulation was made in Proteus v7.10.&amp;nbsp;&lt;span style=&quot;background-color: white;&quot;&gt;To download code and&amp;nbsp;&lt;/span&gt;&lt;i style=&quot;background-color: white;&quot;&gt;Proteus&amp;nbsp;&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;simulation&amp;nbsp;&lt;/span&gt;&lt;a href=&quot;http://bit.ly/2nNHb6t&quot; rel=&quot;nofollow&quot; style=&quot;background-color: white;&quot; target=&quot;_blank&quot;&gt;click here&lt;/a&gt;&lt;span style=&quot;background-color: white;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div&gt;
&lt;table&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;div align=&quot;right&quot; right=&quot;right&quot; text-align:=&quot;text-align:&quot;&gt;
&lt;iframe allowfullscreen=&quot;allowfullscreen&quot; frameborder=&quot;0&quot; height=&quot;180&quot; scrolling=&quot;no&quot; src=&quot;http://wizpert.com/wizapi/widget?beta_key=3ba42&amp;amp;view=227&amp;amp;ep=6570&amp;amp;size=standard&quot; width=&quot;180&quot;&gt; &lt;/iframe&gt;
&lt;/div&gt;
&lt;/td&gt;&lt;td&gt;&lt;div&gt;
&lt;h3&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;span style=&quot;color: #274e13; font-size: large;&quot;&gt;Further Reading Suggestions&lt;/span&gt;&lt;/span&gt;&lt;/h3&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;You may also like,&lt;/span&gt;&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2012/05/how-to-write-configuration-bits-for-pic.html&quot;&gt;How to write configuration bits for PIC micro-controllers ?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2012/06/pic12f675-led-blinking-code-proteus.html&quot; target=&quot;_blank&quot;&gt;PIC12F675 LED blinking code + Proteus simulation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2012/10/pic16f84a-led-blinking-code-proteus.html&quot;&gt;PIC16F84A LED blinking code + Proteus simulation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2012/05/pic18f452-led-blinking-code-proteus.html&quot;&gt;PIC18F452 LED blinking code + Proteus simulation&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://saeedsolutions.blogspot.com/feeds/2359384488832979709/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://saeedsolutions.blogspot.com/2014/08/130.html#comment-form' title='14 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5869154326689252934/posts/default/2359384488832979709'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5869154326689252934/posts/default/2359384488832979709'/><link rel='alternate' type='text/html' href='http://saeedsolutions.blogspot.com/2014/08/130.html' title='PIC16F877A LED blinking code + Proteus simulation'/><author><name>Saeed</name><uri>http://www.blogger.com/profile/18064969138529094661</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgj5tpKM86cfoqIcu-FZLMIcCtwzDaZIBzh6hJhyjZbsK6cAwHOifc7ZSttxc9r6_90ur8DihmcoNF7ysoCDaHSUaWgMxGnMntHF2NYbtLB4FsYP2TBsNuzsm99DDDsZB6BovYIvRv59bQ/s72-c/PIC16F877A+LED+BLINK+CCT.png" height="72" width="72"/><thr:total>14</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5869154326689252934.post-7627057945581700598</id><published>2014-08-05T15:20:00.003+05:00</published><updated>2017-04-02T14:52:39.406+05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="PIC micro-controller Projects"/><category scheme="http://www.blogger.com/atom/ns#" term="PIC16F877A Projects"/><title type='text'>How to set PIC16F877A configuration bits in code ?</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;This post provides the method to set configuration bits in the code for PIC16F877A microcontroller. You have to set configuration bits in order to make PIC microcontroller work correctly. Here I am going to explain the procedure of selecting configuration bits for&amp;nbsp;PIC16F877A. A typical example code for setting configuration bits in the code&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;(using MPLAB + HI-TECH C compiler) is also provided. Following steps are required to setup configuration bits for&amp;nbsp;PIC16F877A.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&lt;b&gt;&lt;i&gt;Step 1:&lt;/i&gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;The first step to start writing the configuration bits is to check &quot;Special Features of the CPU&quot; section in the datasheet of&amp;nbsp;PIC16F877A. In this section, first chapter is about &quot;Configuration bits&quot;. You should read this section and identify what values you want to put in the configuration register.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&lt;b&gt;&lt;i&gt;Step 2:&lt;/i&gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;Open the file &quot;pic16f877a.h&quot; from the directory &quot;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;C:\Program Files\HI-TECH Software\PICC\9.83\include&quot;. In the start of this file configuration bit macros are defined. Snapshot of these macros is shown below.&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgIcioqR9Tlug664V8PnRe-FyXb8Pwnom10fu-AphqfQfWwD0XeTNn4j57LywZ5Saqz6VhwryFGUC2TQcJ5DnButsRbpBXuAznj-Pq6MWH82QI7l-dRCt9x-xCDdw3_rXt6VjiYUAms9Uc/s1600/PIC16F877A+FUSE+SETTINGS.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;640&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgIcioqR9Tlug664V8PnRe-FyXb8Pwnom10fu-AphqfQfWwD0XeTNn4j57LywZ5Saqz6VhwryFGUC2TQcJ5DnButsRbpBXuAznj-Pq6MWH82QI7l-dRCt9x-xCDdw3_rXt6VjiYUAms9Uc/s1600/PIC16F877A+FUSE+SETTINGS.png&quot; width=&quot;518&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;i&gt;Figure 1&lt;/i&gt;. &amp;nbsp; Configuration bits for PIC16F877A&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;i&gt;&lt;b&gt;Step 3:&lt;/b&gt;&lt;/i&gt;&lt;br /&gt;
Here we have to choose appropriate macros to be used in the code to set configuration bits correctly.&amp;nbsp;For each configuration bit we have a choice between some macros. For example, for watchdog timer enable bit, we have a choice between&amp;nbsp;WDTE_ON or&amp;nbsp;WDTE_OFF. If we want to enable the watchdog timer, then&amp;nbsp;&lt;span style=&quot;background-color: white;&quot;&gt;WDTE_ON&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;macro should be used in the code. But if we want to disable watchdog, then&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;WDTE_OFF macro should be used in the code to set the configuration bit accordingly. Similarly, you can select appropriate macros for other configuration bits.&lt;/span&gt;&lt;br /&gt;
&lt;h3&gt;
&lt;span style=&quot;color: #38761d; font-size: x-large;&quot;&gt;Code&lt;/span&gt;&lt;/h3&gt;
We can program configuration bits in the code using&amp;nbsp;&lt;i&gt;__CONFIG&lt;/i&gt;&amp;nbsp;macro&lt;sup&gt;&lt;span style=&quot;color: purple;&quot;&gt;[1]&lt;/span&gt;&lt;/sup&gt;&lt;span style=&quot;background-color: white;&quot;&gt;. The code to make an LED blink with PIC16F877A&lt;/span&gt;&lt;sup&gt;&lt;span style=&quot;color: purple;&quot;&gt;[2]&lt;/span&gt;&lt;/sup&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;is shown below in the figure. You can see how&amp;nbsp;&lt;/span&gt;&lt;i&gt;__CONFIG&amp;nbsp;&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;macro is used here, to set the desired configuration bits in the code.&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhe7knBYn4do2YS7o_lBULByUB5JLe2eol2RbDbKDBeFs2Upcf3yKSVOmn9rVSpllkuIjHNFhQQ4L0YBfSdElz3NZWYkPtD6fHtccfZHwQFSdxLZ71GBW3bq7f3BLzoqhQwl-gdSQqUClo/s1600/PIC16F877A+LED+BLINK+MAIN+CODE.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;400&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhe7knBYn4do2YS7o_lBULByUB5JLe2eol2RbDbKDBeFs2Upcf3yKSVOmn9rVSpllkuIjHNFhQQ4L0YBfSdElz3NZWYkPtD6fHtccfZHwQFSdxLZ71GBW3bq7f3BLzoqhQwl-gdSQqUClo/s1600/PIC16F877A+LED+BLINK+MAIN+CODE.png&quot; width=&quot;382&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;i&gt;Figure 2&lt;/i&gt;. &amp;nbsp; PIC16F877A basic LED blinking code&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;You have to place the&amp;nbsp;&lt;i&gt;__CONFIG&amp;nbsp;&lt;/i&gt;macro outside the main function, preferably in the start of the file. All the selected macros are&amp;nbsp;separated&amp;nbsp;with the &amp;amp; (AND operator) in code. In this code selected configuration bits for PIC16F877A are&lt;/span&gt;&lt;sup&gt;&lt;span style=&quot;color: purple;&quot;&gt;[3]&lt;/span&gt;&lt;/sup&gt;&lt;span style=&quot;background-color: white;&quot;&gt;,&lt;/span&gt;&lt;br /&gt;
&lt;div&gt;
&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt;External HS crystal is selected as CPU clock source.&lt;/li&gt;
&lt;li&gt;Watchdog is disabled.&lt;/li&gt;
&lt;li&gt;Power up timer is enabled.&lt;/li&gt;
&lt;li&gt;Program memory code protection is disabled.&lt;/li&gt;
&lt;li&gt;Brown out is enabled.&lt;/li&gt;
&lt;li&gt;Low voltage programming is disabled.&lt;/li&gt;
&lt;li&gt;Internal EEPROM data memory protection is disabled.&lt;/li&gt;
&lt;li&gt;Flash program memory write is enabled.&lt;/li&gt;
&lt;li&gt;In circuit debugger is disabled, RB6 and RB7 are general purpose pins.&lt;/li&gt;
&lt;/ul&gt;
Using this procedure you can easily write configuration bit settings in the code. You can leave your comments in the comment section below.&lt;br /&gt;
&lt;h3&gt;
&lt;span style=&quot;color: blue; font-size: x-large;&quot;&gt;Notes and References&lt;/span&gt;&lt;/h3&gt;
&lt;sup&gt;&lt;span style=&quot;color: purple;&quot;&gt;[1]&amp;nbsp;&lt;/span&gt;&lt;/sup&gt;&lt;span style=&quot;background-color: white;&quot;&gt;For details check manual of HI-TECH C Chapter 3.2.2.&lt;/span&gt;&lt;br /&gt;
&lt;sup&gt;&lt;span style=&quot;color: purple;&quot;&gt;[2]&amp;nbsp;&lt;/span&gt;&lt;/sup&gt;&lt;span style=&quot;background-color: white;&quot;&gt;You can download LED blinking code for PIC16F877A, from the &#39;&lt;i&gt;Downloads&lt;/i&gt;&#39; section at the bottom of this page.&lt;/span&gt;&lt;br /&gt;
&lt;sup&gt;&lt;span style=&quot;color: purple;&quot;&gt;[3]&amp;nbsp;&lt;/span&gt;&lt;/sup&gt;&lt;span style=&quot;background-color: white;&quot;&gt;For details of these settings, check the PIC16F877A datasheet section &quot;Special features of the CPU&quot;.&lt;/span&gt;&lt;br /&gt;
&lt;div&gt;
&lt;h3&gt;
&lt;span style=&quot;color: red; font-size: x-large;&quot;&gt;Downloads&lt;/span&gt;&lt;/h3&gt;
&lt;/div&gt;
&lt;div&gt;
LED blinking code using PIC16F877A was compiled in&amp;nbsp;MPLAB v8.85 with HI-TECH C v9.83 compiler and simulation was made in Proteus v7.10.&amp;nbsp;&lt;span style=&quot;background-color: white;&quot;&gt;To download code and&amp;nbsp;&lt;/span&gt;&lt;i style=&quot;background-color: white;&quot;&gt;Proteus&amp;nbsp;&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;simulation&amp;nbsp;&lt;/span&gt;&lt;a href=&quot;http://bit.ly/2nNHb6t&quot; rel=&quot;nofollow&quot; style=&quot;background-color: white;&quot; target=&quot;_blank&quot;&gt;click here&lt;/a&gt;&lt;span style=&quot;background-color: white;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div&gt;
&lt;table&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;div align=&quot;right&quot; right=&quot;right&quot; text-align:=&quot;text-align:&quot;&gt;
&lt;iframe allowfullscreen=&quot;allowfullscreen&quot; frameborder=&quot;0&quot; height=&quot;180&quot; scrolling=&quot;no&quot; src=&quot;http://wizpert.com/wizapi/widget?beta_key=3ba42&amp;amp;view=227&amp;amp;ep=6570&amp;amp;size=standard&quot; width=&quot;180&quot;&gt; &lt;/iframe&gt;
&lt;/div&gt;
&lt;/td&gt;&lt;td&gt;&lt;div&gt;
&lt;h3&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;span style=&quot;color: #274e13; font-size: large;&quot;&gt;Further Reading Suggestions&lt;/span&gt;&lt;/span&gt;&lt;/h3&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;You may also like,&lt;/span&gt;&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2012/05/how-to-write-configuration-bits-for-pic.html&quot;&gt;How to write configuration bits for PIC micro-controllers ?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2012/06/pic12f675-led-blinking-code-proteus.html&quot; target=&quot;_blank&quot;&gt;PIC12F675 LED blinking code + Proteus simulation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2012/10/pic16f84a-led-blinking-code-proteus.html&quot;&gt;PIC16F84A LED blinking code + Proteus simulation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2012/05/pic18f452-led-blinking-code-proteus.html&quot;&gt;PIC18F452 LED blinking code + Proteus simulation&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://saeedsolutions.blogspot.com/feeds/7627057945581700598/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://saeedsolutions.blogspot.com/2014/08/129.html#comment-form' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5869154326689252934/posts/default/7627057945581700598'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5869154326689252934/posts/default/7627057945581700598'/><link rel='alternate' type='text/html' href='http://saeedsolutions.blogspot.com/2014/08/129.html' title='How to set PIC16F877A configuration bits in code ?'/><author><name>Saeed</name><uri>http://www.blogger.com/profile/18064969138529094661</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgIcioqR9Tlug664V8PnRe-FyXb8Pwnom10fu-AphqfQfWwD0XeTNn4j57LywZ5Saqz6VhwryFGUC2TQcJ5DnButsRbpBXuAznj-Pq6MWH82QI7l-dRCt9x-xCDdw3_rXt6VjiYUAms9Uc/s72-c/PIC16F877A+FUSE+SETTINGS.png" height="72" width="72"/><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5869154326689252934.post-151181139789038641</id><published>2014-08-04T18:57:00.001+05:00</published><updated>2017-04-02T14:51:36.858+05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="PIC micro-controller Projects"/><category scheme="http://www.blogger.com/atom/ns#" term="PIC16F628A Projects"/><title type='text'>PIC16F628A Comparator Code + Proteus Simulation</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;This post provides the comparator&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;code for PIC16F628A microcontroller. As we know, PIC16F628A&amp;nbsp;microcontroller has one built in op-amp, which can be used as comparator. This code is written in C language using MPLAB with HI-TECH C compiler. You can download this code from the &#39;&lt;/span&gt;&lt;i style=&quot;background-color: white;&quot;&gt;Downloads&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&#39;&amp;nbsp;&lt;span style=&quot;font-family: inherit;&quot;&gt;section at the bottom of this page.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;It is assumed that you know how to blink an LED with PIC16F628A&amp;nbsp;microcontroller. If you don&#39;t then please read&amp;nbsp;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2013/08/pic16f628a-led-blinking-code-proteus.html&quot; target=&quot;_blank&quot;&gt;this page&lt;/a&gt;&amp;nbsp;first, before proceeding with this article.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;The result of simulating the code in&amp;nbsp;&lt;i&gt;Proteus&amp;nbsp;&lt;/i&gt;is shown below&lt;sup&gt;&lt;span style=&quot;color: purple;&quot;&gt;[1]&lt;/span&gt;&lt;/sup&gt;.
&lt;/span&gt;&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjyceNFPOmTbfhUv5rVdPGTF4gorn6GbWsCY5cbmbpMoCSo9BLi4F6hr2VzFS_KvEQSyMsLmuzeLQ-_Jrmuty31HI_iT2sDJ5EL37bHHIqVLfGLHRI8lkuS1iAJzFzGZfA57La6W5k6dNE/s1600/PIC16F628A+Comparator+Circuit.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;306&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjyceNFPOmTbfhUv5rVdPGTF4gorn6GbWsCY5cbmbpMoCSo9BLi4F6hr2VzFS_KvEQSyMsLmuzeLQ-_Jrmuty31HI_iT2sDJ5EL37bHHIqVLfGLHRI8lkuS1iAJzFzGZfA57La6W5k6dNE/s1600/PIC16F628A+Comparator+Circuit.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;i&gt;Figure 1&lt;/i&gt;. &amp;nbsp; PIC16F628A comparator circuit&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
In the above circuit&lt;sup&gt;&lt;span style=&quot;color: purple;&quot;&gt;[2]&lt;/span&gt;&lt;/sup&gt;, a variable voltage signal of 0 to 5 volts can be applied on Pin17 and Pin18 of PIC1&lt;span style=&quot;background-color: white;&quot;&gt;6F628A&lt;/span&gt;&amp;nbsp;controller. There are two comparators in PIC16F628A as shown in the picture below.&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiz3Y2QWx2VPQtLdUkkJ3Lc9-C6eX8AKR_S9GKHH9c-Hp-vio585i-6UXxPFpBNHE8Ek4bI5MaLLxJzvE6BrZ_n3DbVo03qmI4zaiWGaWX5rZ_3XS8zAXLua9SAhK3aoV6wv-acE4V0Uek/s1600/PIC16F628A+Comparators.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;191&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiz3Y2QWx2VPQtLdUkkJ3Lc9-C6eX8AKR_S9GKHH9c-Hp-vio585i-6UXxPFpBNHE8Ek4bI5MaLLxJzvE6BrZ_n3DbVo03qmI4zaiWGaWX5rZ_3XS8zAXLua9SAhK3aoV6wv-acE4V0Uek/s1600/PIC16F628A+Comparators.png&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;i&gt;Figure 2&lt;/i&gt;. &amp;nbsp; PIC16F628A comparators&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
CIS register value was selected to be zero in the code, so Pin17 and Pin18 are the input pins for the comparators and Pin10, Pin11 are selected to be output pins. In the above circuit shown, Pin17 and Pin18 are attached with the -ve pin of the comparator and on the +ve pin a reference voltage of 2.5v is applied&amp;nbsp;internally. LEDs attached on the RB4, RB5 pins show the result of the comparators.&lt;br /&gt;
&lt;br /&gt;
So, in the &lt;i&gt;Figure1&lt;/i&gt;, RB4 pin is high because voltage on Pin17 of PIC1&lt;span style=&quot;background-color: white;&quot;&gt;6F628A&lt;/span&gt;&amp;nbsp;is 2.45v, which is less than 2.5v. If a voltage higher than 2.5v is applied on Pin17, then RB4 pin will become low. Similarly, RB5 pin is low because voltage on Pin18 of PIC1&lt;span style=&quot;background-color: white;&quot;&gt;6F628A&lt;/span&gt;&amp;nbsp;is 2.90v, which is higher than 2.5v. If a voltage less than 2.5v is applied on Pin18, then RB5 pin will become High.&lt;br /&gt;
&lt;h3&gt;
&lt;span style=&quot;color: #38761d; font-size: x-large;&quot;&gt;Code&lt;/span&gt;&lt;/h3&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;The code for the main function is shown below.&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEguctMQcCTu_MCEDKetjGgXQQ6q3f9AcThzOHUa5ukmCa4m8_ZRG1_M445EeIZwNF7z3Era6cKlRpSR8vS42YqmdwGn_wFi3tgYHQPrSXctwLEdXv5lwRH2rj6BBKwNZ93Iy8vE0-zhrEE/s1600/PIC16F628A+Comparator+Main+Code.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;492&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEguctMQcCTu_MCEDKetjGgXQQ6q3f9AcThzOHUa5ukmCa4m8_ZRG1_M445EeIZwNF7z3Era6cKlRpSR8vS42YqmdwGn_wFi3tgYHQPrSXctwLEdXv5lwRH2rj6BBKwNZ93Iy8vE0-zhrEE/s1600/PIC16F628A+Comparator+Main+Code.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;i&gt;Figure 3&lt;/i&gt;. &amp;nbsp; Main function for comparator code for PIC16F628A&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;In the main function, firstly RB4 and RB5 pins are made outputs. Then comparator is initialised using the &lt;i&gt;InitComparator()&lt;/i&gt; function. After that, in the &lt;i&gt;while(1)&lt;/i&gt; loop, C1OUT and C2OUT bits of the CMCON register are continuously&amp;nbsp;checked and state of&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;RB4, RB5&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;pins&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;is changed accordingly. C1OUT or C2OUT bit indicates the result of the comparator output. If voltage on Pin17 of PIC16F628A&amp;nbsp;is higher than 2.5v then C1OUT is 0, but if it is less than 2.5v then C1OUT is 1. Similarly,&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;If voltage on Pin18 of PIC16F628A&amp;nbsp;is higher than 2.5v then C2OUT is 0, but if it is less than 2.5v then C2OUT is 1.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;The code for the &lt;i&gt;InitComparator() &lt;/i&gt;function is shown below.&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj0eTxVJMQEReEt7yQjETt1rolAiklVYdP6oOELKKuqnWtWKHXLjZxJqdpf6ggl9BuRpCbzY2ADt_j-5GraaYPggfeQPK42aP3SHedl4xU5hotqvX0JS4V45R3Y1XABTYoI7pcx9mu3D6s/s1600/PIC16F628A+Comparator+Function.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;108&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj0eTxVJMQEReEt7yQjETt1rolAiklVYdP6oOELKKuqnWtWKHXLjZxJqdpf6ggl9BuRpCbzY2ADt_j-5GraaYPggfeQPK42aP3SHedl4xU5hotqvX0JS4V45R3Y1XABTYoI7pcx9mu3D6s/s1600/PIC16F628A+Comparator+Function.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;i&gt;Figure 4&lt;/i&gt;. &amp;nbsp; InitComparator function code for PIC16F628A&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;In the&amp;nbsp;&lt;/span&gt;&lt;i&gt;InitComparator()&amp;nbsp;&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;function, first 2 lines only make RA0 and RA1 pins inputs. Then VRCON register is used to set a reference voltage of 2.5v on the +ve pin of the comparator internally within the microcontroller. After that, CMCON register is used to configure RA0 and RA1 pins as the comparator pins.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
This is a simple example of how you can use comparators of PIC1&lt;span style=&quot;background-color: white;&quot;&gt;6F628A&lt;/span&gt;&amp;nbsp;microcontroller.&amp;nbsp;&lt;span style=&quot;background-color: white;&quot;&gt;You can modify this code to meet your requirements.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;You can leave your comments and suggestions in the comment section below.&lt;/span&gt;&lt;br /&gt;
&lt;div&gt;
&lt;h3&gt;
&lt;span style=&quot;color: red; font-size: x-large;&quot;&gt;Downloads&lt;/span&gt;&lt;/h3&gt;
&lt;/div&gt;
&lt;div&gt;
Comparator code using PIC16F628A &lt;span style=&quot;background-color: white;&quot;&gt;w&lt;/span&gt;as compiled in&amp;nbsp;MPLAB v8.85 with HI-TECH C v9.83 compiler and simulation was made in Proteus v7.10.&amp;nbsp;&lt;span style=&quot;background-color: white;&quot;&gt;To download code and&amp;nbsp;&lt;/span&gt;&lt;i style=&quot;background-color: white;&quot;&gt;Proteus&amp;nbsp;&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;simulation&amp;nbsp;&lt;/span&gt;&lt;a href=&quot;http://bit.ly/2ntdi7Q&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;click here&lt;/a&gt;&lt;span style=&quot;background-color: white;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div&gt;
&lt;table&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;div align=&quot;right&quot; right=&quot;right&quot; text-align:=&quot;text-align:&quot;&gt;
&lt;iframe allowfullscreen=&quot;allowfullscreen&quot; frameborder=&quot;0&quot; height=&quot;180&quot; scrolling=&quot;no&quot; src=&quot;http://wizpert.com/wizapi/widget?beta_key=3ba42&amp;amp;view=227&amp;amp;ep=6570&amp;amp;size=standard&quot; width=&quot;180&quot;&gt; &lt;/iframe&gt;
&lt;/div&gt;
&lt;/td&gt;&lt;td&gt;&lt;div&gt;
&lt;h3&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;span style=&quot;color: #274e13; font-size: large;&quot;&gt;Further Reading Suggestions&lt;/span&gt;&lt;/span&gt;&lt;/h3&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;You may also like,&lt;/span&gt;&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/03/interfacing-of-pic16f628a-with-ds1307.html&quot;&gt;Interfacing of PIC16F628A with DS1307 RTC&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/03/pic16f628a-based-digital-clock-using.html&quot;&gt;PIC16F628A based digital clock using LCD display&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/02/pic16f628a-lcd-interfacing-code-in-8bit.html&quot;&gt;PIC16F628A LCD Interfacing code (In 8bit mode)&amp;nbsp;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2013/09/pic16f628a-pwm-code-proteus-simulation.html&quot;&gt;PIC16F628A PWM code&amp;nbsp;+ Proteus simulation&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://saeedsolutions.blogspot.com/feeds/151181139789038641/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://saeedsolutions.blogspot.com/2014/08/pic16f628a-comparator-code-proteus.html#comment-form' title='11 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5869154326689252934/posts/default/151181139789038641'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5869154326689252934/posts/default/151181139789038641'/><link rel='alternate' type='text/html' href='http://saeedsolutions.blogspot.com/2014/08/pic16f628a-comparator-code-proteus.html' title='PIC16F628A Comparator Code + Proteus Simulation'/><author><name>Saeed</name><uri>http://www.blogger.com/profile/18064969138529094661</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjyceNFPOmTbfhUv5rVdPGTF4gorn6GbWsCY5cbmbpMoCSo9BLi4F6hr2VzFS_KvEQSyMsLmuzeLQ-_Jrmuty31HI_iT2sDJ5EL37bHHIqVLfGLHRI8lkuS1iAJzFzGZfA57La6W5k6dNE/s72-c/PIC16F628A+Comparator+Circuit.png" height="72" width="72"/><thr:total>11</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5869154326689252934.post-2142680413501388196</id><published>2014-06-02T00:19:00.001+05:00</published><updated>2017-04-02T14:50:31.034+05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="PIC micro-controller Projects"/><category scheme="http://www.blogger.com/atom/ns#" term="Water Level Indicator Projects"/><title type='text'>Water Level Indicator using PIC microcontroller </title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
I have designed a new and better water level indicator using PIC microcontroller. It displays water level in the tank as well as in the reservoir. Also, it controls the motor which pumps the water in the tank from the reservoir. In the auto mode, motor is automatically turned on when water level reaches 20% in the tank and it is turned off when water level reaches 100%. You can also change the water level threshold on which motor will turn on by going in to the menu.&lt;br /&gt;
&lt;br /&gt;
The following picture shows the final circuit in working condition.&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg354we3jXzwyFUJJrxNCMdeHLMHaU-LGBpt-22NXXtTZrzABtFLHh8dDVDF9qyPGXz6wzVjCcvzTt2IzAgMqP2Ml-ahImncqp7yR45CZwL4vqMq1olraze4r2EN3CoJP4My-z1OiQDNus/s1600/IMG_2298.JPG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;480&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg354we3jXzwyFUJJrxNCMdeHLMHaU-LGBpt-22NXXtTZrzABtFLHh8dDVDF9qyPGXz6wzVjCcvzTt2IzAgMqP2Ml-ahImncqp7yR45CZwL4vqMq1olraze4r2EN3CoJP4My-z1OiQDNus/s1600/IMG_2298.JPG&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;i&gt;Figure 1&lt;/i&gt;. &amp;nbsp; Final working circuit for water level indicator&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
In the above image, seven segment display shows the current water level in the tank. While bar LED display shows the current water level in the reservoir. Also LCD shows different messages as well at different times. The yellow LED near bar LED display shows that if motor is turned on or not. When yellow LED is on, then motor is on, otherwise motor is off. The green LED in the top right corner shows if the circuit is powered on or not. The multiple wire connector in the top left corner is attaching sensors placed in the water reservoir to the circuit. While the multiple wire connector in the bottom left corner is attaching sensors placed in the water tank to the circuit. The push button on the left side is the &#39;&lt;i&gt;Select&lt;/i&gt;&#39; button, which is used to enter the menu. While push button in the middle is for going &#39;&lt;i&gt;Up&lt;/i&gt;&#39; in the menu and push button on the right is for going &#39;&lt;i&gt;Down&lt;/i&gt;&#39; in the menu. You can press &#39;Select&#39; button again to select that item in the menu.&lt;br /&gt;
&lt;br /&gt;
Here are some videos.&lt;br /&gt;
&lt;iframe allowfullscreen=&quot;&quot; frameborder=&quot;0&quot; height=&quot;281&quot; mozallowfullscreen=&quot;&quot; src=&quot;//player.vimeo.com/video/100795198&quot; webkitallowfullscreen=&quot;&quot; width=&quot;500&quot;&gt;&lt;/iframe&gt;&lt;br /&gt;
&lt;iframe allowfullscreen=&quot;&quot; frameborder=&quot;0&quot; height=&quot;281&quot; mozallowfullscreen=&quot;&quot; src=&quot;//player.vimeo.com/video/100795199&quot; webkitallowfullscreen=&quot;&quot; width=&quot;500&quot;&gt;&lt;/iframe&gt;

&lt;iframe allowfullscreen=&quot;&quot; frameborder=&quot;0&quot; height=&quot;281&quot; mozallowfullscreen=&quot;&quot; src=&quot;//player.vimeo.com/video/100795305&quot; webkitallowfullscreen=&quot;&quot; width=&quot;500&quot;&gt;&lt;/iframe&gt;

You can enter the menu by pressing the &#39;&lt;i&gt;Select&lt;/i&gt;&#39; button. There are many options in the menu. First option is to start/stop motor. If you press &#39;&lt;i&gt;Select&lt;/i&gt;&#39; button on it then circuit will try to start the motor if motor was in off state. But it will succeed only if there is water present in the reservoir. Otherwise it will flash an error that there is no water and motor will turn off. If motor turns on, then when you enter the menu again, then first option will change to stop motor instead of start motor. You can press &#39;&lt;i&gt;Select&lt;/i&gt;&#39; again to stop the motor immediately. All of this should be tested in the manual mode of operation. You can find out if circuit is in &lt;i&gt;Manual&lt;/i&gt; Mode or &lt;i&gt;Auto&lt;/i&gt; mode by&amp;nbsp;looking on the right bottom of LCD screen during normal operation. If there is&amp;nbsp;&lt;i&gt;M&lt;/i&gt;&amp;nbsp;displayed then circuit is in manual mode, if there is &lt;i&gt;A&lt;/i&gt; displayed then circuit is in auto mode.&lt;br /&gt;
&lt;br /&gt;
The next option is to enable or disable &lt;i&gt;Auto&lt;/i&gt; mode. If auto mode is enabled, then circuit will start and stop the motor automatically. If circuit is in manual mode then it will never start motor by itself, it will only turn the motor off when tank level reaches 100%. You will have to manually start the motor in manual mode.&lt;br /&gt;
&lt;br /&gt;
In the auto mode, you can adjust the thresholds on which it will start or stop the motor. There are 2 thresholds. First is named as TANK Threshold, second is named as RESV Threshold in the menu. You can save any value from 0% or 20% up to 100% in these thresholds. TANK threshold determines when the motor will start. For example, if TANK Threshold is 20%, then when water level in TANK reaches 20%, then motor will start automatically in the auto mode. RESV Threshold determines the low limit at which circuit will assume that there is no water remaining in the reservoir and it will stop the motor. RESV Threshold is the minimum water level you want to keep in the reservoir.&lt;br /&gt;
&lt;br /&gt;
After that, there is an option to disable/enable sound. If sound is enabled then its symbol will be displayed on the LCD in the top right corner during normal operation. If sound is enabled then circuit will generate sound beeps whenever it does something or when tank is completely filled and motor is stopped. If you don&#39;t want to be disturbed with the sounds, then you can disable sound from the menu.&lt;br /&gt;
&lt;br /&gt;
Lastly, there is an option of &#39;Exit&#39; in the menu. When you press &#39;Select&#39; button on it then you will return to the normal operation screen of the circuit.&lt;br /&gt;
&lt;br /&gt;
All of these settings are saved in the internal EEPROM of PIC controller. So, the next time you power up this circuit it will remember its Threshold settings or sound settings or all other settings. You don&#39;t have to put those settings again in it.&lt;br /&gt;
&lt;br /&gt;
There are 2 more symbols displayed on the LCD on the left side of sound symbol and Auto/Manual mode symbol. The one on the left side of sound symbol shows the tank. When water is falling in the tank, then it will show little dots falling inside that tank. This will happen only if you have connected water fall input sensor correctly in the tank. You can understand how to connect this circuit with water level sensors in the &#39;&lt;i&gt;How to use this circuit&lt;/i&gt;&#39; section below. Also, there is a bar displayed on the left side of Auto/Manual mode symbol. This shows motor status. If motor is off then this bar will stay there without moving. But if motor is ON, then this bar will start to rotate showing that motor is running.&lt;br /&gt;
&lt;br /&gt;
Here is another picture of the circuit when it is not powered ON.&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjsh_PmKS-sTMXoxZ5vfxIqYFXI3QnBZmhFkcT4idhMGeQ36Fyyl7BDG2qqMC6RM1TQHxdiPqDjGuMsowih_5KgV7sIM9bux2HvCQihdqdRRa2q4ddziGWqzsa1MrF99dsvVGzn3_ekQMo/s1600/IMG_2311.JPG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;480&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjsh_PmKS-sTMXoxZ5vfxIqYFXI3QnBZmhFkcT4idhMGeQ36Fyyl7BDG2qqMC6RM1TQHxdiPqDjGuMsowih_5KgV7sIM9bux2HvCQihdqdRRa2q4ddziGWqzsa1MrF99dsvVGzn3_ekQMo/s1600/IMG_2311.JPG&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;i&gt;Figure 2&lt;/i&gt;. Water level indicator using PIC controller&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
The bottom of the circuit is the main part on which all the SMD components are attached. Here is a picture of the bottom side.&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiWY7yegzmz_k3no2bYi_WHdxXwJYzSPV7v55Fb6x0DD2yqLK8MuLu-4D62wOk9ks62NY_67c5uEc78QTSRPUXmQl0nVU7l_07hSOVb3uj8gi_gURAWHMXvNXtDp-71qgjCs06iTgGu5Io/s1600/IMG_2301.JPG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;480&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiWY7yegzmz_k3no2bYi_WHdxXwJYzSPV7v55Fb6x0DD2yqLK8MuLu-4D62wOk9ks62NY_67c5uEc78QTSRPUXmQl0nVU7l_07hSOVb3uj8gi_gURAWHMXvNXtDp-71qgjCs06iTgGu5Io/s1600/IMG_2301.JPG&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;i&gt;Figure 3&lt;/i&gt;. Bottom side of the circuit&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;h2 style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;color: blue; font-size: x-large;&quot;&gt;&lt;b&gt;Circuit Diagram&lt;/b&gt;&lt;/span&gt;&lt;/h2&gt;
&lt;/div&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;div style=&quot;text-align: left;&quot;&gt;
Here is the circuit diagram.&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiaudXjE55qSuBD6UaO2xp_Jiyf3V0hr7e5Xl1KzXXrIf0OArVqE27InNkXwkuGySZw2_xuO7m0EsEunxT-HHgfmTJ4myl9b2eBBI-1zP6bK_KxXFE5g3Nr9Ght3Z4Yoh3MBwYnGeDiyqY/s1600/Circuit+Diagram.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;526&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiaudXjE55qSuBD6UaO2xp_Jiyf3V0hr7e5Xl1KzXXrIf0OArVqE27InNkXwkuGySZw2_xuO7m0EsEunxT-HHgfmTJ4myl9b2eBBI-1zP6bK_KxXFE5g3Nr9Ght3Z4Yoh3MBwYnGeDiyqY/s1600/Circuit+Diagram.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;i&gt;Figure 4&lt;/i&gt;. &amp;nbsp; Circuit diagram of water level indicator using PIC controller&lt;br /&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;In the above circuit diagram, J9 connector is used to connect with water sensors in the reservoir and J10 connector is used to connect with water sensors in the tank. You can program your PIC controller through &lt;i&gt;PICkit2&lt;/i&gt; header J8.&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;h2 style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;color: red; font-size: x-large;&quot;&gt;How to use this circuit&lt;/span&gt;&lt;/h2&gt;
&lt;/div&gt;
&lt;div&gt;
To use this circuit you have to install the water level sensors in the water tank. Connector J10 is used to connect the circuit with the water level sensors as shown below.&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiFIgpHVh70E-zMphJWqVXNhdCyt2aFftfz-398rQtfpiBUBprMGKRKTXyLirlKWlEBOOFFXvQm_gkUBU6ktVZH_pqORTwIS7usxnBUVjwvtlqyryrOL88Cq3W0QB_lFdZLz9s11C3ngmM/s1600/watertankconnections.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiFIgpHVh70E-zMphJWqVXNhdCyt2aFftfz-398rQtfpiBUBprMGKRKTXyLirlKWlEBOOFFXvQm_gkUBU6ktVZH_pqORTwIS7usxnBUVjwvtlqyryrOL88Cq3W0QB_lFdZLz9s11C3ngmM/s1600/watertankconnections.jpg&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;i&gt;Figure 5&lt;/i&gt;. Connections to water tank&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
Also you have to&amp;nbsp;install the water level sensors in the water reservoir. Connector J9 is used to connect the circuit with the water level sensors as shown below.&lt;/div&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj6Aq0kRPf4xhcmR17T0vQAAEadKPOk9Ao9Adx5u8w2ZhwJDUA6OXE2pkixCL4qQIbd3Qw30rX7CFBZp3_Ze5PQ8qZGChnUp4Q5MWK0hz7U4bgxqNUGhvzm2G1XUuiBnVq_sniApJRuldE/s1600/waterResvconnections.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj6Aq0kRPf4xhcmR17T0vQAAEadKPOk9Ao9Adx5u8w2ZhwJDUA6OXE2pkixCL4qQIbd3Qw30rX7CFBZp3_Ze5PQ8qZGChnUp4Q5MWK0hz7U4bgxqNUGhvzm2G1XUuiBnVq_sniApJRuldE/s1600/waterResvconnections.jpg&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;i&gt;Figure 6&lt;/i&gt;. Connections to the water reservoir&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
&lt;/div&gt;
&lt;h2 style=&quot;clear: both; text-align: left;&quot;&gt;
&lt;span style=&quot;color: blue; font-size: x-large;&quot;&gt;&lt;b&gt;Components&lt;/b&gt;&lt;/span&gt;&lt;/h2&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both;&quot;&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;table cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;float: right; margin-left: 1em; text-align: right;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;http://upload.wikimedia.org/wikipedia/commons/thumb/8/84/Centre-positive.svg/220px-Centre-positive.svg.png&quot; imageanchor=&quot;1&quot; style=&quot;clear: right; margin-bottom: 1em; margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;http://upload.wikimedia.org/wikipedia/commons/thumb/8/84/Centre-positive.svg/220px-Centre-positive.svg.png&quot; height=&quot;42&quot; width=&quot;200&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;12v Adopter Polarity&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: lime;&quot;&gt;&lt;b&gt;Input Voltage:&amp;nbsp;&lt;/b&gt;&lt;/span&gt;12volt power supply is required. Adopter polarity is shown in the picture.&amp;nbsp;&lt;/div&gt;
&lt;div&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: lime;&quot;&gt;&lt;b&gt;Capacitors: &lt;/b&gt;&lt;/span&gt;The capacitors used in this circuit need to have a rating of at least 16v.&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: lime;&quot;&gt;&lt;b&gt;Q1, Q2, Q3 transistor:&lt;/b&gt;&lt;/span&gt;&amp;nbsp;Any npn transistor will work here.&lt;br /&gt;
&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: lime;&quot;&gt;&lt;b&gt;Water Sensors:&amp;nbsp;&lt;/b&gt;&lt;/span&gt;&amp;nbsp;Simple wires can be placed in the water tank. As the current required to pass through the wire is in nano amps. But if you want then you can place carbon rods at the end of wires which can be extracted from the 1.5v AA cell. These carbon rods should be thoroughly washed and then you should attach the wire to the carbon rod and then use some epoxy to hide the wire part.&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;color: lime;&quot;&gt;LM7805:&lt;/span&gt;&lt;/b&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;This is the 5v regulator used to power up the whole circuit.&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;color: lime;&quot;&gt;Seven Segments:&amp;nbsp;&lt;/span&gt;&lt;/b&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; These are used to display water level. Common cathode seven segments are used.&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both;&quot;&gt;
&lt;/div&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: lime;&quot;&gt;&lt;b&gt;LCD:&amp;nbsp;&lt;/b&gt;&lt;/span&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Common 16x2 LCD can be used in this circuit.&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: lime;&quot;&gt;&lt;b&gt;LED Bar:&lt;/b&gt;&lt;/span&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;J7 is the 10 LEDs bar.&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: lime;&quot;&gt;&lt;b&gt;Crystal:&lt;/b&gt;&lt;/span&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 10Mhz crystal needs to be attached with the PIC18F452.&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: lime;&quot;&gt;&lt;b&gt;PIC18F452:&amp;nbsp;&lt;/b&gt;&lt;/span&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; This microcontroller is used to generate all the required logic for this circuit.&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: lime;&quot;&gt;&lt;b&gt;D2, D4 Diodes:&lt;/b&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; Any diode 1N4007 or 1N4148 or any other general purpose diode will work here.&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: lime;&quot;&gt;&lt;b&gt;LS1 Relay:&lt;/b&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; This is a 12v relay. You should attach that relay which can handle power required by your motor. Preferably, you should use this small relay to drive a bigger relay which should be attached near the motor.&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: lime;&quot;&gt;&lt;b&gt;LS2 Buzzer:&lt;/b&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; Any 5v buzzer will work here.&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
&lt;/div&gt;
&lt;h3 style=&quot;clear: both; text-align: left;&quot;&gt;
&lt;span style=&quot;color: orange; font-size: x-large;&quot;&gt;&lt;b&gt;PCB Design&lt;/b&gt;&lt;/span&gt;&lt;/h3&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both;&quot;&gt;
The circuit diagram was made in the &lt;i&gt;Orcad Schematic&lt;/i&gt; software and it is shown in the &lt;i&gt;Figure 4&lt;/i&gt; above. The PCB was designed in the &lt;i&gt;Orcad Layout&lt;/i&gt;. The final PCB design is shown below.&lt;/div&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi7kYuiWrDzwLehAs1015U1GbGZM_GpPiV6JXg67MCzJesWga1XP4s-5hR9McegmcfaKXRd9adLpV63qJqap9WV6p6nuwocomQjE0voRJaNXhp2UN_LdGa5VvOE_HPp70zvuz8YMP9D2gc/s1600/Screen+Shot+2014-07-15+at+4.14.36+pm.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;392&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi7kYuiWrDzwLehAs1015U1GbGZM_GpPiV6JXg67MCzJesWga1XP4s-5hR9McegmcfaKXRd9adLpV63qJqap9WV6p6nuwocomQjE0voRJaNXhp2UN_LdGa5VvOE_HPp70zvuz8YMP9D2gc/s1600/Screen+Shot+2014-07-15+at+4.14.36+pm.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;i&gt;Figure 7.&lt;/i&gt;&amp;nbsp;&amp;nbsp; PCB design for the water level indicator&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both;&quot;&gt;
&lt;/div&gt;
&lt;div&gt;
Here is a picture of the PCB top side.&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhc_DrP52jkQh9jVNVgeZPJfgIG3773A15x3pB4E1F4jU_dnynZPhxXVKYfOwkS_NcQiXpIbP0L_SO7AtDMpHZAX3WIGUyKwsOp2zOqr3DI8b6-VJrPVxvGygj9u3fRqQ4szpT1T3D-2iA/s1600/IMG_2305.JPG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;480&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhc_DrP52jkQh9jVNVgeZPJfgIG3773A15x3pB4E1F4jU_dnynZPhxXVKYfOwkS_NcQiXpIbP0L_SO7AtDMpHZAX3WIGUyKwsOp2zOqr3DI8b6-VJrPVxvGygj9u3fRqQ4szpT1T3D-2iA/s1600/IMG_2305.JPG&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;i&gt;Figure 8.&lt;/i&gt;&amp;nbsp; Water level indicator PCB top side&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
Here is a picture of the bottom side of the PCB.&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgkD0oEnecy_CuOmnsnZWc2d913luwBXuJDGb-j9mixPcQYk9Gq3HgMN8RS0H7ucN-IGsZzk-4eq1W7IxiztfJzGh_FnaraZVzGRatBMpSquF88gEng-NLRDF1G_Gf6DaWwua3JVhf1zDs/s1600/IMG_2306.JPG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;480&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgkD0oEnecy_CuOmnsnZWc2d913luwBXuJDGb-j9mixPcQYk9Gq3HgMN8RS0H7ucN-IGsZzk-4eq1W7IxiztfJzGh_FnaraZVzGRatBMpSquF88gEng-NLRDF1G_Gf6DaWwua3JVhf1zDs/s1600/IMG_2306.JPG&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;i&gt;Figure 9.&lt;/i&gt;&amp;nbsp; Water level indicator PCB bottom side&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;h2 style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;color: #a64d79; font-size: x-large;&quot;&gt;Simulation&lt;/span&gt;&lt;/h2&gt;
You can download the simulation made in &lt;i&gt;Proteus&lt;/i&gt; software from &#39;&lt;i&gt;Downloads&lt;/i&gt;&#39; section below as well. Here is a screen shot of the simulation.&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhrz4gKYbDocI22Ga8c1oEZLzH8IC_imq5zRCOwVnwUPLkPCKbSTzls30d0jmAr-LGKKCau99X1bSF71Yv3fCAm-TggAlcX14RuAzdbk7dOCap34cx_sV_g0TBbCzl-MhUbJpvkf9j1qNY/s1600/WLI+Simulation.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;454&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhrz4gKYbDocI22Ga8c1oEZLzH8IC_imq5zRCOwVnwUPLkPCKbSTzls30d0jmAr-LGKKCau99X1bSF71Yv3fCAm-TggAlcX14RuAzdbk7dOCap34cx_sV_g0TBbCzl-MhUbJpvkf9j1qNY/s1600/WLI+Simulation.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;i&gt;Figure 10.&lt;/i&gt;&amp;nbsp; Water level indicator simulation&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
You can simulate the circuit by moving the DIP switches labeled as TANK and RESV in the above diagram. For example, in the above picture, it shows a water level of 40%, because the DIP switch named TANK has 2 inputs grounded. Similarly, RESV DIP switch has one input grounded that is why it shows a water level of 20%. Also, you can use &lt;i&gt;Select&lt;/i&gt;, &lt;i&gt;Up&lt;/i&gt;, &lt;i&gt;Down&lt;/i&gt; buttons as well to enter the menu etc. You can completely test the behaviour of the circuit in this simulation.&lt;br /&gt;
&lt;br /&gt;
You can leave your comments in the comment section below.&lt;br /&gt;
&lt;h2 style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;color: red; font-size: x-large;&quot;&gt;Buy it Now&lt;/span&gt;&lt;/h2&gt;
If you want this circuit, but don&#39;t want to go through all the trouble to make it. Then you can purchase it by going on this link &lt;a href=&quot;http://www.ebay.com/itm/Water-level-indicator-circuit-with-automatic-motor-control-and-digital-display-/181496856164?pt=LH_DefaultDomain_0&amp;amp;hash=item2a420e3e64&quot;&gt;click here&lt;/a&gt;&lt;/div&gt;
&lt;div&gt;
&lt;h3&gt;
&lt;/h3&gt;
&lt;h3&gt;
&lt;span style=&quot;color: purple; font-size: x-large;&quot;&gt;Downloads&lt;/span&gt;&lt;/h3&gt;
&lt;/div&gt;
&lt;div&gt;
Water level indicator code with PIC18F452&amp;nbsp;&lt;span style=&quot;background-color: white;&quot;&gt;w&lt;/span&gt;as compiled in&amp;nbsp;MPLAB v8.85 with HI-TECH C v9.83 compiler and simulation was made in Proteus v7.10.&amp;nbsp;&lt;span style=&quot;background-color: white;&quot;&gt;To download code and&amp;nbsp;&lt;/span&gt;&lt;i style=&quot;background-color: white;&quot;&gt;Proteus&amp;nbsp;&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;simulation&amp;nbsp;&lt;/span&gt;&lt;a href=&quot;http://bit.ly/2nvPstf&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;click here&lt;/a&gt;&lt;span style=&quot;background-color: white;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div&gt;
&lt;table&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;div align=&quot;right&quot; right=&quot;right&quot; text-align:=&quot;text-align:&quot;&gt;
&lt;iframe allowfullscreen=&quot;allowfullscreen&quot; frameborder=&quot;0&quot; height=&quot;180&quot; scrolling=&quot;no&quot; src=&quot;http://wizpert.com/wizapi/widget?beta_key=3ba42&amp;amp;view=227&amp;amp;ep=6570&amp;amp;size=standard&quot; width=&quot;180&quot;&gt; &lt;/iframe&gt;
&lt;/div&gt;
&lt;/td&gt;&lt;td&gt;&lt;div&gt;
&lt;h3&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;span style=&quot;color: #274e13; font-size: large;&quot;&gt;Further Reading Suggestions&lt;/span&gt;&lt;/span&gt;&lt;/h3&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;You may also like,&lt;/span&gt;&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2010/10/water-level-indicator-motor-control.html&quot;&gt;Water Level Indicator + (Automatic) Motor Control&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2012/04/digital-water-level-indicator-without.html&quot;&gt;Digital Water Level Indicator (without micro-controller)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2012/04/how-to-interface-lcd-with-pic.html&quot;&gt;How to interface LCD with PIC microcontroller?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2012/04/pic-based-digital-clock-display-project.html&quot;&gt;PIC Based Digital Clock Display Project&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://saeedsolutions.blogspot.com/feeds/2142680413501388196/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://saeedsolutions.blogspot.com/2014/06/water-level-indicator-using-pic.html#comment-form' title='63 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5869154326689252934/posts/default/2142680413501388196'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5869154326689252934/posts/default/2142680413501388196'/><link rel='alternate' type='text/html' href='http://saeedsolutions.blogspot.com/2014/06/water-level-indicator-using-pic.html' title='Water Level Indicator using PIC microcontroller '/><author><name>Saeed</name><uri>http://www.blogger.com/profile/18064969138529094661</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg354we3jXzwyFUJJrxNCMdeHLMHaU-LGBpt-22NXXtTZrzABtFLHh8dDVDF9qyPGXz6wzVjCcvzTt2IzAgMqP2Ml-ahImncqp7yR45CZwL4vqMq1olraze4r2EN3CoJP4My-z1OiQDNus/s72-c/IMG_2298.JPG" height="72" width="72"/><thr:total>63</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5869154326689252934.post-3419943106298500673</id><published>2014-05-12T16:17:00.000+05:00</published><updated>2017-04-02T14:49:06.829+05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="PIC micro-controller Projects"/><category scheme="http://www.blogger.com/atom/ns#" term="PIC16F628A Projects"/><title type='text'>PIC16F628A up down counter code + Proteus simulation</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;This PIC16F628A microcontroller tutorial answers the question,&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&quot; How to implement a up down counter using PIC16F628A&amp;nbsp;? &quot;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;br /&gt;Using PIC16 simulator (&lt;i&gt;Proteus&lt;/i&gt;) you can verify this&amp;nbsp;counter&amp;nbsp;code and change it according to your needs. Using one push button (Labeled as &lt;i&gt;Count&lt;/i&gt;, as shown in figure below) you can increment (When &lt;i&gt;SW1&lt;/i&gt; switch is towards up position) or decrement&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;(When &lt;i&gt;SW1&lt;/i&gt; switch is towards down position)&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;count value (displayed on LCD) as you desire. &lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;This code is written in C language using MPLAB with HI-TECH C compiler. You can download this code from the &#39;&lt;/span&gt;&lt;i&gt;Downloads&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&#39;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;section at the bottom of this page.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;In this article, it is assumed that you know, h&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;ow to interface LCD with PIC16F628A&amp;nbsp;microcontroller. If you don&#39;t then please read&amp;nbsp;&lt;/span&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/02/pic16f628a-lcd-interfacing-code-in-4bit.html&quot; target=&quot;_blank&quot;&gt;this page&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
The following diagram (made in&amp;nbsp;&lt;i&gt;Proteus&lt;/i&gt;) shows the PIC microcontroller circuit diagram.&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEij8Mx-Si2yucNGM2q6CwASqPikx58Xl_mtDoPZfxRCW9-ydqgS12l2l-K5gWVuKr0ubIA5fdCKInfUSY6WRWtJOh8nCLCBwrPnVYiTMB3i-Ps5_NWLQkxlRhMYT5a0aZ7khTfNfZ16Htg/s1600/PIC16F628A+UP+DOWN+COUNTER+CCT.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;266&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEij8Mx-Si2yucNGM2q6CwASqPikx58Xl_mtDoPZfxRCW9-ydqgS12l2l-K5gWVuKr0ubIA5fdCKInfUSY6WRWtJOh8nCLCBwrPnVYiTMB3i-Ps5_NWLQkxlRhMYT5a0aZ7khTfNfZ16Htg/s1600/PIC16F628A+UP+DOWN+COUNTER+CCT.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;i&gt;Figure 1&lt;/i&gt;. &amp;nbsp; PIC16F628A based up down counter circuit&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
The above figure was taken after pressing&amp;nbsp;&lt;span style=&quot;background-color: white;&quot;&gt;&lt;i&gt;Count&amp;nbsp;&lt;/i&gt;&lt;/span&gt;button 4 times&lt;span style=&quot;background-color: white;&quot;&gt;. SW1 switch position is set towards &#39;Up&#39; position, hence count value was incremented with each press of &lt;i&gt;Count &lt;/i&gt;button.&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;LCD is updated with the new value of count&lt;/span&gt;&lt;i&gt;&amp;nbsp;&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;after every 200 milli second.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;You can set SW1 switch to &#39;Down&#39; position and then after pressing &lt;i&gt;Count &lt;/i&gt;button, count value will decrement. When count value reaches 100 then it resets to 0 value again.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;A crystal of 20MHz value is used in this circuit, which makes this PIC16F628A run at a speed of 5MIPS (Million of instructions per second). You can use any other value of crystal ( From 0 to 20 MHz value only) as well.&lt;/span&gt;&lt;br /&gt;
&lt;h3&gt;
&lt;span style=&quot;color: #38761d; font-size: x-large;&quot;&gt;Code&lt;/span&gt;&lt;/h3&gt;
The main function code is shown below.&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiyAIFcN1g7umX-VVCcmPnQR1iCuW3hDc32llOfSaK8uvk2M_WqXU9sJ0ZHpbBCT8Tj4bz8NI3eHNKtAPjM0M8FW-flizY8Jp0t0SdXNi_03s9jrFX632i5WS3mXWSBHpBQTSejBCg00RQ/s1600/PIC16F628A+UP+DOWN+COUNTER+MAIN+CODE.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;640&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiyAIFcN1g7umX-VVCcmPnQR1iCuW3hDc32llOfSaK8uvk2M_WqXU9sJ0ZHpbBCT8Tj4bz8NI3eHNKtAPjM0M8FW-flizY8Jp0t0SdXNi_03s9jrFX632i5WS3mXWSBHpBQTSejBCg00RQ/s1600/PIC16F628A+UP+DOWN+COUNTER+MAIN+CODE.png&quot; width=&quot;457&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;i&gt;Figure 2&lt;/i&gt;. &amp;nbsp; Main function code for up down counter on PIC16F628A&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
In the main function, firstly LCD is initialized using&amp;nbsp;&lt;i&gt;InitLCD()&amp;nbsp;&lt;/i&gt;function. Then &lt;i&gt;Count &lt;/i&gt;button pin and &lt;i&gt;Up/Down&lt;/i&gt; button pin is made input. Then LCD screen is cleared and &quot;&lt;i&gt;Starting...&lt;/i&gt;&quot; is displayed on the LCD for half a second.&lt;br /&gt;
&lt;br /&gt;
After that, in the&lt;i&gt;&amp;nbsp;while(1)&lt;/i&gt;&amp;nbsp;loop, depending up the value of &lt;i&gt;Up_Or_Down &lt;/i&gt;button input value, &lt;i&gt;Count &lt;/i&gt;variable is either incremented or decremented. In the end, &lt;i&gt;Count &lt;/i&gt;value is&amp;nbsp;displayed&amp;nbsp;on the LCD after clearing the LCD screen.&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;You can leave your comments in the comment section below.&lt;/span&gt;&lt;br /&gt;
&lt;div&gt;
&lt;h3&gt;
&lt;span style=&quot;color: red; font-size: x-large;&quot;&gt;Downloads&lt;/span&gt;&lt;/h3&gt;
&lt;/div&gt;
&lt;div&gt;
Up down counter code with PIC16F628A &lt;span style=&quot;background-color: white;&quot;&gt;w&lt;/span&gt;as compiled in&amp;nbsp;MPLAB v8.85 with HI-TECH C v9.83 compiler and simulation was made in Proteus v7.10.&amp;nbsp;&lt;span style=&quot;background-color: white;&quot;&gt;To download code and&amp;nbsp;&lt;/span&gt;&lt;i style=&quot;background-color: white;&quot;&gt;Proteus&amp;nbsp;&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;simulation&amp;nbsp;&lt;/span&gt;&lt;a href=&quot;http://bit.ly/2op7Vv0&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;click here&lt;/a&gt;&lt;span style=&quot;background-color: white;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div&gt;
&lt;table&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;div align=&quot;right&quot; right=&quot;right&quot; text-align:=&quot;text-align:&quot;&gt;
&lt;iframe allowfullscreen=&quot;allowfullscreen&quot; frameborder=&quot;0&quot; height=&quot;180&quot; scrolling=&quot;no&quot; src=&quot;http://wizpert.com/wizapi/widget?beta_key=3ba42&amp;amp;view=227&amp;amp;ep=6570&amp;amp;size=standard&quot; width=&quot;180&quot;&gt; &lt;/iframe&gt;
&lt;/div&gt;
&lt;/td&gt;&lt;td&gt;&lt;div&gt;
&lt;h3&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;span style=&quot;color: #274e13; font-size: large;&quot;&gt;Further Reading Suggestions&lt;/span&gt;&lt;/span&gt;&lt;/h3&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;You may also like,&lt;/span&gt;&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/03/interfacing-of-pic16f628a-with-ds1307.html&quot;&gt;Interfacing of PIC16F628A with DS1307 RTC&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/03/pic16f628a-based-digital-clock-using.html&quot;&gt;PIC16F628A based digital clock using LCD display&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/02/pic16f628a-lcd-interfacing-code-in-8bit.html&quot;&gt;PIC16F628A LCD Interfacing code (In 8bit mode)&amp;nbsp;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2013/09/pic16f628a-pwm-code-proteus-simulation.html&quot;&gt;PIC16F628A PWM code&amp;nbsp;+ Proteus simulation&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://saeedsolutions.blogspot.com/feeds/3419943106298500673/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://saeedsolutions.blogspot.com/2014/05/pic16f628a-up-down-counter-code-proteus.html#comment-form' title='11 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5869154326689252934/posts/default/3419943106298500673'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5869154326689252934/posts/default/3419943106298500673'/><link rel='alternate' type='text/html' href='http://saeedsolutions.blogspot.com/2014/05/pic16f628a-up-down-counter-code-proteus.html' title='PIC16F628A up down counter code + Proteus simulation'/><author><name>Saeed</name><uri>http://www.blogger.com/profile/18064969138529094661</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEij8Mx-Si2yucNGM2q6CwASqPikx58Xl_mtDoPZfxRCW9-ydqgS12l2l-K5gWVuKr0ubIA5fdCKInfUSY6WRWtJOh8nCLCBwrPnVYiTMB3i-Ps5_NWLQkxlRhMYT5a0aZ7khTfNfZ16Htg/s72-c/PIC16F628A+UP+DOWN+COUNTER+CCT.png" height="72" width="72"/><thr:total>11</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5869154326689252934.post-5100698962164181295</id><published>2014-04-01T20:18:00.003+05:00</published><updated>2017-04-02T14:48:12.644+05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="PIC micro-controller Projects"/><category scheme="http://www.blogger.com/atom/ns#" term="PIC16F628A Projects"/><title type='text'>PIC16F628A stop watch code + Proteus simulation</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;This PIC16F628A microcontroller tutorial answers the question,&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&quot; How to implement a stop watch using PIC16F628A ? &quot;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;br /&gt;Using PIC16 simulator (&lt;i&gt;Proteus&lt;/i&gt;) you can verify this stop watch code and change it according to your needs. Using three push buttons (As shown in figure below) you can adjust time as you desire. Then after setting the time, value displayed on the LCD starts to decrease with each second and when this value reaches zero, then LED &lt;i&gt;D1 &lt;/i&gt;is turned on. This code is written in C language using MPLAB with HI-TECH C compiler. You can download this code from the &#39;&lt;i&gt;Downloads&lt;/i&gt;&#39;&amp;nbsp;&lt;span style=&quot;font-family: inherit;&quot;&gt;section at the bottom of this page.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;In this article, it is assumed that you know,&lt;/span&gt;&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;How to make a simple digital clock using&amp;nbsp;&lt;span style=&quot;background-color: white;&quot;&gt;PIC16F628A. If you don&#39;t then please read&amp;nbsp;&lt;/span&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/03/pic16f628a-based-digital-clock-using.html&quot; target=&quot;_blank&quot;&gt;this page&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;background-color: white;&quot;&gt;How to interface LCD with PIC16F628A microcontroller. If you don&#39;t then please read&amp;nbsp;&lt;/span&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/02/pic16f628a-lcd-interfacing-code-in-4bit.html&quot; target=&quot;_blank&quot;&gt;this page&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;How to configure timer0 of&amp;nbsp;&lt;span style=&quot;background-color: white;&quot;&gt;PIC16F628A&amp;nbsp;&lt;/span&gt;microcontroller.&amp;nbsp;&amp;nbsp;&lt;span style=&quot;background-color: white;&quot;&gt;If you don&#39;t then please read&amp;nbsp;&lt;/span&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/02/pic16f628a-timer0-code-proteus.html&quot; target=&quot;_blank&quot;&gt;this page&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;
The following diagram (made in&amp;nbsp;&lt;i&gt;Proteus&lt;/i&gt;) shows the PIC microcontroller circuit diagram.&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh8DRLKHBGVF-OiWR4XEcwUvrgOtVqq9144HHyIirL8ODLsENczPczwUpb7ivVCH6M_AcSy_xylzABkuPW9CPL5mmnFZID1mK0rHmzMyHe3Ngn6fqHiYwCZ68ZZvadqknSdaoXuBUYoOdI/s1600/PIC16F628A+STOP+WATCH+CCT.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;346&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh8DRLKHBGVF-OiWR4XEcwUvrgOtVqq9144HHyIirL8ODLsENczPczwUpb7ivVCH6M_AcSy_xylzABkuPW9CPL5mmnFZID1mK0rHmzMyHe3Ngn6fqHiYwCZ68ZZvadqknSdaoXuBUYoOdI/s1600/PIC16F628A+STOP+WATCH+CCT.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;i&gt;Figure 1&lt;/i&gt;. &amp;nbsp; PIC16F628A based stop watch circuit&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
The above figure was taken when time was 00:00:03&lt;span style=&quot;background-color: white;&quot;&gt;, i-e only 3 seconds were remaining until LED &lt;i&gt;D1 &lt;/i&gt;is turned on. Timer0 is used as the base for digital clock generation. Timer0 is used here to generate 1msec interrupts. After every 1msec a global variable named&amp;nbsp;&lt;/span&gt;&lt;i style=&quot;background-color: white;&quot;&gt;msCounter&amp;nbsp;&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;increments. When&amp;nbsp;&lt;/span&gt;&lt;i style=&quot;background-color: white;&quot;&gt;msCounter&amp;nbsp;&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;reaches a value of 1000 then another global variable named&amp;nbsp;&lt;/span&gt;&lt;i style=&quot;background-color: white;&quot;&gt;secCounter&amp;nbsp;&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;decrements and this process repeats itself. Similarly, when&amp;nbsp;&lt;i&gt;secCounter&amp;nbsp;&lt;/i&gt;reaches 0, then &amp;nbsp;&lt;i&gt;minCounter &lt;/i&gt;decrements. And when&amp;nbsp;&lt;i&gt;minCounter&amp;nbsp;&lt;/i&gt;reaches 0 then&amp;nbsp;&lt;i&gt;hrCounter &lt;/i&gt;decrements. This process continues until&amp;nbsp;&lt;i&gt;hrCounter&amp;nbsp;&lt;/i&gt;reaches 0.&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;LCD is updated with the new values of&amp;nbsp;&lt;/span&gt;&lt;i&gt;hrCounter&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;,&amp;nbsp;&lt;/span&gt;&lt;i&gt;minCounter&amp;nbsp;&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;and&amp;nbsp;&lt;/span&gt;&lt;i&gt;secCounter&amp;nbsp;&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;after every second.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;You can set time using three push buttons attached on &lt;i&gt;RA0&lt;/i&gt;, &lt;i&gt;RA1 &lt;/i&gt;and &lt;i&gt;RA2 &lt;/i&gt;pins (As shown in the above figure). By pressing &#39;&lt;i&gt;Start/Stop&lt;/i&gt;&#39; button one time, code enters in configuration state. &lt;i&gt;Hours &lt;/i&gt;value starts to blink and you can modify it using &lt;i&gt;Up &lt;/i&gt;and&amp;nbsp;&lt;i&gt;Down &lt;/i&gt;buttons. Pressing&amp;nbsp;&lt;i&gt;Up&lt;/i&gt; button increments the value and pressing &lt;i&gt;Down &lt;/i&gt;button decrements the value. When you are done setting &lt;i&gt;Hours &lt;/i&gt;value, press&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&#39;&lt;/span&gt;&lt;i&gt;Start/Stop&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&#39; button again, then &lt;i&gt;Minutes &lt;/i&gt;value will start to blink and you can adjust this value using &lt;i&gt;Up &lt;/i&gt;and &lt;i&gt;Down &lt;/i&gt;buttons. Similarly, after setting Minutes value, you can&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;press&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&#39;&lt;/span&gt;&lt;i&gt;Start/Stop&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&#39; button again, then&amp;nbsp;&lt;i&gt;Seconds&amp;nbsp;&lt;/i&gt;value will start to blink and you can adjust this value using&amp;nbsp;&lt;i&gt;Up&amp;nbsp;&lt;/i&gt;and&amp;nbsp;&lt;i&gt;Down&amp;nbsp;&lt;/i&gt;buttons. When you are done adjusting the time, then press&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&#39;&lt;/span&gt;&lt;i&gt;Start/Stop&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&#39; button for the last time and this stop watch will start to work normally.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;A crystal of 4MHz value is used in this circuit, which makes this PIC16F628A run at a speed of 1MIPS (Million of instructions per second).&lt;/span&gt;&lt;br /&gt;
&lt;h3&gt;
&lt;span style=&quot;color: #38761d; font-size: x-large;&quot;&gt;Code&lt;/span&gt;&lt;/h3&gt;
The main function code is shown below.&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhcWWzANavCGHvVnGxlUnWztKAeVChL12Hcalxq3mUi5gFC9IdNH-E0u_IpE3ys8J4CKDl9lA1pGI8T6Qfq3PY8n3SE3T1D2zIeGbnGSR2JleTKyV548NNsM2ZR0-x6F4w1LnqrxLnTu50/s1600/PIC16F628A+STOP+WATCH+MAIN+CODE.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhcWWzANavCGHvVnGxlUnWztKAeVChL12Hcalxq3mUi5gFC9IdNH-E0u_IpE3ys8J4CKDl9lA1pGI8T6Qfq3PY8n3SE3T1D2zIeGbnGSR2JleTKyV548NNsM2ZR0-x6F4w1LnqrxLnTu50/s1600/PIC16F628A+STOP+WATCH+MAIN+CODE.png&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;i&gt;Figure 2&lt;/i&gt;. &amp;nbsp; Main function code for stop watch on PIC16F628A&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
In the main function, firstly RB0 pin (This is the LED pin) is made output and then PORTA pins are made input and Comparator is turned off on these pins. Then LCD is initialized using&amp;nbsp;&lt;i&gt;InitLCD()&amp;nbsp;&lt;/i&gt;function. Then &lt;i&gt;Timer0 &lt;/i&gt;is initialized to generate 1msec interrupts.&lt;br /&gt;
&lt;br /&gt;
After that, in the&lt;i&gt;&amp;nbsp;while(1)&lt;/i&gt;&amp;nbsp;loop, there is a state machine running. A variable named &lt;i&gt;State &lt;/i&gt;is used to keep track of the current state of the stop watch. When this code starts, then State has a value of &lt;i&gt;NORMAL_STATE&lt;/i&gt;. In the normal state, clock value is decremented after every second and&amp;nbsp;whenever&amp;nbsp;&lt;i&gt;msCounter&amp;nbsp;&lt;/i&gt;reaches a value of zero&lt;sup&gt;&lt;span style=&quot;color: purple;&quot;&gt;[1]&lt;/span&gt;&lt;/sup&gt;, then new values of&amp;nbsp;&lt;i&gt;hrCounter&lt;/i&gt;,&amp;nbsp;&lt;i&gt;minCounter&amp;nbsp;&lt;/i&gt;and&amp;nbsp;&lt;i&gt;secCounter&amp;nbsp;&lt;/i&gt;are updated on the LCD.&amp;nbsp;&lt;i&gt;UpdateTimeCounters()&lt;/i&gt;&amp;nbsp;function is called every time and it corrects the values of every counter variable depending upon the value of&amp;nbsp;&lt;i&gt;msCounter&lt;/i&gt;,&amp;nbsp;which&amp;nbsp;is incremented in the ISR function of timer0.&lt;br /&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
In the normal state, code keeps checking &lt;i&gt;RA0 &lt;/i&gt;pin value, if &#39;&lt;i&gt;Start/Stop&lt;/i&gt;&#39; button is pressed then &lt;i&gt;RA0 &lt;/i&gt;pin value becomes zero. This is detected using &lt;i&gt;if(!RA0)&lt;/i&gt; statement in the code. When&amp;nbsp;&#39;&lt;i&gt;Start/Stop&lt;/i&gt;&#39; button is pressed then state is changed to &#39;&lt;i&gt;CONFIG_HR_STATE&lt;/i&gt;&#39; (i-e configure hours value state). In this state, &lt;i&gt;Up &lt;/i&gt;and &lt;i&gt;Down &lt;/i&gt;buttons are constantly monitored and &lt;i&gt;hrCounter &lt;/i&gt;value can be adjusted. Similarly, by pressing &#39;&lt;i&gt;Start/Stop&lt;/i&gt;&#39; button again, state is changed to&amp;nbsp;&#39;&lt;i&gt;CONFIG_MIN_STATE&lt;/i&gt;&#39; (i-e configure minutes value state). In this state &lt;i&gt;minCounter &lt;/i&gt;value can be adjusted using &lt;i&gt;Up &lt;/i&gt;and &lt;i&gt;Down &lt;/i&gt;buttons and&amp;nbsp;by pressing &#39;&lt;i&gt;Start/Stop&lt;/i&gt;&#39; button again, state is changed to&amp;nbsp;&#39;&lt;i&gt;CONFIG_SEC_STATE&lt;/i&gt;&#39; (i-e configure seconds value state). In this state &lt;i&gt;secCounter &lt;/i&gt;value can be adjusted using &lt;i&gt;Up &lt;/i&gt;and &lt;i&gt;Down &lt;/i&gt;buttons.&amp;nbsp;By pressing &#39;&lt;i&gt;Start/Stop&lt;/i&gt;&#39; button again, state is changed to&amp;nbsp;&#39;&lt;i&gt;NORMAL_STATE&lt;/i&gt;&#39; and stop watch starts to run normally.&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;You can leave your comments in the comment section below.&lt;/span&gt;&lt;br /&gt;
&lt;h3&gt;
&lt;span style=&quot;color: blue; font-size: x-large;&quot;&gt;Notes and References&lt;/span&gt;&lt;/h3&gt;
&lt;sup&gt;&lt;span style=&quot;color: purple;&quot;&gt;[1] &amp;nbsp;&lt;/span&gt;&lt;/sup&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&lt;i&gt;msCounter&lt;/i&gt;&amp;nbsp;increments to a value of 1000, then resets to 0 and starts to increment again. So,&amp;nbsp;&lt;i&gt;msCounter&amp;nbsp;&lt;/i&gt;becomes zero again after every 1 second exactly. So LCD updates with new time value after every second.&lt;/span&gt;&lt;br /&gt;
&lt;div&gt;
&lt;h3&gt;
&lt;span style=&quot;color: red; font-size: x-large;&quot;&gt;Downloads&lt;/span&gt;&lt;/h3&gt;
&lt;/div&gt;
&lt;div&gt;
Stop watch code with PIC16F628A &lt;span style=&quot;background-color: white;&quot;&gt;w&lt;/span&gt;as compiled in&amp;nbsp;MPLAB v8.85 with HI-TECH C v9.83 compiler and simulation was made in Proteus v7.10.&amp;nbsp;&lt;span style=&quot;background-color: white;&quot;&gt;To download code and&amp;nbsp;&lt;/span&gt;&lt;i style=&quot;background-color: white;&quot;&gt;Proteus&amp;nbsp;&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;simulation&amp;nbsp;&lt;/span&gt;&lt;a href=&quot;http://bit.ly/2n0EGP7&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;click here&lt;/a&gt;&lt;span style=&quot;background-color: white;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div&gt;
&lt;table&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;div align=&quot;right&quot; right=&quot;right&quot; text-align:=&quot;text-align:&quot;&gt;
&lt;iframe allowfullscreen=&quot;allowfullscreen&quot; frameborder=&quot;0&quot; height=&quot;180&quot; scrolling=&quot;no&quot; src=&quot;http://wizpert.com/wizapi/widget?beta_key=3ba42&amp;amp;view=227&amp;amp;ep=6570&amp;amp;size=standard&quot; width=&quot;180&quot;&gt; &lt;/iframe&gt;
&lt;/div&gt;
&lt;/td&gt;&lt;td&gt;&lt;div&gt;
&lt;h3&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;span style=&quot;color: #274e13; font-size: large;&quot;&gt;Further Reading Suggestions&lt;/span&gt;&lt;/span&gt;&lt;/h3&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;You may also like,&lt;/span&gt;&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/03/interfacing-of-pic16f628a-with-ds1307.html&quot;&gt;Interfacing of PIC16F628A with DS1307 RTC&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/03/pic16f628a-based-digital-clock-using.html&quot;&gt;PIC16F628A based digital clock using LCD display&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/02/pic16f628a-lcd-interfacing-code-in-8bit.html&quot;&gt;PIC16F628A LCD Interfacing code (In 8bit mode)&amp;nbsp;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2013/09/pic16f628a-pwm-code-proteus-simulation.html&quot;&gt;PIC16F628A PWM code&amp;nbsp;+ Proteus simulation&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://saeedsolutions.blogspot.com/feeds/5100698962164181295/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://saeedsolutions.blogspot.com/2014/04/pic16f628a-stop-watch-code-proteus.html#comment-form' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5869154326689252934/posts/default/5100698962164181295'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5869154326689252934/posts/default/5100698962164181295'/><link rel='alternate' type='text/html' href='http://saeedsolutions.blogspot.com/2014/04/pic16f628a-stop-watch-code-proteus.html' title='PIC16F628A stop watch code + Proteus simulation'/><author><name>Saeed</name><uri>http://www.blogger.com/profile/18064969138529094661</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh8DRLKHBGVF-OiWR4XEcwUvrgOtVqq9144HHyIirL8ODLsENczPczwUpb7ivVCH6M_AcSy_xylzABkuPW9CPL5mmnFZID1mK0rHmzMyHe3Ngn6fqHiYwCZ68ZZvadqknSdaoXuBUYoOdI/s72-c/PIC16F628A+STOP+WATCH+CCT.png" height="72" width="72"/><thr:total>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5869154326689252934.post-8513430414003429390</id><published>2014-03-31T15:33:00.002+05:00</published><updated>2017-04-02T14:47:25.108+05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="PIC micro-controller Projects"/><category scheme="http://www.blogger.com/atom/ns#" term="PIC16F628A Projects"/><title type='text'>Interfacing of PIC16F628A with DS1307 (RTC) code + Proteus simulation</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;This post provides the&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;code for interfacing DS1307 RTC with PIC16F628A microcontroller. This&amp;nbsp;DS1307&amp;nbsp;RTC&amp;nbsp;has i2c based interface.&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;This code is written in C language using MPLAB with HI-TECH C compiler. You can download this code from the &#39;&lt;/span&gt;&lt;i style=&quot;background-color: white;&quot;&gt;Downloads&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&#39;&amp;nbsp;&lt;span style=&quot;font-family: inherit;&quot;&gt;section at the bottom of this page.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;It is assumed that you know how to interface LCD with PIC16F628A&amp;nbsp;microcontroller in 4bit mode. If you don&#39;t then please read&amp;nbsp;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/02/pic16f628a-lcd-interfacing-code-in-4bit.html&quot; target=&quot;_blank&quot;&gt;this page&lt;/a&gt;&amp;nbsp;first, before proceeding with this article. It is also assumed that you know how to use i2c module in PIC16F628A, if you don&#39;t then please read&amp;nbsp;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/03/pic16f628a-i2c-bit-banging-code-proteus.html&quot;&gt;this page&lt;/a&gt;&amp;nbsp;first.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;The result of simulating the code in&amp;nbsp;&lt;i&gt;Proteus&amp;nbsp;&lt;/i&gt;is shown below.&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhUz95kqkvCnV4F8WuoQIfXvMwQ4stCYbBXHrE6WzOXSX9lJW1bN3CDQwAc2DMV9WfUkpv8yNQB7PJyMPgUU7Y1OEzXYbspCjiRz8MUw1yRDlUw6sS_deOxOKcrSfvv2Slq7m0m_KpSqPE/s1600/PIC16F628A+WITH+DS1307+CCT.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;404&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhUz95kqkvCnV4F8WuoQIfXvMwQ4stCYbBXHrE6WzOXSX9lJW1bN3CDQwAc2DMV9WfUkpv8yNQB7PJyMPgUU7Y1OEzXYbspCjiRz8MUw1yRDlUw6sS_deOxOKcrSfvv2Slq7m0m_KpSqPE/s1600/PIC16F628A+WITH+DS1307+CCT.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;i&gt;Figure 1&lt;/i&gt;. &amp;nbsp; Interfacing DS1307 RTC with PIC16F628A circuit diagram&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
In the above circuit&lt;sup&gt;&lt;span style=&quot;color: purple;&quot;&gt;[1]&lt;/span&gt;&lt;/sup&gt;, RA4 pin is being used as SDA pin and RB0 pin is the SCK pin. Both of these pins are pulled up using 100K resistors as required for i2c protocol. DS1307 RTC is the slave device, while PIC16F628A is configured to be the master. LCD is also attached with PIC16F628A, just to show the values received from the RTC, otherwise it is not required in this circuit.&amp;nbsp;&lt;i&gt;Proteus&amp;nbsp;&lt;/i&gt;provides an &#39;&lt;i&gt;I2C Debugger Tool&lt;/i&gt;&#39; which is attached on the SDA and SCK pins in the above circuit, this debugger shows all the activity on i2c bus&lt;sup&gt;&lt;span style=&quot;color: purple;&quot;&gt;[2]&lt;/span&gt;&lt;/sup&gt;. It is attached in the circuit just for debugging purposes.&lt;br /&gt;
&lt;br /&gt;
In the code, in the start a command is sent to DS1307 to set time to 12:40:59 AM and date to 31/03/14. After this, DS1307 starts to increment it&#39;s time after every second. Then new time is read from DS1307 RTC&amp;nbsp;after every second&amp;nbsp;and displayed on the LCD. &lt;i&gt;Figure 1&lt;/i&gt; was taken when time was 12:41:08 AM, which&amp;nbsp;is correctly displayed on the LCD.&lt;br /&gt;
&lt;h3&gt;
&lt;span style=&quot;color: #38761d; font-size: x-large;&quot;&gt;Code&lt;/span&gt;&lt;/h3&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;The code for the main function is shown below.&lt;/span&gt;&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjbcPzMPd83fzeNzXIil7yIXk08Uveu5LWTjBD6e_8fGfqp6KmNybuSe0RjOQB8qsiMK0jKe3P3YWHZdQYBCCM9oZeUGFQ6mCZs4IQ9vprLoLDYcQoPyoKjMW-NRNjPaVqhZ-jG7kmgoO0/s1600/PIC16F628A+WITH+DS1307+MAIN+CODE.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjbcPzMPd83fzeNzXIil7yIXk08Uveu5LWTjBD6e_8fGfqp6KmNybuSe0RjOQB8qsiMK0jKe3P3YWHZdQYBCCM9oZeUGFQ6mCZs4IQ9vprLoLDYcQoPyoKjMW-NRNjPaVqhZ-jG7kmgoO0/s1600/PIC16F628A+WITH+DS1307+MAIN+CODE.png&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;i&gt;Figure 2&lt;/i&gt;. Main code for interfacing DS1307 with PIC16F628A&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;/span&gt;&lt;/div&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;span id=&quot;goog_1723851335&quot;&gt;&lt;/span&gt;&lt;span id=&quot;goog_1723851336&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
In the main function, firstly LCD is initialized using&amp;nbsp;&lt;i&gt;InitLCD()&amp;nbsp;&lt;/i&gt;function. Then&amp;nbsp;i2c pins are initialized using&amp;nbsp;&lt;i&gt;InitI2C()&lt;/i&gt;&amp;nbsp;function. Then using &lt;i&gt;Set_DS1307_RTC_Time(AM_Time, 12, 40, 59)&lt;/i&gt; function, a command is sent to DS1307 to set it&#39;s time to 12:40:59 AM. After that,&amp;nbsp;&lt;i&gt;Set_DS1307_RTC_Date(31, 03, 14, Monday)&amp;nbsp;&lt;/i&gt;command sets DS1307 date registers to 31/03/14 @ Monday date.&lt;br /&gt;
&lt;br /&gt;
In the &lt;i&gt;while(1)&lt;/i&gt; loop,&amp;nbsp;&lt;i&gt;Get_DS1307_RTC_Time()&lt;/i&gt;&amp;nbsp;function reads current time from DS1307 and returns a pointer to an array which contains this current time,&amp;nbsp;&lt;i&gt;DisplayTimeToLCD()&lt;/i&gt; function takes this pointer as input and displays current time on the LCD in HH:MM:SS AM/PM format.&lt;br /&gt;
&lt;br /&gt;
Also,&amp;nbsp;&lt;i&gt;Get_DS1307_RTC_Date()&lt;/i&gt;&amp;nbsp;function reads current date from DS1307 and returns a pointer to an array which contains this current date,&amp;nbsp;&lt;i&gt;DisplayDateToLCD()&lt;/i&gt;&amp;nbsp;function takes this pointer as input and displays current date on second line of the LCD in DD/MM/YY format.&lt;br /&gt;
&lt;br /&gt;
Then there is a delay of one second using &lt;i&gt;__delay_ms(1000);&lt;/i&gt; statement. This means that LCD is updated with new time and date after every second.&lt;br /&gt;
&lt;br /&gt;
Using these simple 4 functions,&amp;nbsp;&lt;i&gt;Set_DS1307_RTC_Time(),&lt;/i&gt;&amp;nbsp;&lt;i&gt;Set_DS1307_RTC_Date(),&amp;nbsp;&lt;/i&gt;&lt;i&gt;Get_DS1307_RTC_Time(),&amp;nbsp;&lt;/i&gt;&lt;i&gt;Get_DS1307_RTC_Date() &lt;/i&gt;you can easily interface DS1307 RTC with PIC16F628A microcontroller.&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;You can leave your comments and suggestions in the comment section below.&lt;/span&gt;&lt;br /&gt;
&lt;h3&gt;
&lt;span style=&quot;color: blue; font-size: x-large;&quot;&gt;Notes and References&lt;/span&gt;&lt;/h3&gt;
&lt;sup&gt;&lt;span style=&quot;color: purple;&quot;&gt;[1] &amp;nbsp;&lt;/span&gt;&lt;/sup&gt;&lt;span style=&quot;background-color: white;&quot;&gt;In this circuit, PIC16F628A is running at 5 MIPS speed using 20 MHz crystal.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;sup&gt;&lt;span style=&quot;color: purple;&quot;&gt;[2]&amp;nbsp;&lt;/span&gt;&lt;/sup&gt;&amp;nbsp;&#39;&lt;i&gt;I2C Debugger Tool&lt;/i&gt;&#39; receives all the i2c messages and displays them on the &#39;&lt;i&gt;I2C Debug&lt;/i&gt;&#39; window. This&amp;nbsp;&#39;&lt;i&gt;I2C Debug&lt;/i&gt;&#39; window is shown below in the figure.&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiseGk9s71dtO1kCls9he_v7MQhZuFkZCnBAWgOzqMvZ1k-PBcG7rwlrSzN6Tvr_KprIWEhpjYx1GkWnPt51TEbcdLNBf4bSk373yM80MhQcMIJ_6ps9C8sZtJihIE_Cblw9WUkT3b_U8M/s1600/I2C+DEBUGGER+TOOL+WINDOW.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiseGk9s71dtO1kCls9he_v7MQhZuFkZCnBAWgOzqMvZ1k-PBcG7rwlrSzN6Tvr_KprIWEhpjYx1GkWnPt51TEbcdLNBf4bSk373yM80MhQcMIJ_6ps9C8sZtJihIE_Cblw9WUkT3b_U8M/s1600/I2C+DEBUGGER+TOOL+WINDOW.png&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;i&gt;Figure 3&lt;/i&gt;. &amp;nbsp; I2C Debug window&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;In the above figure,&amp;nbsp;&#39;&lt;/span&gt;&lt;i&gt;I2C Debugger Tool&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&#39;&amp;nbsp;is telling us that there were 4 sequences on the i2c bus, when this screen shot was taken. First sequence &quot;S D0 A 00 A 59 A 40 A 52&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;A&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;P&quot; is explained below.&lt;/span&gt;&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style=&quot;background-color: white;&quot;&gt;S is the starting bit sent from PIC16F628A(master) to DS1307(slave).&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;background-color: white;&quot;&gt;Then 0xD0 is sent to slave, this is the address of DS1307 RTC (As mentioned in it&#39;s datasheet). Also, LSB bit of 0xD0 is zero, which means it is a writing command for the slave. Slave sends an ACK after receiving this byte.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;background-color: white;&quot;&gt;Then 0x00 is sent to slave, this is the starting address in DS1307 RTC on which we want to write first byte. Slave sends an ACK after receiving this address byte.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;background-color: white;&quot;&gt;Then 0x59&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;(it is the value of seconds) is sent to slave, this is the value we want to save in DS1307 RTC on 0x00 address. Slave sends an ACK after receiving this byte.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;background-color: white;&quot;&gt;Then 0x40 (it is the value of minutes) is sent to slave, this is the value we want to save in DS1307 RTC on 0x01 address. Slave sends an ACK after receiving this byte.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;background-color: white;&quot;&gt;Then 0x52 (12 is the value of hours, while 7th bit in it means it is 12 hour time format with AM time) is sent to slave, this is the value we want to save in DS1307 RTC on 0x02 address. Slave sends an ACK after receiving this byte.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;background-color: white;&quot;&gt;Then a stop bit (P) is sent to slave.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;background-color: white;&quot;&gt;After receiving this command, slave DS1307 sets it&#39;s time to 12:40:59 AM time.&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;Only the first sequence is&amp;nbsp;explained&amp;nbsp;above, but you can easily understand remaining sequences using the same approach as described above.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;h3&gt;
&lt;span style=&quot;color: red; font-size: x-large;&quot;&gt;Downloads&lt;/span&gt;&lt;/h3&gt;
&lt;/div&gt;
&lt;div&gt;
DS1307 RTC interfacing with PIC16F628A &lt;span style=&quot;background-color: white;&quot;&gt;w&lt;/span&gt;as compiled in&amp;nbsp;MPLAB v8.85 with HI-TECH C v9.83 compiler and simulation was made in Proteus v7.10.&amp;nbsp;&lt;span style=&quot;background-color: white;&quot;&gt;To download code and&amp;nbsp;&lt;/span&gt;&lt;i style=&quot;background-color: white;&quot;&gt;Proteus&amp;nbsp;&lt;/i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;simulation&amp;nbsp;&lt;/span&gt;&lt;a href=&quot;http://bit.ly/2op2s7U&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;click here&lt;/a&gt;&lt;span style=&quot;background-color: white;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div&gt;
&lt;table&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;div align=&quot;right&quot; right=&quot;right&quot; text-align:=&quot;text-align:&quot;&gt;
&lt;iframe allowfullscreen=&quot;allowfullscreen&quot; frameborder=&quot;0&quot; height=&quot;180&quot; scrolling=&quot;no&quot; src=&quot;http://wizpert.com/wizapi/widget?beta_key=3ba42&amp;amp;view=227&amp;amp;ep=6570&amp;amp;size=standard&quot; width=&quot;180&quot;&gt; &lt;/iframe&gt;
&lt;/div&gt;
&lt;/td&gt;&lt;td&gt;&lt;div&gt;
&lt;h3&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;span style=&quot;color: #274e13; font-size: large;&quot;&gt;Further Reading Suggestions&lt;/span&gt;&lt;/span&gt;&lt;/h3&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;You may also like,&lt;/span&gt;&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/03/pic16f628a-i2c-bit-banging-code-proteus.html&quot;&gt;PIC16F628A i2c (bit banging) code&amp;nbsp;+ Proteus simulation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/03/pic16f628a-based-digital-clock-using.html&quot;&gt;PIC16F628A based digital clock using LCD display&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2014/02/pic16f628a-lcd-interfacing-code-in-8bit.html&quot;&gt;PIC16F628A LCD Interfacing code (In 8bit mode)&amp;nbsp;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://saeedsolutions.blogspot.com/2013/09/pic16f628a-pwm-code-proteus-simulation.html&quot;&gt;PIC16F628A PWM code&amp;nbsp;+ Proteus simulation&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://saeedsolutions.blogspot.com/feeds/8513430414003429390/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://saeedsolutions.blogspot.com/2014/03/interfacing-of-pic16f628a-with-ds1307.html#comment-form' title='10 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5869154326689252934/posts/default/8513430414003429390'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5869154326689252934/posts/default/8513430414003429390'/><link rel='alternate' type='text/html' href='http://saeedsolutions.blogspot.com/2014/03/interfacing-of-pic16f628a-with-ds1307.html' title='Interfacing of PIC16F628A with DS1307 (RTC) code + Proteus simulation'/><author><name>Saeed</name><uri>http://www.blogger.com/profile/18064969138529094661</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhUz95kqkvCnV4F8WuoQIfXvMwQ4stCYbBXHrE6WzOXSX9lJW1bN3CDQwAc2DMV9WfUkpv8yNQB7PJyMPgUU7Y1OEzXYbspCjiRz8MUw1yRDlUw6sS_deOxOKcrSfvv2Slq7m0m_KpSqPE/s72-c/PIC16F628A+WITH+DS1307+CCT.png" height="72" width="72"/><thr:total>10</thr:total></entry></feed>