{"id":14320,"date":"2018-08-06T04:05:24","date_gmt":"2018-08-06T08:05:24","guid":{"rendered":"http:\/\/embedded-lab.com\/blog\/?p=14320"},"modified":"2018-08-06T12:11:41","modified_gmt":"2018-08-06T16:11:41","slug":"getting-started-nuvoton-8-bit-microcontrollers-coding-part-2","status":"publish","type":"post","link":"https:\/\/embedded-lab.com\/blog\/getting-started-nuvoton-8-bit-microcontrollers-coding-part-2\/","title":{"rendered":"Getting Started with Nuvoton 8-bit Microcontrollers &#8211; Coding Part 2"},"content":{"rendered":"<p>This post is a continuation of the previous post on Nuvoton N76E003 microcontroller\u00a0<a href=\"http:\/\/embedded-lab.com\/blog\/getting-started-nuvoton-8-bit-microcontrollers-coding-part-1\/\">here<\/a>.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/05\/SDK1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-14257\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/05\/SDK1-580x217.png\" alt=\"SDK\" width=\"580\" height=\"217\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/05\/SDK1-580x217.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/05\/SDK1.png 699w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p style=\"text-align: justify\"><!--more--><\/p>\n<h1 style=\"margin-top: 0in\"><span lang=\"EN-GB\">Overview of N76E003 Timers<\/span><\/h1>\n<p style=\"text-align: justify\">Since N76E003 is based on 8051 architecture, many of its hardware features will have similarities with a standard 8051 microcontroller. The timers of N76E003 have such similarities but keep in mind that similarities don\u2019t mean same. Strictly speaking in N76E003, there six timers and these are:<\/p>\n<ul>\n<li>\u00a0<strong><em>Timer 0 and Timer 1<\/em><\/strong><br \/>\nThese are both 16-bit general purpose timers with four modes of operation. With these modes we can allow the timers to operate as general purpose 8-bit auto-reloading timers, 13-bit and 16-bit timers. Most commonly the 16-bit timer mode (Mode 1) is used as it is the simplest mode available. Additionally, we can take outputs of these timers. Unlike typical 8051s, we can use chose from <strong><em>Fsys<\/em><\/strong>, fixed and prescaled <strong><em>Fsys <\/em><\/strong><em>(Fsys \/ 12)<\/em> or external input as timer clock sources.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<ul>\n<li><em><strong>Timer 2<\/strong><\/em><br \/>\nTimer 2 is different from Timers 0 and 1 in many aspects. Mainly it is used for waveform captures. It can also be used in compare mode to generate timed events or compare-match-based PWM. It has auto-reloading feature unlike Timers 0 and 1.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<ul>\n<li><strong><em>Timer 3<\/em><\/strong><br \/>\nTimer 3 is an auto-reloading 16-bit timer with no output and <strong><em>Fsys<\/em><\/strong> as clock source. Like Timer 1, Timer 3 can be used for serial communication hardware (UART).<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<ul>\n<li><strong><em>Self-Wake Up Timer (WKT)<\/em><\/strong><br \/>\nThe self-wake-up timer or simply wake-up timer is a special event timer that is not available in standard 8051s. The purpose of this timer is to wake-up or restore the normal working of a N76E003 chip from low power modes after a certain period of time. It can also be used to trigger timed events or trigger other hardware.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<ul>\n<li><strong><em>Watchdog Timer<\/em><\/strong><br \/>\nThe watchdog timer of N76E003 can be used either as a 6-bit general purpose timer or as a standard watchdog timer. Most commonly it is not used as a general-purpose timer because that is not what it is meant to be.\u00a0 In watchdog mode, it helps in recovering a stuck N76E003 micro by resetting it.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>While using BSPs be careful about timer-based delay routines and UART routines. These built-in routines reset and reconfigure timers according to their purposes. Make sure that there is no conflicting issue when using them. To avoid such conflict, either use software delay routines or one dedicate timer for a particular job. For instance, use Timer 1 for delays and Timer 3 for UART. You have to know what you are doing and with which hardware.<\/p>\n<p><!--more--><\/p>\n<h1>Timer 0 \u2013 Time base Generation<\/h1>\n<p style=\"text-align: justify\">Time-base generation is one of the most basic function of a timer. By using time-bases, we can avoid software delays and unwanted loops when we need to wait or check something after a fixed amount of time. BSP-based delay functions are based on this principle.<\/p>\n<p style=\"text-align: justify\">Here in this section, we will see how to create a time-base to toggle P15 LED.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/TIMER-Mode-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-14321\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/TIMER-Mode-1-580x205.png\" alt=\"TIMER Mode 1\" width=\"580\" height=\"205\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/TIMER-Mode-1-580x205.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/TIMER-Mode-1.png 619w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<h2 style=\"margin-top: 0in\"><span lang=\"EN-GB\">Code<\/span><\/h2>\n<pre><span style=\"color: #0000ff\">#include \"N76E003.h\"<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">#include \"SFR_Macro.h\"<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">#include \"Function_define.h\"<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">#include \"Common.h\"<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">#include \"Delay.h\"<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">#define HIRC\u00a0\u00a0\u00a0\u00a00<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">#define LIRC\u00a0\u00a0\u00a0 1<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">#define ECLK\u00a0\u00a0\u00a0\u00a02<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">void setup(void);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">void set_clock_source(unsigned char clock_source);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">void disable_clock_source(unsigned char clock_source);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">void set_clock_division_factor(unsigned char value);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">void set_Timer_0(unsigned int value);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">unsigned int get_Timer_0(void);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">void main(void)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 setup();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 while(1)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 {\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 if(get_Timer_0() &lt; 32767)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 P15 = 1;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 }<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 else<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 P15 = 0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 }<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 };<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">void setup(void)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 disable_clock_source(ECLK);\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 set_clock_source(HIRC);\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 set_clock_division_factor(80);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 P15_PushPull_Mode;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 set_T0M;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 TIMER0_MODE1_ENABLE;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 set_Timer_0(0);\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 set_TR0;\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">void set_clock_source(unsigned char clock_source)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">switch(clock_source)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 case LIRC:<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 set_OSC1;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 clr_OSC0;\u00a0<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 break;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 }<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 case ECLK:<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 set_EXTEN1;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 set_EXTEN0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 while((CKSWT &amp; SET_BIT3) == 0);\u00a0<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 clr_OSC1;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 set_OSC0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 break;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 }<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 default:<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 set_HIRCEN;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 while((CKSWT &amp; SET_BIT5) == 0);\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 clr_OSC1;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 clr_OSC0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 break;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 }<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 }<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 while((CKEN &amp; SET_BIT0) == 1);\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">void disable_clock_source(unsigned char clock_source)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 switch(clock_source)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 case HIRC:<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0clr_HIRCEN;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0break;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 }<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 default:<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0clr_EXTEN1;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0clr_EXTEN0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0break;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 }<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 }<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">void set_clock_division_factor(unsigned char value)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 CKDIV = value;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">void set_Timer_0(unsigned int value)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 TH0 = ((value &amp;&amp; 0xFF00) &gt;&gt; 8);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 TL0 = (value &amp; 0x00FF);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">unsigned int get_Timer_0(void)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 unsigned int value = 0x0000;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 value = TH0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 value &lt;&lt;= 8;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 value |= TL0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 return value;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<p>&nbsp;<\/p>\n<h2><span lang=\"EN-GB\">Schematic<\/span><\/h2>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/Timer0_Schematic.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-14322\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/Timer0_Schematic-580x339.png\" alt=\"Timer0_Schematic\" width=\"580\" height=\"339\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/Timer0_Schematic-580x339.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/Timer0_Schematic.png 864w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<h2><span lang=\"EN-GB\">Explanation<\/span><\/h2>\n<p style=\"text-align: justify\">For demoing time-base generation, we don\u2019t need any additional hardware since we will just be blinking P15 LED without software-based delays.<\/p>\n<p style=\"text-align: justify\">Before I start explaining the code example, I would like to discuss how Timer 0 and 1 works in Mode 1. In Mode 1, these timers behave like up-counting 16-bit timers. This means they can count (contents of TL and TH registers) from 0 to 65535 with every pulse\/clock tick. There is no down-counting feature in these timers.<\/p>\n<p style=\"text-align: justify\">The source of these pulse\/clock ticks can be from system clock, prescaled system clock or from external inputs. When external inputs are used, these timers can be used like counters. Unlike conventional 8051, there are options to take output from timers. The outputs change state with overflows\/rollovers, i.e. when the timer counts resets from 65535 to 0.<\/p>\n<p style=\"text-align: justify\">In the setup function, we do a couple of things. First, we setup the system clock source to 100kHz with HIRC as clock source. P15 is set as an output. Timer 0 is used and so there a 0 in timer setups. Timer 0 is clocked with system clock, i.e. 100kHz and so one tick is:<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/Timer-Tick-Formula.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-14323\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/Timer-Tick-Formula.png\" alt=\"Timer Tick Formula\" width=\"374\" height=\"78\" \/><\/a><\/p>\n<p style=\"text-align: justify\">In Mode 1, the timer will reset\/overflow after:<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/Time-Formula.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-14324\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/Time-Formula.png\" alt=\"Time Formula\" width=\"232\" height=\"47\" \/><\/a><\/p>\n<p style=\"text-align: justify\">provided that the initial count of the timer was set to 0. This is what we are doing in the set up. After setting the timer\u2019s count and the mode of operation, the timer is started.<\/p>\n<pre><span style=\"color: #0000ff\">disable_clock_source(ECLK);\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">set_clock_source(HIRC);\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">set_clock_division_factor(80);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">P15_PushPull_Mode;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">set_T0M;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">TIMER0_MODE1_ENABLE;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">set_Timer_0(0);\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">set_TR0;<\/span><\/pre>\n<p style=\"text-align: justify\">Functions <strong><em>set_Timer_0<\/em><\/strong> and <strong><em>get_Timer_0<\/em><\/strong> writes Timer 0\u2019s count and reads Timer 0\u2019s count respectively.<\/p>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">void set_Timer_0(unsigned int value);<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">unsigned int get_Timer_0(void);<\/span><\/pre>\n<p style=\"text-align: justify\">It is needed to toggle the state of the P15 LED and yeah, that is done at every half count of the timer, i.e. at every 327ms. In the main, the timer\u2019s count is checked using <strong><em>get_Timer_0<\/em><\/strong> function. When the timer\u2019s count is less than 32767 counts, P15 LED is held high. P15 LED is set low when the timer\u2019s count is greater than this 32767. In this way the toggling effect is achieved without software delays. However, the timer\u2019s count is frequently polled.<\/p>\n<pre><span style=\"color: #0000ff\">if(get_Timer_0() &lt; 32767)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 P15 = 1;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">else<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 P15 = 0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<p>&nbsp;<\/p>\n<h2 style=\"margin-top: 0in\"><span lang=\"EN-GB\">Demo<\/span><\/h2>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/Time-base-logic-analyzer.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-14325\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/Time-base-logic-analyzer-580x172.png\" alt=\"Time base logic analyzer\" width=\"580\" height=\"172\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/Time-base-logic-analyzer-580x172.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/Time-base-logic-analyzer.png 781w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/Timer-0-1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-14326\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/Timer-0-1-580x435.jpg\" alt=\"Timer 0 (1)\" width=\"580\" height=\"435\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/Timer-0-1-580x435.jpg 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/Timer-0-1.jpg 590w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a> <a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/Timer-0-2.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-14327\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/Timer-0-2-580x435.jpg\" alt=\"Timer 0 (2)\" width=\"580\" height=\"435\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/Timer-0-2-580x435.jpg 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/Timer-0-2.jpg 590w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p><iframe loading=\"lazy\" width=\"860\" height=\"484\" src=\"https:\/\/www.youtube.com\/embed\/IJvFhdL24B8?feature=oembed\" frameborder=\"0\" allow=\"autoplay; encrypted-media\" allowfullscreen><\/iframe><\/p>\n<p><!--more--><\/p>\n<h1 style=\"margin-top: 0in\"><span lang=\"EN-GB\">Timer 1 \u2013 Stopwatch <\/span><\/h1>\n<p style=\"text-align: justify\">We have already seen the operation of general-purpose timers in Mode 1. In this segment, we will how to use these timers in Mode 0. Everything is same between Mode 0 and 1. However, the timer counts or resolutions vary in these modes. Mode 0 makes general purpose timers behave as 13-bit timer\/counters.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/TIMER-Mode-0.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-14328\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/TIMER-Mode-0-580x207.png\" alt=\"TIMER Mode 0\" width=\"580\" height=\"207\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/TIMER-Mode-0-580x207.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/TIMER-Mode-0-1024x365.png 1024w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/TIMER-Mode-0.png 1044w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p style=\"text-align: justify\">Check the block diagram shown above and compare with the one previously shown. Note the greyed-out part in this diagram. The upper bits of TL registers are not used in this mode. Between Mode 0 and 1, personally Mode 1 is easier than Mode 0.<\/p>\n<p style=\"text-align: justify\">Here Timer 1 in Mode 0 is used to create a digital stopwatch.<\/p>\n<h2 style=\"margin-top: 0in\"><span lang=\"EN-GB\">Code<\/span><\/h2>\n<pre><span style=\"color: #0000ff\">#include \"N76E003_iar.h\"<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">#include \"SFR_Macro.h\"<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">#include \"Function_define.h\"<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">#include \"Common.h\"<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">#include \"Delay.h\"<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">#include \"LCD_3_Wire.h\"<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">#define HIRC\u00a0\u00a0\u00a0\u00a00<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">#define LIRC\u00a0\u00a0\u00a0 1<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">#define ECLK\u00a0\u00a0\u00a0\u00a02<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">unsigned char toggle = 0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">unsigned int ms = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">unsigned char sec = 0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">unsigned char min = 0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">unsigned char hrs = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">void setup(void);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">void set_clock_source(unsigned char clock_source);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">void disable_clock_source(unsigned char clock_source);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">void set_clock_division_factor(unsigned char value);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">void set_Timer_1_for_Mode_0(unsigned int value);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">unsigned int get_Timer_1_for_Mode_0(void);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">void print_C(unsigned char x_pos, unsigned char y_pos, unsigned char value);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">void print_I(unsigned char x_pos, unsigned char y_pos, unsigned int value);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">#pragma vector = 0x1B<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">__interrupt void Timer1_ISR (void)\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 set_Timer_1_for_Mode_0(0x1D64);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 ms++;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 if(ms == 499)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 \u00a0\u00a0\u00a0toggle = ~toggle;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 }<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 if(ms &gt; 999)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 ms = 0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 sec++;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 if(sec &gt; 59)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 sec = 0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 min++;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 if(min &gt; 59)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 min = 0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 hrs++;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 if(hrs &gt; 23)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 hrs = 0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 }<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">void main(void)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 static char txt[] = {\"Nu Stopwatch\"};<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 setup();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 LCD_goto(2, 0);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 LCD_putstr(txt);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 while(1)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 {\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 if(P05 == 1)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 set_ET1;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 set_EA;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 set_TR1;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 set_Timer_1_for_Mode_0(0x1D64);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 }<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 if(P06 == 1)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 clr_ET1;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 clr_EA;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 clr_TR1;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 toggle = 0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 }<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 if((P05 == 1) &amp;&amp; (P06 == 1))<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 clr_ET1;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 clr_EA;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 clr_TR1;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 ms = 0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 sec = 0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 min = 0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 hrs = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 \u00a0\u00a0\u00a0\u00a0toggle = 0;\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 set_Timer_1_for_Mode_0(0x1D64);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 }<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 print_C(2, 1, hrs);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 print_C(5, 1, min);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 print_C(8, 1, sec);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 print_I(11, 2, ms);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 if(!toggle)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 LCD_goto(4, 1);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 LCD_putchar(':');<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 LCD_goto(7, 1);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 LCD_putchar(':');<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 LCD_goto(10, 1);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 LCD_putchar(':');<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 }<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 else<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 LCD_goto(4, 1);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 LCD_putchar(' ');<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 LCD_goto(7, 1);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 LCD_putchar(' ');<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 LCD_goto(10, 1);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 LCD_putchar(' ');<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 }<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 };<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">void setup(void)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 disable_clock_source(ECLK);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 set_clock_source(HIRC);\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 set_clock_division_factor(1);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 P05_Input_Mode;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 P06_Input_Mode;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 clr_T1M;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 TIMER1_MODE0_ENABLE;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 set_Timer_1_for_Mode_0(0x1D64);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 LCD_init();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 LCD_clear_home();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">void set_clock_source(unsigned char clock_source)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">switch(clock_source)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 case LIRC:<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 set_OSC1;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 clr_OSC0;\u00a0<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 break;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 }<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 case ECLK:<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 set_EXTEN1;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 set_EXTEN0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 while((CKSWT &amp; SET_BIT3) == 0);\u00a0<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 clr_OSC1;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 set_OSC0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 break;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 }<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 default:<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 set_HIRCEN;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 while((CKSWT &amp; SET_BIT5) == 0);\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 clr_OSC1;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 clr_OSC0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 break;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 }<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 }<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 while((CKEN &amp; SET_BIT0) == 1);\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">void disable_clock_source(unsigned char clock_source)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 switch(clock_source)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 case HIRC:<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 clr_HIRCEN;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 \u00a0break;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 }<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 default:<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 clr_EXTEN1;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 clr_EXTEN0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 break;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 }<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 }<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">void set_clock_division_factor(unsigned char value)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 CKDIV = value;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">void set_Timer_1_for_Mode_0(unsigned int value)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 TL1 = (value &amp; 0x1F);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 TH1 = ((value &amp; 0xFFE0) &gt;&gt; 5);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">unsigned int get_Timer_1_for_Mode_0(void)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 unsigned char hb = 0x00;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 unsigned char lb = 0x00;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 unsigned int value = 0x0000;<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 value = TH1;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 value &lt;&lt;= 8;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 value |= TL1;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 lb = (value &amp; 0x001F);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 hb = ((value &amp; 0xFFE0) &gt;&gt; 5);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 value = hb;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 value &lt;&lt;= 8;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 value |= lb;\u00a0<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 return value;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">void print_C(unsigned char x_pos, unsigned char y_pos, unsigned char value)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 LCD_goto(x_pos, y_pos);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 LCD_putchar((value \/ 10) + 0x30);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 LCD_goto((x_pos + 1), y_pos);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0LCD_putchar((value % 10) + 0x30);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">void print_I(unsigned char x_pos, unsigned char y_pos, unsigned int value)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 LCD_goto(x_pos, y_pos);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 LCD_putchar((value \/ 100) + 0x30);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 LCD_goto((x_pos + 1), y_pos);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 LCD_putchar(((value % 100) \/ 10) + 0x30);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 LCD_goto((x_pos + 2), y_pos);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 LCD_putchar((value % 10) + 0x30);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<p>&nbsp;<\/p>\n<h2><span lang=\"EN-GB\">Schematic<\/span><\/h2>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/Timer-1-Stopwatch-Schematic.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-14329\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/Timer-1-Stopwatch-Schematic-580x257.png\" alt=\"Timer 1 Stopwatch Schematic\" width=\"580\" height=\"257\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/Timer-1-Stopwatch-Schematic-580x257.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/Timer-1-Stopwatch-Schematic.png 843w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<h2><span lang=\"EN-GB\">Explanation<\/span><\/h2>\n<p style=\"text-align: justify\">Firstly, let us inspect the setup code:<\/p>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">disable_clock_source(ECLK);<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">set_clock_source(HIRC);\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">set_clock_division_factor(1);<\/span><\/pre>\n<pre style=\"text-align: justify\"><\/pre>\n<p>&nbsp;<\/p>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">P05_Input_Mode;<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">P06_Input_Mode;<\/span><\/pre>\n<pre style=\"text-align: justify\"><\/pre>\n<p>&nbsp;<\/p>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">clr_T1M;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">TIMER1_MODE0_ENABLE;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">set_Timer_1_for_Mode_0(0x1D64);<\/span><\/pre>\n<p style=\"text-align: justify\">The system clock is set to 8MHz using HIRC. As per schematic and code, two buttons labelled <strong><em>Start<\/em><\/strong> and <strong><em>Stop <\/em><\/strong>are connected with P05 and P06 respectively. Timer 1 is set up in Mode 1 with prescaled system clock source (Fsys \/ 12). Thus, it has an input clock frequency of 666.67kHz. Thus, one timer tick is 1.5\u03bcs. To get the timer overflow and reset every 1 millisecond we will need:<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/tim1-formula-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-14330\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/tim1-formula-1.png\" alt=\"tim1 formula 1\" width=\"206\" height=\"63\" \/><\/a><\/p>\n<p style=\"text-align: justify\">Since the timer is an up-counting timer and has a resolution of 13-bits, it will overflow after 8191 counts. Thus, to get 667 counts, we need to set is at:<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/tim1-formula-2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-14331\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/tim1-formula-2.png\" alt=\"tim1 formula 2\" width=\"389\" height=\"39\" \/><\/a><\/p>\n<p style=\"text-align: justify\">The timer is, therefore, set at this count value.<\/p>\n<p style=\"text-align: justify\">Now how do we set the timer in Mode 1?<\/p>\n<p style=\"text-align: justify\">Check the register diagram below. The grey part is what is ignored. Remember that TL and TH are 16-bits as a whole but out of that we will just be using 13-bits.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/Timer-Mode-0-Count.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-14333\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/Timer-Mode-0-Count.png\" alt=\"Timer Mode 0 Count\" width=\"418\" height=\"76\" \/><\/a><\/p>\n<p>In our case, the count 0x1D64 is segmented as the following in binary:<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/tim1-formula-3.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-14332\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/tim1-formula-3.png\" alt=\"tim1 formula 3\" width=\"282\" height=\"44\" \/><\/a><\/p>\n<p style=\"text-align: justify\">This number is masked as follows:<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/mode-1-explain.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-14334\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/mode-1-explain.png\" alt=\"mode 1 explain\" width=\"274\" height=\"97\" \/><\/a><\/p>\n<p style=\"text-align: justify\">All of these is done by the following function:<\/p>\n<pre><span style=\"color: #0000ff\">void set_Timer_1_for_Mode_0(unsigned int value)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 TL1 = (value &amp; 0x1F);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 TH1 = ((value &amp; 0xFFE0) &gt;&gt; 5);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<p style=\"text-align: justify\">Reading the time is doing just the opposite of writing and this is accomplished by the following function:<\/p>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">unsigned int get_Timer_1_for_Mode_0(void)<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">\u00a0 unsigned char hb = 0x00;<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">\u00a0 unsigned char lb = 0x00;<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">\u00a0 unsigned int value = 0x0000;<\/span><\/pre>\n<pre style=\"text-align: justify\"><\/pre>\n<p>&nbsp;<\/p>\n<pre style=\"text-align: justify\"><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">\u00a0 value = TH1;<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">\u00a0 value &lt;&lt;= 8;<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">\u00a0 value |= TL1;<\/span><\/pre>\n<pre style=\"text-align: justify\"><\/pre>\n<p>&nbsp;<\/p>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">\u00a0 lb = (value &amp; 0x001F);<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">\u00a0 hb = ((value &amp; 0xFFE0) &gt;&gt; 5);<\/span><\/pre>\n<pre style=\"text-align: justify\"><\/pre>\n<p>&nbsp;<\/p>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">\u00a0 value = hb;<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">\u00a0 value &lt;&lt;= 8;<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">\u00a0 value |= lb;\u00a0<\/span><\/pre>\n<pre style=\"text-align: justify\"><\/pre>\n<p>&nbsp;<\/p>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">\u00a0 return value;<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">}<\/span><\/pre>\n<p style=\"text-align: justify\">Unlike the previous example, here interrupt is used to keep time. The timer run bit, global interrupt and Timer 1 interrupt are all enabled when the start button is pressed and are all disabled when the stop button is pressed or the stopwatch is reset by pressing both buttons.<\/p>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">if(P05 == 1)<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">\u00a0\u00a0 set_ET1;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">\u00a0\u00a0 set_EA;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">\u00a0\u00a0 set_TR1;\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">\u00a0\u00a0 set_Timer_1_for_Mode_0(0x1D64);<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre style=\"text-align: justify\"><\/pre>\n<p>&nbsp;<\/p>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">if(P06 == 1)<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">\u00a0\u00a0 clr_ET1;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">\u00a0\u00a0 clr_EA;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">\u00a0\u00a0 clr_TR1;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">\u00a0\u00a0 toggle = 0;<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre style=\"text-align: justify\"><\/pre>\n<p>&nbsp;<\/p>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">if((P05 == 1) &amp;&amp; (P06 == 1))<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">\u00a0\u00a0 clr_ET1;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">\u00a0\u00a0 clr_EA;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">\u00a0\u00a0 clr_TR1;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">\u00a0\u00a0 ms = 0;<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">\u00a0\u00a0 sec = 0;<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">\u00a0\u00a0 min = 0;<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">\u00a0\u00a0 hrs = 0;<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">\u00a0\u00a0 toggle = 0;\u00a0<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">\u00a0\u00a0 set_Timer_1_for_Mode_0(0x1D64);<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">}<\/span><\/pre>\n<p style=\"text-align: justify\">Inside the interrupt subroutine, the time keeping is done:<\/p>\n<pre><span style=\"color: #0000ff\">#pragma vector = 0x1B<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">__interrupt void Timer1_ISR (void)\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 set_Timer_1_for_Mode_0(0x1D64);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 ms++;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 if(ms == 499)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 toggle = ~toggle;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 }<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 if(ms &gt; 999)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 ms = 0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 sec++;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 if(sec &gt; 59)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 sec = 0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 min++;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 if(min &gt; 59)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 min = 0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 hrs++;<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 if(hrs &gt; 23)<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 hrs = 0;<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 }<\/span><\/pre>\n<p style=\"text-align: justify\">At every timer overflow interrupt the timer\u2019s counter is reset to 0x1D64 to make sure that there are 667 counts before the overflow.<\/p>\n<p style=\"text-align: justify\">The time data is displayed on an LCD screen.<\/p>\n<h2><span lang=\"EN-GB\">Demo<\/span><\/h2>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/IMG_0769.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-14335\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/IMG_0769-580x607.jpg\" alt=\"IMG_0769\" width=\"580\" height=\"607\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/IMG_0769-580x607.jpg 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/IMG_0769.jpg 590w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p><iframe loading=\"lazy\" width=\"860\" height=\"484\" src=\"https:\/\/www.youtube.com\/embed\/egzuPC8Yox4?feature=oembed\" frameborder=\"0\" allow=\"autoplay; encrypted-media\" allowfullscreen><\/iframe><\/p>\n<p><!--more--><\/p>\n<h1 style=\"margin-top: 0in\"><span lang=\"EN-GB\">Timer 2 Input Capture \u2013 Frequency Counter<\/span><\/h1>\n<p style=\"text-align: justify\">Timer 2, as I stated before, is different in many areas from the other timers of N76E003. In my opinion, it is best suited for measuring pulse widths, their periods and thereby frequencies. In other words, it is the best wave capturing tool that we have in N76E003s.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/Timer-2-Block.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-14337\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/Timer-2-Block-580x410.png\" alt=\"Timer 2 Block\" width=\"580\" height=\"410\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/Timer-2-Block-580x410.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/Timer-2-Block-1024x724.png 1024w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/Timer-2-Block.png 1092w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p style=\"text-align: justify\">Unlike conventional 8051s, there varieties of option to capture wave or generate compare-match events.<\/p>\n<p style=\"text-align: justify\">In this segment, we will see how to make a frequency counter with Timer 2\u2019s capture facility. We will also see how to generate Timer 1\u2019s output.<\/p>\n<h2 style=\"margin-top: 0in\"><span lang=\"EN-GB\">Code<\/span><\/h2>\n<pre><span style=\"color: #0000ff\">#include \"N76E003_iar.h\"<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">#include \"SFR_Macro.h\"<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">#include \"Function_define.h\"<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">#include \"Common.h\"<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">#include \"Delay.h\"<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">#include \"LCD_3_Wire.h\"<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">#define HIRC\u00a0\u00a0\u00a0\u00a00<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">#define LIRC\u00a0\u00a0\u00a0 1<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">#define ECLK\u00a0\u00a0\u00a0\u00a02<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">#define timer_clock_speed\u00a0 8000000.0<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">unsigned long overflow = 0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">unsigned long pulse_time = 0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">unsigned long start_time = 0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">unsigned long end_time = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">void setup(void);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">void set_clock_source(unsigned char clock_source);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">void disable_clock_source(unsigned char clock_source);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">void set_clock_division_factor(unsigned char value);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">void setup_GPIOs(void);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">void setup_Timer_1(void);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">void setup_Timer_2(void);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">void setup_capture(void);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">void set_Timer_1(unsigned int value);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">void lcd_print(unsigned char x_pos, unsigned char y_pos, unsigned long value);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">#pragma vector = 0x1B<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">__interrupt void Timer_1_ISR(void)\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 set_Timer_1(0);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">#pragma vector = 0x2B<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">__interrupt void Timer_2_ISR(void)\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 clr_TF2;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 overflow++;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">#pragma vector = 0x63<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">__interrupt void Input_Capture_ISR(void)\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 if((CAPCON0 &amp; SET_BIT0) != 0)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 clr_CAPF0;\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 end_time = C0H;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 end_time &lt;&lt;= 8;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 end_time |= C0L;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 pulse_time = ((overflow &lt;&lt; 16) - start_time + end_time);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 start_time = end_time;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 overflow = 0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 }<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">void main(void)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 register float f = 0.0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 setup();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 LCD_init();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 LCD_clear_home();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 LCD_goto(1, 0);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 LCD_putstr(\"Nu Freq. Meter\");<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 LCD_goto(0, 1);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 LCD_putstr(\"Freq.\/Hz:\");<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 while(1)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 f = (timer_clock_speed \/ ((float)pulse_time));<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 lcd_print(11, 1, ((unsigned long)f));<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 Timer0_Delay1ms(100);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 };<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">void setup(void)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 disable_clock_source(ECLK);\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 set_clock_source(HIRC);\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 set_clock_division_factor(1);\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 setup_GPIOs();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 setup_capture();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 setup_Timer_1();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 setup_Timer_2();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 set_EA;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">void set_clock_source(unsigned char clock_source)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 switch(clock_source)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 case LIRC:<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 set_OSC1;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 clr_OSC0;\u00a0<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 break;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 }<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 case ECLK:<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 set_EXTEN1;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 set_EXTEN0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 while((CKSWT &amp; SET_BIT3) == 0);\u00a0<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 clr_OSC1;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 set_OSC0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 break;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 }<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 default:<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 set_HIRCEN;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 while((CKSWT &amp; SET_BIT5) == 0);\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 clr_OSC1;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 clr_OSC0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 \u00a0\u00a0\u00a0break;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 }<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 }<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 while((CKEN &amp; SET_BIT0) == 1);\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">void disable_clock_source(unsigned char clock_source)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 switch(clock_source)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 case HIRC:<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 clr_HIRCEN;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 break;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 }<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 default:<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 clr_EXTEN1;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 clr_EXTEN0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 break;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 }<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 }<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">void set_clock_division_factor(unsigned char value)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 CKDIV = value;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">void setup_GPIOs(void)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 P00_PushPull_Mode;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 P12_Input_Mode;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">void setup_Timer_1(void)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 set_T1M;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 TIMER1_MODE1_ENABLE;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 set_Timer_1(0);\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 P2S |= SET_BIT3;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 set_TR1;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 set_ET1;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">void setup_Timer_2(void)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 T2CON &amp;= ~SET_BIT0;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 T2MOD = 0x00;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 set_TR2;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 set_ET2;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">void setup_capture(void)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 IC0_P12_CAP0_FallingEdge_Capture;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 set_ECAP;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">void set_Timer_1(unsigned int value)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 TH1 = ((value &amp;&amp; 0xFF00) &gt;&gt; 8);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 TL1 = (value &amp; 0x00FF);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">void lcd_print(unsigned char x_pos, unsigned char y_pos, unsigned long value)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 LCD_goto(x_pos, y_pos);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 LCD_putchar((value \/ 10000) + 0x30);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 LCD_goto((x_pos + 1), y_pos);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 LCD_putchar(((value % 10000) \/ 1000) + 0x30);\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 LCD_goto((x_pos + 2), y_pos);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 LCD_putchar(((value % 1000) \/ 100) + 0x30);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 LCD_goto((x_pos + 3), y_pos);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 LCD_putchar(((value % 100) \/ 10) + 0x30);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 LCD_goto((x_pos + 4), y_pos);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 LCD_putchar((value % 10) + 0x30);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<p>&nbsp;<\/p>\n<h2><span lang=\"EN-GB\">Schematic<\/span><\/h2>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/Timer-2-Capture-Schematic.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-14338\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/Timer-2-Capture-Schematic-580x257.png\" alt=\"Timer 2 Capture Schematic\" width=\"580\" height=\"257\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/Timer-2-Capture-Schematic-580x257.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/Timer-2-Capture-Schematic.png 843w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<h2><span lang=\"EN-GB\">Explanation<\/span><\/h2>\n<p style=\"text-align: justify\">Before explaining the capture part, I would like to put emphasis on how to generate Timer 1\u2019s output. The setup for timer is nothing different from other timer settings in Mode 1 except for the part shown below:<\/p>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">P2S |= SET_BIT3;<\/span><\/pre>\n<p style=\"text-align: justify\">This code sets the timer output. Note that in this example, the system clock speed and hence the clock speed of all hardware sub-systems is set to 8MHz with HIRC and clock divider. Thus, Timer 1\u2019s output will be high for:<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/tim-2-formula-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-14339\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/tim-2-formula-1.png\" alt=\"tim 2 formula 1\" width=\"209\" height=\"70\" \/><\/a><\/p>\n<p style=\"text-align: justify\">and low for the same amount of time. So, the total time period of Timer 1\u2019s output is roughly 16ms. Therefore, the frequency of this output is about 60Hz.<\/p>\n<p style=\"text-align: justify\">P00 is the Timer 1\u2019s output pin and P12 is the capture pin of Timer 2. When P00 and P12 are shorted together, the frequency counter made here with Timer 2 and its capture unit will read 60Hz.<\/p>\n<p style=\"text-align: justify\">So how it is done by Timer 2 and the capture unit?<\/p>\n<p style=\"text-align: justify\">Here in this example, we don\u2019t need the compare-match feature for the measurement of waveform timings and so all compare match features are disabled. We need to reload the timer when it overflows. Since the timer\u2019s counter registers are not manipulated, they are set to zeros by default and so the reload count is zero. These are reflected by the first two lines of the timer\u2019s setup:<\/p>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">void setup_Timer_2(void)<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">\u00a0 T2CON &amp;= ~SET_BIT0;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">\u00a0 T2MOD = 0x00;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">\u00a0 set_TR2;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">\u00a0 set_ET2;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">}<\/span><\/pre>\n<p style=\"text-align: justify\">Next, Timer 2 is started with its interrupt enabled.<\/p>\n<p style=\"text-align: justify\">Finally, the capture pin and its channel to be used are enabled using BSP-based definition. Capture interrupt is also enabled.<\/p>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">void setup_capture(void)<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">\u00a0 IC0_P12_CAP0_FallingEdge_Capture;<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">\u00a0 set_ECAP;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">}<\/span><\/pre>\n<p style=\"text-align: justify\">Note that this is the confusing part of the code. There are three capture channels (<strong><em>CAP0<\/em><\/strong>, <strong><em>CAP1<\/em><\/strong> and <strong><em>CAP2<\/em><\/strong>). These channels share 9 input capture GPIO pins. The pins are cleverly multiplexed. Detection edge selections can be also done.\u00a0 The BSP definition-names state these parameters. For example, the definition used here means:<\/p>\n<h6 style=\"text-align: justify\">\u00a0\u201c<em>input capture pin 0 connected to P12 GPIO pin is connected to capture channel 0 to detect falling edge transitions<\/em>\u201d.<\/h6>\n<p style=\"text-align: justify\">Once the hardware setup is complete, the game now resides in the capture interrupt part:<\/p>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">#pragma vector = 0x63<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">__interrupt void Input_Capture_ISR(void)\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">\u00a0 if((CAPCON0 &amp; SET_BIT0) != 0)<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">\u00a0 {<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 clr_CAPF0;\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 end_time = C0H;<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 end_time &lt;&lt;= 8;<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 end_time |= C0L;<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 pulse_time = ((overflow &lt;&lt; 16) - start_time + end_time);<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">\u00a0\u00a0 \u00a0start_time = end_time;<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 overflow = 0;<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">\u00a0 }<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">}<\/span><\/pre>\n<p style=\"text-align: justify\">Since there is one interrupt vector for all three capture channels, we have check first which capture channel caused the interrupt and clear that particular interrupt flag soon. Capture interrupt, in this case, occurs when a falling edge is detected by CAP0 channel. When such an interrupt occurs, the time of its happening, i.e. the counter value of Timer 2 at that instance is stored. This marks the first falling edge event. To measure period or frequency of a waveform, another such falling edge is needed. Therefore, when another falling edge is detected by the capture hardware, the process repeats. This results in the time capture of two events.<\/p>\n<p style=\"text-align: justify\">We know the frequency of the timer\u2019s clock and so we can find the value of its each tick. We have two captures in form of two Timer 2 counts. We can find the difference between them and thereby compute the time period (<strong><em>pulse_time<\/em><\/strong>).<\/p>\n<p style=\"text-align: justify\">Since the time period of the captured waveform is calculated, its frequency can be deduced. This is done in the main loop.<\/p>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">f = (timer_clock_speed \/ ((float)pulse_time));<\/span><\/pre>\n<p>&nbsp;<\/p>\n<h2><span lang=\"EN-GB\">Demo<\/span><\/h2>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/Timer-2-F-Meter.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-14340\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/Timer-2-F-Meter-580x279.jpg\" alt=\"Timer 2 F-Meter\" width=\"580\" height=\"279\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/Timer-2-F-Meter-580x279.jpg 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/Timer-2-F-Meter.jpg 590w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p><iframe loading=\"lazy\" width=\"860\" height=\"484\" src=\"https:\/\/www.youtube.com\/embed\/33lRj7zE1JM?feature=oembed\" frameborder=\"0\" allow=\"autoplay; encrypted-media\" allowfullscreen><\/iframe><\/p>\n<p><!--more--><\/p>\n<h1 style=\"margin-top: 0in\"><span lang=\"EN-GB\">Timer 2 Pulse Width Capture \u2013 Interfacing HC-SR04 SONAR<\/span><\/h1>\n<p style=\"text-align: justify\">In the last segment, it was demonstrated how to capture a waveform and compute its frequency using Timer 2\u2019s capture hardware. This segment is basically an extension of the last segment. Here, it will be shown how we can use two capture channels connected to the same capture pin to find out the pulse width of a pulse. For demoing this idea, a HC-SR04 ultrasonic SONAR sensor is used. The SONAR sensor gives a pulse output that varies in width according to the distance between the sensor and a target.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/HC-SR04.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-14341\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/HC-SR04-580x310.jpg\" alt=\"HC-SR04\" width=\"580\" height=\"310\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/HC-SR04-580x310.jpg 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/HC-SR04-1024x547.jpg 1024w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/HC-SR04.jpg 1087w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<h2 style=\"margin-top: 0in\"><span lang=\"EN-GB\">Code<\/span><\/h2>\n<pre><span style=\"color: #0000ff\">#include \"N76E003.h\"<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">#include \"SFR_Macro.h\"<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">#include \"Function_define.h\"<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">#include \"Common.h\"<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">#include \"Delay.h\"<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">#include \"LCD_3_Wire.h\"<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">#define HIRC\u00a0\u00a0\u00a0\u00a00<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">#define LIRC\u00a0\u00a0\u00a0 1<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">#define ECLK\u00a0\u00a0\u00a0\u00a02<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">unsigned int pulse_width = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">void setup(void);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">void set_clock_source(unsigned char clock_source);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">void disable_clock_source(unsigned char clock_source);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">void set_system_clock_frequency(unsigned long F_osc, unsigned long F_sys);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">void setup_GPIOs(void);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">void setup_Timer_2(void);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">void setup_capture(void);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">void set_Timer_2(unsigned int value);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">void set_Timer_2_reload_compare(unsigned int value);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">void lcd_print(unsigned char x_pos, unsigned char y_pos, unsigned int value);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">void Input_Capture_ISR(void)\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">interrupt 12<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 if(CAPCON0 &amp; 0x01)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 clr_CAPF0;\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 }<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 if(CAPCON0 &amp; 0x02)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 clr_CAPF1;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 pulse_width = C1H;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 pulse_width &lt;&lt;= 8;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 pulse_width |= C1L;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 }<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">void main(void)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 unsigned int range = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 LCD_init();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 LCD_clear_home();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 LCD_goto(0, 0);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 LCD_putstr(\"Pamge\/cm:\");<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 LCD_goto(0, 1);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 LCD_putstr(\"Pulse\/us:\");<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 setup();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 while(1)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 set_P11;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 Timer3_Delay10us(1);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 clr_P11;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 range = ((unsigned int)(((float)pulse_width) \/ 58.0));<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 lcd_print(11, 0, range);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 lcd_print(11, 1, pulse_width);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 Timer0_Delay1ms(900);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 };<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">void setup(void)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 disable_clock_source(ECLK);\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 set_clock_source(HIRC);\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 set_system_clock_frequency(16, 16);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 setup_GPIOs();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 setup_capture();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 setup_Timer_2();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 set_EA;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">void set_clock_source(unsigned char clock_source)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 switch(clock_source)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 case LIRC:<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 set_OSC1;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 clr_OSC0;\u00a0<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 break;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 }<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 case ECLK:<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 set_EXTEN1;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 set_EXTEN0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 while((CKSWT &amp; SET_BIT3) == 0);\u00a0<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 clr_OSC1;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 set_OSC0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 break;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 }<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 default:<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 set_HIRCEN;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 while((CKSWT &amp; SET_BIT5) == 0);\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 clr_OSC1;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 clr_OSC0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 break;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 }<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 }<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 while((CKEN &amp; SET_BIT0) == 1);\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">void disable_clock_source(unsigned char clock_source)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 switch(clock_source)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 case HIRC:<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 clr_HIRCEN;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 break;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 }<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 default:<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 clr_EXTEN1;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 clr_EXTEN0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 break;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 }<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 }<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">void set_system_clock_frequency(unsigned long F_osc, unsigned long F_sys)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 F_osc = (F_osc \/ (0x02 * F_sys));<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 if((F_osc &gt;= 0x00) &amp;&amp; (F_osc &lt;= 0xFF))<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 CKDIV = ((unsigned char)F_osc);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 }<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">void setup_GPIOs(void)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 P11_PushPull_Mode;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 P12_Input_Mode;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">void setup_Timer_2(void)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 set_Timer_2_reload_compare(0);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 set_Timer_2(0);\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 set_LDEN;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 T2MOD |= 0x01;\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 T2MOD |= 0x20;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 set_TR2;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">void setup_capture(void)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 CAPCON0 = 0x30;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 CAPCON1 = 0x01;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 CAPCON2 = 0x30;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 CAPCON3 = 0x00;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 CAPCON4 = 0x00;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 set_ECAP;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">void set_Timer_2(unsigned int value)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 TL2 = (value &amp; 0x00FF);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 TH2 = ((value &amp; 0xFF00) &gt;&gt; 0x08);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">void set_Timer_2_reload_compare(unsigned int value)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 RCMP2L = (value &amp; 0x00FF);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 RCMP2H = ((value &amp; 0xFF00) &gt;&gt; 0x08);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">void lcd_print(unsigned char x_pos, unsigned char y_pos, unsigned int value)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 LCD_goto(x_pos, y_pos);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 LCD_putchar((value \/ 10000) + 0x30);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 LCD_goto((x_pos + 1), y_pos);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 LCD_putchar(((value % 10000) \/ 1000) + 0x30);\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 LCD_goto((x_pos + 2), y_pos);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 LCD_putchar(((value % 1000) \/ 100) + 0x30);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 LCD_goto((x_pos + 3), y_pos);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 LCD_putchar(((value % 100) \/ 10) + 0x30);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 LCD_goto((x_pos + 4), y_pos);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 LCD_putchar((value % 10) + 0x30);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<p>&nbsp;<\/p>\n<h2><span lang=\"EN-GB\">Schematic<\/span><\/h2>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/Timer-2-Pulse-Width-Capture-Schematic.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-14342\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/Timer-2-Pulse-Width-Capture-Schematic-580x257.png\" alt=\"Timer 2 Pulse Width Capture Schematic\" width=\"580\" height=\"257\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/Timer-2-Pulse-Width-Capture-Schematic-580x257.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/Timer-2-Pulse-Width-Capture-Schematic.png 843w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<h2><span lang=\"EN-GB\">Explanation<\/span><\/h2>\n<p style=\"text-align: justify\">HC-SR04 SONAR sensor has two pins apart from power supply pins. These pins are labelled \u201c<strong><em>Echo<\/em><\/strong>\u201d and \u201c<strong><em>Trigger<\/em><\/strong>\u201d. When the trigger pin of a HC-SR04 is set high for about 10\u03bcs, it acknowledges this short duration pulse in the trigger pin as a command from its host micro to measure and return distance data.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/HC-SR04-Data.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-14343\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/HC-SR04-Data-580x224.png\" alt=\"HC-SR04 Data\" width=\"580\" height=\"224\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/HC-SR04-Data-580x224.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/HC-SR04-Data.png 949w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p style=\"text-align: justify\">P11 is setup as the trigger pin and P12 is set up as the echo pin. The concepts of input capture are same as in the last capture example but there are a few minor changes. Firstly, the system clock speed is set to full 16MHz using HIRC. Secondly, the setup of Timer 2 is slightly changed. Auto-reloading is enabled this time. Timer 2\u2019s clock is prescaled to 1MHz. This is done so in order to make sure that the capture has a resolution of 1\u03bcs and the timer doesn\u2019t overflow while taking measurements. The maximum possible width of a pulse from HC-SR04 is 38ms when no obstacle is detected by it but with this timer setup we can measure pulse widths up to 65ms. Timer 2 is also set up as to reset its count when a CAP0 event occurs.<\/p>\n<pre><span style=\"color: #0000ff\">void setup_Timer_2(void)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 set_Timer_2_reload_compare(0);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 set_Timer_2(0);\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 set_LDEN;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 T2MOD |= 0x01;\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 T2MOD |= 0x20;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 set_TR2;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<p style=\"text-align: justify\">In order to measure pulse widths, we can use one capture channel in both edge capture mode or use two capture channels connected to the same input capture GPIO pin detecting different edges. The latter is used here.<\/p>\n<pre><span style=\"color: #0000ff\">void setup_capture(void)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 CAPCON0 = 0x30;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 CAPCON1 = 0x01;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 CAPCON2 = 0x30;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 CAPCON3 = 0x00;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 CAPCON4 = 0x00;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 set_ECAP;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<p style=\"text-align: justify\">I didn\u2019t use BSP-based definitions for setting up captures in this example because of some limitation. BSP-based definitions reset selections. This is why I configured the input capture control registers manually. Two capture channels having different edge detections are used and both of these channels share P12 or IC0 pin.<\/p>\n<p style=\"text-align: justify\">Now when HC-SR04 is triggered, it will give out a pulse. We have to measure the width of that pulse. A pulse is defined by a rising edge and a falling edge. CAP0 channel is set to detect the rising edge of the pulse and reset Timer 2 to 0 count. CAP1 channel is used to detect the falling edge of the same pulse and read Timer 2\u2019s count. This count represents pulse width in microseconds.<\/p>\n<pre><span style=\"color: #0000ff\">void Input_Capture_ISR(void)\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">interrupt 12<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 if(CAPCON0 &amp; 0x01)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 clr_CAPF0;\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 }<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 if(CAPCON0 &amp; 0x02)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 clr_CAPF1;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 pulse_width = C1H;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 pulse_width &lt;&lt;= 8;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 pulse_width |= C1L;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 }<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<p style=\"text-align: justify\">In the main loop, distance is calculated using this measured pulse width time and the formula given in HC-SR04\u2019s datasheet.<\/p>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">range = ((unsigned int)(((float)pulse_width) \/ 58.0));\u00a0\u00a0<\/span><\/pre>\n<p style=\"text-align: justify\">The distance found between an object and HC-SR04 is displayed on a text LCD.<\/p>\n<h2><span lang=\"EN-GB\">Demo<\/span><\/h2>\n<p><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/TIM2-HC-SR04.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-14344\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/TIM2-HC-SR04-580x445.jpg\" alt=\"TIM2 HC-SR04\" width=\"580\" height=\"445\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/TIM2-HC-SR04-580x445.jpg 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/TIM2-HC-SR04.jpg 590w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><br \/>\n<iframe loading=\"lazy\" width=\"860\" height=\"484\" src=\"https:\/\/www.youtube.com\/embed\/icGIjSAh8SM?feature=oembed\" frameborder=\"0\" allow=\"autoplay; encrypted-media\" allowfullscreen><\/iframe><\/p>\n<p><!--more--><\/p>\n<h1 style=\"margin-top: 0in\"><span lang=\"EN-GB\">Timer 3 \u2013 Driving 7 Segments, LED and Scanning Keypad<\/span><\/h1>\n<p style=\"text-align: justify\">We all know that N76E003 is a cool chip but it lacks GPIOs unlike other chips of similar capabilities. Thus, we must be miser while using GPIOs. When it is needed to interface several inputs and outputs with a microcontroller using fewer pins, we often take the assistance of logic ICs like shift registers, counters, multiplexers, etc. We have already used this technique while making the 3-wire LCD interface. An LCD has its own controller(s) to take care of projecting data on the screen once data has been feed to it. However, that\u2019s not the case with seven segment displays and keypads when used without any specialized driver IC like TM1640\/MAX7219. It is, then, a job for a host micro to do the scanning and data manipulation periodically with affecting other tasks. This can be achieved easily with a timer.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/7-segment-keypad-module.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-14345\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/7-segment-keypad-module-580x565.jpg\" alt=\"7 segment keypad module\" width=\"580\" height=\"565\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/7-segment-keypad-module-580x565.jpg 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/7-segment-keypad-module.jpg 590w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p style=\"text-align: justify\">In this segment, we will see how the aforementioned is done with Timer 3. For the demo, I used a salvaged temperature controller\u2019s I\/O unit. The I\/O unit consists of two 4-digit seven segment displays, four LEDs and four push buttons. It is made with a 74HC164 Serial-In-Parallel-Out (SPIO) shift register and a 74HC145 BCD-to-Decimal decoder. In order to use it in real-time, its displays, LEDs and buttons should be scanned and updated at a fast rate without hindering any part of an application running in main loop.<\/p>\n<h2 style=\"margin-top: 0in\"><span lang=\"EN-GB\">Code<\/span><\/h2>\n<pre><span style=\"color: #0000ff\">#include \"N76E003_iar.h\"<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">#include \"SFR_Macro.h\"<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">#include \"Function_define.h\"<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">#include \"Common.h\"<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">#include \"Delay.h\"<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">#define HIRC\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a00<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">#define LIRC\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a01<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">#define ECLK\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a02<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">#define GATE_HIGH\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 set_P15<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">#define GATE_LOW\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 clr_P15<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">#define CLK_HIGH\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 set_P16<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">#define CLK_LOW\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 clr_P16<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">#define A_HIGH\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 set_P00<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">#define A_LOW\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 clr_P00<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">#define B_HIGH\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 set_P01<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">#define B_LOW\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 clr_P01<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">#define C_HIGH\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0set_P02<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">#define C_LOW\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 clr_P02<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">#define D_HIGH\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 set_P03<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">#define D_LOW\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 clr_P03<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">#define SW\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 P17<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">#define top_seg\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 4<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">#define bot_seg\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a00<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">#define HIGH\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 1<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">#define LOW\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">const unsigned char num[0x0A] = {0xED, 0x21, 0x8F, 0xAB, 0x63, 0xEA, 0xEE, 0xA1, 0xEF, 0xEB};<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">unsigned char data_values[0x09] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">unsigned char SW_in = 0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">unsigned char n = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">void setup(void);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">void setup_GPIOs(void);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">void setup_Timer_3(void);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">void set_Timer_3(unsigned int value);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">unsigned int get_Timer_3(void);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">void write_74HC164(register unsigned char value);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">void write_74HC145(register unsigned char channel);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">void show_LEDs(unsigned char LED1_state, unsigned char LED2_state, unsigned char LED3_state, unsigned char LED4_state);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">void show_numbers(signed int value, unsigned char pos);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">#pragma vector = 0x83<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">__interrupt void Timer3_ISR(void)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 write_74HC164(data_values[n]);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 write_74HC145(n);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 n++;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 if(n &gt; 9)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 n = 0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 }<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 clr_TF3;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">void main (void)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 unsigned int i = 0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 unsigned int j = 9999;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 setup();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 while(1)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 switch(SW_in)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 case 1:<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 show_LEDs(1, 0, 0, 0);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 break;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 }<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 case 2:<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 show_LEDs(0, 1, 0, 0);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 break;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 }<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 case 3:<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 show_LEDs(0, 0, 1, 0);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 break;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 }<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 case 4:<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 show_LEDs(0, 0, 0, 1);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 break;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 }<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 }<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 SW_in = 0x00;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 i++;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 j--;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 if(i &gt; 9999)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 i = 0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 j = 9999;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 }<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 show_numbers(i, bot_seg);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 show_numbers(j, top_seg);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 Timer1_Delay10ms(40);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 show_LEDs(0, 0, 0, 0);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 };<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">void setup(void)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 setup_GPIOs();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 setup_Timer_3();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">void setup_GPIOs(void)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 P00_PushPull_Mode;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 P01_PushPull_Mode;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 P02_PushPull_Mode;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 P03_PushPull_Mode;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 P15_PushPull_Mode;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 P16_PushPull_Mode;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 P17_Input_Mode;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">void setup_Timer_3(void)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 set_Timer_3(0xF9C0);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 set_ET3;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 set_EA;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 set_TR3;\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">void set_Timer_3(unsigned int value)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 RL3 = (value &amp; 0x00FF);\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 RH3 = ((value &amp;&amp; 0xFF00) &gt;&gt; 8);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">unsigned int get_Timer_3(void)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 unsigned int value = 0x0000;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 value = RH3;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 value &lt;&lt;= 8;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 value |= RL3;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 return value;\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">void write_74HC164(register unsigned char value)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 register unsigned char s = 0x08;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 while(s &gt; 0)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 if((value &amp; 0x80) != 0x00)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 GATE_HIGH;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 }<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 else<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 GATE_LOW;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 }<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 CLK_HIGH;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 CLK_LOW;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 value &lt;&lt;= 1;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 s--;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 };<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">void write_74HC145(register unsigned char channel)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 P0 = 0x00;\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 switch(channel)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 case 0:<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 asm(\"nop\");<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 if(SW == LOW)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 SW_in = 1;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 break;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 }<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 case 1:<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 P0 = 0x01;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 break;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 }<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 case 2:<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 P0 = 0x02;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 break;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 }<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 case 3:<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 P0 = 0x03;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 break;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 }<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 case 4:<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 P0 = 0x04;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 break;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 }<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 case 5:<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 P0 = 0x05;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 break;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 }<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 case 6:<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 P0 = 0x06;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 break;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 }<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 case 7:<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 P0 = 0x07;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 asm(\"nop\");<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 if(SW == LOW)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 SW_in = 2;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 break;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 }<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 case 8:<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 P0 = 0x08;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 asm(\"nop\");<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 if(SW == LOW)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 SW_in = 3;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 break;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 }<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 case 9:<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 P0 = 0x09;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 asm(\"nop\");<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 if(SW == LOW)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 SW_in = 4;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 break;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 }<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 }<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">void show_LEDs(unsigned char LED1_state, unsigned char LED2_state, unsigned char LED3_state, unsigned char LED4_state)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 switch(LED1_state)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 case HIGH:<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 data_values[8] |= 0x80;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 break;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 }<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 case LOW:<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 data_values[8] &amp;= 0x7F;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 break;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 }<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 }<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 switch(LED2_state)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 case HIGH:<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 data_values[8] |= 0x40;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 break;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 }<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 case LOW:<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 data_values[8] &amp;= 0xBF;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 break;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 }<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 }<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 switch(LED3_state)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 case HIGH:<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 data_values[8] |= 0x08;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 break;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 }<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 case LOW:<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 data_values[8] &amp;= 0xF7;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 break;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 }<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 }<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 switch(LED4_state)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 case HIGH:<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 data_values[8] |= 0x02;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 break;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 }<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 case LOW:<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 data_values[8] &amp;= 0xFD;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 break;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 }<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 }<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">void show_numbers(signed int value, unsigned char pos)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 register unsigned char ch = 0x00;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 if((value &gt;= 0) &amp;&amp; (value &lt;= 9))<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 ch = (value % 10);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 data_values[(0 + pos)] = num[ch];<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 data_values[(1 + pos)] = 0x00;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 data_values[(2 + pos)] = 0x00;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 data_values[(3 + pos)] = 0x00;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 }<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 else if((value &gt; 9) &amp;&amp; (value &lt;= 99))<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 ch = (value % 10);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 data_values[(0 + pos)] = num[ch];<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 ch = ((value \/ 10) % 10);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 data_values[(1 + pos)] = num[ch];<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 data_values[(2 + pos)] = 0x00;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 data_values[(3 + pos)] = 0x00;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 }<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 else if((value &gt; 99) &amp;&amp; (value &lt;= 999))<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 ch = (value % 10);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 data_values[(0 + pos)] = num[ch];<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 ch = ((value \/ 10) % 10);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 data_values[(1 + pos)] = num[ch];<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 ch = ((value \/ 100) % 10);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 data_values[(2 + pos)] = num[ch];<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 data_values[(3 + pos)] = 0x00;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 }<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 else if((value &gt; 999) &amp;&amp; (value &lt;= 9999))<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 ch = (value % 10);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 data_values[(0 + pos)] = num[ch];<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 ch = ((value \/ 10) % 10);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 data_values[(1 + pos)] = num[ch];<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 ch = ((value \/ 100) % 10);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 data_values[(2 + pos)] = num[ch];<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 ch = (value \/ 1000);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 data_values[(3 + pos)] = num[ch];<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0}<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<p>&nbsp;<\/p>\n<h2><span lang=\"EN-GB\">Schematic<\/span><\/h2>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/Timer-3_Schematic.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-14347\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/Timer-3_Schematic-580x257.png\" alt=\"Timer 3_Schematic\" width=\"580\" height=\"257\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/Timer-3_Schematic-580x257.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/Timer-3_Schematic.png 843w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a> <a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/7-Segment-Module.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-14348\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/7-Segment-Module-580x278.png\" alt=\"7 Segment Module\" width=\"580\" height=\"278\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/7-Segment-Module-580x278.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/7-Segment-Module-1024x492.png 1024w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/7-Segment-Module.png 1377w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<h2><span lang=\"EN-GB\">Explanation<\/span><\/h2>\n<p style=\"text-align: justify\">Timer 3\u2019s internal hardware is very simple. It is an up-counting timer and is run by using system clock as clock source. There is a prescalar to reduce the system clock input. The main feature of Timer 3 is it auto-reload feature. This feature basically allows us to forget reloading its 16-bit counter unlike Timer 0 and 1. There no external input or output option for this timer. Owing to all these, it is most suitable for time-base generation and serial communication.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/Timer-3-Block.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-14346\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/Timer-3-Block-580x96.png\" alt=\"Timer 3 Block\" width=\"580\" height=\"96\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/Timer-3-Block-580x96.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/Timer-3-Block-1024x170.png 1024w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/Timer-3-Block.png 1082w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p style=\"text-align: justify\">In this demo, Timer 3 is set to interrupt every 100\u03bcs.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/tim3.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-14349\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/tim3-580x53.png\" alt=\"tim3\" width=\"580\" height=\"53\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/tim3-580x53.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/tim3.png 717w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p style=\"text-align: justify\">The following code sets up Timer 3 as discussed:<\/p>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">void setup_Timer_3(void)<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">\u00a0 set_Timer_3(0xF9C0);<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">\u00a0 set_ET3;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">\u00a0 set_EA;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">\u00a0 set_TR3;\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">}<\/span><\/pre>\n<p style=\"text-align: justify\">Note that Timer 3\u2019s input clock and system clock frequency is 16MHz since no prescalar is used.<\/p>\n<p style=\"text-align: justify\">Now what we are doing inside the timer interrupt? According to the schematic and objective of this project, we need to update the seven segment displays and read the 4-bit keypad so fast that it looks as if everything is being done without any delay and in real time.<\/p>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">#pragma vector = 0x83<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">__interrupt void Timer3_ISR(void)<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">\u00a0 write_74HC164(data_values[n]);<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">\u00a0 write_74HC145(n);<\/span><\/pre>\n<pre style=\"text-align: justify\"><\/pre>\n<p>&nbsp;<\/p>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">\u00a0 n++;<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">\u00a0 if(n &gt; 9)<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">\u00a0 {<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 n = 0;<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">\u00a0 }<\/span><\/pre>\n<pre style=\"text-align: justify\"><\/pre>\n<p>&nbsp;<\/p>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">\u00a0 clr_TF3;<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">}<\/span><\/pre>\n<p style=\"text-align: justify\">Inside timer ISR, both logic ICs are updated. Firstly, the number to be displayed is sent to the 74HC164 IC and then the seven-segment display to show the number is updated by writing the 74HC145 IC. At every interrupt, one seven segment display is updated. There are 8 such displays and so it takes less than a millisecond to update all these displays. Everything seems to complete in the blink of an eye. During this time the keypad is also scanned in the main. With different keys, different LEDs light up.<\/p>\n<h2><span lang=\"EN-GB\">Demo<\/span><\/h2>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/TIM3.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-14350\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/TIM3-580x374.jpg\" alt=\"TIM3\" width=\"580\" height=\"374\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/TIM3-580x374.jpg 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/TIM3.jpg 590w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p><iframe loading=\"lazy\" width=\"860\" height=\"484\" src=\"https:\/\/www.youtube.com\/embed\/Gd6BecOFtxk?feature=oembed\" frameborder=\"0\" allow=\"autoplay; encrypted-media\" allowfullscreen><\/iframe><\/p>\n<p><!--more--><\/p>\n<h1 style=\"margin-top: 0in\"><span lang=\"EN-GB\">Simple PWM \u2013 RGB LED Fading<\/span><\/h1>\n<p style=\"text-align: justify\">PWM hardware is another basic requirement for any modern-era microcontroller. We can use PWM for a number of applications like motor control, light control, switch-mode power supplies (SMPSs), etc. With PWM we can also simulate digital-to-analogue converter (DAC). Fortunately, N76E003 comes with a separate PWM block that is not a part of any internal timer. It has all the feature that you can imagine. It can be used to generate simple independent 6 single channel PWMs. It can also be used to generate complementary and interdependent PWMs with dead time feature.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/PWM-Block.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-14352\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/PWM-Block-564x650.png\" alt=\"PWM Block\" width=\"564\" height=\"650\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/PWM-Block-564x650.png 564w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/PWM-Block.png 746w\" sizes=\"auto, (max-width: 564px) 100vw, 564px\" \/><\/a><\/p>\n<h2 style=\"margin-top: 0in\"><span lang=\"EN-GB\">Code<\/span><\/h2>\n<pre><span style=\"color: #0000ff\">#include \"N76E003.h\"<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">#include \"SFR_Macro.h\"<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">#include \"Function_define.h\"<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">#include \"Common.h\"<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">#include \"Delay.h\"<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">#include \"soft_delay.h\"<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">unsigned int R_value[10] = {20, 150, 250, 360, 440, 560, 680, 820, 900, 1020};<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">unsigned int G_value[10] = {440, 560, 680, 820, 900, 1020, 20, 150, 250, 360};<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">unsigned int B_value[10] = {900, 1020, 20, 150, 250, 360, 440, 560, 680, 820};<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">void set_PWM_period(unsigned int value);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">void set_PWM0(unsigned int value);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">void set_PWM1(unsigned int value);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">void set_PWM2(unsigned int value);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">void set_PWM3(unsigned int value);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">void set_PWM4(unsigned int value);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">void set_PWM5(unsigned int value);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">void main(void)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0signed int i = 0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0signed char j = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0P01_PushPull_Mode;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0P10_PushPull_Mode;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0P11_PushPull_Mode;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0PWM1_P11_OUTPUT_ENABLE;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0PWM2_P10_OUTPUT_ENABLE;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0PWM4_P01_OUTPUT_ENABLE;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0PWM_IMDEPENDENT_MODE;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0PWM_EDGE_TYPE;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0set_CLRPWM;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0PWM_CLOCK_FSYS;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0PWM_CLOCK_DIV_64;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0PWM_OUTPUT_ALL_NORMAL;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0set_PWM_period(1023);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0set_PWMRUN;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0while(1)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0for(i = 0; i &lt; 1024; i += 10)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0set_PWM1(i);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0delay_ms(20);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0for(i = 1023; i &gt; 0; i -= 10)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0set_PWM1(i);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0delay_ms(20);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0for(i = 0; i &lt; 1024; i += 10)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0set_PWM2(i);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0delay_ms(20);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0for(i = 1023; i &gt; 0; i -= 10)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0set_PWM2(i);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0delay_ms(20);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0for(i = 0; i &lt; 1024; i += 10)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0set_PWM4(i);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0delay_ms(20);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0for(i = 1023; i &gt; 0; i -= 10)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0set_PWM4(i);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0delay_ms(20);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0delay_ms(600);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0for(i = 0; i &lt;=9; i++)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0for(j = 0; j &lt;= 9; j++)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0set_PWM4(R_value[j]);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0set_PWM1(G_value[j]);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0set_PWM2(B_value[j]);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0delay_ms(200);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0for(j = 9; j &gt;= 0; j--)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0set_PWM4(R_value[j]);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0set_PWM1(G_value[j]);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0set_PWM2(B_value[j]);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0delay_ms(200);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0delay_ms(600);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0}<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">void set_PWM_period(unsigned int value)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 PWMPL = (value &amp; 0x00FF);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 PWMPH = ((value &amp; 0xFF00) &gt;&gt; 8);\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">void set_PWM0(unsigned int value)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 PWM0L = (value &amp; 0x00FF);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 PWM0H = ((value &amp; 0xFF00) &gt;&gt; 8);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 set_LOAD;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">void set_PWM1(unsigned int value)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 PWM1L = (value &amp; 0x00FF);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 PWM1H = ((value &amp; 0xFF00) &gt;&gt; 8);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 set_LOAD;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">void set_PWM2(unsigned int value)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 PWM2L = (value &amp; 0x00FF);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 PWM2H = ((value &amp; 0xFF00) &gt;&gt; 8);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 set_LOAD;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">void set_PWM3(unsigned int value)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 PWM3L = (value &amp; 0x00FF);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 PWM3H = ((value &amp; 0xFF00) &gt;&gt; 8);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 set_LOAD;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">void set_PWM4(unsigned int value)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 set_SFRPAGE;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 PWM4L = (value &amp; 0x00FF);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 PWM4H = ((value &amp; 0xFF00) &gt;&gt; 8);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 clr_SFRPAGE;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 set_LOAD;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">void set_PWM5(unsigned int value)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 set_SFRPAGE;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 PWM5L = (value &amp; 0x00FF);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0PWM5H = ((value &amp; 0xFF00) &gt;&gt; 8);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 clr_SFRPAGE;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 set_LOAD;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<p>&nbsp;<\/p>\n<h2><span lang=\"EN-GB\">Schematic<\/span><\/h2>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/Simple-PWM_Schematic.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-14353\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/Simple-PWM_Schematic-580x259.png\" alt=\"Simple PWM_Schematic\" width=\"580\" height=\"259\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/Simple-PWM_Schematic-580x259.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/Simple-PWM_Schematic.png 827w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<h2><span lang=\"EN-GB\">Explanation<\/span><\/h2>\n<p style=\"text-align: justify\">PWM generated by the N76E003\u2019s PWM hardware is based on compare-match principle and this is evident from its block diagram. There are 6 PWM channels and they can be used independently or as interdependent groups to form complimentary PWMs. PWMs generated by N76E003 can be edge-aligned or centre-aligned PWMs as per user\u2019s requirement.\u00a0 The PWM block can be clocked directly by the system clock or by Timer 1 overflow. Additionally, there is a prescalar unit to reduce input clock source.<\/p>\n<p style=\"text-align: justify\">For demonstrating N76E003\u2019s PWM feature, I used an RGB LED. Three independent PWMs were generated using PWM channels 1,2 and 4. Let us look into the process of setting up the PWM channels and the PWM hardware. We must firstly set the PWM GPIOs as push-pull GPIOs since PWM is an output function of GPIO pins. We must also enable the PWM output channels we want to use.<\/p>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">P01_PushPull_Mode;<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">P10_PushPull_Mode;<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">P11_PushPull_Mode;<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">PWM1_P11_OUTPUT_ENABLE;<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">PWM2_P10_OUTPUT_ENABLE;<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">PWM4_P01_OUTPUT_ENABLE;<\/span><\/pre>\n<p style=\"text-align: justify\">Secondly, the PWM hardware is set up according to our needs. Since we need independent PWMs here, independent PWM mode is selected. Edge-align PWM is selected since it is most easy to understand and use. The PWM channels are reset before using them. The PWM outputs are non-inverted and so they are characterized as normal PWMs. Here, I use system clock as the clock source for the PWM block but it is prescaled\/divided by 64 to get an effective PWM clock of 250kHz. The PWM resolution is set to 10-bits when we set the period count or maximum PWM value\/duty cycle. Setting the period count yields in setting up the internal PWM counter. This gives a PWM of approximately 245Hz frequency or 4ms period. Finally, the PWM hardware is enabled after setting up all these.<\/p>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">PWM_IMDEPENDENT_MODE;<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">PWM_EDGE_TYPE;<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">set_CLRPWM;<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">PWM_CLOCK_FSYS;<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">PWM_CLOCK_DIV_64;<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">PWM_OUTPUT_ALL_NORMAL;<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">set_PWM_period(1023);<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">set_PWMRUN;<\/span><\/pre>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/PWM3.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-14354\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/PWM3-580x338.png\" alt=\"PWM3\" width=\"580\" height=\"338\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/PWM3-580x338.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/PWM3.png 946w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a> <a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/PWM1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-14355\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/PWM1-580x272.png\" alt=\"PWM1\" width=\"580\" height=\"272\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/PWM1-580x272.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/PWM1-1024x481.png 1024w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/PWM1.png 1167w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a> <a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/PWM2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-14356\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/PWM2-580x293.png\" alt=\"PWM2\" width=\"580\" height=\"293\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/PWM2-580x293.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/PWM2-1024x517.png 1024w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/PWM2.png 1089w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p style=\"text-align: justify\">To change PWM duty cycle, functions like the one shown below is called. After altering the duty cycle or compare value, the new value is loaded and run.<\/p>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">void set_PWMn(unsigned int value)<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">\u00a0 PWMnL = (value &amp; 0x00FF);<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">\u00a0 PWMnH = ((value &amp; 0xFF00) &gt;&gt; 8);<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">\u00a0 set_LOAD;<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">}<\/span><\/pre>\n<p style=\"text-align: justify\">In the demo, the RGB LED fades different colours as a symbol of changing PWMs.<\/p>\n<h2><span lang=\"EN-GB\">Demo<\/span><\/h2>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/PWM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-14357\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/PWM-417x650.png\" alt=\"PWM\" width=\"417\" height=\"650\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/PWM-417x650.png 417w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/PWM.png 590w\" sizes=\"auto, (max-width: 417px) 100vw, 417px\" \/><\/a><\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/PWMS-1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-14358\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/PWMS-1-580x463.jpg\" alt=\"PWMS (1)\" width=\"580\" height=\"463\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/PWMS-1-580x463.jpg 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/PWMS-1.jpg 590w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a> <a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/PWMS-2.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-14359\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/PWMS-2-580x463.jpg\" alt=\"PWMS (2)\" width=\"580\" height=\"463\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/PWMS-2-580x463.jpg 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/PWMS-2.jpg 590w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a> <a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/PWMS-3.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-14360\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/PWMS-3-580x463.jpg\" alt=\"PWMS (3)\" width=\"580\" height=\"463\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/PWMS-3-580x463.jpg 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/PWMS-3.jpg 590w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p><iframe loading=\"lazy\" width=\"860\" height=\"484\" src=\"https:\/\/www.youtube.com\/embed\/vomQy0alnPU?feature=oembed\" frameborder=\"0\" allow=\"autoplay; encrypted-media\" allowfullscreen><\/iframe><\/p>\n<p><!--more--><\/p>\n<h1 style=\"margin-top: 0in\"><span lang=\"EN-GB\">Complementary PWM with Dead Time<\/span><\/h1>\n<p style=\"text-align: justify\">In the last section, we saw how we can generate independent PWM. Now we will see complimentary or group PWM with dead-time. We will also see the things that were skipped in the previous segment.<\/p>\n<p style=\"text-align: justify\">Complementary PWM with dead-time feature is a must-have feature of any PWM drives in today\u2019s embedded-system industry. Unlike the simple PWM we saw previously, this kind of PWM has most usage. Complementary PWM with dead-time is used in applications where we need to design motor controllers, SMPSs, inverters, etc with H-bridges or half-bridges. In such applications, PWMs need to operate in groups while ensuring that both PWMs don\u2019t turn on at the same time. One PWM in a group should be the opposite\/antiphase of the other in terms of duty-cycle or waveshape.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/bridges.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-14362\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/bridges.png\" alt=\"bridges\" width=\"479\" height=\"340\" \/><\/a><\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/Complimentary-PWM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-14363\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/Complimentary-PWM-580x227.png\" alt=\"Complimentary PWM\" width=\"580\" height=\"227\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/Complimentary-PWM-580x227.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/Complimentary-PWM.png 985w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<h2 style=\"margin-top: 0in\"><span lang=\"EN-GB\">Code<\/span><\/h2>\n<pre><span style=\"color: #0000ff\">#include \"N76E003.h\"<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">#include \"SFR_Macro.h\"<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">#include \"Function_define.h\"<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">#include \"Common.h\"<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">#include \"Delay.h\"<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">void set_PWM_period(unsigned int value);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">void set_PWM0(unsigned int value);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">void set_PWM1(unsigned int value);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">void set_PWM2(unsigned int value);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">void set_PWM3(unsigned int value);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">void set_PWM4(unsigned int value);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">void set_PWM5(unsigned int value);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">void set_PWM_dead_time(unsigned int\u00a0value);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">void main(void)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 signed int i = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 P11_PushPull_Mode;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 P12_PushPull_Mode;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 PWM0_P12_OUTPUT_ENABLE;\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0PWM1_P11_OUTPUT_ENABLE;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0PWM_COMPLEMENTARY_MODE;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0PWM_CENTER_TYPE;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0set_CLRPWM;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0PWM_CLOCK_FSYS;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0PWM_CLOCK_DIV_64;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0PWM0_OUTPUT_INVERSE;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0PWM1_OUTPUT_INVERSE;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0set_PWM_period(600);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0set_PWM_dead_time(40);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0PWM01_DEADTIME_ENABLE;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0set_PWMRUN;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0while(1)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0for(i = 0; i &lt; 600; i++)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0set_PWM0(i);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Timer0_Delay1ms(5);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0}<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0for(i = 600; i &gt; 0; i--)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0set_PWM0(i);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Timer0_Delay1ms(5);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0}<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0};<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">void set_PWM_period(unsigned int value)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 PWMPL = (value &amp; 0x00FF);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 PWMPH = ((value &amp; 0xFF00) &gt;&gt; 8);\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">void set_PWM0(unsigned int value)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 PWM0L = (value &amp; 0x00FF);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 PWM0H = ((value &amp; 0xFF00) &gt;&gt; 8);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 set_LOAD;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">void set_PWM1(unsigned int value)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 PWM1L = (value &amp; 0x00FF);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 PWM1H = ((value &amp; 0xFF00) &gt;&gt; 8);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 set_LOAD;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">void set_PWM2(unsigned int value)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 PWM2L = (value &amp; 0x00FF);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 PWM2H = ((value &amp; 0xFF00) &gt;&gt; 8);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 set_LOAD;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">void set_PWM3(unsigned int value)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 PWM3L = (value &amp; 0x00FF);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 PWM3H = ((value &amp; 0xFF00) &gt;&gt; 8);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 set_LOAD;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">void set_PWM4(unsigned int value)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 set_SFRPAGE;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 PWM4L = (value &amp; 0x00FF);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 PWM4H = ((value &amp; 0xFF00) &gt;&gt; 8);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 clr_SFRPAGE;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 set_LOAD;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">void set_PWM5(unsigned int value)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 set_SFRPAGE;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 PWM5L = (value &amp; 0x00FF);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 PWM5H = ((value &amp; 0xFF00) &gt;&gt; 8);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 clr_SFRPAGE;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 set_LOAD;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">void set_PWM_dead_time(unsigned int\u00a0value)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0unsigned char hb = 0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0unsigned char lb = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0lb = (value &amp; 0x00FF);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0hb = ((value &amp; 0x0100) &gt;&gt; 8);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0BIT_TMP = EA;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0EA = 0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0TA = 0xAA;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0TA = 0x55;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0PDTEN &amp;= 0xEF;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 PDTEN |= hb;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 PDTCNT = lb;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0EA = BIT_TMP;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<p>&nbsp;<\/p>\n<h2><span lang=\"EN-GB\">Schematic<\/span><\/h2>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/Comp-PWM-Schematic.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-14364\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/Comp-PWM-Schematic-580x257.png\" alt=\"Comp PWM Schematic\" width=\"580\" height=\"257\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/Comp-PWM-Schematic-580x257.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/Comp-PWM-Schematic.png 843w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<h2><span lang=\"EN-GB\">Explanation<\/span><\/h2>\n<p style=\"text-align: justify\">As discussed, there are two types of PWM in terms of count alignment. These are shown below:<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/PWM-types.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-14365\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/PWM-types-465x650.png\" alt=\"PWM types\" width=\"465\" height=\"650\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/PWM-types-465x650.png 465w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/PWM-types.png 716w\" sizes=\"auto, (max-width: 465px) 100vw, 465px\" \/><\/a><\/p>\n<p style=\"text-align: justify\">The first type was demonstrated in the simple PWM example. The second is demonstrated here. From the perspective of a general user, the difference in them don\u2019t affect much. Make sure that you check the formulae for each type before using.<\/p>\n<p style=\"text-align: justify\">The set up for complimentary PWM with dead time is no different from the simple PWM set up. As mentioned, there are a few minor differences. First the mode is set for complimentary PWM mode. Secondly, centre-aligned PWM is used here. The outputs are also set as inverted outputs, i.e. 100% duty cycle means minimum PWM value\/count. The dead time period is set and implemented.<\/p>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">P11_PushPull_Mode;<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">P12_PushPull_Mode;<\/span><\/pre>\n<pre style=\"text-align: justify\"><\/pre>\n<p>&nbsp;<\/p>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">PWM0_P12_OUTPUT_ENABLE;\u00a0<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">PWM1_P11_OUTPUT_ENABLE;<\/span><\/pre>\n<pre style=\"text-align: justify\"><\/pre>\n<p>&nbsp;<\/p>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">PWM_COMPLEMENTARY_MODE;<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">PWM_CENTER_TYPE;<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">set_CLRPWM;<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">PWM_CLOCK_FSYS;<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">PWM_CLOCK_DIV_64;<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">PWM0_OUTPUT_INVERSE;<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">PWM1_OUTPUT_INVERSE;<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">set_PWM_period(600);<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">set_PWM_dead_time(40);<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">PWM01_DEADTIME_ENABLE;<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">set_PWMRUN;<\/span><\/pre>\n<p style=\"text-align: justify\">Now what is dead time in complimentary PWMs? Well simply is a short duration delay that is inserted between the polarity shifts of two PWMs in a group.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/MOSFETs-Half-bridge.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-14361\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/MOSFETs-Half-bridge.png\" alt=\"MOSFETs Half-bridge\" width=\"234\" height=\"275\" \/><\/a><\/p>\n<p style=\"text-align: justify\">Consider a half-bridge MOSFET configuration as shown above. Perhaps it is the best way to describe the concept of dead-time. Surely, nobody would ever want to turn on both MOSFETs simultaneously in any condition and also during transition. Doing so would lead to a temporary short circuit between voltage source and ground, and would also lead to unnecessary heating of the MOSFETs and even permanent damage. By applying dead-time this can be avoided. In half\/H-bridges, complimentary PWMs ensure that when one MOSFET is on, the other is off. However, at the edges of PWM polarity shifts, i.e. when one is rising while the other is falling, there is short but certain such short-circuit duration. If a dead time is inserted between the transitions, it will ensure that one MOSFET is only turned on when the other has been turned off fully.<\/p>\n<p style=\"text-align: justify\">Setting dead time requires us to disable TA protection. The function for setting dead time is shown below:<\/p>\n<pre><span style=\"color: #0000ff\">void set_PWM_dead_time(unsigned int\u00a0value)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0unsigned char hb = 0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0unsigned char lb = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0lb = (value &amp; 0x00FF);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0hb = ((value &amp; 0x0100) &gt;&gt; 8);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0BIT_TMP = EA;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0EA = 0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0TA = 0xAA;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0TA = 0x55;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0PDTEN &amp;= 0xEF;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 PDTEN |= hb;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 PDTCNT = lb;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0EA = BIT_TMP;<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">}<\/span><\/pre>\n<p style=\"text-align: justify\">Since complimentary PWMs work in groups, changing the duty cycle of one PWM channel will affect the other and so we don\u2019t need to change the duty cycles of both PWMs individually. This is why only one PWM channel is manipulated in the code.<\/p>\n<h2><span lang=\"EN-GB\">Demo<\/span><\/h2>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/PWMC-1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-14366\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/PWMC-1-580x558.jpg\" alt=\"PWMC (1)\" width=\"580\" height=\"558\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/PWMC-1-580x558.jpg 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/PWMC-1.jpg 590w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a> <a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/PWMC-2.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-14367\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/PWMC-2-580x568.jpg\" alt=\"PWMC (2)\" width=\"580\" height=\"568\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/PWMC-2-580x568.jpg 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/PWMC-2.jpg 590w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p><iframe loading=\"lazy\" width=\"860\" height=\"484\" src=\"https:\/\/www.youtube.com\/embed\/0APOUon1IVE?feature=oembed\" frameborder=\"0\" allow=\"autoplay; encrypted-media\" allowfullscreen><\/iframe><\/p>\n<p><!--more--><\/p>\n<h1 style=\"margin-top: 0in\"><span lang=\"EN-GB\">Wakeup Timer and Power Modes<\/span><\/h1>\n<p style=\"text-align: justify\">One of key feature of many modern era microcontrollers is a way to wake up a micro once it went to low power, sleep or idle mode. Like STM8s, N76E003 has this feature. The wakeup timer (WKT) is not a complex hardware. As the block diagram below shows it is just a timer-counter with <strong><em>LIRC<\/em><\/strong> as clock source. When the counter overflows, an interrupt is triggered. This interrupt wakes up the N76E003 chip.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/WKT_Block.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-14368\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/WKT_Block-580x115.png\" alt=\"WKT_Block\" width=\"580\" height=\"115\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/WKT_Block-580x115.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/WKT_Block.png 788w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p style=\"text-align: justify\">Wakeup timer is particularly very useful when used with low power modes. In many applications, we need to measure data on a fixed time basis while at other times when measurements are not taken, idle times are passed. These idle periods consume power and so if we use a wakeup timer with low power sleep mode, we can save energy. This is a big requirement for portable battery-operated devices. Examples of such portable devices include data-loggers, energy meters and smart watches.<\/p>\n<h2 style=\"margin-top: 0in\"><span lang=\"EN-GB\">Code<\/span><\/h2>\n<pre><span style=\"color: #0000ff\">#include \"N76E003_iar.h\"<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">#include \"Common.h\"<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">#include \"SFR_Macro.h\"<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">#include \"Function_define.h\"<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">#include \"soft_delay.h\"<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">#pragma vector = 0x8B<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">__interrupt void WKT_ISR(void)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 clr_WKTR;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 clr_WKTF;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">void main(void)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 unsigned char s = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 P15_PushPull_Mode;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 WKCON = 0x03; \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 RWK = 0X00;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 set_EWKT;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 set_EA;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 while(1)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 for(s = 0; s &lt; 9; s++)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 P15 = ~P15;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 delay_ms(100);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 }<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 set_WKTR; \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 set_PD;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 for(s = 0; s &lt;= 9; s++)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 P15 = ~P15;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 delay_ms(300);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 }<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 set_WKTR; \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 set_PD;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 };<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<p>&nbsp;<\/p>\n<h2><span lang=\"EN-GB\">Schematic<\/span><\/h2>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/WKT_Schematic.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-14369\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/WKT_Schematic-580x339.png\" alt=\"WKT_Schematic\" width=\"580\" height=\"339\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/WKT_Schematic-580x339.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/WKT_Schematic.png 864w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<h2><span lang=\"EN-GB\">Explanation<\/span><\/h2>\n<p style=\"text-align: justify\">In the demo demonstrated here, P15 LED is toggle fast for nine times. This marks the start of the demo. After this toggling has been done, both the wakeup timer and the power down modes are turned on.<\/p>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">set_WKTR; \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">set_PD;<\/span><\/pre>\n<p style=\"text-align: justify\">During <strong><em>Power Down<\/em><\/strong> time, all operations inside the micro are suspended. There is also another low power mode called <strong><em>Idle Mode<\/em><\/strong>. Both are similar but in idle mode peripherals are kept active.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/Power-Mode-Registers.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-14370\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/Power-Mode-Registers-580x344.png\" alt=\"Power Mode Registers\" width=\"580\" height=\"344\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/Power-Mode-Registers-580x344.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/Power-Mode-Registers.png 660w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p style=\"text-align: justify\">After the first set of LED toggles and power down mode, it takes about 1.5 seconds (according to the prescalar value of 64 and 256 counts) for the wakeup timer to get triggered.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/WKT.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-14371\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/WKT-580x49.png\" alt=\"WKT\" width=\"580\" height=\"49\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/WKT-580x49.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/WKT.png 791w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p style=\"text-align: justify\">When the interrupt kicks in, the MCU is brought back to working mode and the wakeup timer is disabled. Again, the P15 LED is toggled. This time the LED is toggled at a slower rate, indicating the continuation of the rest of the tasks after power down. After this the process is repeated with power down and wake up timer reenabled.<\/p>\n<p style=\"text-align: justify\">Setting up the wakeup timer requires the same stuffs that we need during a timer configuration, i.e. a prescalar value, counter value and interrupt. These three settings are done by the following lines of code:<\/p>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">WKCON = 0x03; \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">RWK = 0X00;<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">set_EWKT;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">set_EA;<\/span><\/pre>\n<p style=\"text-align: justify\">Since interrupt is used, WKT interrupt will be triggered when its counter rolls over.<\/p>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">#pragma vector = 0x8B<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">__interrupt void WKT_ISR(void)<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">\u00a0 clr_WKTR;<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">\u00a0 clr_WKTF;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre style=\"text-align: justify\"><span style=\"color: #0000ff\">}<\/span><\/pre>\n<p style=\"text-align: justify\">When WKT interrupt is triggered, its interrupt flag should be cleared in the software.<\/p>\n<h2><span lang=\"EN-GB\">Demo<\/span><\/h2>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/Timer-0-2.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-14327\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/Timer-0-2-580x435.jpg\" alt=\"Timer 0 (2)\" width=\"580\" height=\"435\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/Timer-0-2-580x435.jpg 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/Timer-0-2.jpg 590w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a> <a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/Timer-0-1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-14326\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/Timer-0-1-580x435.jpg\" alt=\"Timer 0 (1)\" width=\"580\" height=\"435\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/Timer-0-1-580x435.jpg 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/Timer-0-1.jpg 590w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p><iframe loading=\"lazy\" width=\"860\" height=\"484\" src=\"https:\/\/www.youtube.com\/embed\/ZyQZmJB3FRI?feature=oembed\" frameborder=\"0\" allow=\"autoplay; encrypted-media\" allowfullscreen><\/iframe><\/p>\n<p><!--more--><\/p>\n<h1 style=\"margin-top: 0in\"><span lang=\"EN-GB\">Watchdog Timer<\/span><\/h1>\n<p style=\"text-align: justify\">The watchdog timer (WDT) of N76E003 is just a reset-issuing timer and the purpose of this timer is to recover a N76E003 micro from an unanticipated event\/loop that may result in unresponsive or erratic behaviour. It is clocked with LIRC oscillator and this makes it independent from main clock (HIRC or ECLK) failure.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/WDT_Block_Diagram.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-14373\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/WDT_Block_Diagram-580x125.png\" alt=\"WDT_Block_Diagram\" width=\"580\" height=\"125\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/WDT_Block_Diagram-580x125.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/WDT_Block_Diagram.png 762w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p style=\"text-align: justify\">LIRC is prescaled and feed to a counter. When the counter overflows, WDT interrupt is issued, furthermore a reset is also generated based on delay. In normal running condition, the counter must be periodically reset to 0 count to avoid reset. If for some reason this is not done then a reset will be triggered.<\/p>\n<p style=\"text-align: justify\">The watchdog timer of N76E003 can also be used as a 6-bit general-purpose timer. However, this usually not used as such. The only difference between using it as timer and as a watchdog timer is the reset part.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/WDT_GPT_Block_Diagram.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-14374\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/WDT_GPT_Block_Diagram-580x98.png\" alt=\"WDT_GPT_Block_Diagram\" width=\"580\" height=\"98\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/WDT_GPT_Block_Diagram-580x98.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/WDT_GPT_Block_Diagram.png 770w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<h2 style=\"margin-top: 0in\"><span lang=\"EN-GB\">Code<\/span><\/h2>\n<pre><span style=\"color: #0000ff\">#include \"N76E003.h\"<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">#include \"Common.h\"<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">#include \"Delay.h\"<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">#include \"SFR_Macro.h\"<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">#include \"Function_define.h\"<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">void main (void)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 unsigned char s = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P15_PushPull_Mode;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 Timer0_Delay1ms(1000);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0for(s = 0; s &lt;= 9; s++)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0P15 = ~P15;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Timer0_Delay1ms(60);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0}<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0TA = 0xAA;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0TA = 0x55;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0WDCON = 0x07;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0set_WDCLR;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0while((WDCON | ~SET_BIT6) == 0xFF);\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0EA = 1;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0set_WDTR;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 while(1)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0for(s = 0; s &lt;= 9; s++)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0P15 = ~P15;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Timer0_Delay1ms(200);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0while(1);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0}<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<p>&nbsp;<\/p>\n<h2 style=\"margin-top: 0in\"><span lang=\"EN-GB\">Schematic<\/span><\/h2>\n<p><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/WDT_Schematic.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-14375 aligncenter\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/WDT_Schematic-580x339.png\" alt=\"WDT_Schematic\" width=\"580\" height=\"339\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/WDT_Schematic-580x339.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/WDT_Schematic.png 864w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<h2 style=\"margin-top: 0in\"><span lang=\"EN-GB\">Explanation<\/span><\/h2>\n<p style=\"text-align: justify\">For demoing WDT, again the P15 LED is used. The code starts with it toggling states ten times. This marks the beginning of the code and the interval prior to WDT configuration.<\/p>\n<p style=\"text-align: justify\">Next the WDT is setup. Note that the WDT is <strong><em>Timed-Access (TA)<\/em><\/strong> protected. TA protection protects crucial hardware like brownout detection circuit and WDT hardware from erratic writes.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/TA-Register.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-14376\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/TA-Register-580x182.png\" alt=\"TA Register\" width=\"580\" height=\"182\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/TA-Register-580x182.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/TA-Register.png 659w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p style=\"text-align: justify\">For using the TA hardware, we need not to follow any special procedure. To enable access to some hardware that are TA protected like the WDT all we have to do is to write the followings to TA register:<\/p>\n<pre><span style=\"color: #0000ff\">TA = 0xAA;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">TA = 0x55;<\/span><\/pre>\n<p style=\"text-align: justify\">Without TA protection access, any changes to made to the registers of the TA protected hardware is unaffected.<\/p>\n<p style=\"text-align: justify\">After enabling access to WDT, we can set it up. Setting the WDT requires us mainly to setup LIRC clock prescalar. Once the prescalar is set we are good to go for enabling the WDT.<\/p>\n<pre><span style=\"color: #0000ff\">WDCON = 0x07;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">set_WDCLR;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">while((WDCON | ~SET_BIT6) == 0xFF);\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">set_EA;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">set_WDTR;\u00a0\u00a0<\/span><\/pre>\n<p style=\"text-align: justify\">Before all these can be done, we have to enable the WDT hardware either through <strong><em>In-Application Programming (IAP) <\/em><\/strong>or by configuration bits during programming.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/WDT-Config.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-14372\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/WDT-Config.png\" alt=\"WDT Config\" width=\"381\" height=\"396\" \/><\/a><\/p>\n<h2 style=\"margin-top: 0in\"><span lang=\"EN-GB\">Demo<\/span><\/h2>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/Timer-0-2.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-14327\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/Timer-0-2-580x435.jpg\" alt=\"Timer 0 (2)\" width=\"580\" height=\"435\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/Timer-0-2-580x435.jpg 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/Timer-0-2.jpg 590w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a> <a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/Timer-0-1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-14326\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/Timer-0-1-580x435.jpg\" alt=\"Timer 0 (1)\" width=\"580\" height=\"435\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/Timer-0-1-580x435.jpg 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2018\/08\/Timer-0-1.jpg 590w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p><iframe loading=\"lazy\" width=\"860\" height=\"484\" src=\"https:\/\/www.youtube.com\/embed\/h8LeHpnA2vY?feature=oembed\" frameborder=\"0\" allow=\"autoplay; encrypted-media\" allowfullscreen><\/iframe><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This post is a continuation of the previous post on Nuvoton N76E003 microcontroller\u00a0here.<\/p>\n","protected":false},"author":5,"featured_media":14257,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1723],"tags":[1646,1722,1729,1728,1721,1720,1730],"class_list":["post-14320","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-nuvoton","tag-8-bit-microcontroller","tag-1722","tag-iar","tag-keil","tag-n76e003","tag-nuvoton","tag-timers"],"_links":{"self":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts\/14320","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/comments?post=14320"}],"version-history":[{"count":5,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts\/14320\/revisions"}],"predecessor-version":[{"id":14381,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts\/14320\/revisions\/14381"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/media\/14257"}],"wp:attachment":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/media?parent=14320"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/categories?post=14320"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/tags?post=14320"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}