{"id":13631,"date":"2017-08-25T04:43:41","date_gmt":"2017-08-25T08:43:41","guid":{"rendered":"http:\/\/embedded-lab.com\/blog\/?p=13631"},"modified":"2018-03-30T09:40:38","modified_gmt":"2018-03-30T13:40:38","slug":"introducing-ti-msp430-microcontrollers","status":"publish","type":"post","link":"https:\/\/embedded-lab.com\/blog\/introducing-ti-msp430-microcontrollers\/","title":{"rendered":"Introducing TI MSP430 Microcontrollers"},"content":{"rendered":"<p style=\"text-align: justify\">Texas Instruments (TI) is a well-known US-based semiconductor manufacturer. TI is perhaps best known to many as the manufacturer of some of the fanciest scientific calculators in the market. Of the long list of electronic devices produced by TI, microcontrollers are on the top. TI manufactures some of the coolest and advanced microcontrollers of the market today. There are several categories of micros from TI. These include general purpose low power MCUs which mainly comprise of MSP430s, ARMs like TM4Cs, MSP432s, etc, micros for wireless communications like CC2xxx series, ARM + DSP micros, DSP-specialized micros like the TMS32xxx series and so on. It will look as if TI is committed toward mixed signal microcontrollers that are engineered for highly sophisticated industrial challenges. This issue will cover an insight of value-line MSP430 general purpose micros.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Launchpad-Board.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-13632\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Launchpad-Board.png\" alt=\"MSP430 Launchpad Board\" width=\"390\" height=\"274\" \/><\/a><\/p>\n<p style=\"text-align: justify\">MSP430s are not seen as much as the popular 8051s, PICs and AVRs. In most of the Asian market, for example, MSP430s are rare when compared to other microcontrollers and even still rare when compared to other chips produced by TI itself. I don\u2019t know why there is such an imbalance. Perhaps one big reason is its inclination towards low power consumption and limited resources. Low-power means that these MCUs are crafted for special low power applications unlike most other micros. Secondly TI micros are a bit expensive than other micros. Despites these, TI has provided some great tools for making things simple. You can get your hands on some cool MSP430 chips through some affordable Launchpad boards and still it worth every penny learning MSP430s. Firstly, it is a family of ultra-low power high performance 16-bit (16-bit data bus) micros which are unlike the popular 8-bit platforms. Secondly MSP430s have highly rich internal hardware peripherals that are second to none. For instance, MSP430s can be operated over a wide voltage and frequency ranges. Another great feature that is less common in most 8-bit micros is the DMA controller. Fortunately, MSP430s possess this. Probably it is your first such micro family that is somewhere between 8-bit and 32-bit micros. In the end, MSP430s will surely give you a taste of absolute American technology and concepts.<\/p>\n<p style=\"text-align: justify\"><!--more--><\/p>\n<h1>The MSP430 Family<\/h1>\n<p>Shown below is the family tree of MSP430 series microcontrollers from TI.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/TI-MSP430-Family.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13633\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/TI-MSP430-Family-580x296.png\" alt=\"TI MSP430 Family\" width=\"580\" height=\"296\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/TI-MSP430-Family-580x296.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/TI-MSP430-Family-1024x522.png 1024w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/TI-MSP430-Family.png 1274w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p style=\"text-align: justify\">The most common MSP430 micros are the MSP430FR series, MSP430F series, MSP430G series and the newly introduced MSP432 series.<\/p>\n<p style=\"text-align: justify\">MSP430FR series micros mainly feature high reliability, high endurance, 10-year data retention non-volatile\u00a0<a href=\"https:\/\/en.wikipedia.org\/wiki\/Ferroelectric_RAM\">FRAM<\/a>\u00a0(ferroelectric random-access memory) memories. This series offer 16-bit solutions for ultra-low-power sensing and system management in areas like smart building management systems, smart grids, military and industrial designs. They feature the lowest standby power consumption of about 350 nA with RTC, 100 \u00b5A\/MHz active power consumption and the unique ability to save and instantly restore system state right after power failures.<\/p>\n<p style=\"text-align: justify\">MSP432 series micros are the perfect combinations of MSP430 low-power portfolio with advanced mixed-signal features and the high-performance processing capabilities of 32-bit ARM M4F engine. These micros have high measurement precisions and contain high performance peripherals like high resolution differential ADCs, DMA, IOT connectivity, etc. This series fills the gap between 16-bit MSP430s and 32-bit ARM architecture and as of this moment this series is the most recent development in the MSP430 family.<\/p>\n<p style=\"text-align: justify\">CC430 series is a small series of MSP430s with almost all features one can find in a typical MSP430 micro and embedded sub-GHz radio transceivers. They are well-stuffed true <strong><em>System-on-Chip (SOC)<\/em><\/strong> solutions that remove the necessity of additional off-board wireless solutions.<\/p>\n<p style=\"text-align: justify\">Yet another tiny series of MSP430 micros include MSP430Cxx and MSP430Lxx micros. These micros are intended for extreme low voltage (0.9 \u2013 1.65V) operations. With just one Nickel Cadmium (NiCd) battery it is possible to run any micro of this series. They feature low resolution analogue frontends and low pin counts.<\/p>\n<p style=\"text-align: justify\">The only series that is left to be discussed is the general purpose MSP430s series microcontrollers. By general purpose, it is meant that these micros can be use in almost any scenario. It is this series of micros that we will be dealing here. The table below summarizes this family:<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/General-Purpose-MSP430s.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13635\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/General-Purpose-MSP430s-580x397.png\" alt=\"General Purpose MSP430s\" width=\"580\" height=\"397\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/General-Purpose-MSP430s-580x397.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/General-Purpose-MSP430s.png 840w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p style=\"text-align: justify\">For now, don\u2019t struggle to understand the abbreviations of this table. You\u2019ll eventually know about them as we proceed.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/MSP430Fxxx-and-MSP430Gxxx.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13634\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/MSP430Fxxx-and-MSP430Gxxx-580x548.png\" alt=\"MSP430Fxxx and MSP430Gxxx\" width=\"580\" height=\"548\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/MSP430Fxxx-and-MSP430Gxxx-580x548.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/MSP430Fxxx-and-MSP430Gxxx.png 605w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p style=\"text-align: justify\">Note that there are some other MSP430 devices that are not accounted here. These devices are either rare or subset of the mentioned families. If you want to know more about MSP430 microcontrollers then you can go through the following Wikipedia article: <a href=\"https:\/\/en.wikipedia.org\/wiki\/TI_MSP430\">https:\/\/en.wikipedia.org\/wiki\/TI_MSP430<\/a>.<\/p>\n<p style=\"text-align: justify\"><!--more--><\/p>\n<h1>Launchpad Boards and BoosterPacks<\/h1>\n<p style=\"text-align: justify\">Launchpad boards are affordable MSP430 evaluation kits. For just few dollars, you can have your own Launchpad boards. They are so cheap that an average school-going student can afford one with his\/her own pocket money. However, this cheapness doesn\u2019t compromise quality nor performance. They are more-or-less alike Arduino boards in terms of board size and on-board resources except they don\u2019t share same pin naming conventions and board layouts. Well that\u2019s not a big issue. However, it would have been better if the Launchpads shared Arduino-like form factor. This would have enabled using Arduino shields with Launchpads. TI has, however, its own brand of shields called <strong><em>BoosterPacks<\/em><\/strong> and they seem to like promoting their own idea, owing to which there\u2019s still no official TI Launchpad that share exactly Arduino form-factor\/shape. It is a very aggressive marketing\u00a0boldness. Shown below is the <strong><em>BOOSTXL-EDUMKII BoosterPack<\/em><\/strong>. It is just like Arduino Esplora with lots of on-board sensors and devices except for the MCU. It is good for game development and sensor applications. There are other useful BoosterPacks dedicated for capacitive touch, displays and so on.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/BoosterPack.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-13637\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/BoosterPack.png\" alt=\"BoosterPack\" width=\"480\" height=\"224\" \/><\/a><\/p>\n<p style=\"text-align: justify\">Launchpad boards are not the only development boards offered by TI. There are many other dev boards too. Shown below is the TI MSP-EXP430F5438 Bluetooth platform development board.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/MSP430-Bluetooth-Development-Platform.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-13639\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/MSP430-Bluetooth-Development-Platform.png\" alt=\"MSP430 Bluetooth Development Platform\" width=\"459\" height=\"332\" \/><\/a><\/p>\n<p style=\"text-align: justify\">However due to cheapness and relatively good availability Launchpad boards are by far most popular, particularly the <strong><em>MSP-EXP430G2 Experimenter\u2019s Launchpad<\/em><\/strong> board.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Launchpad-Box.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-13638\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Launchpad-Box.png\" alt=\"Launchpad Box\" width=\"453\" height=\"338\" \/><\/a><\/p>\n<p style=\"text-align: justify\">This board comes boxed with two micros \u2013 <strong><em>MSP430G2452<\/em><\/strong> and <strong><em>MSP430G2553<\/em><\/strong>. Both of these mixed-signal MCUs come in PDIP packages. We can take them off from the board and use them in bread boards, strip boards, PCBs, etc. Shown below is the layout of a MSP-EXP430G2 Launchpad board.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Launchpad-Board-Layout.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-13640\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Launchpad-Board-Layout.png\" alt=\"Launchpad Board Layout\" width=\"536\" height=\"375\" \/><\/a><\/p>\n<p style=\"text-align: justify\">Just like any other evaluation kit, every Launchpad comes embedded with user LEDs, buttons, I\/O port headers (also known as BoosterPack connectors), onboard power supply, USB-to-serial converter and programming interfaces.<\/p>\n<p style=\"text-align: justify\">MSP430s are ultra-low power 16-bit general purpose microcontrollers. A MSP430 micro consists of a 16-bit RISC CPU, wide variety of feature-rich peripherals and a flexible clock system all under the hood of a <em>von-Neumann<\/em> architecture. Because of their ultra-low energy consumption profile, MSP430s are well-suited for battery\/solar powered\/limited or renewable energy applications.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/MSP430-Internal-Architecture.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13642\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/MSP430-Internal-Architecture-580x325.png\" alt=\"MSP430 Internal Architecture\" width=\"580\" height=\"325\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/MSP430-Internal-Architecture-580x325.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/MSP430-Internal-Architecture-600x337.png 600w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/MSP430-Internal-Architecture.png 602w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p style=\"text-align: justify\">MSP430G2452 and MSP430G2553 are both almost identical in terms of hardware peripherals and pin count. MSP430G2553 has some additional hardware features like more RAM-ROM memories, timers and USCI-based hardware interfaces for LIN and IrDA communications.<a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/MSP430-Value-Line-Chips.png\"><br \/>\n<\/a><\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/MSP430-Value-Line-Chips.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-13641\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/MSP430-Value-Line-Chips.png\" alt=\"MSP430 Value-Line Chips\" width=\"360\" height=\"226\" \/><\/a><\/p>\n<p style=\"text-align: justify\">Shown below is a comparison table of some common MSP430 chips usually found with Launchpad boards.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Comparison-of-Various-MSP430-Value-Line-Devices.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-13644\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Comparison-of-Various-MSP430-Value-Line-Devices.png\" alt=\"Comparison of Various MSP430 Value Line Devices\" width=\"557\" height=\"315\" \/><\/a><\/p>\n<p style=\"text-align: justify\">Other Launchpad boards contain other chips but fortunately they share the same pin layout and so they are fully pin compatible. The fourteen pin parts and twenty pin parts share the same pin layout as shown below:<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/MSP430-Pin-Compatibility.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-13643\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/MSP430-Pin-Compatibility.png\" alt=\"MSP430 Pin Compatibility\" width=\"335\" height=\"197\" \/><\/a><\/p>\n<p style=\"text-align: justify\">Though the pin names are labelled properly on the silkscreens of the boards, it is sometimes necessary to check the schematic for details. Shown below is the basic schematic of a MSP-EXP430G2 Launchpad.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/MSP430G2-Launchpad-Basic-Schematic.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13645\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/MSP430G2-Launchpad-Basic-Schematic-580x371.png\" alt=\"MSP430G2 Launchpad Basic Schematic\" width=\"580\" height=\"371\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/MSP430G2-Launchpad-Basic-Schematic-580x371.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/MSP430G2-Launchpad-Basic-Schematic.png 892w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p style=\"text-align: justify\">From the schematic, we can see what has been placed on the board by default. Parts labelled<strong><em> DNP<\/em><\/strong> are not placed on a fresh Launchpad board. These have been left for the users.<\/p>\n<p style=\"text-align: justify\">If these don\u2019t matter much and you need something simpler, then there are Arduino-like pin maps for Launchpads. Though they are made for Energia IDE, they are useful for quick overviews.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/MSP430G2553-Pinmap.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13647\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/MSP430G2553-Pinmap-580x355.png\" alt=\"MSP430G2553 Pinmap\" width=\"580\" height=\"355\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/MSP430G2553-Pinmap-580x355.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/MSP430G2553-Pinmap.png 602w\" 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\/2017\/08\/MSP430G2452-Pinmap.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13646\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/MSP430G2452-Pinmap-580x348.png\" alt=\"MSP430G2452 Pinmap\" width=\"580\" height=\"348\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/MSP430G2452-Pinmap-580x348.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/MSP430G2452-Pinmap-600x360.png 600w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/MSP430G2452-Pinmap.png 602w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><br \/>\n<!--more--><\/p>\n<h1>Hardware<\/h1>\n<p style=\"text-align: justify\">We will obviously need a MSP430 Launchpad board. For this tutorial, I used MSP-EXP430G2 Launchpad board with MSP430G2553 and MSP430G2452 microcontrollers.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/VLD-Launchpad-Board.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-13649\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/VLD-Launchpad-Board.jpg\" alt=\"VLD Launchpad Board\" width=\"381\" height=\"283\" \/><\/a><\/p>\n<p style=\"text-align: justify\">Apart from the Launchpad board, we will need basic tools like a digital multimeter (DMM), wires or jumper cables, a power bank and other stuffs typically available in an Arduino starter kit like the <a href=\"http:\/\/riaspire.com\/\">RIASpire<\/a> one shown below.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/RIAspire-Arduino-Starter-Kit.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13650\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/RIAspire-Arduino-Starter-Kit-580x339.jpg\" alt=\"RIAspire Arduino Starter Kit\" width=\"580\" height=\"339\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/RIAspire-Arduino-Starter-Kit-580x339.jpg 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/RIAspire-Arduino-Starter-Kit.jpg 590w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p style=\"text-align: justify\">An additional external or off board programmer\/debugger is not needed since MSP430 Launchpad boards come with on board <strong><em>Spy-Bi-Wire (SBW)<\/em><\/strong> programming\/debugging interface. This interface utilizes pins labelled <strong><em>TEST <\/em><\/strong>and <strong><em>RST<\/em><\/strong> apart from power pins and so only four wires are needed. Note reset pin should be externally pulled up. Check the diagram below.<\/p>\n<p style=\"text-align: justify\">Similarly, we don\u2019t need to buy\/use any external USB-serial converter for serial communication as the boards come with onboard hardware for this communication.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Spy-Bi-Wire-Interface.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-13651\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Spy-Bi-Wire-Interface.png\" alt=\"Spy-Bi-Wire Interface\" width=\"554\" height=\"485\" \/><\/a><\/p>\n<p style=\"text-align: justify\">In all Launchpad boards, there are headers with jumpers as shown below to separate onboard programmer from the target. This allows us to use a Launchpad board as standalone programmer. We can also detach it. The top dotted line marks the border between the target and the programmer.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/SWI.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13655\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/SWI-580x444.jpg\" alt=\"SWI\" width=\"580\" height=\"444\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/SWI-580x444.jpg 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/SWI.jpg 627w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p style=\"text-align: justify\">It is still nice to have one external MSP-FET programmer. <strong><em>FET<\/em><\/strong> stands for Flash Emulation Tool and it supports both JTAG and SWD interfaces. FETs are pretty expensive tools.<a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/MSP-FET-Programmer.png\"><br \/>\n<\/a><\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/MSP-FET-Programmer.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13654\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/MSP-FET-Programmer-580x331.png\" alt=\"MSP-FET Programmer\" width=\"580\" height=\"331\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/MSP-FET-Programmer-580x331.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/MSP-FET-Programmer.png 584w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p style=\"text-align: justify\">Lastly, I strongly recommend having an oscilloscope or a logic analyser for checking signals and timing-related info. In many cases, informations provided by these tools become extremely necessary.<a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Logic-Analyzer.png\"><br \/>\n<\/a><\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Logic-Analyzer.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-13653\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Logic-Analyzer.png\" alt=\"Logic Analyzer\" width=\"480\" height=\"370\" \/><\/a><\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Analyzer-and-Oscilloscope.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13652\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Analyzer-and-Oscilloscope-580x310.png\" alt=\"Analyzer and Oscilloscope\" width=\"580\" height=\"310\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Analyzer-and-Oscilloscope-580x310.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Analyzer-and-Oscilloscope-1024x547.png 1024w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Analyzer-and-Oscilloscope.png 1364w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p><!--more--><\/p>\n<h1>Software<\/h1>\n<p style=\"text-align: justify\">There are numerous ways of learning and using a new microcontroller family effectively. A number of C\/C++ compilers are available for coding MSP430 micros. Hobbyists and novice users find Arduino-like solutions easy and quick but from an engineer\u2019s perspective such solutions are inexplicably incapable of extracting the sweet fruits of a well-armed microcontroller. Rawer approaches are preferred by professionals but they too seek reduced efforts and quick solutions. The learning curve is also needed to be a smooth one. In this segment, we will checkout some common software solutions for MSP430s.<\/p>\n<p style=\"text-align: justify\">Firstly, there is the free open-source <strong><em>Energia <\/em><\/strong>IDE. This is an Arduino-like IDE that enables users to code MSP430s in the Arduino way. It supports many Launchpad boards including those which are based on ARM cores. I have used it a lot and it is fun using it for simple hobby projects. However, I wanted to harness the true power of MSP430s. As with Arduino, you can access MSP430 registers in Energia too but that doesn\u2019t make significant differences in terms of coding efficiency and memory consumptions. Energia has the same issues as with Arduino. Arduino framework on top of an AVR makes it much less robust when compared to a crude AVR. The same thing applies for Energia too. Another key limitation of Energia is the fact that not all MSP430 chips are supported by it. Energia is, however, very easy to use, quick and useful for rapid prototyping or testing. The costs are low overall efficiency and larger code size. Just like Arduino, Energia is not well-suited for highly sophisticate professional projects. It is just a rapid prototyping tool that we can use to check a proof-of-concept but not the right tool to build that concept. A smaller hammer is useful for nailing a pin but it is not the perfect tool for breaking a giant boulder.<\/p>\n<p style=\"text-align: justify\">Energia is available at <a href=\"http:\/\/energia.nu\/\">http:\/\/energia.nu\/<\/a>.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Energia-IDE.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13659\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Energia-IDE-580x308.png\" alt=\"Energia IDE\" width=\"580\" height=\"308\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Energia-IDE-580x308.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Energia-IDE-600x320.png 600w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Energia-IDE.png 602w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p style=\"text-align: justify\">Please note that Energia is not supported by the Arduino LCC which means it not developed or maintained by the guys from the Arduino team. This doesn\u2019t matter much for its users. However, the IDE is not frequently updated like the Arduino IDE.<\/p>\n<p style=\"text-align: justify\">Next, we have TI\u2019s own compiler \u2013 the <strong><em>Code Composer Studio (CCS)<\/em><\/strong>. CCS is a C\/C++ compiler based on Eclipse IDE. CCS comes with TI\u2019s proprietary compilers that are best in code optimization. Those who have used Eclipse-based IDEs before know the advantages Eclipse framework brings with it. It has an excellent code navigation system, perspective views, refactoring, etc. CCS compiler comes with all of these stuffs and many more like debugger interface and TI App Store. Apart from all these there are some helpful cloud-based tools from TI. This tutorial is based on CCS compiler. It is free for download from TI\u2019s website &#8211; <a href=\"http:\/\/www.ti.com\/tool\/ccstudio\">http:\/\/www.ti.com\/tool\/ccstudio<\/a>. Make sure you have a TI account.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/CCS-IDE.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13660\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/CCS-IDE-580x308.png\" alt=\"CCS IDE\" width=\"580\" height=\"308\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/CCS-IDE-580x308.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/CCS-IDE-600x320.png 600w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/CCS-IDE.png 602w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p style=\"text-align: justify\">Then there is IAR compiler. I have never used IAR products but I have heard from many that it is very popular and widely used. However, it is expensive too.<\/p>\n<p style=\"text-align: justify\">Lastly, like for many other platforms we have the free open-source GCC compiler for MSP430s. MSP430 GCC can be integrated with CCS IDE.<\/p>\n<p style=\"text-align: justify\">We will see during coding that compilers don\u2019t make significant differences in coding style, making codes cross-platform compatible. I also intend to keep things simple and quick. This is why I focus on tools rather than other stuffs. With right set of tools, any issue can be addressed decently and rapidly.<\/p>\n<p style=\"text-align: justify\">Additionally, some more software tools are needed for supportive purposes. <strong><em>Proteus VSM<\/em><\/strong> is a good interactive simulator cum PCB design software. Luckily it supports MSP430 micros. However, it is very expensive unless you are using a pirated version of it. Frankly speaking, I have never advocated for simulations because simulations do not address the real-world challenges we encounter in a real-life real-time project. Simulations, for example, cannot simulate real-world environment conditions nor can they emulate situations which result in \u201c<em>hang<\/em>\u201d-like stuck up conditions. Simulations don\u2019t take the effect of noise and electromagnetic disturbances into account. Additionally, sometimes simulations give wrong results. I have spent many wasted hours trying to debug an issue with simulation only to find out that the simulation was incorrect. Still however, simulations are helpful in some special cases. For instance, when designing a LCD menu, simulation is a time and effort saver. Personally, I recommend and use real-world debugging over simulations. This gives me a lot of confidence.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Proteus-VSM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13661\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Proteus-VSM-580x314.png\" alt=\"Proteus VSM\" width=\"580\" height=\"314\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Proteus-VSM-580x314.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Proteus-VSM-1024x554.png 1024w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Proteus-VSM.png 1918w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p style=\"text-align: justify\">The coolest stuffs for MSP430s are TI\u2019s<strong><em> MSP430Ware driver libraries (driverlib)<\/em><\/strong> and <strong><em>GRACE<\/em><\/strong>. Driver libraries remove the pain of traditionally coding MSP430s using registers. Driver libraries provide easy-to-use API functions for configuring MSP430 peripherals just like the Standard Peripheral Libraries (SPL) of STM8 micros. However, these libraries are only supported for the newest and resource-rich MSP430 microcontrollers like the MSP430F5529LP. This idea of driver libraries is quickly gaining mass popularity and is becoming standard day-by-day for all modern era micros. MSP430Ware can be downloaded for free from <a href=\"http:\/\/www.ti.com\/tool\/mspware\">http:\/\/www.ti.com\/tool\/mspware<\/a>.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/MSP430Ware.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13662\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/MSP430Ware-580x317.png\" alt=\"MSP430Ware\" width=\"580\" height=\"317\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/MSP430Ware-580x317.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/MSP430Ware-1024x559.png 1024w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/MSP430Ware.png 1916w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p style=\"text-align: justify\">You can use TI\u2019s Resource Explorer to check out what\u2019s in <strong><em>driverlib <\/em><\/strong>and if your target MCU is supported.<\/p>\n<p style=\"text-align: justify\"><strong>GRACE,<\/strong> on the other hand, is intended for relatively less-resourceful micros like the MSP430G2231 that are mostly well-suited for lower level assembly language environments. GRACE is basically a graphical code generator tool, much like the STM32CubeMX that can be used to generate setup configuration codes for MSP430F2xx, MSP430G2xx and some FR series micros. However, GRACE only generates register values for small MSP430 micros. For large and advanced MSP430 micros driverlib-based codes are generated instead of register-level codes. The rest is just like coding any other microcontroller. Throughout this tutorial, GRACE has been used for all demos.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Grace.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13663\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Grace-580x315.png\" alt=\"Grace\" width=\"580\" height=\"315\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Grace-580x315.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Grace-1024x557.png 1024w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Grace.png 1916w\" 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\/2017\/08\/Configuring-BCS-with-Grace.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13665\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Configuring-BCS-with-Grace-580x632.png\" alt=\"Configuring BCS+ with Grace\" width=\"580\" height=\"632\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Configuring-BCS-with-Grace-580x632.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Configuring-BCS-with-Grace.png 774w\" 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\/2017\/08\/Grace-Generated-Code.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13664\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Grace-Generated-Code-453x650.png\" alt=\"Grace Generated Code\" width=\"453\" height=\"650\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Grace-Generated-Code-453x650.png 453w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Grace-Generated-Code.png 657w\" sizes=\"auto, (max-width: 453px) 100vw, 453px\" \/><\/a><\/p>\n<p style=\"text-align: justify\">As we can see the generated code snippet sets appropriate registers as per our setup in GRACE GUI.<\/p>\n<p style=\"text-align: left\">GRACE can be downloaded without any charge from TI\u2019s website: <a href=\"http:\/\/www.ti.com\/tool\/GRACE?keyMatch=grace%203&amp;tisearch=Search-EN-Products\">http:\/\/www.ti.com\/tool\/GRACE?keyMatch=grace%203&amp;tisearch=Search-EN-Products<\/a>.<\/p>\n<p style=\"text-align: justify\">We will also need a separate standalone programmer GUI tool. Why? Because it looks totally stupid to open the heavy CCS software every time to upload a code to a new target after having built the final code for it. For this purpose, we will need <strong><em>UniFlash<\/em><\/strong> programmer GUI.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/UniFlash-GUI.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13666\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/UniFlash-GUI-580x312.png\" alt=\"UniFlash GUI\" width=\"580\" height=\"312\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/UniFlash-GUI-580x312.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/UniFlash-GUI-1024x551.png 1024w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/UniFlash-GUI.png 1918w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p>UniFlash can be downloaded from <a href=\"http:\/\/www.ti.com\/tool\/uniflash\">http:\/\/www.ti.com\/tool\/uniflash<\/a> or can be accessed via TI cloud.<\/p>\n<p style=\"text-align: justify\">Additionally, I recommend using <strong><em>Sublime Text<\/em><\/strong> (<a href=\"https:\/\/www.sublimetext.com\/3\">https:\/\/www.sublimetext.com\/3<\/a>) or <strong><em>Notepad++<\/em><\/strong> (<a href=\"https:\/\/notepad-plus-plus.org\/download\/v7.4.2.html\">https:\/\/notepad-plus-plus.org\/download\/v7.4.2.html<\/a>) as a code viewer\/editor.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Sublime-Text.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13667\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Sublime-Text-580x334.png\" alt=\"Sublime Text\" width=\"580\" height=\"334\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Sublime-Text-580x334.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Sublime-Text.png 660w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p style=\"text-align: justify\">Make sure you also downloaded Launchpad board drivers from here: <a href=\"http:\/\/www.ti.com\/lit\/sw\/slac524\/slac524.zip\">http:\/\/www.ti.com\/lit\/sw\/slac524\/slac524.zip<\/a>.<\/p>\n<p style=\"text-align: justify\"><!--more--><\/p>\n<h1>Documents, Pages and Forums<\/h1>\n<p style=\"text-align: justify\">The following documents must be acquired:<\/p>\n<ul>\n<li style=\"text-align: justify\"><strong><em>Device Datasheet<br \/>\n<\/em><\/strong>This write up covers MSP430G2452 and MSP430G2553 microcontrollers and so we will be needing the datasheet of these microcontroller for technical specs and characteristics. These can be found in the following links:<br \/>\n<a href=\"http:\/\/www.ti.com\/lit\/ds\/symlink\/msp430g2452.pdf\">http:\/\/www.ti.com\/lit\/ds\/symlink\/msp430g2452.pdf<br \/>\n<\/a><a href=\"http:\/\/www.ti.com\/lit\/ds\/symlink\/msp430g2553.pdf\">http:\/\/www.ti.com\/lit\/ds\/symlink\/msp430g2553.pdf<br \/>\n<\/a>If you are using some other microcontroller then you must acquire its datasheet first.<\/li>\n<\/ul>\n<ul>\n<li style=\"text-align: justify\"><strong><em>Reference Manual<br \/>\n<\/em><\/strong>MSP430x2xx reference manual covers the details of all the hardware available in this family of microcontrollers. Unlike other microcontrollers, datasheet of a MSP430 micro, only says about technical specs and characteristics. Reference manuals say about internal hardware, how to use them and about internal registers. This is the most important document of all.<br \/>\n<a href=\"http:\/\/www.ti.com\/lit\/ug\/slau144j\/slau144j.pdf\">http:\/\/www.ti.com\/lit\/ug\/slau144j\/slau144j.pdf<\/a><strong><em>\u00a0<\/em><\/strong><\/li>\n<\/ul>\n<ul>\n<li style=\"text-align: justify\"><strong><em>Launchpad User Manual and Associated Files<br \/>\n<\/em><\/strong>Visit the following link for Launchpad board user manual and other docs:<br \/>\n<a href=\"http:\/\/www.ti.com\/tool\/msp-exp430g2\">http:\/\/www.ti.com\/tool\/msp-exp430g2<\/a>. This document not just introduces the Launchpad board, it also contains schematics, layouts and other stuffs. Off all the stuffs in the user manual, Launchpad board\u2019s schematic is the most valuable thing.<\/li>\n<\/ul>\n<ul>\n<li style=\"text-align: justify\"><strong><em>App Notes.<br \/>\n<\/em><\/strong>Though not mandatory, having a collection of MSP430 application notes is a surplus. These show various ideas and design concepts. Visit TI\u2019s website for these docs.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">There are some important websites, online communities and forums that are very helpful. Some of the most popular ones are:<\/p>\n<p style=\"text-align: justify\"><a href=\"http:\/\/www.43oh.com\/\">http:\/\/www.43oh.com\/<\/a><\/p>\n<p style=\"text-align: justify\"><a href=\"http:\/\/forum.43oh.com\/\">http:\/\/forum.43oh.com\/<\/a><\/p>\n<p style=\"text-align: justify\"><a href=\"https:\/\/e2e.ti.com\/support\/microcontrollers\/msp430\/\">https:\/\/e2e.ti.com\/support\/microcontrollers\/msp430\/<\/a><\/p>\n<p style=\"text-align: justify\"><a href=\"http:\/\/processors.wiki.ti.com\/index.php\/Main_Page\">http:\/\/processors.wiki.ti.com\/index.php\/Main_Page<\/a><\/p>\n<p style=\"text-align: justify\"><a href=\"http:\/\/www.ti.com\/lsds\/ti\/microcontrollers-16-bit-32-bit\/msp\/overview.page\">http:\/\/www.ti.com\/lsds\/ti\/microcontrollers-16-bit-32-bit\/msp\/overview.page<\/a><\/p>\n<p style=\"text-align: justify\"><a href=\"http:\/\/www.ti.com\/lsds\/ti\/tools-software\/launchpads\/overview\/overview.page\">http:\/\/www.ti.com\/lsds\/ti\/tools-software\/launchpads\/overview\/overview.page<\/a><!--more--><\/p>\n<h1>Starting a New CCS\u00a0Project<\/h1>\n<p style=\"text-align: justify\">Beginning a new CCS project is not too complicated. Provided that CCS is installed in your PC, simply run it.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/CCS-Icon.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-13674\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/CCS-Icon.png\" alt=\"CCS Icon\" width=\"104\" height=\"104\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/CCS-Icon.png 104w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/CCS-Icon-90x90.png 90w\" sizes=\"auto, (max-width: 104px) 100vw, 104px\" \/><\/a><\/p>\n<p>In just a few seconds, CCS\u2019s logo splashes.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Logo.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13679\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Logo-580x337.png\" alt=\"CCS Logo\" width=\"580\" height=\"337\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Logo-580x337.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Logo.png 597w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p style=\"text-align: justify\">You\u2019ll be asked for workspace location. Either select an existing workspace if you have one or create a new one.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Workspace-Selection.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13683\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Workspace-Selection-580x306.png\" alt=\"Workspace Selection\" width=\"580\" height=\"306\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Workspace-Selection-580x306.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Workspace-Selection.png 636w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p>Next click <strong><em>File &gt;&gt; New &gt;&gt; CCS Project<\/em><\/strong>.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/CCS-Project-Selection.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13676\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/CCS-Project-Selection-580x303.png\" alt=\"CCS Project Selection\" width=\"580\" height=\"303\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/CCS-Project-Selection-580x303.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/CCS-Project-Selection.png 624w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p>The following window will show up then:<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Project-Parameter-Setup.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13681\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Project-Parameter-Setup-538x650.png\" alt=\"Project Parameter Setup\" width=\"538\" height=\"650\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Project-Parameter-Setup-538x650.png 538w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Project-Parameter-Setup.png 593w\" sizes=\"auto, (max-width: 538px) 100vw, 538px\" \/><\/a><\/p>\n<p style=\"text-align: justify\">Here we just need to setup target MCU, name of the project, compiler version and project type. Keep other options unchanged unless you are sure of your actions.<\/p>\n<p style=\"text-align: justify\">The following window appears after hitting the finish and we are good to go for coding. It is just that simple.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Project-Editor.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13680\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Project-Editor-580x309.png\" alt=\"Project Editor\" width=\"580\" height=\"309\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Project-Editor-580x309.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Project-Editor-1024x546.png 1024w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Project-Editor.png 1362w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p style=\"text-align: justify\">One advice I would like to give here, never delete any workspace file or folder unless you created it. It is possible to rename and remove projects from CCS IDE.<\/p>\n<p style=\"text-align: justify\"><!--more--><\/p>\n<h1>GRACE<\/h1>\n<p>As stated earlier, GRACE is a graphical configuration tool. It reduces the effort of thoroughly reading datasheets and reference manuals. I highly recommend using it no matter if you are novice or expert.<\/p>\n<p>First run GRACE.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Grace-Icon.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-13687\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Grace-Icon.png\" alt=\"Grace Icon\" width=\"104\" height=\"104\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Grace-Icon.png 104w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Grace-Icon-90x90.png 90w\" sizes=\"auto, (max-width: 104px) 100vw, 104px\" \/><\/a><\/p>\n<p>Wait for it to launch.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Grace-Welcome-Screen.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13691\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Grace-Welcome-Screen-580x315.png\" alt=\"Grace Welcome Screen\" width=\"580\" height=\"315\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Grace-Welcome-Screen-580x315.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Grace-Welcome-Screen-1024x556.png 1024w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Grace-Welcome-Screen.png 1920w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p style=\"text-align: justify\">TI\u2019s Resource Explorer kicks in on first start up. It may not do so every time though.<\/p>\n<p>Click <strong><em>File &gt;&gt; New<\/em><\/strong> to begin a new GRACE project.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/New-Grace-Project.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13693\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/New-Grace-Project-580x282.png\" alt=\"New Grace Project\" width=\"580\" height=\"282\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/New-Grace-Project-580x282.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/New-Grace-Project.png 924w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p style=\"text-align: justify\">You\u2019ll be prompted for project location, MCU part number and project name as shown below:<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Grace-Project-Properties.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13690\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Grace-Project-Properties-580x318.png\" alt=\"Grace Project Properties\" width=\"580\" height=\"318\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Grace-Project-Properties-580x318.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Grace-Project-Properties.png 589w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p style=\"text-align: justify\">Once all the parameter fields are filled, we are ready to configure our target MCU. A welcome screen shows up next.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Grace-Project-Overview.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13689\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Grace-Project-Overview-580x315.png\" alt=\"Grace Project Overview\" width=\"580\" height=\"315\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Grace-Project-Overview-580x315.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Grace-Project-Overview-1024x557.png 1024w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Grace-Project-Overview.png 1920w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p style=\"text-align: justify\">From the welcome screen, we have to click on <strong><em>Device Overview<\/em><\/strong> and get an insight of the device\u2019s peripherals as peripheral blocks.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Grace1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13692\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Grace1-580x315.png\" alt=\"Grace\" width=\"580\" height=\"315\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Grace1-580x315.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Grace1-1024x557.png 1024w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Grace1.png 1916w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p style=\"text-align: justify\">We can now click desired hardware peripheral block (blue blocks) to check what features and code examples it offers and set it up as required. There are two types of setups for all modules &#8211; <strong><em>Basic<\/em><\/strong> and <strong><em>Power<\/em><\/strong> users. <strong><em>Basic User<\/em><\/strong> setup is for simple setup when you don\u2019t know everything of a peripheral in details and don\u2019t want to mess things up. <strong><em>Power User<\/em><\/strong> setup is for expert users with more advanced options. Shown below are the Power User options for setting up the clock system:<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Configuring-BCS-with-Grace1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13684\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Configuring-BCS-with-Grace1-580x632.png\" alt=\"Configuring BCS+ with Grace\" width=\"580\" height=\"632\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Configuring-BCS-with-Grace1-580x632.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Configuring-BCS-with-Grace1.png 774w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p style=\"text-align: justify\">After setting up everything, just hit the hammer or <strong><em>Build<\/em><\/strong> button and the configuration codes are generated in the preset folder. It may take some time to complete the process. Next, we just need to open the generated files and copy them in our main CCS code.<\/p>\n<p>How to create a new CCS project and use Grace is well documented in this video: <a href=\"https:\/\/www.youtube.com\/watch?v=QCYMbsKwRfY\">https:\/\/www.youtube.com\/watch?v=QCYMbsKwRfY<\/a>.<\/p>\n<p style=\"text-align: justify\"><!--more--><\/p>\n<h1>UniFlash<\/h1>\n<p style=\"text-align: justify\">Most of the times during development, a separate standalone programmer software is not a compulsory necessity as CCS IDE provides an inbuilt programmer\/debugger interface. However, at times long after final application development, it becomes completely unnecessary to reopen CCS just to load a firmware to a new micro. UniFlash comes in aid at that time.<\/p>\n<p style=\"text-align: justify\">Run Uniflash by clicking its icon.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/UniFlash-Icon.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-13695\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/UniFlash-Icon.png\" alt=\"UniFlash Icon\" width=\"104\" height=\"105\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/UniFlash-Icon.png 104w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/UniFlash-Icon-90x90.png 90w\" sizes=\"auto, (max-width: 104px) 100vw, 104px\" \/><\/a><\/p>\n<p>\u00a0The following window appears:<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/UniFlash-GUI1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13701\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/UniFlash-GUI1-580x312.png\" alt=\"UniFlash GUI\" width=\"580\" height=\"312\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/UniFlash-GUI1-580x312.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/UniFlash-GUI1-1024x551.png 1024w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/UniFlash-GUI1.png 1918w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p style=\"text-align: justify\">From here we need to select either target board or target microcontroller. No need to type the whole part number of a micro, just few digits\/letters are enough to find the correct micro:<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/UniFlash-Chip-Selection.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13698\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/UniFlash-Chip-Selection-580x231.png\" alt=\"UniFlash Chip Selection\" width=\"580\" height=\"231\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/UniFlash-Chip-Selection-580x231.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/UniFlash-Chip-Selection-1024x408.png 1024w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/UniFlash-Chip-Selection.png 1339w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p style=\"text-align: justify\">Similarly, we need to setup connection to target too. <strong><em>USB1<\/em><\/strong> &#8211; the first option is what we need to select.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/UniFlash-Connection-Selction.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13699\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/UniFlash-Connection-Selction-580x242.png\" alt=\"UniFlash Connection Selction\" width=\"580\" height=\"242\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/UniFlash-Connection-Selction-580x242.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/UniFlash-Connection-Selction-1024x427.png 1024w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/UniFlash-Connection-Selction.png 1297w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p style=\"text-align: justify\">Now we are ready to begin firmware upload. Click the <strong><em>Start<\/em><\/strong> button.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/UniFlash-Start-Button.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13696\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/UniFlash-Start-Button-580x189.png\" alt=\"UniFlash Start Button\" width=\"580\" height=\"189\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/UniFlash-Start-Button-580x189.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/UniFlash-Start-Button-1024x334.png 1024w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/UniFlash-Start-Button-860x280.png 860w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/UniFlash-Start-Button.png 1297w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p>Browse the firmware you wish to upload.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/UniFlash-File-Selection.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13700\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/UniFlash-File-Selection-580x315.png\" alt=\"UniFlash File Selection\" width=\"580\" height=\"315\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/UniFlash-File-Selection-580x315.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/UniFlash-File-Selection-1024x556.png 1024w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/UniFlash-File-Selection.png 1916w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p style=\"text-align: justify\">You can optionally set some more parameters for the target as shown:<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/UniFlash-Additional-Properties.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13697\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/UniFlash-Additional-Properties-580x315.png\" alt=\"UniFlash Additional Properties\" width=\"580\" height=\"315\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/UniFlash-Additional-Properties-580x315.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/UniFlash-Additional-Properties-1024x556.png 1024w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/UniFlash-Additional-Properties.png 1916w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p>To flash new firmware, just hit the <strong><em>Load Image<\/em><\/strong> button.<\/p>\n<p>Watch this video for details: <a href=\"https:\/\/www.youtube.com\/watch?v=4uwQSSX-HrM\">https:\/\/www.youtube.com\/watch?v=4uwQSSX-HrM<\/a>.<\/p>\n<p><!--more--><\/p>\n<h1>Strategies and Tactics<\/h1>\n<p>Before we begin exploring MSP430 micros, I would like to discuss certain things though they may look like advanced stuffs for the moment.<\/p>\n<p>&nbsp;<\/p>\n<h2 style=\"text-align: justify\">Generating HEX Output Files<\/h2>\n<p style=\"text-align: justify\">By default, CCS doesn\u2019t generate any hex formatted output file. Everyone working in the embedded system sector is familiar with it. Hex outputs are useful for Proteus VSM simulations and loading code to a new MSP430 micro using an external programmer like UniFlash. Thus, it is essential if not imperative to unlock hex utility.<\/p>\n<p style=\"text-align: justify\">First go to <strong><em>Project &gt;&gt; Properties<\/em><\/strong>.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Project-Properties.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-13682\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Project-Properties.png\" alt=\"Project Properties\" width=\"528\" height=\"522\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Project-Properties.png 528w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Project-Properties-90x90.png 90w\" sizes=\"auto, (max-width: 528px) 100vw, 528px\" \/><\/a><\/p>\n<p style=\"text-align: justify\">Navigate to find <strong><em>MSP430 Hex Utility<\/em><\/strong> and then enable it as shown below.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Hex-Utility.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13678\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Hex-Utility-580x407.png\" alt=\"Hex Utility\" width=\"580\" height=\"407\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Hex-Utility-580x407.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Hex-Utility.png 873w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p>Finally select hex output format as shown:<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Hex-File-Format.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13677\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Hex-File-Format-580x408.png\" alt=\"Hex File Format\" width=\"580\" height=\"408\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Hex-File-Format-580x408.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Hex-File-Format.png 867w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p style=\"text-align: justify\">Select <strong><em>Intel<\/em><\/strong> hex format as it is the one widely used.<\/p>\n<p style=\"text-align: justify\">Having set as shown, from now on, whenever you build your current project, you\u2019ll get an output <strong><em>.hex<\/em><\/strong> file in the project\u2019s debug\/release folder depending on your project type selection.<\/p>\n<h2>Building New Libraries<\/h2>\n<p>Building new libraries is simple. All we need to do is to follow a few set of rules:<\/p>\n<ul>\n<li>For each module, there should be a separate header file and source file.<\/li>\n<li>Header file should contain definitions, variables, constants and function prototypes only.<\/li>\n<li>Header files should begin with the inclusion of MSP430 header file.<\/li>\n<li>Source files should include their respective header files and addition header files (if any).<\/li>\n<li>Source files should contain actual function codes only.<\/li>\n<li>Be aware of reserved keywords and constants.<\/li>\n<li>Global variables with same names should never be declared more than once.<\/li>\n<li>Empty functions and functions without prototypes must be avoided.<\/li>\n<li>Functions should have small and meaningful names.<\/li>\n<li>Be careful about case-senstivity, function type assignment and argument type of functions.<\/li>\n<li>Hierarchical order of library inclusion must be followed.<\/li>\n<\/ul>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Delay-Library.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13702\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Delay-Library-580x567.png\" alt=\"Delay Library\" width=\"580\" height=\"567\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Delay-Library-580x567.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Delay-Library.png 607w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<h2>Adding Custom Library Files<\/h2>\n<p style=\"text-align: justify\">Adding own developed libraries to a project is key requirement for any software developer. This is because no compiler includes libraries for all hardware. We must realize a compiler as a tool only. The rest is how we use it and what we do with it. As for example, CCS comes with <strong><em>_delay_cycles<\/em><\/strong> instead of more commonly preferred <strong><em>delay_ms<\/em><\/strong> or <strong><em>delay_us<\/em><\/strong> functions. We will, thus, need software delay library. We need to code it and include it in our projects.<\/p>\n<p style=\"text-align: justify\">Custom libraries can be included in two ways. The easiest way is to add the include and source files in your projects root location, i.e. its folder. No additional job is needed because the root contains the <strong><em>main.c<\/em><\/strong> source file and its location is automatically included when you start a project.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Root-Library-Location.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-13708\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Root-Library-Location.png\" alt=\"Root Library Location\" width=\"214\" height=\"218\" \/><\/a><\/p>\n<p style=\"text-align: justify\">However, the aforementioned method becomes clumsy and unprofessional when there are too many custom library files in a large project and if you care for some neatness. The other method needs some additional tasks to be done before we can use our library files.<\/p>\n<p style=\"text-align: justify\">First make a folder in your project directory and give it a name. For example, I named this folder <strong><em>Libraries<\/em><\/strong> in my examples.<\/p>\n<p style=\"text-align: justify\">Next add the desired library source and header files in this folder.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Library-Folder.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-13706\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Library-Folder.png\" alt=\"Library Folder\" width=\"194\" height=\"234\" \/><\/a><\/p>\n<p style=\"text-align: justify\">At this stage, we still cannot use these libraries because the compiler does not know their path(s) and so we need to inform the compiler about their location. We need to go to <strong><em>Project &gt;&gt; Properties<\/em><\/strong> first and then navigate to <strong><em>Include Options <\/em><\/strong>menu under <strong><em>Build &gt;&gt; MSP430 Compiler <\/em><\/strong>menu as shown:<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Library-Folder-Browse.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13704\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Library-Folder-Browse-580x406.png\" alt=\"Library Folder Browse\" width=\"580\" height=\"406\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Library-Folder-Browse-580x406.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Library-Folder-Browse.png 867w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p style=\"text-align: justify\">Right after clicking the circled icon as shown above, we will be asked for folder location. It is just a simple browsing to the library folder location.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Library-Folder-Inclusion.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13705\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Library-Folder-Inclusion-580x409.png\" alt=\"Library Folder Inclusion\" width=\"580\" height=\"409\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Library-Folder-Inclusion-580x409.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Library-Folder-Inclusion.png 867w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p style=\"text-align: justify\">To use the libraries we added, we now just need to add some <strong><em>#include<\/em><\/strong> statements in our<strong><em> main.c<\/em><\/strong> file.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Order-of-Include-Files-in-Main-Source-Code.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-13707\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Order-of-Include-Files-in-Main-Source-Code.png\" alt=\"Order of Include Files in Main Source Code\" width=\"189\" height=\"78\" \/><\/a><\/p>\n<p style=\"text-align: justify\">Be careful about the hierarchical order of library files because they may be interdependent. For example, as shown above, the LCD library has dependency on software delay library and so the delay library is added or called before the LCD library.<\/p>\n<h2>Using GRACE Simply but Effectively<\/h2>\n<p style=\"text-align: justify\">GRACE should be used for quick setups. Who would like to waste time fixing register values when we have such a useful tool at our side. We will need a code viewer\/editor like Sublime Text for viewing codes generated by GRACE. I like Sublime Text for its way of highlighting keywords and important stuffs with different colours. This helps in building quick situational awareness. A dark IDE is also good for night-time coding and less stressful for eyes.<\/p>\n<p style=\"text-align: justify\">Previously I showed how to use GRACE to generate configuration codes. GRACE generates individual source files for each hardware used. In this way, it doesn\u2019t create too much mess. We will open each of these files with Sublime Text and copy only the needed <strong><em>init<\/em><\/strong> functions in our main source code. GRACE also generates other stuffs but that are like mere junks to us and so we will just ignore them.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Important-Parts-in-Grace.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13703\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Important-Parts-in-Grace-514x650.png\" alt=\"Important Parts in Grace\" width=\"514\" height=\"650\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Important-Parts-in-Grace-514x650.png 514w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Important-Parts-in-Grace.png 542w\" sizes=\"auto, (max-width: 514px) 100vw, 514px\" \/><\/a><\/p>\n<p style=\"text-align: justify\">My code examples will give you an idea of what to copy. Keep in mind to stop the watchdog timer in the beginning of your code or it may reset your micro before entering actual application.<\/p>\n<h2>Optional Customizations<\/h2>\n<p style=\"text-align: justify\">Explore the IDE <strong><em>Preferences<\/em><\/strong> for customizing CCS IDE according to your wishes. For instance, unlike the default white theme, I like a full black IDE interface like the one in Sublime Text. This is really effective when you work at night and in low light conditions. Just like Sublime Text key words are highlighted and it is easy to navigate in such an environment.<\/p>\n<p style=\"text-align: justify\">In a busy world, we often forget to update software in regular schedules and therefore miss important changes and bug fixes. Automatic updates come in aid in such case. I configured my CCS in such a way that it auto updates and notifies me about new software versions. I also added some tools from CCS\/TI App Store like the GCC compiler.<\/p>\n<p style=\"text-align: justify\">Other customizations include <strong><em>MSP430Ware <\/em><\/strong>and <strong><em>EnergyTrace Technology<\/em><\/strong> debugger. <strong><em>EnergyTrace Technology <\/em><\/strong>allows us to compute energy consumptions. It helps in estimating battery requirements (if any).<\/p>\n<p style=\"text-align: justify\">I recommend readers to explore CCS IDE properties for more custom settings.<\/p>\n<h2>Advanced Concepts<\/h2>\n<p style=\"text-align: justify\">Most of the times during code compilation you\u2019ll notice that the compiler not only compiled your code but has also given you some optional advices. These advices aid in code optimizations and hint ways to enhance overall performance. For instance, it is better to use <strong><em>switch-case<\/em><\/strong> statements instead of <strong><em>if-else<\/em><\/strong> statements when dealing with fixed-discrete conditions. Try to follow the advices whenever possible. Same goes for compiler warnings. You must address the warnings for flawless coding.<\/p>\n<p style=\"text-align: justify\">In the internet, we can find lot of documents on C code optimization and good C language practices. Here is one such document from Atmel: <a href=\"http:\/\/www.atmel.com\/images\/doc8453.pdf\">http:\/\/www.atmel.com\/images\/doc8453.pdf<\/a>. Although it was written for Atmel AVR microcontrollers but the document applies for all microcontrollers and C compilers. Similarly, Microchip has documents named <strong><em>Tips \u2018n Tricks<\/em><\/strong>. Visit and search Microchip\u2019s site for these documents These tricks and tips help in designs significantly. Try to follow these to achieve best utilization of your micro\u2019s assets. TI\u2019s application notes are also equally helpful literatures. Personally, I recommend studying the app notes, and other documents of other micro families too. This will help advance in developing new concepts, strategies and techniques.<\/p>\n<p style=\"text-align: justify\">Additionally, I would like to point out some issues and techniques when using CCS. Here are followings:<\/p>\n<ul style=\"text-align: justify\">\n<li>Be careful about case sensitivity. Also, be careful about compiler\u2019s reserved keywords and constants.<\/li>\n<\/ul>\n<ul style=\"text-align: justify\">\n<li>Although not mandatory, it is, however, a good practice not to keep empty argument field in any function. Argumentless functions should have <em>void<\/em> argument instead of empty spaces.<\/li>\n<\/ul>\n<ul style=\"text-align: justify\">\n<li>Flags are important event markers and so wherever they are present and needed you must use and check them unless automatically cleared. For instance, it necessary to clear timer flags upon exiting timer interrupts.<\/li>\n<\/ul>\n<ul style=\"text-align: justify\">\n<li>Try to avoid polling methods. Try to use interrupt-based ones but make sure that there is no interrupt-within-interrupt case or otherwise your code may behave erratically. Best is to attach interrupts for important tasks like timing-related jobs, ADC conversions and communications. It is up to your design requirements and choices.<\/li>\n<\/ul>\n<ul style=\"text-align: justify\">\n<li>Where fast processing is required, try to mix assembly with your C-code if you can or temporarily speed up your micro by increasing its oscillator speed or switching to a faster clock source. Checkout the assembly examples from <strong><em>TI\u2019s Resource Explorer<\/em><\/strong> and <strong><em>MSP430Ware<\/em><\/strong>. Also try to study and learn about advanced C concepts like pointer, structures, functions, etc.<\/li>\n<\/ul>\n<ul style=\"text-align: justify\">\n<li>Avoid empty loops and blank conditional statements.<\/li>\n<\/ul>\n<ul style=\"text-align: justify\">\n<li>When you hover mouse cursor over a function, a small window appears. This window shows the internal coding of that function and relieve you from opening a new tab for it.<\/li>\n<\/ul>\n<ul style=\"text-align: justify\">\n<li><strong><em>CTRL + Space<\/em><\/strong> or code assist is a great helper. Likewise, CCS has some auto complete features.<\/li>\n<\/ul>\n<ul style=\"text-align: justify\">\n<li>If you are using multiple computers during your project\u2019s development stage, make sure that your custom library locations and workspace paths are properly added.<\/li>\n<\/ul>\n<ul>\n<li>Try to follow compiler advices and optimizations. Study the MSP430 header files if you can.<\/li>\n<li style=\"text-align: justify\">You can straight include the header file of your target MCU like as shown below if code cross compatibility among different MCUs of the same group like MSP430x2xx is not needed:\n<pre><span style=\"color: #0000ff\"><em><strong>#include<\/strong> &lt;msp430g2553.h&gt;<\/em><\/span><\/pre>\n<p>instead of:<\/p>\n<pre><span style=\"color: #0000ff\"><em><strong>#include<\/strong> &lt;msp430.h&gt;<\/em><\/span><\/pre>\n<p>&nbsp;<\/p>\n<p>The latter is universal for all MSP430 micros and should be used unless otherwise.<\/li>\n<li style=\"text-align: justify\">Bitwise and logic operations are useful. Not only they are fast, they just deal with the designated bits only. Although the MSP430 header files have efficient support for such operations, it is still better to know them. Shown below are some such common operations:<\/li>\n<\/ul>\n<pre><em><span style=\"color: #0000ff\">#define bit_set(reg, bit_val)\u00a0\u00a0 reg |= (1 &lt;&lt; bit_val)\u00a0\u00a0\u00a0 \/\/For setting a bit of a register\u00a0\u00a0<\/span><\/em><\/pre>\n<pre><em><span style=\"color: #0000ff\">#define bit_clr(reg, bit_val)\u00a0\u00a0 reg &amp;= (~(1 &lt;&lt; bit_val)) \/\/For clearing a bit of a register<\/span><\/em><\/pre>\n<pre><em><span style=\"color: #0000ff\">#define bit_tgl(reg, bit_val)\u00a0\u00a0 reg ^= (1 &lt;&lt; bit_val)\u00a0\u00a0\u00a0 \/\/For toggling a bit of a register<\/span><\/em><\/pre>\n<pre><em><span style=\"color: #0000ff\">#define get_bit(reg, bit_val)\u00a0\u00a0 (reg &amp; (1 &lt;&lt; bit_val))\u00a0\u00a0 \/\/For extracting the bit state of a register<\/span><\/em><\/pre>\n<pre><em><span style=\"color: #0000ff\">#define get_reg(reg, msk)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 (reg &amp; msk)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/For extracting masked bits of a register<\/span><\/em><\/pre>\n<p><!--more--><\/p>\n<h1>Basic Clock System Plus (BCS+)<\/h1>\n<p style=\"text-align: justify\">In all microcontrollers, power consumption and operating speed are interdependent and it is needed to balance these well to maximize overall performance while conserving energy. MSP430s are crafted with ultra-low power consumption feature in mind while not compromising performance. For this reason, MSP430s are equipped with a number of clock sources that vary in speed, accuracy and area of use. They also have clock dividers at various points apart from peripherals prescalers. This combination leads to a highly flexible clock system called <strong><em>Basic Clock System Plus<\/em><\/strong> <strong><em>(BCS+)<\/em><\/strong>.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Block-Diagram.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-13711\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Block-Diagram.png\" alt=\"Block Diagram\" width=\"542\" height=\"607\" \/><\/a><\/p>\n<p style=\"text-align: justify\">The block diagram for MSP420x2xx BCS+ module shown above highlights important components. It looks very sophisticated but if we divide it into important sections then it becomes simple to understand. Highlighted in green are clock sources and highlighted in purple are various clock signals that can be used for peripherals and the CPU. Now let\u2019s check BCS+ in short.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Clock-Sources.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13713\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Clock-Sources-580x386.png\" alt=\"Clock Sources\" width=\"580\" height=\"386\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Clock-Sources-580x386.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Clock-Sources.png 860w\" 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\/2017\/08\/Clock-Signals.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13712\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Clock-Signals-580x206.png\" alt=\"Clock Signals\" width=\"580\" height=\"206\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Clock-Signals-580x206.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Clock-Signals.png 858w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p style=\"text-align: justify\">After having a sneak-a-peek of the MSP430\u2019s clock system, we have to know some basic rules of using the BCS+ module of MSP430G2xx devices:<\/p>\n<ul>\n<li style=\"text-align: justify\">XT2CLK and LFXT1CLK high frequency mode are both unavailable. We can\u2019t use them.<\/li>\n<li style=\"text-align: justify\">DCOCLK is the most reliable clock source and should be used for both MCLK and SMCLK.<\/li>\n<li style=\"text-align: justify\">DCOCLK is dependent on VDD and so set VDD in GRACE before trying to setup BCS+.<\/li>\n<li style=\"text-align: justify\">Pre-calibrated DCOCLK values should be used as they offer good tolerance figures.<\/li>\n<li style=\"text-align: justify\">It is not wise to use custom DCOCLK values as accuracy issues surface.<\/li>\n<li style=\"text-align: justify\">If LFXT1CLK is used and needs precise timings, use a good clock crystal\/TCXO\/clock source.<\/li>\n<li style=\"text-align: justify\">Use proper capacitance value for LFXT1CLK when an external crystal is used.<\/li>\n<li style=\"text-align: justify\">Unused clock sources should be disabled to reduce power consumption.<\/li>\n<li style=\"text-align: justify\">Pins that connect with external clock sources should be set as inputs if they are not used.<\/li>\n<li style=\"text-align: justify\">Add a system start up delay of about 10 \u2013 100ms to allow stabilization of clock sources.<\/li>\n<li style=\"text-align: justify\">Clock outputs are available only in certain pins. If used, they are needed to be set accordingly.<\/li>\n<li style=\"text-align: justify\">Use oscillator fault interrupt if needed. This becomes extremely necessary to ensure fail-safe clock operation when external clock sources are used alongside internal clock sources.<\/li>\n<li style=\"text-align: justify\">For time sensitive hardware like timers, try to use a reliable clock source.<\/li>\n<li style=\"text-align: justify\">Check for warnings in GRACE.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h2>Code Example<\/h2>\n<pre><span style=\"color: #0000ff\"><strong>#include<\/strong> &lt;msp430.h&gt;<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>BCSplus_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>GPIO_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>WDTplus_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>System_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>main<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Stop watchdog timer from timing out during initial start-up. *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 WDTCTL = WDTPW | WDTHOLD;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 \u00a0\u00a0\/* initialize Config for the MSP430 GPIO *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>GPIO_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* initialize Config for the MSP430 2xx family clock systems (BCS) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>BCSplus_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* initialize Config for the MSP430 System Registers *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>System_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 \u00a0\u00a0\/* initialize Config for the MSP430 WDT+ *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>WDTplus_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>while<\/strong>(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\u00a0\u00a0\u00a0 P1OUT ^= BIT6;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 _delay_cycles(1);<\/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><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>BCSplus_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: BCSplus_graceInit_prologue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User initialization code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: BCSplus_graceInit_prologue) *\/<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Basic Clock System Control 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 * SELM_0 -- DCOCLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * DIVM_1 -- Divide by 2<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * SELS -- XT2CLK when XT2 oscillator present. LFXT1CLK or VLOCLK when XT2 oscillator not present<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * DIVS_3 -- Divide by 8<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~DCOR -- DCO uses internal resistor<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~DCOR indicates that DCOR has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 BCSCTL2 = SELM_0 | DIVM_1 | SELS | DIVS_3;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>if<\/strong> (CALBC1_1MHZ != 0xFF) {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/* Follow recommended flow. First, clear all DCOx and MODx bits. Then<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 * apply new RSELx values. Finally, apply new DCOx and MODx bit values.<\/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 DCOCTL = 0x00;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 BCSCTL1 = CALBC1_1MHZ;\u00a0\u00a0\u00a0\u00a0\u00a0 \/* Set DCO to 1MHz *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0DCOCTL = CALDCO_1MHZ;<\/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 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Basic Clock System Control 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 * XT2OFF -- Disable XT2CLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~XTS -- Low Frequency<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * DIVA_0 -- Divide by 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 * Note: ~XTS indicates that XTS has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 BCSCTL1 |= XT2OFF | DIVA_0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Basic Clock System Control 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 * XT2S_0 -- 0.4 - 1 MHz<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * LFXT1S_2 -- If XTS = 0, XT1 = VLOCLK ; If XTS = 1, XT1 = 3 - 16-MHz crystal or resonator<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * XCAP_1 -- ~6 pF<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 BCSCTL3 = XT2S_0 | LFXT1S_2 | XCAP_1;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: BCSplus_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: BCSplus_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>GPIO_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: GPIO_graceInit_prologue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User initialization code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: GPIO_graceInit_prologue) *\/<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Output Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1OUT = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Port Select Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1SEL = BIT0 | BIT4;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Direction Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1DIR = BIT0 | BIT4 | BIT6;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Interrupt Edge Select Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1IES = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Interrupt Flag Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1IFG = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Output Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2OUT = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Direction Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2DIR = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Interrupt Edge Select Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2IES = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Interrupt Flag Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2IFG = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 3 Output Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P3OUT = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 3 Direction Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P3DIR = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: GPIO_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: GPIO_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>WDTplus_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: RTC_B_graceInit_prologue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User initialization code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: RTC_B_graceInit_prologue) *\/<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * WDTCTL, Watchdog Timer+ Register<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * WDTPW -- Watchdog password<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * WDTHOLD -- Watchdog timer+ is stopped<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTNMIES -- NMI on rising edge<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTNMI -- Reset function<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTTMSEL -- Watchdog mode<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTCNTCL -- No action<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTSSEL -- SMCLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTIS0 -- Watchdog clock source bit0 disabled<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTIS1 -- Watchdog clock source bit1 disabled<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 WDTCTL = WDTPW | WDTHOLD;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: RTC_B_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: RTC_B_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>System_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: System_graceInit_prologue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User initialization code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: System_graceInit_prologue) *\/<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Clear oscillator fault flag with software delay *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>do<\/strong><\/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 \/\/ Clear OSC fault flag<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 IFG1 &amp;= ~OFIFG;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ 50us delay<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>__delay_cycles<\/strong>(25);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 } <strong>while<\/strong> (IFG1 &amp; OFIFG);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * SR, Status Register<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~SCG1 -- Disable System clock generator 1<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~SCG0 -- Disable System clock generator 0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~OSCOFF -- Oscillator On<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~CPUOFF -- CPU On<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * GIE -- General interrupt enable<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>__bis_SR_register<\/strong>(GIE);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: System_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: System_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<p>&nbsp;<\/p>\n<h2>Simulation<\/h2>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/BCSP-Simulation.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13717\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/BCSP-Simulation-580x260.png\" alt=\"BCSP Simulation\" width=\"580\" height=\"260\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/BCSP-Simulation-580x260.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/BCSP-Simulation.png 1023w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<h2>Explanation<\/h2>\n<p style=\"text-align: justify\">The demo example here is primarily used to extract SMCLK and ACLK signals. Here we just verified if these signals are as they are supposed to be. Using GRACE, we set MCLK 500 kHz, SMCLK 1500 Hz and ACLK 12 kHz. Clock outputs are obtained from respective pins as shown in the snap. The only thing additional here (not shown) is the P1.6 digital I\/O. In the code, this I\/O is toggled every one CPU cycle. It is not an indicator of CPU clock speed but just a test of I\/O toggling speed for. MCLK has no output associated with it and so we can\u2019t see its signal.<\/p>\n<p style=\"text-align: justify\">One major thing to note is the power supply voltage level. This is so because DCOCLK is dependent on VDD level. For low VDD voltages, high frequency oscillation generation is not possible. We have to remember that MSP430s are energy efficient devices and so there is always a trade-off between operating frequency and operating power consumption.<\/p>\n<p style=\"text-align: justify\">Another odd but important thing you may notice is the fact that the VDD value in MSP430 Launchpads is 3.6V instead of the more commonly used 3.3V. This is the maximum recommended VDD value although MSP430s can tolerate voltages up to 4.0V.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/GRACE-Settings.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13714\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/GRACE-Settings-580x411.png\" alt=\"GRACE Settings\" width=\"580\" height=\"411\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/GRACE-Settings-580x411.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/GRACE-Settings.png 715w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p style=\"text-align: justify\">Oscillators may show deviations in frequency due to changes in ambient temperature conditions. This in turn may affect communication and timing-related tasks.<\/p>\n<h2>Demo<\/h2>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/BCSP.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13718\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/BCSP-580x435.jpg\" alt=\"BCSP\" width=\"580\" height=\"435\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/BCSP-580x435.jpg 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/BCSP.jpg 590w\" 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\/2017\/08\/Saleae-Logic-Screenshot.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13715\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Saleae-Logic-Screenshot-580x363.png\" alt=\"Saleae Logic Screenshot\" width=\"580\" height=\"363\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Saleae-Logic-Screenshot-580x363.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Saleae-Logic-Screenshot-1024x640.png 1024w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Saleae-Logic-Screenshot.png 1120w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p>Demo video: <a href=\"https:\/\/www.youtube.com\/watch?v=gHDibVxfegU\">https:\/\/www.youtube.com\/watch?v=gHDibVxfegU<\/a>.<\/p>\n<p><!--more--><\/p>\n<h1>Digital I\/Os (DIO)<\/h1>\n<p style=\"text-align: justify\">MSP430s, just like any micro, have digital I\/Os for general-purpose input-output operations. The resources and features of MSP430 digital I\/Os are very rich and more or less comparable to a typical ARM microcontroller. All I\/O can be independently programmed. Many I\/Os have external interrupt feature. Another cool feature is the availability of both internal pull-up and pull-down resistors for all I\/Os and they can be individually and independently set. Additionally, many I\/Os have alternate roles for communication buses, clock, etc. However, the digital I\/Os are not 5V tolerant and we must be careful interfacing external devices with our MSP430 chips. I strongly recommend using some form of logic-level conversion circuitry in such cases.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Block-Diagram1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-13721\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Block-Diagram1.png\" alt=\"Block Diagram\" width=\"531\" height=\"571\" \/><\/a><\/p>\n<p style=\"text-align: justify\">Typically, there are four major components in a digital I\/O as highlighted in the block diagram above. The yellow region is responsible for setting I\/O properties, the light blue area is dedicated for output functionalities, the orange area for inputs and external interrupts, the pink zone for internal pull resistors and finally the red area signifying the presence of a Schmitt trigger input stage which is very useful for noisy environments.<\/p>\n<h2>Code Example<\/h2>\n<pre><span style=\"color: #0000ff\"><strong>#include<\/strong> &lt;msp430g2452.h&gt;<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>GPIO_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>BCSplus_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>WDTplus_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>System_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>main<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Stop watchdog timer from timing out during initial start-up. *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 WDTCTL = WDTPW | WDTHOLD;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* initialize Config for the MSP430 GPIO *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>GPIO_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* initialize Config for the MSP430 2xx family clock systems (BCS) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>BCSplus_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* initialize Config for the MSP430 System Registers *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>System_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* initialize Config for the MSP430 WDT+ *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>WDTplus_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>for<\/strong>(;;)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 <strong>if<\/strong>((P1IN &amp; BIT3) == !BIT3)<\/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\u00a0\u00a0 P1OUT |= BIT0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 _delay_cycles(40000);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 P1OUT &amp;= ~BIT0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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\u00a0\u00a0 P1OUT ^= BIT6;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 _delay_cycles(30000);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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\"><strong>void<\/strong> <strong>BCSplus_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: BCSplus_graceInit_prologue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User initialization code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: BCSplus_graceInit_prologue) *\/<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Basic Clock System Control 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 * SELM_0 -- DCOCLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * DIVM_0 -- Divide by 1<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~SELS -- DCOCLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * DIVS_0 -- Divide by 1<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~DCOR -- DCO uses internal resistor<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 BCSCTL2 = SELM_0 | DIVM_0 | DIVS_0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>if<\/strong> (CALBC1_1MHZ != 0xFF) {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/* Follow recommended flow. First, clear all DCOx and MODx bits. Then<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 * apply new RSELx values. Finally, apply new DCOx and MODx bit values.<\/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 DCOCTL = 0x00;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 BCSCTL1 = CALBC1_1MHZ;\u00a0\u00a0\u00a0\u00a0\u00a0 \/* Set DCO to 1MHz *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 DCOCTL = CALDCO_1MHZ;<\/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 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Basic Clock System Control 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 * XT2OFF -- Disable XT2CLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~XTS -- Low Frequency<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * DIVA_0 -- Divide by 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 * Note: ~XTS indicates that XTS has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 BCSCTL1 |= XT2OFF | DIVA_0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Basic Clock System Control 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 * XT2S_0 -- 0.4 - 1 MHz<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * LFXT1S_2 -- If XTS = 0, XT1 = VLOCLK ; If XTS = 1, XT1 = 3 - 16-MHz crystal or resonator<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * XCAP_1 -- ~6 pF<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 BCSCTL3 = XT2S_0 | LFXT1S_2 | XCAP_1;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: BCSplus_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: BCSplus_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>GPIO_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: GPIO_graceInit_prologue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User initialization code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: GPIO_graceInit_prologue) *\/<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Output Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1OUT = BIT3;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Direction Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1DIR = BIT0 | BIT6;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Resistor Enable Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1REN = BIT3;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Interrupt Edge Select Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1IES = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Interrupt Flag Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1IFG = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Output Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2OUT = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Direction Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2DIR = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Interrupt Edge Select Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2IES = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Interrupt Flag Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2IFG = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: GPIO_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: GPIO_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>System_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: System_graceInit_prologue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User initialization code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: System_graceInit_prologue) *\/<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Clear oscillator fault flag with software delay *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>do<\/strong><\/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 \/\/ Clear OSC fault flag<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 IFG1 &amp;= ~OFIFG;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ 50us delay<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>__delay_cycles<\/strong>(50);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 } <strong>while<\/strong> (IFG1 &amp; OFIFG);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * SR, Status Register<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~SCG1 -- Disable System clock generator 1<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~SCG0 -- Disable System clock generator 0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~OSCOFF -- Oscillator On<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~CPUOFF -- CPU On<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * GIE -- General interrupt enable<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>__bis_SR_register<\/strong>(GIE);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: System_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: System_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>WDTplus_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: RTC_B_graceInit_prologue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User initialization code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: RTC_B_graceInit_prologue) *\/<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * WDTCTL, Watchdog Timer+ Register<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * WDTPW -- Watchdog password<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * WDTHOLD -- Watchdog timer+ is stopped<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTNMIES -- NMI on rising edge<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTNMI -- Reset function<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTTMSEL -- Watchdog mode<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTCNTCL -- No action<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTSSEL -- SMCLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTIS0 -- Watchdog clock source bit0 disabled<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTIS1 -- Watchdog clock source bit1 disabled<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 WDTCTL = WDTPW | WDTHOLD;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: RTC_B_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: RTC_B_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<p>&nbsp;<\/p>\n<h2>Simulation<\/h2>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/DIO-Simulation.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13722\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/DIO-Simulation-580x234.png\" alt=\"DIO Simulation\" width=\"580\" height=\"234\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/DIO-Simulation-580x234.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/DIO-Simulation.png 767w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<h2>Explanation<\/h2>\n<p style=\"text-align: justify\">The most classical <strong><em>\u201cHello World\u201d<\/em><\/strong> demo for digital I\/Os is blinking a LED. Here I demonstrated the same but with some minor differences. Here Launchpad board\u2019s user button and LEDs are used. LED connected to P1.6 blinks continuously while P1.0 LED blinks only when the user button is pressed, slowing down P1.6 LED\u2019s blink rate. Note <strong><em>_delay_cycles<\/em><\/strong> were used to create software delays.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Grace-Setting.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13723\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Grace-Setting-580x133.png\" alt=\"Grace Setting\" width=\"580\" height=\"133\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Grace-Setting-580x133.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Grace-Setting-1024x234.png 1024w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Grace-Setting.png 1177w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p style=\"text-align: justify\">At this stage I must point out, some basic rules that we must follow when we use digital I\/Os of MSP430s:<\/p>\n<ul>\n<li style=\"text-align: justify\">Unused I\/Os should be declared as inputs or they should be externally pulled to VDD\/VSS.<\/li>\n<li style=\"text-align: justify\">Unused digital I\/Os can be left unconnected and floating although it is not wise to do so.<\/li>\n<li style=\"text-align: justify\">The same applies for oscillator pins. By default, GRACE treats them as oscillator pins.<\/li>\n<li style=\"text-align: justify\">I\/Os are not 5V tolerant. Some sort of mechanism must be applied when using 5V devices.<\/li>\n<li style=\"text-align: justify\">Most I\/Os have more than one function and so be aware of conflicts.<\/li>\n<li style=\"text-align: justify\">When driving large loads with I\/O, use external components like BJTs, FETs, drivers, etc.<\/li>\n<li style=\"text-align: justify\">When using GRACE, be sure of the IC package you are using.<\/li>\n<li style=\"text-align: justify\">When external pull resistor is present, do not use the internal ones and vice versa.<\/li>\n<li style=\"text-align: justify\">Explore your device\u2019s datasheet for I\/O features and limits although most are common.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h2>Demo<\/h2>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/DIO-Demo.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13724\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/DIO-Demo-580x419.jpg\" alt=\"DIO Demo\" width=\"580\" height=\"419\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/DIO-Demo-580x419.jpg 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/DIO-Demo.jpg 590w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p style=\"text-align: justify\">Demo video: <a href=\"https:\/\/www.youtube.com\/watch?v=fWlNiEZk4iM\">https:\/\/www.youtube.com\/watch?v=fWlNiEZk4iM<\/a>.<\/p>\n<p style=\"text-align: justify\"><!--more--><\/p>\n<h1>External Interrupts (EXTI)<\/h1>\n<p style=\"text-align: justify\">External interrupt is an extended feature of digital I\/Os in input mode. External interrupts make a micro to respond instantly to changes done on it digital input pin(s) by an external event(s)\/trigger(s), skipping other tasks. Such interrupts are useful in a wide variety of applications. In case of low power energy efficient micros like the MSP430s, interrupts as such can be used to bring a micro out of sleep mode. In other words, an external interrupt acts like a wakeup call. For example, it is extremely important to conserve very limited battery energy in a TV remote controller while at the same time it is also necessary to keep it completely responsive to button presses. Thus, we need to put its host micro in sleep mode when we are not using it and make it respond to button presses immediately when any button is pressed. A MSP430 micro in sleep\/idle mode consumes literally no energy at all and that is why they are the best micros for battery-backed low power applications.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Interrupt-Vectors.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13727\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Interrupt-Vectors-580x427.png\" alt=\"Interrupt Vectors\" width=\"580\" height=\"427\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Interrupt-Vectors-580x427.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Interrupt-Vectors.png 654w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p style=\"text-align: justify\">Fortunately for us, most MSP430G2xx digital I\/Os have external interrupt handling capability \u2013 a much desired feature. Shown above is the interrupt map for MSP430G2553. Note external interrupts are maskable interrupts and have low priority compares to other interrupts. We must consider this fact when coding a multi-interrupt-based application.<\/p>\n<h2>Code Example<\/h2>\n<pre><span style=\"color: #0000ff\"><strong>#include<\/strong> &lt;MSP430G2452.h&gt;<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>unsigned<\/strong> <strong>char<\/strong> state = 0x00;<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>BCSplus_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>GPIO_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>System_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>WDTplus_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#pragma<\/strong> vector=PORT1_VECTOR<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>__interrupt<\/strong> <strong>void<\/strong> <strong>PORT1_ISR_HOOK<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 state = ~state;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1OUT ^= BIT0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1IFG = 0x00;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>main<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Stop watchdog timer from timing out during initial start-up. *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 WDTCTL = WDTPW | WDTHOLD;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* initialize Config for the MSP430 GPIO *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>GPIO_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* initialize Config for the MSP430 2xx family clock systems (BCS) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>BCSplus_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* initialize Config for the MSP430 System Registers *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>System_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* initialize Config for the MSP430 WDT+ *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>WDTplus_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>do<\/strong><\/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\u00a0 P1OUT ^= BIT6;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>if<\/strong>(state)<\/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\u00a0 _delay_cycles(60000);<\/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 <strong>else<\/strong><\/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\u00a0 _delay_cycles(30000);<\/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 }<strong>while<\/strong>(1);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>BCSplus_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: BCSplus_graceInit_prologue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User initialization code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: BCSplus_graceInit_prologue) *\/<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Basic Clock System Control 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 * SELM_0 -- DCOCLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * DIVM_0 -- Divide by 1<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~SELS -- DCOCLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * DIVS_0 -- Divide by 1<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~DCOR -- DCO uses internal resistor<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 BCSCTL2 = SELM_0 | DIVM_0 | DIVS_0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>if<\/strong> (CALBC1_1MHZ != 0xFF) {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/* Follow recommended flow. First, clear all DCOx and MODx bits. Then<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 * apply new RSELx values. Finally, apply new DCOx and MODx bit values.<\/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 DCOCTL = 0x00;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 BCSCTL1 = CALBC1_1MHZ;\u00a0\u00a0\u00a0\u00a0\u00a0 \/* Set DCO to 1MHz *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 DCOCTL = CALDCO_1MHZ;<\/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 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Basic Clock System Control 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 * XT2OFF -- Disable XT2CLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~XTS -- Low Frequency<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * DIVA_0 -- Divide by 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 * Note: ~XTS indicates that XTS has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 BCSCTL1 |= XT2OFF | DIVA_0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Basic Clock System Control 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 * XT2S_0 -- 0.4 - 1 MHz<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * LFXT1S_2 -- If XTS = 0, XT1 = VLOCLK ; If XTS = 1, XT1 = 3 - 16-MHz crystal or resonator<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * XCAP_1 -- ~6 pF<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 BCSCTL3 = XT2S_0 | LFXT1S_2 | XCAP_1;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: BCSplus_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: BCSplus_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>GPIO_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: GPIO_graceInit_prologue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User initialization code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: GPIO_graceInit_prologue) *\/<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Output Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1OUT = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Direction Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1DIR = BIT0 | BIT6;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Interrupt Edge Select Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1IES = BIT3;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Interrupt Flag Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1IFG = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Interrupt Enable Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1IE = BIT3;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Output Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2OUT = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Port Select Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2SEL &amp;= ~(BIT6 | BIT7);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Direction Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2DIR = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Interrupt Edge Select Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2IES = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Interrupt Flag Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2IFG = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: GPIO_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: GPIO_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>System_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: System_graceInit_prologue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User initialization code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: System_graceInit_prologue) *\/<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Clear oscillator fault flag with software delay *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>do<\/strong><\/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 \/\/ Clear OSC fault flag<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 IFG1 &amp;= ~OFIFG;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ 50us delay<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>__delay_cycles<\/strong>(50);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 } <strong>while<\/strong> (IFG1 &amp; OFIFG);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * SR, Status Register<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~SCG1 -- Disable System clock generator 1<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~SCG0 -- Disable System clock generator 0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~OSCOFF -- Oscillator On<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~CPUOFF -- CPU On<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * GIE -- General interrupt enable<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>__bis_SR_register<\/strong>(GIE);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: System_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: System_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>WDTplus_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: RTC_B_graceInit_prologue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User initialization code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: RTC_B_graceInit_prologue) *\/<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * WDTCTL, Watchdog Timer+ Register<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * WDTPW -- Watchdog password<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * WDTHOLD -- Watchdog timer+ is stopped<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTNMIES -- NMI on rising edge<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTNMI -- Reset function<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTTMSEL -- Watchdog mode<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTCNTCL -- No action<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTSSEL -- SMCLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTIS0 -- Watchdog clock source bit0 disabled<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTIS1 -- Watchdog clock source bit1 disabled<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 WDTCTL = WDTPW | WDTHOLD;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: RTC_B_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: RTC_B_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<p>&nbsp;<\/p>\n<h2>Simulation<\/h2>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/EXTI-Simulation.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13725\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/EXTI-Simulation-580x231.png\" alt=\"EXTI Simulation\" width=\"580\" height=\"231\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/EXTI-Simulation-580x231.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/EXTI-Simulation-1024x408.png 1024w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/EXTI-Simulation.png 1136w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<h2>Explanation<\/h2>\n<p style=\"text-align: justify\">The basic theme and the hardware setup for this demo is same as that of the previous one. The only difference is the Launchpad user button. Rather than using polling method, external interrupt method is used. When P1.3 detects a falling edge, P1.0\u2019s state is altered while P1.6 toggles independently in the main loop, denoting two separate independent processes at work simultaneously. Every external interrupt changes the toggle speed of P1.6 LED.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Grace-Settings.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13726\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Grace-Settings-580x125.png\" alt=\"Grace Settings\" width=\"580\" height=\"125\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Grace-Settings-580x125.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Grace-Settings-1024x220.png 1024w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Grace-Settings.png 1211w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<h2>Demo<\/h2>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/EXTI.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13728\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/EXTI-580x310.jpg\" alt=\"EXTI\" width=\"580\" height=\"310\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/EXTI-580x310.jpg 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/EXTI.jpg 590w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p>Demo video: <a href=\"https:\/\/www.youtube.com\/watch?v=LlTvj-CSiBE\">https:\/\/www.youtube.com\/watch?v=LlTvj-CSiBE<\/a>.<\/p>\n<p><!--more--><\/p>\n<h1>Alphanumeric LCD<\/h1>\n<p style=\"text-align: justify\">Alphanumeric LCDs are popular for projecting data and information quickly and efficiently. To use them, we do not need any additional hardware feature in a micro. Digital I\/Os are what we need to use these displays.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/LCD.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-13731\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/LCD.jpg\" alt=\"LCD\" width=\"454\" height=\"243\" \/><\/a><\/p>\n<p style=\"text-align: justify\">Usually to use these LCDs we need 5V power supply. 3.3V versions of these LCD are rare on the other hand.\u00a0 It is, however, possible to power up 5V version LCDs with 5V supply while using 3.3V logic for communication. In TTL logic, something above 2.8V is just above the minimum allowed logic high voltage. When VDD is less than 3V, it, then, becomes necessary to use logic level conversion circuitry. Here in my demo I used a 3.3V version LCD to keep things simple and tidy. Shown below are LCDs for different voltage levels. Both LCDs look same but if you check the backside of both LCDs you\u2019ll notice a difference. In the 3.3V version LCD, there are some additional components present, notably an 8 pin SMD IC. This is a ICL7660 negative voltage generator IC.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Seeeduino-LCD-1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13732\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Seeeduino-LCD-1-580x159.jpg\" alt=\"Seeeduino LCD (1)\" width=\"580\" height=\"159\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Seeeduino-LCD-1-580x159.jpg 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Seeeduino-LCD-1-1024x281.jpg 1024w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Seeeduino-LCD-1.jpg 1369w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a> <a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Seeeduino-LCD-2.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13733\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Seeeduino-LCD-2-580x174.jpg\" alt=\"Seeeduino LCD (2)\" width=\"580\" height=\"174\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Seeeduino-LCD-2-580x174.jpg 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Seeeduino-LCD-2-1024x307.jpg 1024w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Seeeduino-LCD-2.jpg 1369w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p style=\"text-align: justify\">Similarly, alphanumeric LCDs also have operating frequency limit. Usually it is about 250 kHz. Refer to the datasheet of the LCD you are using. If the digital I\/Os are faster than this max limit value, it is highly likely that the LCD won\u2019t show any valid data at all or it will show up garbage characters. To counter this issue, either we have to use a low MCU clock frequency or add some delays in our LCD library to slow the processes down.<\/p>\n<p style=\"text-align: justify\">Software delays are often required to introduce time delays in a code. Such delays will be needed for the LCD library. As discussed before by default, CCS doesn\u2019t provide delay time functions i.e. delay milliseconds (<strong><em>delay_ms<\/em><\/strong>) and delay micro-seconds (<strong><em>delay_us<\/em><\/strong>). Instead it provides <strong><em>_delay_cycles<\/em><\/strong> function for delays. Most of us will not be comfortable with delay cycles function as it doesn\u2019t directly signify the amount of time wasted. Thus, delay time functions are musts. Although software delays are inefficient in terms of coding and performance, they are helpful in debugging stuffs quickly in a rudimentary fashion. A much novel and precise approach of creating time delays is achieved using a timer. In this example, we will need both LCD and delay libraries. I already showed how to incorporate custom libraries in a CCS Project and here I implemented that addition.<\/p>\n<p style=\"text-align: justify\">There are other types of displays in the realm of LCDs. These include monochrome graphical LCDs, TFT LCDs, OLED LCDs, etc. However, it is literally impossible to integrate these displays with MSP430G2xx micros. Firstly, this is so due to low memories and secondly due to low pin counts. MSP430G2xx micros are also not as fast as ARM micros. Speed plays a vital role in processing graphics.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/TFT.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13734\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/TFT-580x410.jpg\" alt=\"TFT LCD\" width=\"580\" height=\"410\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/TFT-580x410.jpg 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/TFT.jpg 590w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<h2>Code Example<\/h2>\n<p>&nbsp;<\/p>\n<p><strong><em>delay.h<\/em><\/strong><\/p>\n<pre><span style=\"color: #0000ff\"><strong>#include<\/strong> &lt;msp430.h&gt;<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>F_CPU<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 8<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>delay_us<\/strong>(<strong>unsigned<\/strong> <strong>int<\/strong> value);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>delay_ms<\/strong>(<strong>unsigned<\/strong> <strong>int<\/strong> value);<\/span><\/pre>\n<p>&nbsp;<\/p>\n<p><strong><em>delay.c<\/em><\/strong><\/p>\n<pre><span style=\"color: #0000ff\"><strong>#include<\/strong> \"delay.h\"<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>delay_us<\/strong>(<strong>unsigned<\/strong> <strong>int<\/strong> value)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong><u>register<\/u><\/strong> <strong><u>unsigned<\/u><\/strong> <strong><u>int<\/u><\/strong> <u>loops<\/u> <u>=<\/u><u>\u00a0 <\/u><u>((<\/u><u>F_CPU <\/u><u>*<\/u> <u>value<\/u><u>)<\/u> <u>&gt;&gt;<\/u> <u>2<\/u><u>)<\/u> <u>;<\/u><\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>while<\/strong>(loops)<\/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 _delay_cycles(1);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 loops--;<\/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><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>delay_ms<\/strong>(<strong>unsigned<\/strong> <strong>int<\/strong> value)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>while<\/strong>(value)<\/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 <strong>delay_us<\/strong>(1000);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 value--;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 };<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<p><strong><em>\u00a0<\/em><\/strong><\/p>\n<p><strong><em>lcd.h<\/em><\/strong><\/p>\n<pre><span style=\"color: #0000ff\"><strong>#include<\/strong> &lt;msp430.h&gt;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#include<\/strong> &lt;delay.h&gt;<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>LCD_PORT<\/strong>\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 P2OUT<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>LCD_RS<\/strong>\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 BIT0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>LCD_EN<\/strong>\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 BIT1<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>LCD_DB4<\/strong>\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 BIT2<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>LCD_DB5<\/strong>\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 BIT3<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>LCD_DB6<\/strong>\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 BIT4<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>LCD_DB7<\/strong>\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 BIT5<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>LCD_RS_HIGH<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 LCD_PORT |= LCD_RS<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>LCD_RS_LOW<\/strong>\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 LCD_PORT &amp;= ~LCD_RS<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>LCD_EN_HIGH<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0LCD_PORT |= LCD_EN<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>LCD_EN_LOW<\/strong>\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 LCD_PORT &amp;= ~LCD_EN<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>LCD_DB4_HIGH<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 LCD_PORT |= LCD_DB4<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>LCD_DB4_LOW<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 LCD_PORT &amp;= ~LCD_DB4<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>LCD_DB5_HIGH<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0LCD_PORT |= LCD_DB5<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>LCD_DB5_LOW<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 LCD_PORT &amp;= ~LCD_DB5<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>LCD_DB6_HIGH<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 LCD_PORT |= LCD_DB6<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>LCD_DB6_LOW<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 LCD_PORT &amp;= ~LCD_DB6<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>LCD_DB7_HIGH<\/strong>\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0LCD_PORT |= LCD_DB7<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>LCD_DB7_LOW<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 LCD_PORT &amp;= ~LCD_DB7<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>clear_display<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0x01<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>goto_home<\/strong>\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 0x02<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>cursor_direction_inc<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 (0x04 | 0x02)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>cursor_direction_dec<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 (0x04 | 0x00)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>display_shift<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 (0x04 | 0x01)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>display_no_shift<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 (0x04 | 0x00)<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>display_on<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 (0x08 | 0x04)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>display_off<\/strong>\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0(0x08 | 0x02)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>cursor_on<\/strong>\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 (0x08 | 0x02)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>cursor_off<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 (0x08 | 0x00)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>blink_on<\/strong>\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 (0x08 | 0x01)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>blink_off<\/strong>\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 (0x08 | 0x00)<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>_8_pin_interface<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 (0x20 | 0x10)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>_4_pin_interface<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 (0x20 | 0x00)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>_2_row_display<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 (0x20 | 0x08)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>_1_row_display<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 (0x20 | 0x00)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>_5x10_dots<\/strong> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0(0x20 | 0x40)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>_5x7_dots<\/strong>\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 (0x20 | 0x00)<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>DAT<\/strong>\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 1<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>CMD<\/strong>\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 0<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>LCD_init<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>LCD_send<\/strong>(<strong>unsigned<\/strong> <strong>char<\/strong> value, <strong>unsigned<\/strong> <strong>char<\/strong> mode);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>LCD_4bit_send<\/strong>(<strong>unsigned<\/strong> <strong>char<\/strong> lcd_data);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>LCD_putstr<\/strong>(<strong>char<\/strong> *lcd_string);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>LCD_putchar<\/strong>(<strong>char<\/strong> char_data);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>LCD_clear_home<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>LCD_goto<\/strong>(<strong>unsigned<\/strong> <strong>char<\/strong> x_pos, <strong>unsigned<\/strong> <strong>char<\/strong> y_pos);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>toggle_EN_pin<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>toggle_io<\/strong>(<strong>unsigned<\/strong> <strong>char<\/strong> lcd_data, <strong>unsigned<\/strong> <strong>char<\/strong> bit_pos, <strong>unsigned<\/strong> <strong>char<\/strong> pin_num);<\/span><\/pre>\n<p>&nbsp;<\/p>\n<p><strong><em>lcd.c<\/em><\/strong><\/p>\n<pre><span style=\"color: #0000ff\"><strong>#include<\/strong> \"lcd.h\"<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>LCD_init<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 LCD_PORT |= (LCD_RS | LCD_EN | LCD_DB4 | LCD_DB5 | LCD_DB6 | LCD_DB7);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>delay_ms<\/strong>(20);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>toggle_EN_pin<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 LCD_RS_LOW;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 LCD_DB7_LOW;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 LCD_DB6_LOW;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 LCD_DB5_HIGH;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 LCD_DB4_HIGH;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>toggle_EN_pin<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 LCD_DB7_LOW;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 LCD_DB6_LOW;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 LCD_DB5_HIGH;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 LCD_DB4_HIGH;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>toggle_EN_pin<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 LCD_DB7_LOW;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 LCD_DB6_LOW;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 LCD_DB5_HIGH;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 LCD_DB4_HIGH;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>toggle_EN_pin<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 LCD_DB7_LOW;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 LCD_DB6_LOW;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 LCD_DB5_HIGH;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 LCD_DB4_LOW;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>toggle_EN_pin<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_send<\/strong>((_4_pin_interface | _2_row_display | _5x7_dots), CMD);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_send<\/strong>((display_on | cursor_off | blink_off), CMD);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_send<\/strong>(clear_display, CMD);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_send<\/strong>((cursor_direction_inc | display_no_shift), CMD);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>LCD_send<\/strong>(<strong>unsigned<\/strong> <strong>char<\/strong> value, <strong>unsigned<\/strong> <strong>char<\/strong> mode)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>switch<\/strong>(mode)<\/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 <strong>case<\/strong> DAT:<\/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\u00a0\u00a0 LCD_RS_HIGH;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>break<\/strong>;<\/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 <strong>case<\/strong> CMD:<\/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\u00a0\u00a0 LCD_RS_LOW;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>break<\/strong>;<\/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 }<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_4bit_send<\/strong>(value);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>LCD_4bit_send<\/strong>(<strong>unsigned<\/strong> <strong>char<\/strong> lcd_data)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>toggle_io<\/strong>(lcd_data, 7, LCD_DB7);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>toggle_io<\/strong>(lcd_data, 6, LCD_DB6);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>toggle_io<\/strong>(lcd_data, 5, LCD_DB5);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>toggle_io<\/strong>(lcd_data, 4, LCD_DB4);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>toggle_EN_pin<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>toggle_io<\/strong>(lcd_data, 3, LCD_DB7);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>toggle_io<\/strong>(lcd_data, 2, LCD_DB6);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>toggle_io<\/strong>(lcd_data, 1, LCD_DB5);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>toggle_io<\/strong>(lcd_data, 0, LCD_DB4);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>toggle_EN_pin<\/strong>();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>LCD_putstr<\/strong>(<strong>char<\/strong> *lcd_string)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>do<\/strong><\/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 <strong>LCD_send<\/strong>(*lcd_string++, DAT);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 }<strong>while<\/strong>(*lcd_string != '\\0');<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>LCD_putchar<\/strong>(<strong>char<\/strong> char_data)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_send<\/strong>(char_data, DAT);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>LCD_clear_home<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_send<\/strong>(clear_display, CMD);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_send<\/strong>(goto_home, CMD);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>LCD_goto<\/strong>(<strong>unsigned<\/strong> <strong>char<\/strong> x_pos, <strong>unsigned<\/strong> <strong>char<\/strong> y_pos)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>if<\/strong>(y_pos == 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\u00a0\u00a0 <strong>LCD_send<\/strong>((0x80 | x_pos), CMD);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 }<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>else<\/strong><\/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 <strong>LCD_send<\/strong>((0x80 | 0x40 | x_pos), CMD);<\/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><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>toggle_EN_pin<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 LCD_EN_HIGH;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>delay_ms<\/strong>(2);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 LCD_EN_LOW;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>delay_ms<\/strong>(2);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>toggle_io<\/strong>(<strong>unsigned<\/strong> <strong>char<\/strong> lcd_data, <strong>unsigned<\/strong> <strong>char<\/strong> bit_pos, <strong>unsigned<\/strong> <strong>char<\/strong> pin_num)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>unsigned<\/strong> <strong>char<\/strong> temp = 0x00;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 temp = (0x01 &amp; (lcd_data &gt;&gt; bit_pos));<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>switch<\/strong>(temp)<\/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 <strong>case<\/strong> 0:<\/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\u00a0\u00a0 LCD_PORT &amp;= ~pin_num;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>break<\/strong>;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 <strong>default<\/strong>:<\/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\u00a0\u00a0 LCD_PORT |= pin_num;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>break<\/strong>;<\/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 }<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<p>&nbsp;<\/p>\n<p><strong><em>main.c<\/em><\/strong><\/p>\n<pre><span style=\"color: #0000ff\"><strong>#include<\/strong> &lt;msp430.h&gt;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#include<\/strong> \"delay.h\"<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#include<\/strong> \"lcd.h\"<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>GPIO_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>BCSplus_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>System_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>WDTplus_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>main<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>unsigned<\/strong> <strong>char<\/strong> s = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>const<\/strong> <strong>char<\/strong> txt1[] = {\"MICROARENA\"};<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>const<\/strong> <strong>char<\/strong> txt2[] = {\"SShahryiar\"};<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>const<\/strong> <strong>char<\/strong> txt3[] = {\"MSP-EXP430G2\"};<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>const<\/strong> <strong>char<\/strong> txt4[] = {\"Launchpad!\"};<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Stop watchdog timer from timing out during initial start-up. *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 WDTCTL = WDTPW | WDTHOLD;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* initialize Config for the MSP430 GPIO *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>GPIO_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* initialize Config for the MSP430 2xx family clock systems (BCS) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>BCSplus_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* initialize Config for the MSP430 System Registers *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>System_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* initialize Config for the MSP430 WDT+ *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>WDTplus_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_init<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_clear_home<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_goto<\/strong>(3, 0);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_putstr<\/strong>(txt1);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_goto<\/strong>(3, 1);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_putstr<\/strong>(txt2);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>delay_ms<\/strong>(2600);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_clear_home<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>for<\/strong>(s = 0; s &lt; 12; 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\u00a0\u00a0 <strong>LCD_goto<\/strong>((2 + s), 0);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>LCD_putchar<\/strong>(txt3[s]);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>delay_ms<\/strong>(90);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 }<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>for<\/strong>(s = 0; s &lt; 10; 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\u00a0\u00a0 <strong>LCD_goto<\/strong>((3 + s), 1);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>LCD_putchar<\/strong>(txt4[s]);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>delay_ms<\/strong>(90);<\/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 <strong>while<\/strong>(1)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>GPIO_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: GPIO_graceInit_prologue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User initialization code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: GPIO_graceInit_prologue) *\/<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Output Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1OUT = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Direction Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1DIR = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Interrupt Edge Select Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1IES = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 \u00a0\u00a0\/* Port 1 Interrupt Flag Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1IFG = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Output Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2OUT = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Port Select Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2SEL &amp;= ~(BIT6 | BIT7);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Direction Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2DIR = BIT0 | BIT1 | BIT2 | BIT3 | BIT4 | BIT5;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Interrupt Edge Select Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2IES = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Interrupt Flag Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2IFG = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: GPIO_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: GPIO_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>BCSplus_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: BCSplus_graceInit_prologue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User initialization code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: BCSplus_graceInit_prologue) *\/<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Basic Clock System Control 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 * SELM_0 -- DCOCLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * DIVM_0 -- Divide by 1<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~SELS -- DCOCLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * DIVS_0 -- Divide by 1<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~DCOR -- DCO uses internal resistor<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 BCSCTL2 = SELM_0 | DIVM_0 | DIVS_0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>if<\/strong> (CALBC1_8MHZ != 0xFF) {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/* Adjust this accordingly to your VCC rise time *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>__delay_cycles<\/strong>(100000);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Follow recommended flow. First, clear all DCOx and MODx bits. Then<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ apply new RSELx values. Finally, apply new DCOx and MODx bit values.<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 DCOCTL = 0x00;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 BCSCTL1 = CALBC1_8MHZ;\u00a0\u00a0\u00a0\u00a0\u00a0 \/* Set DCO to 8MHz *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 DCOCTL = CALDCO_8MHZ;<\/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 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Basic Clock System Control 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 * XT2OFF -- Disable XT2CLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~XTS -- Low Frequency<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * DIVA_0 -- Divide by 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 * Note: ~XTS indicates that XTS has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 BCSCTL1 |= XT2OFF | DIVA_0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Basic Clock System Control 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 * XT2S_0 -- 0.4 - 1 MHz<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * LFXT1S_0 -- If XTS = 0, XT1 = 32768kHz Crystal ; If XTS = 1, XT1 = 0.4 - 1-MHz crystal or resonator<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * XCAP_1 -- ~6 pF<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 BCSCTL3 = XT2S_0 | LFXT1S_0 | XCAP_1;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: BCSplus_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: BCSplus_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>System_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: System_graceInit_prologue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User initialization code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: System_graceInit_prologue) *\/<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * SR, Status Register<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~SCG1 -- Disable System clock generator 1<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~SCG0 -- Disable System clock generator 0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~OSCOFF -- Oscillator On<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~CPUOFF -- CPU On<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * GIE -- General interrupt enable<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>__bis_SR_register<\/strong>(GIE);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: System_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: System_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>WDTplus_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: RTC_B_graceInit_prologue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User initialization code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: RTC_B_graceInit_prologue) *\/<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * WDTCTL, Watchdog Timer+ Register<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * WDTPW -- Watchdog password<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * WDTHOLD -- Watchdog timer+ is stopped<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTNMIES -- NMI on rising edge<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTNMI -- Reset function<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTTMSEL -- Watchdog mode<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTCNTCL -- No action<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTSSEL -- SMCLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTIS0 -- Watchdog clock source bit0 disabled<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTIS1 -- Watchdog clock source bit1 disabled<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 WDTCTL = WDTPW | WDTHOLD;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: RTC_B_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: RTC_B_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<p>&nbsp;<\/p>\n<h2>Simulation<\/h2>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/LCD-Simulation.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13730\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/LCD-Simulation-580x244.png\" alt=\"LCD Simulation\" width=\"580\" height=\"244\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/LCD-Simulation-580x244.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/LCD-Simulation.png 989w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<h2>Explanation<\/h2>\n<p>Hardly there is a thing to explain here. However, there are a few things to note. After having decided the CPU clock frequency, we must edit the following line of the delay header file to make our delays work as much as accurate as possible:<\/p>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>F_CPU<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 8\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/CPU Clock in MHz<\/span><\/pre>\n<p>Software delays are not 100% accurate since they employ wasteful loops of delay cycles which in turn add extra CPU cycles. They are always somewhat close to the actual required value. Delays are also dependent on oscillator accuracy.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/grace.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-13729\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/grace.png\" alt=\"grace\" width=\"444\" height=\"196\" \/><\/a><\/p>\n<p style=\"text-align: justify\">Though it is not mandatory but I still recommend that we try to use digital I\/Os that have no or least alternate functionalities. This will ensure maximum utilization of limited resources and avoid conflicts as well. Here in this demo, I used P2 port just for that.<\/p>\n<p style=\"text-align: justify\">Choosing the right display for a project is often tedious. We have lot of options nowadays ranging from simple LED displays to TFT LCDs. However, considering low power consumption and limited resource availability, it wise to use the simplest form of display. If a project can be completed using seven segment displays, it wasteful and expensive to use a OLED display with seven segment fonts.<\/p>\n<p style=\"text-align: justify\">LCD displays host their own set of electronics and are prone to\u00a0<strong><em>Electromagnetic\u00a0Interference (EMI)<\/em><\/strong> and related issues. In many cases, one may end up having a EMI troubled LCD while having the host MCU fully functional and vice-versa. It is always wise to use a short path between a LCD and its host MCU. If needed, use passive low pass antialiasing filters.<\/p>\n<h2>Demo<\/h2>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/LCD1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13735\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/LCD1-580x640.jpg\" alt=\"LCD Demo\" width=\"580\" height=\"640\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/LCD1-580x640.jpg 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/LCD1.jpg 590w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p style=\"text-align: justify\">Demo video: <a href=\"https:\/\/www.youtube.com\/watch?v=sJF8oJPu18s\">https:\/\/www.youtube.com\/watch?v=sJF8oJPu18s<\/a>.<\/p>\n<p style=\"text-align: justify\"><!--more--><\/p>\n<h1>Timer Overview<\/h1>\n<p style=\"text-align: justify\">There are several types of timers in MSP430 micros. These include <strong><em>Watchdog Timer (WDT)<\/em><\/strong>, <strong><em>Real Time Clock (RTC)<\/em><\/strong>,<strong><em> Basic Timer 1 and Timer A <\/em><\/strong>and<strong><em> Timer B<\/em><\/strong>. Timer As and Bs are general purpose 16-bit timers that are suitable for time base generations, pulse width modulations (PWM) and input captures. the other three types have special uses and their names suggest their purposes. All MSP430 devices have at least one Timer A module and a WDT module. Since both are common amongst all devices, we will be studying about them here. Timer As and Bs are almost same. The difference between the two is the presence of some additional features like more <strong><em>Capture\/Compare (CC)<\/em><\/strong> channels in Timer Bs.<\/p>\n<h2>Timer A<\/h2>\n<p style=\"text-align: justify\">In TI\u2019s literature, timers are named like <strong><em>Timer0_A3<\/em><\/strong>, <strong><em>Timer_B7<\/em><\/strong>, etc. A number can be present right after the word <strong><em>Timer<\/em><\/strong> in some case and in other cases, it may be absent. This number is present whenever there are multiple timers of same type in a MSP430 micro. For example, there are two Timer A3s in MSP430G2553 micros and so they are labelled as <strong><em>Timer0_A3<\/em><\/strong> and <strong><em>Timer1_A3<\/em><\/strong>. The other number in the timer\u2019s name after the timer type letter denotes the number of CC channels available with it.<\/p>\n<p style=\"text-align: justify\">Shown below is the block diagram of Timer A module. MSP430G2452 has one and MSP430G2553 has two Timer A3 modules. Typically, in any micro\u2019s timer we would expect two things &#8211; first a counter block (highlighted in purple) and second CC modules (highlighted in green). As we can see the CC channels share the same TAR register and so they share the similar properties.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Block-Diagram2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-13739\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Block-Diagram2.png\" alt=\"Block Diagram\" width=\"521\" height=\"509\" \/><\/a><\/p>\n<p style=\"text-align: justify\">The diagram below shows that clock sources we can use for a Timer A module. It can be clocked with two internal clock sources \u2013 ACLK and SMCLK or with two external clock sources \u2013 TACLK and INCLK. Actually, INCLK and TACLK are same but one is complementary of the other. These external clocks can be used to make the timer module work as counter.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Counter-Block-Diagram.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-13745\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Counter-Block-Diagram.png\" alt=\"Counter Block Diagram\" width=\"517\" height=\"130\" \/><\/a><\/p>\n<p style=\"text-align: justify\">Next, we have a prescaler to scale down selected clock source followed by the counter block. What\u2019s interesting about MSP430 timers is the counter\u2019s mode of operation. There are four modes of counter operation and these modes define counting direction:<\/p>\n<ul style=\"text-align: justify\">\n<li><span style=\"color: #333399\"><strong><em><u>Stop Mode<br \/>\n<\/u><\/em><\/strong><\/span>It is basically a hold state. All timer registers are retained and the timer is halted.<\/li>\n<\/ul>\n<ul style=\"text-align: justify\">\n<li><span style=\"color: #333399\"><strong><em><u>Continuous Mode<br \/>\n<\/u><\/em><\/strong><\/span>In this mode, the timer counts up from 0 to top value (here 0xFFFF or 65535 in 16-bit Timer A3) and then rolls over to zero.<\/li>\n<\/ul>\n<ul style=\"text-align: justify\">\n<li><span style=\"color: #333399\"><strong><em><u>Up Mode<br \/>\n<\/u><\/em><\/strong><\/span>This mode is same as continuous mode except for the top value. The top value of the timer is set by the value in TACCR0.<\/li>\n<\/ul>\n<ul style=\"text-align: justify\">\n<li><span style=\"color: #333399\"><strong><em><u>Up\/Down Mode<br \/>\n<\/u><\/em><\/strong><\/span>In this mode, the timer counts from 0 to TACCR0 value and then rolls back from that value to 0. The period of the timer is twice the TACCR0 counts.<\/li>\n<\/ul>\n<p style=\"text-align: justify\">Then we have timer interrupt just as in any other microcontroller.<\/p>\n<p style=\"text-align: justify\">MSP430G2452 has one Timer A3 module and so it has three CC channels. Likewise, MSP430G2553 has six CC channels. When it comes to extreme engineering, TI sometimes seems to overengineer their products. For example, CC channels are not hard fixed to dedicated pins only unlike other micros. Each CC channel has a set of pins associated with it and so they can be remapped if needed. Shown below is the block diagram of a Timer A3 CC channel. The left side of the diagram has all the components for input capture while the right side is intended for compare-match or PWM output. Common to both is the <strong><em>TACCRn<\/em><\/strong> block. It is a very important block.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/CC-Block-Diagram.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-13744\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/CC-Block-Diagram.png\" alt=\"CC Block Diagram\" width=\"521\" height=\"379\" \/><\/a><\/p>\n<p style=\"text-align: justify\">The basic theme of PWM generation as with any microcontroller is to change the logic state of an output pin when the count in it associated TACCR register matches with the count in its timer\u2019s counter register \u2013 simply like a binary comparator. This process is called <strong><em>compare-match <\/em><\/strong>process. This is exactly the same idea used in MSP430s. Check the rudimentary timing diagram below. For five successive falling edges of the reference clock, the PWM output is high and for one edge, the output is low, resulting in about 83% duty cycle. The reference clock here is actually the timer clock and the comparison is done by comparing the count stored in TACCRn. Varying TACCRn\u2019s count results in duty cycle change.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/PWM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13761\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/PWM-580x140.png\" alt=\"PWM\" width=\"580\" height=\"140\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/PWM-580x140.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/PWM-1024x246.png 1024w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/PWM.png 1031w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p style=\"text-align: justify\">Input capture is somewhat just the opposite of PWM generation. In capture mode, CC channels can be used to record time-related info of an incoming waveform. A timer in this mode can be left to run on its own. When a waveform edge is detected, the corresponding time count of the timer is stored in CC register. With two such consecutive captures, we get a difference in timer\u2019s time counts. This difference can be used to measure frequency if the captured events are alike (two successive rising\/falling edges) or duty cycle if the captured events are different (different edges). Again, TACCRn stores the time capture when a capture event occurs.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Capture_modes.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-13743\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Capture_modes.png\" alt=\"Capture_modes\" width=\"432\" height=\"512\" \/><\/a><\/p>\n<p>As an example, check the arbitrary timing diagram below:<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Capture-Mode.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13740\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Capture-Mode-580x161.png\" alt=\"Capture Mode\" width=\"580\" height=\"161\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Capture-Mode-580x161.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Capture-Mode.png 961w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Here four falling edges of reference clock (timer clock) is equal to the high or on time of the captured waveform. Since the reference clock\u2019s period is known, we can deduce pulse width.<\/p>\n<h2>WDT+<\/h2>\n<p style=\"text-align: justify\">WDT+ is a 15\/16-bit watchdog timer. It is mainly intended to prevent unanticipated loops or stuck up conditions due to malfunctions or firmware bugs. The concept behind any watchdog timer is to regularly refresh a counter so that the count never reaches a predefined limit. If due to any reason this limit is exceeded, a reset is issued, causing the host micro to start over again.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/WDT-Block-Diagram.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-13766\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/WDT-Block-Diagram.png\" alt=\"WDT+ Block Diagram\" width=\"547\" height=\"568\" \/><\/a><\/p>\n<p style=\"text-align: justify\">WDT+ can additional be used as an interval timer just like other timers if watchdog timer functionality is not needed. We can, then, use WDT+ for time-base generations.<a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/WDT-Block-Diagram.png\"><br \/>\n<\/a>WDT+ is password protected and so a wrong password causes it to fail and reset immediately. This is the red box in the diagram above. Whenever we need to change anything related to WDT+, we have to enter the correct password which is 0x5A00.\u00a0 WDT+ consists of a 16-bit counter (green box) but we don\u2019t have access to it. The purple region consists of WDT options. The red and the purple boxes make up <strong><em>Watchdog Control (WDTCTL)<\/em><\/strong> register and this is what we are only allowed to code.<\/p>\n<p style=\"text-align: justify\"><!--more--><\/p>\n<h1>Free Running Timer<\/h1>\n<p style=\"text-align: justify\">Free running timers are useful in many cases. Free running timers can be used as random number generators, time delay generators, instance markers, etc. Consider the case of time delay generation for instance. Rather than using wasteful CPU-cycle dependent software delay loops, it is much wiser to use a hardware timer to create precise delays and timed events. In terms of ideal coding, no task should keep CPU busy unnecessarily nor should it keep other tasks waiting for its completion. Best coding and design are achieved if things are arranged in such an orderly way that that there is almost no wastage of any resource at all.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Ill.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13756\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Ill-580x530.png\" alt=\"Free Running Timer\" width=\"580\" height=\"530\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Ill-580x530.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Ill.png 690w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p style=\"text-align: justify\">By free-running what I really mean is we start a timer at the beginning of our code and keep it running without timer interrupt. We just take note of its counter. Here we will see how to use Timer_A3 like a free running timer and we will use it to blink Launchpad board\u2019s LEDs.<\/p>\n<h2>Code Example<\/h2>\n<pre><span style=\"color: #0000ff\"><strong>#include<\/strong> &lt;msp430.h&gt;<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>GPIO_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>BCSplus_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>Timer0_A3_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>System_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>WDTplus_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#pragma<\/strong> vector=TIMER0_A1_VECTOR<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>__interrupt<\/strong> <strong>void<\/strong> <strong>TIMER0_A1_ISR_HOOK<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>main<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Stop watchdog timer from timing out during initial start-up. *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 WDTCTL = WDTPW | WDTHOLD;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* initialize Config for the MSP430 GPIO *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>GPIO_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* initialize Config for the MSP430 2xx family clock systems (BCS) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>BCSplus_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* initialize Config for the MSP430 A3 Timer0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>Timer0_A3_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* initialize Config for the MSP430 System Registers *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>System_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* initialize Config for the MSP430 WDT+ *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>WDTplus_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>while<\/strong>(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\u00a0\u00a0 <strong>if<\/strong>(TA0R &gt;= 32768)<\/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\u00a0\u00a0 P1OUT |= BIT6;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 P1OUT &amp;= ~BIT0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 <strong>else<\/strong><\/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\u00a0\u00a0 P1OUT |= BIT0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 P1OUT &amp;= ~BIT6;<\/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 };<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>BCSplus_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: BCSplus_graceInit_prologue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User initialization code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: BCSplus_graceInit_prologue) *\/<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Basic Clock System Control 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 * SELM_0 -- DCOCLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * DIVM_0 -- Divide by 1<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~SELS -- DCOCLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * DIVS_0 -- Divide by 1<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~DCOR -- DCO uses internal resistor<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 BCSCTL2 = SELM_0 | DIVM_0 | DIVS_0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>if<\/strong> (CALBC1_1MHZ != 0xFF) {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/* Follow recommended flow. First, clear all DCOx and MODx bits. Then<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 * apply new RSELx values. Finally, apply new DCOx and MODx bit values.<\/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 DCOCTL = 0x00;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 BCSCTL1 = CALBC1_1MHZ;\u00a0\u00a0\u00a0\u00a0\u00a0 \/* Set DCO to 1MHz *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 DCOCTL = CALDCO_1MHZ;<\/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 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Basic Clock System Control 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 * XT2OFF -- Disable XT2CLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~XTS -- Low Frequency<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * DIVA_0 -- Divide by 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 * Note: ~XTS indicates that XTS has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 \u00a0\u00a0*\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 BCSCTL1 |= XT2OFF | DIVA_0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Basic Clock System Control 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 * XT2S_0 -- 0.4 - 1 MHz<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * LFXT1S_0 -- If XTS = 0, XT1 = 32768kHz Crystal ; If XTS = 1, XT1 = 0.4 - 1-MHz crystal or resonator<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * XCAP_1 -- ~6 pF<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 BCSCTL3 = XT2S_0 | LFXT1S_0 | XCAP_1;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: BCSplus_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: BCSplus_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>GPIO_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: GPIO_graceInit_prologue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User initialization code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: GPIO_graceInit_prologue) *\/<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Output Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1OUT = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Direction Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1DIR = BIT0 | BIT6;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Interrupt Edge Select Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1IES = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Interrupt Flag Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1IFG = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Output Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2OUT = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Port Select Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2SEL &amp;= ~(BIT6 | BIT7);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Direction Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2DIR = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Interrupt Edge Select Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2IES = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Interrupt Flag Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2IFG = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: GPIO_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: GPIO_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>Timer0_A3_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: Timer0_A3_graceInit_prologue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User initialization code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: Timer0_A3_graceInit_prologue) *\/<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * TA0CTL, Timer_A3 Control Register<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * TASSEL_2 -- SMCLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ID_3 -- Divider - \/8<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * MC_2 -- Continuous Mode<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 TA0CTL = TASSEL_2 | ID_3 | MC_2;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: Timer0_A3_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: Timer0_A3_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>System_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: System_graceInit_prologue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User initialization code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: System_graceInit_prologue) *\/<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * SR, Status Register<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~SCG1 -- Disable System clock generator 1<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~SCG0 -- Disable System clock generator 0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~OSCOFF -- Oscillator On<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~CPUOFF -- CPU On<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * GIE -- General interrupt enable<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>__bis_SR_register<\/strong>(GIE);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: System_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: System_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>WDTplus_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: RTC_B_graceInit_prologue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User initialization code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: RTC_B_graceInit_prologue) *\/<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * WDTCTL, Watchdog Timer+ Register<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * WDTPW -- Watchdog password<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * WDTHOLD -- Watchdog timer+ is stopped<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTNMIES -- NMI on rising edge<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTNMI -- Reset function<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTTMSEL -- Watchdog mode<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTCNTCL -- No action<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTSSEL -- SMCLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTIS0 -- Watchdog clock source bit0 disabled<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTIS1 -- Watchdog clock source bit1 disabled<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 WDTCTL = WDTPW | WDTHOLD;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: RTC_B_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: RTC_B_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<p>&nbsp;<\/p>\n<h2>Simulation<a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Free-Running-Timer.png\"><br \/>\n<\/a><\/h2>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Free-Running-Timer.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13746\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Free-Running-Timer-580x244.png\" alt=\"Free Running Timer Simulation\" width=\"580\" height=\"244\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Free-Running-Timer-580x244.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Free-Running-Timer-1024x431.png 1024w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Free-Running-Timer.png 1184w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<h2>Explanation<\/h2>\n<p style=\"text-align: justify\">In this demo, Timer_A3 is clocked with SMCLK divided by 8. SMCLK has a frequency of 1MHz and so Timer_A3 is feed with a 125kHz clock. This gives us a timer tick period of 8 microseconds. Since the timer is programmed to operate in continuous mode, i.e. it will count from 0 to top value of 65535, the timer will overflow roughly about every 500 milliseconds. We, thus, have an interval window of 500 milliseconds.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Grace-Free-Running-Timer.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13747\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Grace-Free-Running-Timer-580x428.png\" alt=\"Grace Free Running Timer Settings\" width=\"580\" height=\"428\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Grace-Free-Running-Timer-580x428.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Grace-Free-Running-Timer.png 681w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * TA0CTL, Timer_A3 Control Register<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * TASSEL_2 -- SMCLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ID_3 -- Divider - \/8<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * MC_2 -- Continuous Mode<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 TA0CTL = TASSEL_2 | ID_3 | MC_2;<\/span><\/pre>\n<p>We want to blink the Launchpad board\u2019s LEDs at the rate of 250 milliseconds. Thus, we need to check or compare if the counter register <strong><em>TA0R<\/em><\/strong> has gone past 50% of the full scale 16-bit value. This is an event marker because with reference to this mark, we toggle the states of the LEDs. The idea shown here is actually the concept with which PWMs are generated by compare-match principle inside timer hardware and so it is worth understanding this idea.<\/p>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>if<\/strong>(TA0R &gt;= 32768)<\/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\u00a0\u00a0 P1OUT |= BIT6;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 P1OUT &amp;= ~BIT0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 <strong>else<\/strong><\/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\u00a0\u00a0 P1OUT |= BIT0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 P1OUT &amp;= ~BIT6;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/span><\/pre>\n<p>&nbsp;<\/p>\n<h2>Demo<\/h2>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Free-Running-Timer.gif\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13770\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Free-Running-Timer-580x419.gif\" alt=\"Free Running Timer\" width=\"580\" height=\"419\" \/><\/a> <a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Free-Running-Timer-1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13771\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Free-Running-Timer-1-580x419.jpg\" alt=\"Free Running Timer (1)\" width=\"580\" height=\"419\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Free-Running-Timer-1-580x419.jpg 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Free-Running-Timer-1.jpg 590w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a> <a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Free-Running-Timer-2.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13772\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Free-Running-Timer-2-580x419.jpg\" alt=\"Free Running Timer (2)\" width=\"580\" height=\"419\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Free-Running-Timer-2-580x419.jpg 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Free-Running-Timer-2.jpg 590w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p>Demo video: <a href=\"https:\/\/www.youtube.com\/watch?v=OGvswRhb7ds\">https:\/\/www.youtube.com\/watch?v=OGvswRhb7ds<\/a>.<\/p>\n<p style=\"text-align: justify\"><!--more--><\/p>\n<h1>Timer Interrupt<\/h1>\n<p style=\"text-align: justify\">We have already seen how to use a timer as a free-running timer. However, in most cases we will need timer interrupts. Timer interrupts are periodic interrupts which means they occur at fixed intervals just like clock ticks. Owing to this nature we can split multiple tasks and make them appear as if all of them are happening concurrently. For instance, we can use an ADC to measure the temperature of a room while using a timer to periodically update the temperature display. This is the main concept in driving segment displays, dot-matrix displays and many more although it is not the only thing we can do with timer interrupts.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/serial-7-seg-display-2-board-large_default-12x.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13764\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/serial-7-seg-display-2-board-large_default-12x-580x315.jpg\" alt=\"serial-7-seg-display-2-board-large_default-12x\" width=\"580\" height=\"315\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/serial-7-seg-display-2-board-large_default-12x-580x315.jpg 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/serial-7-seg-display-2-board-large_default-12x.jpg 590w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p style=\"text-align: justify\">A timer interrupt is also at the heart of any typical <strong><em>Real-Time Operating System (RTOS)<\/em><\/strong>.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/RTOS.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-13762\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/RTOS.png\" alt=\"Basic RTOS Concept\" width=\"572\" height=\"560\" \/><\/a><\/p>\n<p style=\"text-align: justify\">Here we will stick to a simple example of a seven-segment display-based second counter. Rather than demoing LED blinking with timer interrupt I chose this example because this has many applications in the field of displaying information on LED-based displays. The same concept can be expanded for dot-matrix displays, LED bar graphs, alphanumerical segmented displays and many more.<\/p>\n<h2>Code Example<\/h2>\n<pre><span style=\"color: #0000ff\"><strong>#include<\/strong> &lt;msp430.h&gt;<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>unsigned<\/strong> <strong>int<\/strong> ms = 0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>unsigned<\/strong> <strong>int<\/strong> value = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>unsigned<\/strong> <strong>char<\/strong> n = 0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>unsigned<\/strong> <strong>char<\/strong> seg = 0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>const<\/strong> <strong>unsigned<\/strong> <strong>char<\/strong> num[10] = {0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8, 0x80, 0x90};<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>GPIO_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>BCSplus_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>Timer0_A3_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>System_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>WDTplus_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#pragma<\/strong> vector=TIMER0_A1_VECTOR<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>__interrupt<\/strong> <strong>void<\/strong> <strong>TIMER0_A1_ISR_HOOK<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 ms++;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>if<\/strong>(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\u00a0\u00a0 ms = 0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 value++;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>if<\/strong>(value &gt; 9999)<\/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\u00a0\u00a0 value = 0;<\/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 }<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>switch<\/strong>(seg)<\/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 <strong>case<\/strong> 1:<\/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\u00a0\u00a0 n = (value \/ 1000);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 P2OUT = num[n];<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 P1OUT = 0xE0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>break<\/strong>;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 <strong>case<\/strong> 2:<\/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\u00a0\u00a0 n = ((value \/ 100) % 10);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 P2OUT = num[n];<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 P1OUT = 0xD0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>break<\/strong>;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 <strong>case<\/strong> 3:<\/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\u00a0\u00a0 n = ((value \/ 10) % 10);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 P2OUT = num[n];<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 P1OUT = 0xB0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>break<\/strong>;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 <strong>case<\/strong> 4:<\/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\u00a0\u00a0 n = (value % 10);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 P2OUT = num[n];<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 P1OUT = 0x70;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>break<\/strong>;<\/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 }<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 seg++;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>if<\/strong>(seg &gt; 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\u00a0\u00a0 seg = 1;<\/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 TA0CTL &amp;= ~TAIFG;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 TAIV &amp;= ~TA0IV_TAIFG;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>main<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>GPIO_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* initialize Config for the MSP430 2xx family clock systems (BCS) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>BCSplus_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* initialize Config for the MSP430 A3 Timer0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>Timer0_A3_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* initialize Config for the MSP430 System Registers *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>System_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* initialize Config for the MSP430 WDT+ *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>WDTplus_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>while<\/strong>(1)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>GPIO_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: GPIO_graceInit_prologue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User initialization code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: GPIO_graceInit_prologue) *\/<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Output Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1OUT = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Direction Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1DIR = BIT4 | BIT5 | BIT6 | BIT7;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Interrupt Edge Select Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1IES = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Interrupt Flag Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1IFG = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Output Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2OUT = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Port Select Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2SEL &amp;= ~(BIT6 | BIT7);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Direction Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2DIR = BIT0 | BIT1 | BIT2 | BIT3 | BIT4 | BIT5 | BIT6 | BIT7;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Interrupt Edge Select Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2IES = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Interrupt Flag Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2IFG = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: GPIO_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: GPIO_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>BCSplus_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: BCSplus_graceInit_prologue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User initialization code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: BCSplus_graceInit_prologue) *\/<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Basic Clock System Control 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 * SELM_0 -- DCOCLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * DIVM_0 -- Divide by 1<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~SELS -- DCOCLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * DIVS_0 -- Divide by 1<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~DCOR -- DCO uses internal resistor<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 BCSCTL2 = SELM_0 | DIVM_0 | DIVS_0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>if<\/strong> (CALBC1_1MHZ != 0xFF) {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/* Follow recommended flow. First, clear all DCOx and MODx bits. Then<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 * apply new RSELx values. Finally, apply new DCOx and MODx bit values.<\/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 DCOCTL = 0x00;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 BCSCTL1 = CALBC1_1MHZ;\u00a0\u00a0\u00a0\u00a0\u00a0 \/* Set DCO to 1MHz *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 DCOCTL = CALDCO_1MHZ;<\/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 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Basic Clock System Control 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 * XT2OFF -- Disable XT2CLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~XTS -- Low Frequency<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * DIVA_0 -- Divide by 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 * Note: ~XTS indicates that XTS has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 BCSCTL1 |= XT2OFF | DIVA_0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Basic Clock System Control 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 * XT2S_0 -- 0.4 - 1 MHz<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * LFXT1S_0 -- If XTS = 0, XT1 = 32768kHz Crystal ; If XTS = 1, XT1 = 0.4 - 1-MHz crystal or resonator<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * XCAP_1 -- ~6 pF<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 BCSCTL3 = XT2S_0 | LFXT1S_0 | XCAP_1;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: BCSplus_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: BCSplus_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>Timer0_A3_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: Timer0_A3_graceInit_prologue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User initialization code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: Timer0_A3_graceInit_prologue) *\/<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* TA0CCR0, Timer_A Capture\/Compare Register 0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 TA0CCR0 = 999;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * TA0CTL, Timer_A3 Control Register<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * TASSEL_2 -- SMCLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ID_0 -- Divider - \/1<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * MC_1 -- Up Mode<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 TA0CTL = TASSEL_2 | ID_0 | MC_1 | TAIE;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: Timer0_A3_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 \u00a0\u00a0\/* USER CODE END (section: Timer0_A3_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>System_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: System_graceInit_prologue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User initialization code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: System_graceInit_prologue) *\/<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * SR, Status Register<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~SCG1 -- Disable System clock generator 1<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~SCG0 -- Disable System clock generator 0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~OSCOFF -- Oscillator On<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~CPUOFF -- CPU On<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * GIE -- General interrupt enable<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>__bis_SR_register<\/strong>(GIE);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: System_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: System_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>WDTplus_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: RTC_B_graceInit_prologue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User initialization code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: RTC_B_graceInit_prologue) *\/<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * WDTCTL, Watchdog Timer+ Register<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * WDTPW -- Watchdog password<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * WDTHOLD -- Watchdog timer+ is stopped<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTNMIES -- NMI on rising edge<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTNMI -- Reset function<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTTMSEL -- Watchdog mode<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTCNTCL -- No action<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTSSEL -- SMCLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTIS0 -- Watchdog clock source bit0 disabled<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \u00a0* ~WDTIS1 -- Watchdog clock source bit1 disabled<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 WDTCTL = WDTPW | WDTHOLD;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: RTC_B_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: RTC_B_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<p>&nbsp;<\/p>\n<h2>Simulation<a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Timer-ISR.png\"><br \/>\n<\/a><\/h2>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Timer-ISR.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13765\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Timer-ISR-580x228.png\" alt=\"Timer Interrupt Simulation\" width=\"580\" height=\"228\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Timer-ISR-580x228.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Timer-ISR-1024x402.png 1024w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Timer-ISR.png 1135w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<h2>Explanation<\/h2>\n<p style=\"text-align: justify\">In this demo, Timer_A3 is configured as an up counting timer, i.e. it will count from 0 to a top value determined by the contents of <strong><em>TA0CCR0<\/em><\/strong> register. \u00a0Again, SMCLK is used as the clock source for the timer but this time it is not scaled down. SMCLK is set to 1 MHz and so does Timer_A3. This means every one tick of Timer_A3 is one microsecond in duration. To make it appear that all four seven segments are simultaneously on without any flickering, we need to scan them fast enough to fool our eyes. We also have to ensure that each segment gets enough time to light up properly. In order to do so we need to scan the segments at one millisecond rate. To get one millisecond from a timer with one microsecond tick interval, we have to load it with 999, not 1000. This is so because from 0 to 999 the total number of ticks is 1000. In short, 1000 times 1 microsecond equals 1 millisecond. Thus, TA0CCR0 is loaded with 999.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Grace-Timer-Interrupt.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13754\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Grace-Timer-Interrupt-580x419.png\" alt=\"Grace Settings for Timer Interrupt \" width=\"580\" height=\"419\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Grace-Timer-Interrupt-580x419.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Grace-Timer-Interrupt.png 689w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* TA0CCR0, Timer_A Capture\/Compare Register 0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 TA0CCR0 = 999;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * TA0CTL, Timer_A3 Control Register<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * TASSEL_2 -- SMCLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ID_0 -- Divider - \/1<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * MC_1 -- Up Mode<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 TA0CTL = TASSEL_2 | ID_0 | MC_1 | TAIE;<\/span><\/pre>\n<p style=\"text-align: justify\">Everything else in this demo is done inside Timer_A3 interrupt subroutine (ISR). There are two parts inside the ISR. The first as shown below is responsible for counting time.<\/p>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 ms++;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>if<\/strong>(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\u00a0\u00a0 ms = 0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 value++;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>if<\/strong>(value &gt; 9999)<\/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\u00a0\u00a0 value = 0;<\/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 }<\/span><\/pre>\n<p style=\"text-align: justify\">The other portion is tasked with the LED segment scanning and data display part. At every millisecond, a new segment is turned on, keeping others off. At the end of the ISR, interrupt flags are cleared.<\/p>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>switch<\/strong>(seg)<\/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 <strong>case<\/strong> 1:<\/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\u00a0\u00a0 n = (value \/ 1000);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 P2OUT = num[n];<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 P1OUT = 0xE0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>break<\/strong>;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 <strong>case<\/strong> 2:<\/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\u00a0\u00a0 n = ((value \/ 100) % 10);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 P2OUT = num[n];<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0P1OUT = 0xD0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>break<\/strong>;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 <strong>case<\/strong> 3:<\/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\u00a0\u00a0 n = ((value \/ 10) % 10);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 P2OUT = num[n];<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 P1OUT = 0xB0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>break<\/strong>;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 <strong>case<\/strong> 4:<\/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\u00a0\u00a0 n = (value % 10);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0P2OUT = num[n];<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 P1OUT = 0x70;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>break<\/strong>;<\/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 }<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 seg++;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>if<\/strong>(seg &gt; 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\u00a0\u00a0 seg = 1;<\/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 TA0CTL &amp;= ~TAIFG;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 TAIV &amp;= ~TA0IV_TAIFG;<\/span><\/pre>\n<p>&nbsp;<\/p>\n<h2>Demo<\/h2>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Timer-ISR.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13773\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Timer-ISR-580x390.jpg\" alt=\"Timer Interrupt Demo\" width=\"580\" height=\"390\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Timer-ISR-580x390.jpg 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Timer-ISR.jpg 590w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p style=\"text-align: justify\">Demo video: <a href=\"https:\/\/www.youtube.com\/watch?v=8EG9rcOAATo\">https:\/\/www.youtube.com\/watch?v=8EG9rcOAATo<\/a>.<\/p>\n<p style=\"text-align: justify\"><!--more--><\/p>\n<h1>Pulse Width Modulation (PWM)<\/h1>\n<p style=\"text-align: justify\">At present PWM hardware is a must for any microcontroller because in many cases, it is needed to extract something more than ones and zeros from it. For example, consider the case of a sine wave generator. Without the aid of a <strong><em>Digital-to-Analog Converter (DAC)<\/em><\/strong>, generating sine waves seems nearly impossible. However, we can still achieve that using <strong><em>Pulse Width Modulation (PWM)<\/em><\/strong> and some mathematical tricks.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/pic-microcontrollers-examples-in-assembly-language-chapter-05-fig5-5.gif\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13758\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/pic-microcontrollers-examples-in-assembly-language-chapter-05-fig5-5-580x361.gif\" alt=\"PWM Sine Generation\" width=\"580\" height=\"361\" \/><\/a><\/p>\n<p style=\"text-align: justify\">Generating waveforms, pulses with variable widths, patterns, sounds, communications pulses like those in IR remotes, speed control, etc require PWM hardware. Keep in mind that all MSP30x2xxx devices do not have any inbuilt DAC and so if analogue output is needed, we can use PWM with necessary external RC filtering to create analogue output.<\/p>\n<p style=\"text-align: justify\">A few things must be observed before using MSP430 PWM hardware:<\/p>\n<ul style=\"text-align: justify\">\n<li>Timer A PWMs are general purpose PWMs.<\/li>\n<\/ul>\n<ul style=\"text-align: justify\">\n<li>There\u2019s no separate option for dead-time.<\/li>\n<\/ul>\n<ul style=\"text-align: justify\">\n<li>Maximum PWM resolution is 16-bit.<\/li>\n<\/ul>\n<ul style=\"text-align: justify\">\n<li>CC Channel 0 is not like the other two channels. It has limited output options and in GRACE you\u2019ll notice that you can\u2019t set PWM duty cycle.<\/li>\n<\/ul>\n<ul>\n<li style=\"text-align: justify\">PWM frequency and resolution are interdependent.<\/li>\n<\/ul>\n<ul>\n<li>PWM pins are limitedly remappable.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h2>Code Example<\/h2>\n<pre><span style=\"color: #0000ff\"><strong>#include<\/strong> &lt;msp430.h&gt;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#include<\/strong> \"delay.h\"<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>GPIO_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>BCSplus_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>Timer0_A3_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>System_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>WDTplus_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>main<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>unsigned<\/strong> <strong>int<\/strong> pwm_value = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Stop watchdog timer from timing out during initial start-up. *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 WDTCTL = WDTPW | WDTHOLD;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* initialize Config for the MSP430 GPIO *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>GPIO_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* initialize Config for the MSP430 2xx family clock systems (BCS) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>BCSplus_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* initialize Config for the MSP430 A3 Timer0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>Timer0_A3_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 \u00a0\/* initialize Config for the MSP430 System Registers *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>System_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* initialize Config for the MSP430 WDT+ *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>WDTplus_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>while<\/strong>(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\u00a0\u00a0\u00a0\u00a0 <strong>for<\/strong>(pwm_value = 0; pwm_value &lt; 1000; pwm_value++)<\/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\u00a0\u00a0TA0CCR1 = pwm_value;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 TA0CCR2 = pwm_value;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>delay_ms<\/strong>(1);<\/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 <strong>for<\/strong>(pwm_value = 999; pwm_value &gt; 0; pwm_value--)<\/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\u00a0\u00a0 TA0CCR1 = pwm_value;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 TA0CCR2 = pwm_value;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>delay_ms<\/strong>(1);<\/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 };<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>GPIO_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: GPIO_graceInit_prologue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User initialization code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: GPIO_graceInit_prologue) *\/<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Port Select 2 Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1SEL2 = BIT4;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Output Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1OUT = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Port Select Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1SEL = BIT1 | BIT4 | BIT6;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Direction Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1DIR = BIT1 | BIT4 | BIT6;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Interrupt Edge Select Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1IES = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Interrupt Flag Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1IFG = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Output Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2OUT = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Port Select Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2SEL &amp;= ~(BIT6 | BIT7);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Direction Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2DIR = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Interrupt Edge Select Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2IES = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Interrupt Flag Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2IFG = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: GPIO_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: GPIO_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>BCSplus_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: BCSplus_graceInit_prologue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User initialization code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: BCSplus_graceInit_prologue) *\/<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Basic Clock System Control 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 * SELM_0 -- DCOCLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * DIVM_0 -- Divide by 1<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~SELS -- DCOCLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * DIVS_0 -- Divide by 1<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~DCOR -- DCO uses internal resistor<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 BCSCTL2 = SELM_0 | DIVM_0 | DIVS_0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>if<\/strong> (CALBC1_1MHZ != 0xFF) {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/* Follow recommended flow. First, clear all DCOx and MODx bits. Then<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 * apply new RSELx values. Finally, apply new DCOx and MODx bit values.<\/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 DCOCTL = 0x00;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 BCSCTL1 = CALBC1_1MHZ;\u00a0\u00a0\u00a0\u00a0\u00a0 \/* Set DCO to 1MHz *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 DCOCTL = CALDCO_1MHZ;<\/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 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Basic Clock System Control 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 * XT2OFF -- Disable XT2CLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~XTS -- Low Frequency<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * DIVA_0 -- Divide by 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 * Note: ~XTS indicates that XTS has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \u00a0*\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 BCSCTL1 |= XT2OFF | DIVA_0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Basic Clock System Control 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 * XT2S_0 -- 0.4 - 1 MHz<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * LFXT1S_2 -- If XTS = 0, XT1 = VLOCLK ; If XTS = 1, XT1 = 3 - 16-MHz crystal or resonator<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * XCAP_1 -- ~6 pF<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 BCSCTL3 = XT2S_0 | LFXT1S_2 | XCAP_1;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: BCSplus_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: BCSplus_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>Timer0_A3_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: Timer0_A3_graceInit_prologue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User initialization code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: Timer0_A3_graceInit_prologue) *\/<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * TA0CCTL0, Capture\/Compare Control Register 0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * CM_0 -- No Capture<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * CCIS_0 -- CCIxA<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~SCS -- Asynchronous Capture<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~SCCI -- Latched capture signal (read)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~CAP -- Compare mode<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * OUTMOD_4 -- PWM output mode: 4 - Toggle<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 TA0CCTL0 = CM_0 | CCIS_0 | OUTMOD_4;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * TA0CCTL1, Capture\/Compare Control Register 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 * CM_0 -- No Capture<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * CCIS_0 -- CCIxA<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~SCS -- Asynchronous Capture<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~SCCI -- Latched capture signal (read)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~CAP -- Compare mode<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * OUTMOD_3 -- PWM output mode: 3 - PWM set\/reset<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 TA0CCTL1 = CM_0 | CCIS_0 | OUTMOD_3;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * TA0CCTL2, Capture\/Compare Control Register 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 * CM_0 -- No Capture<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * CCIS_0 -- CCIxA<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~SCS -- Asynchronous Capture<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~SCCI -- Latched capture signal (read)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~CAP -- Compare mode<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * OUTMOD_7 -- PWM output mode: 7 - PWM reset\/set<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 TA0CCTL2 = CM_0 | CCIS_0 | OUTMOD_7;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* TA0CCR0, Timer_A Capture\/Compare Register 0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 TA0CCR0 = 999;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* TA0CCR1, Timer_A Capture\/Compare Register 1 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 TA0CCR1 = 10;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* TA0CCR2, Timer_A Capture\/Compare Register 2 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 TA0CCR2 = 10;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * TA0CTL, Timer_A3 Control Register<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * TASSEL_2 -- SMCLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ID_0 -- Divider - \/1<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * MC_1 -- Up Mode<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 TA0CTL = TASSEL_2 | ID_0 | MC_1;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: Timer0_A3_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: Timer0_A3_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>System_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: System_graceInit_prologue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User initialization code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: System_graceInit_prologue) *\/<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Clear oscillator fault flag with software delay *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>do<\/strong><\/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 \/\/ Clear OSC fault flag<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 IFG1 &amp;= ~OFIFG;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ 50us delay<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>__delay_cycles<\/strong>(50);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 } <strong>while<\/strong> (IFG1 &amp; OFIFG);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * SR, Status Register<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~SCG1 -- Disable System clock generator 1<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~SCG0 -- Disable System clock generator 0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~OSCOFF -- Oscillator On<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~CPUOFF -- CPU On<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * GIE -- General interrupt enable<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>__bis_SR_register<\/strong>(GIE);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: System_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: System_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>WDTplus_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: RTC_B_graceInit_prologue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User initialization code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: RTC_B_graceInit_prologue) *\/<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * WDTCTL, Watchdog Timer+ Register<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * WDTPW -- Watchdog password<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * WDTHOLD -- Watchdog timer+ is stopped<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTNMIES -- NMI on rising edge<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTNMI -- Reset function<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTTMSEL -- Watchdog mode<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTCNTCL -- No action<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTSSEL -- SMCLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTIS0 -- Watchdog clock source bit0 disabled<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTIS1 -- Watchdog clock source bit1 disabled<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 WDTCTL = WDTPW | WDTHOLD;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: RTC_B_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: RTC_B_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<p>&nbsp;<\/p>\n<h2>Simulation<\/h2>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/PWM-Sim.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13760\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/PWM-Sim-580x402.png\" alt=\"PWM Simulation\" width=\"580\" height=\"402\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/PWM-Sim-580x402.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/PWM-Sim.png 803w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<h2>Explanation<\/h2>\n<p style=\"text-align: justify\">Just as in the previous example, SMCLK is set to 1 MHz. Timer_A3 is also setup for up counting with a top value of 999, resulting in 1ms time duration. Note no interrupt is used and other CCR registers are loaded with 10 \u2013 an arbitrary value.<\/p>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 \u00a0\/* TA0CCR0, Timer_A Capture\/Compare Register 0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 TA0CCR0 = 999;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* TA0CCR1, Timer_A Capture\/Compare Register 1 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 TA0CCR1 = 10;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* TA0CCR2, Timer_A Capture\/Compare Register 2 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 TA0CCR2 = 10;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * TA0CTL, Timer_A3 Control Register<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * TASSEL_2 -- SMCLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ID_0 -- Divider - \/1<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * MC_1 -- Up Mode<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 TA0CTL = TASSEL_2 | ID_0 | MC_1;<\/span><\/pre>\n<p>Prior to Timer_A3 setup, CC channels are setup. <strong><em>OUTMOD <\/em><\/strong>is that stuff that sets PWM type.<\/p>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * TA0CCTL0, Capture\/Compare Control Register 0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * CM_0 -- No Capture<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * CCIS_0 -- CCIxA<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~SCS -- Asynchronous Capture<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~SCCI -- Latched capture signal (read)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~CAP -- Compare mode<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * OUTMOD_4 -- PWM output mode: 4 - Toggle<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \u00a0* Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 TA0CCTL0 = CM_0 | CCIS_0 | OUTMOD_4;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * TA0CCTL1, Capture\/Compare Control Register 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 * CM_0 -- No Capture<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * CCIS_0 -- CCIxA<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~SCS -- Asynchronous Capture<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 \u00a0\u00a0* ~SCCI -- Latched capture signal (read)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~CAP -- Compare mode<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * OUTMOD_3 -- PWM output mode: 3 - PWM set\/reset<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 TA0CCTL1 = CM_0 | CCIS_0 | OUTMOD_3;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * TA0CCTL2, Capture\/Compare Control Register 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 * CM_0 -- No Capture<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * CCIS_0 -- CCIxA<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~SCS -- Asynchronous Capture<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~SCCI -- Latched capture signal (read)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~CAP -- Compare mode<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * OUTMOD_7 -- PWM output mode: 7 - PWM reset\/set<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 TA0CCTL2 = CM_0 | CCIS_0 | OUTMOD_7;<\/span><\/pre>\n<p>In this demo, three different PWMs are set to show the differences.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Grace-PWM1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13748\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Grace-PWM1-580x419.png\" alt=\"Grace PWM1\" width=\"580\" height=\"419\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Grace-PWM1-580x419.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Grace-PWM1.png 686w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a> <a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Grace-PWM2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13749\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Grace-PWM2-580x430.png\" alt=\"Grace PWM2\" width=\"580\" height=\"430\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Grace-PWM2-580x430.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Grace-PWM2.png 670w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a> <a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Grace-PWM3.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13750\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Grace-PWM3-580x437.png\" alt=\"Grace PWM3\" width=\"580\" height=\"437\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Grace-PWM3-580x437.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Grace-PWM3.png 668w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p>PWMs can be of the following types:<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/PWM-Modes.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13759\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/PWM-Modes-580x238.png\" alt=\"PWM Modes\" width=\"580\" height=\"238\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/PWM-Modes-580x238.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/PWM-Modes-1024x421.png 1024w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/PWM-Modes.png 1093w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p style=\"text-align: justify\">Note that CCR0 has limited PWM options and it is the value of its register that sets PWM frequency. The other CCR registers are loaded with values that determine respective PWM duty cycles.<\/p>\n<h2>Demo<\/h2>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/PWM-1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13776\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/PWM-1-580x215.jpg\" alt=\"PWM Demo 1\" width=\"580\" height=\"215\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/PWM-1-580x215.jpg 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/PWM-1.jpg 590w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a> <a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/PWM-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13777\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/PWM-1-580x243.png\" alt=\"PWM Capture Demo 1\" width=\"580\" height=\"243\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/PWM-1-580x243.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/PWM-1.png 590w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a> <a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/PWM-2.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13778\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/PWM-2-580x238.jpg\" alt=\"PWM Demo 2\" width=\"580\" height=\"238\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/PWM-2-580x238.jpg 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/PWM-2.jpg 590w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a> <a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/PWM-2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13779\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/PWM-2-580x243.png\" alt=\"PWM Capture Demo 2\" width=\"580\" height=\"243\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/PWM-2-580x243.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/PWM-2.png 590w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p>Demo video: <a href=\"https:\/\/www.youtube.com\/watch?v=dbSPi6LbsQg\">https:\/\/www.youtube.com\/watch?v=dbSPi6LbsQg<\/a>.<\/p>\n<p style=\"text-align: center\"><!--more--><\/p>\n<h1>Timer Input Capture<\/h1>\n<p style=\"text-align: justify\">In many cases, it is needed to measure the timing info like period, frequency, duty cycle, etc of an incoming signal. Based on these data we can find out the RPM of a robot\u2019s wheel, the pulse widths of an IR remote stream carrying command information, the frequency of AC mains, the patterns of an incoming waveform, etc. Hence comes the purpose of timer input captures.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Capture.gif\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13742\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Capture-580x365.gif\" alt=\"Capture\" width=\"580\" height=\"365\" \/><\/a><\/p>\n<p style=\"text-align: justify\">We know that Timer A3 has three such CC channels and so there are three capture inputs per Timer A module. We can use these channels to capture incoming waveforms of unknown frequencies\/duty cycles and have them measured with respect to a known clock like SMCLK.<\/p>\n<p style=\"text-align: justify\">Again a few things must be observed before using MSP430 input capture hardware:<\/p>\n<ul style=\"text-align: justify\">\n<li>Signals coming to input pins must never cross the max. VDD limit or fall below ground level (i.e. sensing negative potentials).<\/li>\n<\/ul>\n<ul style=\"text-align: justify\">\n<li>It is better to galvanically isolate input pins if they are to sense external high voltage signals.<\/li>\n<\/ul>\n<ul style=\"text-align: justify\">\n<li>Input pins must not be left floating.<\/li>\n<\/ul>\n<ul style=\"text-align: justify\">\n<li>Unless needed, it is wise not to use RC filters for inputs.<\/li>\n<\/ul>\n<ul style=\"text-align: justify\">\n<li>Input capture pins are limited remappable just like PWM pins.<\/li>\n<\/ul>\n<ul style=\"text-align: justify\">\n<li>Timer clock must be set as such that we get maximum measurement resolution without compromising reliability.<\/li>\n<\/ul>\n<ul style=\"text-align: justify\">\n<li>Timer overruns\/overflows must be taken into account.<\/li>\n<\/ul>\n<ul>\n<li style=\"text-align: justify\">Timer capture inputs can be tied to power pins internally and connecting so results in no measurements. When not capturing anything, select stop mode and clear the timer.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h2>Code Example<\/h2>\n<pre><span style=\"color: #0000ff\"><strong>#include<\/strong> &lt;msp430.h&gt;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#include<\/strong> \"delay.h\"<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#include<\/strong> \"lcd.h\"<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>unsigned<\/strong> <strong>int<\/strong> overflow_count = 0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>unsigned<\/strong> <strong>int<\/strong> pulse_ticks = 0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>unsigned<\/strong> <strong>int<\/strong> start_time = 0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>unsigned<\/strong> <strong>int<\/strong> end_time = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>GPIO_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>BCSplus_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>Timer0_A3_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>Timer1_A3_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>System_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>WDTplus_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>lcd_print<\/strong>(<strong>unsigned<\/strong> <strong>char<\/strong> x_pos, <strong>unsigned<\/strong> <strong>char<\/strong> y_pos, <strong>unsigned<\/strong> <strong>long<\/strong> value);<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#pragma<\/strong> vector=TIMER1_A1_VECTOR<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>__interrupt<\/strong> <strong>void<\/strong> <strong>TIMER1_A1_ISR_HOOK<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>if<\/strong>(TA1IV == TA1IV_TACCR1)<\/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 end_time = TA1CCR1;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 pulse_ticks = (end_time - start_time);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 start_time = end_time;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 TA1CCTL1 &amp;= ~CCIFG;<\/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><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>main<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>unsigned<\/strong> <strong>char<\/strong> i = 0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>unsigned<\/strong> <strong>long<\/strong> time_period = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Stop watchdog timer from timing out during initial start-up. *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 WDTCTL = WDTPW | WDTHOLD;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* initialize Config for the MSP430 GPIO *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>GPIO_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* initialize Config for the MSP430 2xx family clock systems (BCS) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>BCSplus_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* initialize Config for the MSP430 A3 Timer0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>Timer0_A3_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* initialize Config for the MSP430 A3 Timer0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>Timer1_A3_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* initialize Config for the MSP430 System Registers *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>System_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* initialize Config for the MSP430 WDT+ *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>WDTplus_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_init<\/strong>();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_clear_home<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_goto<\/strong>(0, 0);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_putstr<\/strong>(\"Capt.\/us:\");<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>delay_ms<\/strong>(10);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>while<\/strong>(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\u00a0\u00a0 <strong>if<\/strong>((P1IN &amp; BIT3) == 0)<\/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\u00a0\u00a0 P1OUT |= BIT0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>while<\/strong>((P1IN &amp; BIT3) == 0);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 i++;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0<strong>if<\/strong>(i &gt; 9)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 i = 0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 P1OUT &amp;= ~BIT0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 <strong>switch<\/strong>(i)<\/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\u00a0\u00a0 <strong>case<\/strong> 1:<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 TA0CCR0 = 9999;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>LCD_goto<\/strong>(0, 1);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>LCD_putstr<\/strong>(\"Period\/ms:\u00a0\u00a0\u00a0 20\");<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>break<\/strong>;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 <strong>case<\/strong> 2:<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 TA0CCR0 = 4999;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>LCD_goto<\/strong>(0, 1);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>LCD_putstr<\/strong>(\"Period\/ms:\u00a0\u00a0\u00a0 10\");<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>break<\/strong>;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 <strong>case<\/strong> 3:<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 TA0CCR0 = 1999;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>LCD_goto<\/strong>(0, 1);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>LCD_putstr<\/strong>(\"Period\/ms:\u00a0\u00a0\u00a0\u00a0 4\");<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>break<\/strong>;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 <strong>case<\/strong> 4:<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 TA0CCR0 = 999;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>LCD_goto<\/strong>(0, 1);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>LCD_putstr<\/strong>(\"Period\/ms:\u00a0\u00a0\u00a0\u00a0 2\");<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>break<\/strong>;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 <strong>case<\/strong> 5:<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 TA0CCR0 = 166;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>LCD_goto<\/strong>(0, 1);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>LCD_putstr<\/strong>(\"Period\/us:\u00a0\u00a0 334\");<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>break<\/strong>;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 <strong>case<\/strong> 6:<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 TA0CCR0 = 1230;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>LCD_goto<\/strong>(0, 1);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>LCD_putstr<\/strong>(\"Period\/ms:\u00a0\u00a0 2.5\");<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>break<\/strong>;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 <strong>case<\/strong> 7:<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 TA0CCR0 = 2626;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>LCD_goto<\/strong>(0, 1);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>LCD_putstr<\/strong>(\"Period\/ms:\u00a0\u00a0 5.3\");<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>break<\/strong>;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 <strong>case<\/strong> 8:<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 TA0CCR0 = 4579;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>LCD_goto<\/strong>(0, 1);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>LCD_putstr<\/strong>(\"Period\/ms:\u00a0\u00a0 9.2\");<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>break<\/strong>;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 <strong>case<\/strong> 9:<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 TA0CCR0 = 499;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>LCD_goto<\/strong>(0, 1);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>LCD_putstr<\/strong>(\"Period\/ms:\u00a0\u00a0\u00a0\u00a0 1\");<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>break<\/strong>;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 <strong>default<\/strong>:<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 TA0CCR0 = 6964;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>LCD_goto<\/strong>(0, 1);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>LCD_putstr<\/strong>(\"Period\/ms:\u00a0 13.9\");<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>break<\/strong>;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 }<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 time_period = (pulse_ticks &gt;&gt; 1);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>lcd_print<\/strong>(10, 0, time_period);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>delay_ms<\/strong>(400);<\/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><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>GPIO_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: GPIO_graceInit_prologue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User initialization code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: GPIO_graceInit_prologue) *\/<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Output Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 \u00a0\u00a0P1OUT = BIT3;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Port Select Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1SEL = BIT1;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Direction Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1DIR = BIT0 | BIT1 | BIT6 | BIT7;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Resistor Enable Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1REN = BIT3;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Interrupt Edge Select Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1IES = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Interrupt Flag Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1IFG = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Output Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2OUT = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Port Select Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2SEL = BIT1;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Port Select Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2SEL &amp;= ~(BIT6 | BIT7);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Direction Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2DIR = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Interrupt Edge Select Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2IES = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Interrupt Flag Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2IFG = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 3 Output Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P3OUT = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 3 Direction Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P3DIR = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: GPIO_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: GPIO_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>BCSplus_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: BCSplus_graceInit_prologue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User initialization code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: BCSplus_graceInit_prologue) *\/<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Basic Clock System Control 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 * SELM_0 -- DCOCLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * DIVM_0 -- Divide by 1<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~SELS -- DCOCLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * DIVS_0 -- Divide by 1<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~DCOR -- DCO uses internal resistor<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 BCSCTL2 = SELM_0 | DIVM_0 | DIVS_0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>if<\/strong> (CALBC1_1MHZ != 0xFF) {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/* Follow recommended flow. First, clear all DCOx and MODx bits. Then<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 * apply new RSELx values. Finally, apply new DCOx and MODx bit values.<\/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 DCOCTL = 0x00;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 BCSCTL1 = CALBC1_1MHZ;\u00a0\u00a0\u00a0\u00a0\u00a0 \/* Set DCO to 1MHz *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 DCOCTL = CALDCO_1MHZ;<\/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 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Basic Clock System Control 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 * XT2OFF -- Disable XT2CLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~XTS -- Low Frequency<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * DIVA_0 -- Divide by 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 * Note: ~XTS indicates that XTS has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 BCSCTL1 |= XT2OFF | DIVA_0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Basic Clock System Control 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 * XT2S_0 -- 0.4 - 1 MHz<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * LFXT1S_2 -- If XTS = 0, XT1 = VLOCLK ; If XTS = 1, XT1 = 3 - 16-MHz crystal or resonator<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * XCAP_1 -- ~6 pF<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 BCSCTL3 = XT2S_0 | LFXT1S_2 | XCAP_1;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: BCSplus_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: BCSplus_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>Timer0_A3_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: Timer0_A3_graceInit_prologue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User initialization code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: Timer0_A3_graceInit_prologue) *\/<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * TA0CCTL0, Capture\/Compare Control Register 0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * CM_0 -- No Capture<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * CCIS_0 -- CCIxA<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~SCS -- Asynchronous Capture<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~SCCI -- Latched capture signal (read)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~CAP -- Compare mode<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * OUTMOD_4 -- PWM output mode: 4 - Toggle<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 TA0CCTL0 = CM_0 | CCIS_0 | OUTMOD_4;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* TA0CCR0, Timer_A Capture\/Compare Register 0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 TA0CCR0 = 9999;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * TA0CTL, Timer_A3 Control Register<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * TASSEL_2 -- SMCLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ID_1 -- Divider - \/2<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * MC_1 -- Up Mode<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 TA0CTL = TASSEL_2 | ID_1 | MC_1;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: Timer0_A3_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: Timer0_A3_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>Timer1_A3_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: Timer1_A3_graceInit_prologue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User initialization code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: Timer1_A3_graceInit_prologue) *\/<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 \u00a0\u00a0* TA1CCTL0, Capture\/Compare Control Register 0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * CM_1 -- Rising Edge<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * CCIS_0 -- CCIxA<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~SCS -- Asynchronous Capture<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~SCCI -- Latched capture signal (read)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~CAP -- Compare mode<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * OUTMOD_1 -- PWM output mode: 1 - Set<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 TA1CCTL0 = CM_1 | CCIS_0 | OUTMOD_1;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * TA1CCTL1, Capture\/Compare Control Register 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 * CM_1 -- Rising Edge<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * CCIS_0 -- CCIxA<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * SCS -- Sychronous Capture<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~SCCI -- Latched capture signal (read)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * CAP -- Capture mode<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * OUTMOD_0 -- PWM output mode: 0 - OUT bit value<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~SCCI indicates that SCCI has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 TA1CCTL1 = CM_1 | CCIS_0 | SCS | CAP | OUTMOD_0 | CCIE;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * TA1CTL, Timer_A3 Control Register<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * TASSEL_2 -- SMCLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ID_0 -- Divider - \/1<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * MC_2 -- Continuous Mode<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 TA1CTL = TASSEL_2 | ID_0 | MC_2;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: Timer1_A3_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: Timer1_A3_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>System_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: System_graceInit_prologue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User initialization code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: System_graceInit_prologue) *\/<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Clear oscillator fault flag with software delay *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>do<\/strong><\/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 \/\/ Clear OSC fault flag<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 IFG1 &amp;= ~OFIFG;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ 50us delay<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>__delay_cycles<\/strong>(50);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 } <strong>while<\/strong> (IFG1 &amp; OFIFG);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * SR, Status Register<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~SCG1 -- Disable System clock generator 1<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~SCG0 -- Disable System clock generator 0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~OSCOFF -- Oscillator On<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~CPUOFF -- CPU On<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * GIE -- General interrupt enable<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>__bis_SR_register<\/strong>(GIE);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: System_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: System_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>WDTplus_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: RTC_B_graceInit_prologue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User initialization code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: RTC_B_graceInit_prologue) *\/<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * WDTCTL, Watchdog Timer+ Register<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * WDTPW -- Watchdog password<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * WDTHOLD -- Watchdog timer+ is stopped<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTNMIES -- NMI on rising edge<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTNMI -- Reset function<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTTMSEL -- Watchdog mode<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTCNTCL -- No action<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTSSEL -- SMCLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTIS0 -- Watchdog clock source bit0 disabled<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTIS1 -- Watchdog clock source bit1 disabled<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 WDTCTL = WDTPW | WDTHOLD;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: RTC_B_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: RTC_B_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>lcd_print<\/strong>(<strong>unsigned<\/strong> <strong>char<\/strong> x_pos, <strong>unsigned<\/strong> <strong>char<\/strong> y_pos, <strong>unsigned<\/strong> <strong>long<\/strong> value)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>char<\/strong> tmp[6] = {0x20, 0x20, 0x20, 0x20, 0x20, 0x20} ;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 tmp[0] = (((value \/ 100000) % 10) + 0x30);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 tmp[1] = (((value \/ 10000) % 10) + 0x30);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 tmp[2] = (((value \/ 1000) % 10) + 0x30);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 tmp[3] = (((value \/ 100) % 10) + 0x30);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 tmp[4] = (((value \/ 10) % 10) + 0x30);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 tmp[5] = ((value % 10) + 0x30);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_goto<\/strong>(x_pos, y_pos);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_putstr<\/strong>(tmp);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<p>&nbsp;<\/p>\n<h2>Simulation<\/h2>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Capture-sim1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13789\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Capture-sim1-580x480.png\" alt=\"Capture sim\" width=\"580\" height=\"480\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Capture-sim1-580x480.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Capture-sim1.png 910w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<h2>Explanation<\/h2>\n<p style=\"text-align: justify\">For this demo, I used a MSP430G2553 micro as it has two independent timers. One of these timers is used as a waveform generator and the other as a waveform capture unit.<\/p>\n<p style=\"text-align: justify\">Timer0_A3 is set up as a PWM output generator with only CC channel 0 active. CC channel 0 is set just to toggle its output logic states and not duty cycle. SMCLK is divided by two and feed to this timer, i.e. this timer has a tick frequency of 500 kHz. As stated before, in order to change the PWM frequency, we need to change the value of TA0CCR0 in up mode counting. We will employ this fact to alter waveform frequency in the main loop.<\/p>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * TA0CCTL0, Capture\/Compare Control Register 0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * CM_0 -- No Capture<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * CCIS_0 -- CCIxA<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~SCS -- Asynchronous Capture<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~SCCI -- Latched capture signal (read)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~CAP -- Compare mode<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * OUTMOD_4 -- PWM output mode: 4 - Toggle<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 TA0CCTL0 = CM_0 | CCIS_0 | OUTMOD_4;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* TA0CCR0, Timer_A Capture\/Compare Register 0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 TA0CCR0 = 9999;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * TA0CTL, Timer_A3 Control Register<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * TASSEL_2 -- SMCLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ID_1 -- Divider - \/2<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * MC_1 -- Up Mode<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 TA0CTL = TASSEL_2 | ID_1 | MC_1;<\/span><\/pre>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Grace-Timer-Capture-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13751\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Grace-Timer-Capture-1-580x421.png\" alt=\"Grace Timer Capture 1\" width=\"580\" height=\"421\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Grace-Timer-Capture-1-580x421.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Grace-Timer-Capture-1.png 678w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p style=\"text-align: justify\">The other timer &#8211; Timer1_A3 is setup to capture the waveform generated by Timer0_A3. Timer interrupt is used to quickly respond to incoming waveform\u2019s rising edges. We will be measuring the time period of Timer0_A3\u2019s waveform and so we are interest in measuring the time difference between like edges \u2013 here rising edges. To get high accuracy, SMCLK is not divided. This gives us a minimum capture period of 1 microsecond and a maximum of about 65 milliseconds provided that we are using continuous mode counting. In other words, Timer1_A3 has double scanning rate than what Timer0_A3 can throw at it \u2013 a concept similar to <a href=\"https:\/\/en.wikipedia.org\/wiki\/Nyquist%E2%80%93Shannon_sampling_theorem\">Nyquist theorem<\/a>.<\/p>\n<pre><span style=\"color: #0000ff\">\/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * TA1CCTL0, Capture\/Compare Control Register 0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * CM_1 -- Rising Edge<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * CCIS_0 -- CCIxA<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~SCS -- Asynchronous Capture<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~SCCI -- Latched capture signal (read)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~CAP -- Compare mode<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * OUTMOD_1 -- PWM output mode: 1 - Set<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 TA1CCTL0 = CM_1 | CCIS_0 | OUTMOD_1;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * TA1CCTL1, Capture\/Compare Control Register 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 * CM_1 -- Rising Edge<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * CCIS_0 -- CCIxA<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * SCS -- Sychronous Capture<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~SCCI -- Latched capture signal (read)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * CAP -- Capture mode<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * OUTMOD_0 -- PWM output mode: 0 - OUT bit value<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~SCCI indicates that SCCI has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 TA1CCTL1 = CM_1 | CCIS_0 | SCS | CAP | OUTMOD_0 | CCIE;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * TA1CTL, Timer_A3 Control Register<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 \u00a0\u00a0*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * TASSEL_2 -- SMCLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ID_0 -- Divider - \/1<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * MC_2 -- Continuous Mode<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 TA1CTL = TASSEL_2 | ID_0 | MC_2;<\/span><\/pre>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Grace-Timer-Capture-2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13752\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Grace-Timer-Capture-2-580x422.png\" alt=\"Grace Timer Capture 2\" width=\"580\" height=\"422\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Grace-Timer-Capture-2-580x422.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Grace-Timer-Capture-2.png 684w\" 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\/2017\/08\/Grace-Timer-Capture-3.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13753\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Grace-Timer-Capture-3-580x436.png\" alt=\"Grace Timer Capture 3\" width=\"580\" height=\"436\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Grace-Timer-Capture-3-580x436.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Grace-Timer-Capture-3.png 667w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p style=\"text-align: justify\">In the timer ISR, we need to check first what caused the interrupt. If it was due to a rising edge capture then we have to take note of current time count in TA1CCR1 since we are using input capture channel 1 of Timer1_A3. Two such time counts are needed to find out the time difference between two adjacent rising edges. This gives us the time period of the captured incoming waveform. However, we are not stopping input capture or the timer even after two successive capture events. This is because we are continuous monitoring the incoming waveform.<\/p>\n<pre><span style=\"color: #0000ff\"><strong>#pragma<\/strong> vector=TIMER1_A1_VECTOR<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>__interrupt<\/strong> <strong>void<\/strong> <strong>TIMER1_A1_ISR_HOOK<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>if<\/strong>(TA1IV == TA1IV_TACCR1)<\/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 end_time = TA1CCR1;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 pulse_ticks = (end_time - start_time);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 start_time = end_time;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 TA1CCTL1 &amp;= ~CCIFG;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 }<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<p style=\"text-align: justify\">In the main loop, we are using the Launchpad\u2019s user button to alter TA0CCR0\u2019s value and hence PWM frequency or period. There are ten different time periods to select. The main code also displays captured waveform time period vs expected time period on a LCD.<\/p>\n<pre><strong>\u00a0 \u00a0 \u00a0 <span style=\"color: #0000ff\">\u00a0 if<\/span><\/strong><span style=\"color: #0000ff\">((P1IN &amp; BIT3) == 0)<\/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\u00a0\u00a0 P1OUT |= BIT0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0<strong>while<\/strong>((P1IN &amp; BIT3) == 0);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 i++;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>if<\/strong>(i &gt; 9)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 i = 0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 P1OUT &amp;= ~BIT0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 <strong>switch<\/strong>(i)<\/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\u00a0\u00a0 <strong>case<\/strong> 1:<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 TA0CCR0 = 9999;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>LCD_goto<\/strong>(0, 1);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>LCD_putstr<\/strong>(\"Period\/ms:\u00a0\u00a0\u00a0 20\");<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>break<\/strong>;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 <strong>case<\/strong> 2:<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 TA0CCR0 = 4999;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>LCD_goto<\/strong>(0, 1);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>LCD_putstr<\/strong>(\"Period\/ms:\u00a0\u00a0\u00a0 10\");<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>break<\/strong>;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 <strong>case<\/strong> 3:<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 TA0CCR0 = 1999;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>LCD_goto<\/strong>(0, 1);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>LCD_putstr<\/strong>(\"Period\/ms:\u00a0\u00a0\u00a0\u00a0 4\");<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>break<\/strong>;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 <strong>case<\/strong> 4:<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 TA0CCR0 = 999;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>LCD_goto<\/strong>(0, 1);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>LCD_putstr<\/strong>(\"Period\/ms:\u00a0\u00a0\u00a0\u00a0 2\");<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>break<\/strong>;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 <strong>case<\/strong> 5:<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 TA0CCR0 = 166;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>LCD_goto<\/strong>(0, 1);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<strong>LCD_putstr<\/strong>(\"Period\/us:\u00a0\u00a0 334\");<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>break<\/strong>;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 <strong>case<\/strong> 6:<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 TA0CCR0 = 1230;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>LCD_goto<\/strong>(0, 1);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>LCD_putstr<\/strong>(\"Period\/ms:\u00a0\u00a0 2.5\");<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>break<\/strong>;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 <strong>case<\/strong> 7:<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 TA0CCR0 = 2626;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>LCD_goto<\/strong>(0, 1);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>LCD_putstr<\/strong>(\"Period\/ms:\u00a0\u00a0 5.3\");<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>break<\/strong>;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 <strong>case<\/strong> 8:<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 TA0CCR0 = 4579;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<strong>LCD_goto<\/strong>(0, 1);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>LCD_putstr<\/strong>(\"Period\/ms:\u00a0\u00a0 9.2\");<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>break<\/strong>;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 <strong>case<\/strong> 9:<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 TA0CCR0 = 499;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>LCD_goto<\/strong>(0, 1);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>LCD_putstr<\/strong>(\"Period\/ms:\u00a0\u00a0\u00a0\u00a0 1\");<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>break<\/strong>;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 <strong>default<\/strong>:<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 TA0CCR0 = 6964;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>LCD_goto<\/strong>(0, 1);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>LCD_putstr<\/strong>(\"Period\/ms:\u00a0 13.9\");<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>break<\/strong>;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 }<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 time_period = (pulse_ticks &gt;&gt; 1);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>lcd_print<\/strong>(10, 0, time_period);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>delay_ms<\/strong>(400);<\/span><\/pre>\n<p>&nbsp;<\/p>\n<h2>Demo<\/h2>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Capture-1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13782\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Capture-1-580x227.jpg\" alt=\"Capture (1)\" width=\"580\" height=\"227\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Capture-1-580x227.jpg 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Capture-1.jpg 590w\" 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\/2017\/08\/Capture-Logic.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13784\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Capture-Logic-580x113.png\" alt=\"Capture Logic\" width=\"580\" height=\"113\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Capture-Logic-580x113.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Capture-Logic-1024x200.png 1024w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Capture-Logic.png 1298w\" 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\/2017\/08\/Capture-2.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13783\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Capture-2-580x245.jpg\" alt=\"Capture (2)\" width=\"580\" height=\"245\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Capture-2-580x245.jpg 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Capture-2.jpg 590w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p style=\"text-align: justify\">Demo video: <a href=\"https:\/\/www.youtube.com\/watch?v=CHLkXf8MqbQ\">https:\/\/www.youtube.com\/watch?v=CHLkXf8MqbQ<\/a>.<\/p>\n<p style=\"text-align: justify\"><!--more--><\/p>\n<h1>Watchdog Timer Plus (WDT+)<\/h1>\n<p>At present, any commercial\/industrial\/professional electronic good must pass a number of tests and obtain some certifications before its introduction to market, most notably <strong><em>CE<\/em><\/strong>,<strong><em> FCC<\/em><\/strong>,<strong><em> UL <\/em><\/strong>and<strong><em> TUV<\/em><\/strong> certifications. This is so as it is imperative that a device pass <strong><em>Electromagnetic Compliance (EMC)<\/em><\/strong> test not just for flawless performance but also for user safety. Hobby electronics projects don\u2019t need these and most hobbyists don\u2019t fully understand the issues caused by EMI or what causes them. This is why many simple robots like the line follower robot shown below fail to perform properly in robotics competitions. Some of them seem to behave erratically while others seem to be unresponsive after working for some time. If both hardware and software designs are well designed and tested against harsh conditions, the chances of failure reduce significantly. A hardware designer should consider proper PCB layout and component placement as well and component selection. Likewise, a programmer should avoid polling-based solutions, blocking codes, unwanted loops, and should consider using watchdog timers and other coding tricks. To avoid getting a device into a stalled state, both hardware and software ends must merge properly and accordingly.<a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/WDT-Line-Follower.jpg\"><br \/>\n<\/a><\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/WDT-Line-Follower.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13767\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/WDT-Line-Follower-580x410.jpg\" alt=\"WDT+ Line Follower\" width=\"580\" height=\"410\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/WDT-Line-Follower-580x410.jpg 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/WDT-Line-Follower.jpg 590w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p style=\"text-align: justify\">A watchdog timer is basically a fail-safe time. It is a very important module when considering an embedded-system-based design that is likely to operate in noisy environments or when there is a probability of its the application firmware to get stuck due to malfunctions. Any programmer would want to get that stuck up firmware up and running again after recovering from the issue that cause it to fail. All MSP430s are equipped with a WDT module and here we will see how it helps us in recovering it when we simulate an entry into an unanticipated loop.<\/p>\n<h2>Code Example<\/h2>\n<pre><span style=\"color: #0000ff\"><strong>#include<\/strong> &lt;msp430.h&gt;<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>GPIO_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>BCSplus_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>InterruptVectors_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>System_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>WDTplus_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>main<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/\/ Stop WDT+<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 WDTCTL = WDTPW + WDTHOLD; \/\/ Set hold bit and clear others<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* initialize Config for the MSP430 GPIO *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>GPIO_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* initialize Config for the MSP430 2xx family clock systems (BCS) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>BCSplus_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* initialize Config for the MSP430 System Registers *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>System_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* initialize Config for the MSP430 WDT+ *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>WDTplus_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>while<\/strong>(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\u00a0\u00a0 P1OUT ^= BIT0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 _delay_cycles(60000);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 WDTCTL = WDTPW | WDTCNTCL;<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>if<\/strong>((P1IN &amp; BIT3) == !BIT3)<\/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\u00a0\u00a0 WDTCTL = WDTPW | WDTSSEL;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>while<\/strong>(1)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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\u00a0P1OUT ^= BIT6;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 _delay_cycles(45000);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 }<\/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><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>GPIO_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: GPIO_graceInit_prologue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User initialization code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: GPIO_graceInit_prologue) *\/<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Output Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1OUT = BIT3;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Direction Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1DIR = BIT0 | BIT6;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Resistor Enable Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1REN = BIT3;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Interrupt Edge Select Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1IES = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Interrupt Flag Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1IFG = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Output Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2OUT = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Port Select Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2SEL &amp;= ~(BIT6 | BIT7);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Direction Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2DIR = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Interrupt Edge Select Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2IES = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Interrupt Flag Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2IFG = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: GPIO_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: GPIO_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>BCSplus_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: BCSplus_graceInit_prologue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User initialization code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: BCSplus_graceInit_prologue) *\/<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Basic Clock System Control 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 * SELM_0 -- DCOCLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * DIVM_0 -- Divide by 1<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~SELS -- DCOCLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * DIVS_0 -- Divide by 1<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~DCOR -- DCO uses internal resistor<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 BCSCTL2 = SELM_0 | DIVM_0 | DIVS_0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>if<\/strong> (CALBC1_1MHZ != 0xFF) {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/* Follow recommended flow. First, clear all DCOx and MODx bits. Then<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 * apply new RSELx values. Finally, apply new DCOx and MODx bit values.<\/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 DCOCTL = 0x00;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 BCSCTL1 = CALBC1_1MHZ;\u00a0\u00a0\u00a0\u00a0\u00a0 \/* Set DCO to 1MHz *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 DCOCTL = CALDCO_1MHZ;<\/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 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Basic Clock System Control 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 * XT2OFF -- Disable XT2CLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~XTS -- Low Frequency<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * DIVA_3 -- Divide by 8<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~XTS indicates that XTS has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 BCSCTL1 |= XT2OFF | DIVA_3;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Basic Clock System Control 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 * XT2S_0 -- 0.4 - 1 MHz<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * LFXT1S_2 -- If XTS = 0, XT1 = VLOCLK ; If XTS = 1, XT1 = 3 - 16-MHz crystal or resonator<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * XCAP_1 -- ~6 pF<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 BCSCTL3 = XT2S_0 | LFXT1S_2 | XCAP_1;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: BCSplus_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: BCSplus_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>System_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: System_graceInit_prologue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User initialization code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: System_graceInit_prologue) *\/<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Clear oscillator fault flag with software delay *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>do<\/strong><\/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 \/\/ Clear OSC fault flag<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 IFG1 &amp;= ~OFIFG;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ 50us delay<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0<strong>__delay_cycles<\/strong>(50);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 } <strong>while<\/strong> (IFG1 &amp; OFIFG);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * SR, Status Register<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~SCG1 -- Disable System clock generator 1<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~SCG0 -- Disable System clock generator 0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~OSCOFF -- Oscillator On<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~CPUOFF -- CPU On<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * GIE -- General interrupt enable<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>__bis_SR_register<\/strong>(GIE);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: System_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: System_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>WDTplus_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: RTC_B_graceInit_prologue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User initialization code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: RTC_B_graceInit_prologue) *\/<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * WDTCTL, Watchdog Timer+ Register<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * WDTPW -- Watchdog password<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTHOLD -- Watchdog timer+ is not stopped<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTNMIES -- NMI on rising edge<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTNMI -- Reset function<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTTMSEL -- Watchdog mode<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTCNTCL -- No action<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * WDTSSEL -- ACLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTIS0 -- Watchdog clock source bit0 disabled<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTIS1 -- Watchdog clock source bit1 disabled<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 WDTCTL = WDTPW | WDTSSEL;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: RTC_B_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: RTC_B_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<p>&nbsp;<\/p>\n<h2>Simulation<\/h2>\n<p><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/WDT-Sim.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13768 aligncenter\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/WDT-Sim-580x249.png\" alt=\"WDT+ Simulation\" width=\"580\" height=\"249\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/WDT-Sim-580x249.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/WDT-Sim.png 747w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<h2>Explanation<\/h2>\n<p style=\"text-align: justify\">Unlike previous timer examples, 12 kHz ACLK is used. ACLK is divided by 8 to make it 1.5 kHz low speed clock source for the WDT+ module. This clock is further prescaled by 32768 to get a WDT+ timeout of about 22 seconds.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Grace-WDT-.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-13755\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Grace-WDT-.png\" alt=\"Grace WDT+ Settings\" width=\"455\" height=\"271\" \/><\/a><\/p>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * WDTCTL, Watchdog Timer+ Register<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * WDTPW -- Watchdog password<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTHOLD -- Watchdog timer+ is not stopped<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTNMIES -- NMI on rising edge<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTNMI -- Reset function<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTTMSEL -- Watchdog mode<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTCNTCL -- No action<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * WDTSSEL -- ACLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTIS0 -- Watchdog clock source bit0 disabled<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTIS1 -- Watchdog clock source bit1 disabled<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 WDTCTL = WDTPW | WDTSSEL;<\/span><\/pre>\n<p style=\"text-align: justify\">In the main loop, P1.0 toggles without any issue. WDT+ is regularly refreshed. However, when the user button is pressed, WDT+ is no longer refreshed and P1.6 LED is toggled inside a simulated undesired loop. P1.0 LED appears to have gotten stuck. This causes the WDT+ to cross maximum timeout limit and thereby trigger a reset.<\/p>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 P1OUT ^= BIT0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 _delay_cycles(60000);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 WDTCTL = WDTPW | WDTCNTCL;<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<strong>if<\/strong>((P1IN &amp; BIT3) == !BIT3)<\/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\u00a0\u00a0 WDTCTL = WDTPW | WDTSSEL;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>while<\/strong>(1)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 P1OUT ^= BIT6;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 _delay_cycles(45000);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 }<\/span><\/pre>\n<p style=\"text-align: justify\">In reality, the timeout time may vary due to variations in ACLK time period. In my demo, I noticed this variation. During that time, I found that ACLK is about 10 kHz instead of 12 kHz.<\/p>\n<h2>Demo<\/h2>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/WDT-1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13785\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/WDT-1-580x432.jpg\" alt=\"WDT+ (1)\" width=\"580\" height=\"432\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/WDT-1-580x432.jpg 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/WDT-1.jpg 590w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a> <a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/WDT-2.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13786\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/WDT-2-580x431.jpg\" alt=\"WDT+ (2)\" width=\"580\" height=\"431\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/WDT-2-580x431.jpg 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/WDT-2.jpg 590w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p>Demo video: <a href=\"https:\/\/www.youtube.com\/watch?v=vYCLeWZZt7U\">https:\/\/www.youtube.com\/watch?v=vYCLeWZZt7U<\/a>.<\/p>\n<p><!--more--><\/p>\n<h1>WDT+ as an Interval Timer<\/h1>\n<p style=\"text-align: justify\">There are cases in which we don\u2019t need the protection feature of WDT+. This leaves with a free timer which can be used for other jobs. As I said before, Americans think differently than the rest of the world and here is one proof of that ingenious concept. However, since WDT+ was intended for a special mission, we cannot expect it to be completely like other timers. For example, it doesn\u2019t have any capture-compare pin associated with it nor do we have access to its counter. Even with these limitations, it is still a useful bonus.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/rtos2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13763\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/rtos2-580x347.png\" alt=\"rtos2\" width=\"580\" height=\"347\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/rtos2-580x347.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/rtos2-600x360.png 600w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/rtos2.png 666w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p>A lame demonstration of RTOS concept is demoed here.<\/p>\n<p>&nbsp;<\/p>\n<h2>Code Example<\/h2>\n<pre><span style=\"color: #0000ff\"><strong>#include<\/strong> &lt;msp430.h&gt;<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>unsigned<\/strong> <strong>char<\/strong> state = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>GPIO_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>BCSplus_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>System_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>WDTplus_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0*\u00a0 ======== Watchdog Timer Interval Interrupt Handler Generation ========<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0*\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#pragma<\/strong> vector=WDT_VECTOR<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>__interrupt<\/strong> <strong>void<\/strong> <strong>WDT_ISR_HOOK<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 state++;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>if<\/strong>(state &gt;= 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\u00a0\u00a0 state = 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 IFG1 &amp;= ~WDTIFG;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>main<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>unsigned<\/strong> <strong>int<\/strong> s = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>unsigned<\/strong> <strong>char<\/strong> i = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Stop watchdog timer from timing out during initial start-up. *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 WDTCTL = WDTPW | WDTHOLD;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* initialize Config for the MSP430 GPIO *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>GPIO_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* initialize Config for the MSP430 2xx family clock systems (BCS) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>BCSplus_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* initialize Config for the MSP430 System Registers *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>System_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* initialize Config for the MSP430 WDT+ *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>WDTplus_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>while<\/strong>(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\u00a0\u00a0 <strong>switch<\/strong>(state)<\/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\u00a0\u00a0 <strong>case<\/strong> 1:<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 <strong>switch<\/strong>(i)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>case<\/strong> 0:<\/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 {<\/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\u00a0\u00a0 P1OUT &amp;= ~BIT0;<\/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\u00a0\u00a0 <strong>break<\/strong>;<\/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 }<\/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 <strong>default<\/strong>:<\/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 {<\/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\u00a0\u00a0P1OUT |= BIT0;<\/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\u00a0\u00a0 <strong>break<\/strong>;<\/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 }<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>break<\/strong>;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 <strong>case<\/strong> 2:<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 _delay_cycles(1);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 s++;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>if<\/strong>(s &gt; 20000)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 P1OUT ^= BIT6;<\/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 s = 0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>break<\/strong>;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 <strong>default<\/strong>:<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 <strong>if<\/strong>((P1IN &amp; BIT3) != BIT3)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0i ^= BIT0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>break<\/strong>;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 }<\/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><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>GPIO_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: GPIO_graceInit_prologue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User initialization code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: GPIO_graceInit_prologue) *\/<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Output Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1OUT = BIT3;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Direction Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1DIR = BIT0 | BIT6;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Resistor Enable Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1REN = BIT3;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Interrupt Edge Select Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1IES = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Interrupt Flag Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1IFG = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Output Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2OUT = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Port Select Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2SEL &amp;= ~(BIT6 | BIT7);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Direction Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2DIR = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Interrupt Edge Select Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2IES = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Interrupt Flag Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2IFG = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: GPIO_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: GPIO_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>BCSplus_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: BCSplus_graceInit_prologue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User initialization code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: BCSplus_graceInit_prologue) *\/<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Basic Clock System Control 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 * SELM_0 -- DCOCLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * DIVM_0 -- Divide by 1<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~SELS -- DCOCLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * DIVS_0 -- Divide by 1<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~DCOR -- DCO uses internal resistor<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 BCSCTL2 = SELM_0 | DIVM_0 | DIVS_0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>if<\/strong> (CALBC1_1MHZ != 0xFF) {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/* Follow recommended flow. First, clear all DCOx and MODx bits. Then<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 * apply new RSELx values. Finally, apply new DCOx and MODx bit values.<\/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 DCOCTL = 0x00;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 BCSCTL1 = CALBC1_1MHZ;\u00a0\u00a0\u00a0\u00a0\u00a0 \/* Set DCO to 1MHz *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0DCOCTL = CALDCO_1MHZ;<\/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 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Basic Clock System Control 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 * XT2OFF -- Disable XT2CLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~XTS -- Low Frequency<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * DIVA_2 -- Divide by 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 * Note: ~XTS indicates that XTS has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 BCSCTL1 |= XT2OFF | DIVA_2;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Basic Clock System Control 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 * XT2S_0 -- 0.4 - 1 MHz<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * LFXT1S_2 -- If XTS = 0, XT1 = VLOCLK ; If XTS = 1, XT1 = 3 - 16-MHz crystal or resonator<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * XCAP_1 -- ~6 pF<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 BCSCTL3 = XT2S_0 | LFXT1S_2 | XCAP_1;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: BCSplus_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: BCSplus_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>System_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: System_graceInit_prologue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User initialization code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: System_graceInit_prologue) *\/<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Clear oscillator fault flag with software delay *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>do<\/strong><\/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 \/\/ Clear OSC fault flag<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 IFG1 &amp;= ~OFIFG;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ 50us delay<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>__delay_cycles<\/strong>(50);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 } <strong>while<\/strong> (IFG1 &amp; OFIFG);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * IFG1, Interrupt Flag Register 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 * ~ACCVIFG -- No interrupt pending<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~NMIIFG -- No interrupt pending<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~OFIFG -- No interrupt pending<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * WDTIFG -- Interrupt pending<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 IFG1 &amp;= ~(WDTIFG);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * IE1, Interrupt Enable Register 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 * ~ACCVIE -- Interrupt not enabled<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~NMIIE -- Interrupt not enabled<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~OFIE -- Interrupt not enabled<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * WDTIE -- Interrupt enabled<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 IE1 |= WDTIE;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * SR, Status Register<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~SCG1 -- Disable System clock generator 1<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~SCG0 -- Disable System clock generator 0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~OSCOFF -- Oscillator On<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~CPUOFF -- CPU On<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * GIE -- General interrupt enable<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>__bis_SR_register<\/strong>(GIE);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: System_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: System_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>WDTplus_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: RTC_B_graceInit_prologue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User initialization code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: RTC_B_graceInit_prologue) *\/<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * WDTCTL, Watchdog Timer+ Register<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * WDTPW -- Watchdog password<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTHOLD -- Watchdog timer+ is not stopped<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTNMIES -- NMI on rising edge<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTNMI -- Reset function<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * WDTTMSEL -- Interval timer mode<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTCNTCL -- No action<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTSSEL -- SMCLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTIS0 -- Watchdog clock source bit0 disabled<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * WDTIS1 -- Watchdog clock source bit1 enabled<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 WDTCTL = WDTPW | WDTTMSEL | WDTIS1;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: RTC_B_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: RTC_B_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<p>&nbsp;<\/p>\n<h2>Simulation<\/h2>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/WDT-Interval-Mode-Sim.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13769\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/WDT-Interval-Mode-Sim-580x226.png\" alt=\"WDT+ Interval Mode Simulation\" width=\"580\" height=\"226\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/WDT-Interval-Mode-Sim-580x226.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/WDT-Interval-Mode-Sim-1024x400.png 1024w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/WDT-Interval-Mode-Sim.png 1094w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<h2>Explanation<\/h2>\n<p style=\"text-align: justify\">WDT+ is used here as an interval timer with a time period of 512 microseconds. After every 512 microseconds, there is a WDT+ interrupt.<\/p>\n<pre><span style=\"color: #0000ff\">\/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">* WDTCTL, Watchdog Timer+ Register<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">* WDTPW -- Watchdog password<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">* ~WDTHOLD -- Watchdog timer+ is not stopped<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">* ~WDTNMIES -- NMI on rising edge<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">* ~WDTNMI -- Reset function<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">* WDTTMSEL -- Interval timer mode<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">* ~WDTCNTCL -- No action<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">* ~WDTSSEL -- SMCLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">* ~WDTIS0 -- Watchdog clock source bit0 disabled<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">* WDTIS1 -- Watchdog clock source bit1 enabled<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">* Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">*\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">WDTCTL = WDTPW | WDTTMSEL | WDTIS1;<\/span><\/pre>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/WDT-Interval-Mode.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-13738\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/WDT-Interval-Mode.png\" alt=\"Grace WDT+ Interval Mode Settings\" width=\"464\" height=\"271\" \/><\/a><\/p>\n<p>Inside this interrupt we just change the value of a variable called <em>task<\/em>.<\/p>\n<pre><span style=\"color: #0000ff\"><strong>#pragma<\/strong> vector=WDT_VECTOR<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>__interrupt<\/strong> <strong>void<\/strong> <strong>WDT_ISR_HOOK<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 \u00a0\u00a0task++;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>if<\/strong>(task &gt;= 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\u00a0\u00a0 task = 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 IFG1 &amp;= ~WDTIFG;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<p>The variable <em>task <\/em>in the main loop is used to switch between different tasks, each task having same time frame and priority. This is called a task scheduling.<\/p>\n<pre><span style=\"color: #0000ff\"><strong>\u00a0 \u00a0 \u00a0 \u00a0 switch<\/strong>(task)<\/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\u00a0\u00a0 <strong>case<\/strong> 1:<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 <strong>switch<\/strong>(i)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>case<\/strong> 0:<\/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 {<\/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\u00a0\u00a0 P1OUT &amp;= ~BIT0;<\/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\u00a0\u00a0 <strong>break<\/strong>;<\/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 }<\/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 <strong>default<\/strong>:<\/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 {<\/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\u00a0\u00a0 P1OUT |= BIT0;<\/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\u00a0\u00a0 <strong>break<\/strong>;<\/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 }<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>break<\/strong>;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 <strong>case<\/strong> 2:<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 _delay_cycles(1);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0s++;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>if<\/strong>(s &gt; 20000)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 P1OUT ^= BIT6;<\/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 s = 0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>break<\/strong>;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 <strong>default<\/strong>:<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 <strong>if<\/strong>((P1IN &amp; BIT3) != BIT3)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 i ^= BIT0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>break<\/strong>;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 }<\/span><\/pre>\n<p style=\"text-align: justify\">Task 1 lights P1.0 LED based on the logic state of the user button. Task 0 checks the state of the user button. Task 2 blinks P1.6 LED independent of the other tasks. The total time for the completion of all these processes is about 1.5 milliseconds \u2013 a very short time. In this method, no task waits for other tasks. The whole process is so fast to human eyes that everything this code does will appear to occur parallelly.<\/p>\n<h2>Demo<\/h2>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/WDT-IM-1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13787\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/WDT-IM-1-580x367.jpg\" alt=\"WDT IM (1)\" width=\"580\" height=\"367\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/WDT-IM-1-580x367.jpg 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/WDT-IM-1.jpg 590w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a> <a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/WDT-IM-2.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13788\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/WDT-IM-2-580x379.jpg\" alt=\"WDT IM (2)\" width=\"580\" height=\"379\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/WDT-IM-2-580x379.jpg 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/WDT-IM-2.jpg 590w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p>Demo video: <a href=\"https:\/\/www.youtube.com\/watch?v=BNwxdgLQerU\">https:\/\/www.youtube.com\/watch?v=BNwxdgLQerU<\/a>.<\/p>\n<p><!--more--><\/p>\n<h1>Analogue Frontend Overview<\/h1>\n<p style=\"text-align: justify\"><strong><em>MSP<\/em><\/strong> stands for mixed signal processor. Mixed signal means combination of both analogue and digital. We can, therefore, expect a great deal of cool stuffs when it comes to their analogue features. Chips vary in features and so do the analogue peripherals. There are various types of analogue peripherals offered by MSP430s and we can categorize them into four basic categories:<\/p>\n<h2>Analogue-to-Digital Converters (ADC)<\/h2>\n<p style=\"text-align: justify\">ADC are used to measure time-varying voltages. They digitize analogue signals by representing them in quantized binary formats. The most common ADCs in MSP430s are <strong><em>ADC10<\/em><\/strong> and <strong><em>ADC12<\/em><\/strong>. These are <strong><em>Successive Approximation (SAR)<\/em><\/strong> ADCs. Both of these ADCs are similar in many aspects except in resolution. Some MSP430 devices have more advanced high-resolution delta-sigma ADCs like <strong><em>SD16_A and<\/em><\/strong> <strong><em>SD24_A<\/em><\/strong>. All MSP430s additionally have internal temperature sensors.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/ADC.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13794\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/ADC-580x304.png\" alt=\"ADC\" width=\"580\" height=\"304\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/ADC-580x304.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/ADC.png 883w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<h2>Digital-to-Analogue Converters (DAC)<\/h2>\n<p style=\"text-align: justify\">DACs are opposites of ADCs. They give variable voltage output with respect to binary inputs and can be used to generate waveforms, audio signals, wave patterns, control actuator and power supplies, etc. 12-bit DACs &#8211; <strong><em>DAC12<\/em><\/strong> are available in some advanced MSP430 devices.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/DAC.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13796\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/DAC-580x294.png\" alt=\"DAC\" width=\"580\" height=\"294\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/DAC-580x294.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/DAC.png 872w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<h2>Comparators (COMP)<\/h2>\n<p style=\"text-align: justify\">A comparator compares two analogue voltage levels. This comparison results in an indication of which signal is at a higher\/lower voltage level than the other. In simple terms, it is a one-bit ADC. Though it may look that a comparator is unnecessary when we have a good built-in ADC, it is otherwise. A comparator is a very important analogue building block. A whole lot of electronics is based on it. Examples of such electronics include oscillators, level sensing, VU meters, capacitive touch sensing, measurement devices, etc. A LC meter is a perfect example. A LC meter is usually based on an oscillator. This oscillator uses a comparator. Its frequency varies with the L and C components, oscillating at a fixed frequency with known L and C values. Measuring frequency shifts as a result of changing L\/C values leads us to measure unknown L\/C effectively.<a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Comp.png\"><br \/>\n<\/a><\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Comp.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13795\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Comp-580x210.png\" alt=\"Comp\" width=\"580\" height=\"210\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Comp-580x210.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Comp.png 706w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<h2>Op-Amps (OA)<\/h2>\n<p style=\"text-align: justify\">Some MSP430s are equipped with single supply general-purpose Op-Amps. These can be used like any other external Op-Amps but they have wide variety of goodies like PGA built-in. We can use them as comparators (although it is unnecessary in the presence of comparator modules) signal amplifiers, etc. We can also use them to make filters, oscillators, analogue computers, etc.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/OA.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13797\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/OA-580x246.png\" alt=\"OA\" width=\"580\" height=\"246\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/OA-580x246.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/OA.png 687w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p style=\"text-align: center\"><!--more--><\/p>\n<h1>Comp_A+ Module<\/h1>\n<p style=\"text-align: justify\">Apart from ADCs MSP430x2xx devices are equipped with an analogue comparator called <strong><em>Comparator A+ <\/em><\/strong>or simply <strong><em>Comp_A+<\/em><\/strong> module.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Block-Diagram3.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13799\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Block-Diagram3-580x495.png\" alt=\"Block Diagram\" width=\"580\" height=\"495\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Block-Diagram3-580x495.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Block-Diagram3.png 601w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p style=\"text-align: justify\">Shown above is the block diagram for Comp A+ module. The left most side includes comparator inputs. A good thing to note is that unlike other micros where comparator pins are generally fixed to some dedicated I\/Os only, the pins of COMP A+ can be tied with a number of I\/O, adding great flexibility in design. The purple region in the centre houses reference sources that can be tied to the comparator inputs. At the comparator output stage (orange area), there is an optional low pass filter. Additionally, comparator inputs can be shorted to remove any stray static. Use GRACE to explore more features of Comp A+.<\/p>\n<h2>Code Example<\/h2>\n<pre><span style=\"color: #0000ff\"><strong>#include<\/strong> &lt;msp430.h&gt;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#include<\/strong> \"delay.h\"<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>BCSplus_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>GPIO_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>Comparator_Aplus_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>System_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>WDTplus_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#pragma<\/strong> vector=COMPARATORA_VECTOR<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>__interrupt<\/strong> <strong>void<\/strong> <strong>COMPARATORA_ISR_HOOK<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1OUT ^= BIT0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 CACTL2 &amp;= ~CAIFG;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>main<\/strong>(void)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Stop watchdog timer from timing out during initial start-up. *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 WDTCTL = WDTPW | WDTHOLD;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* initialize Config for the MSP430 GPIO *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>GPIO_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* initialize Config for the MSP430 2xx family clock systems (BCS) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>BCSplus_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* initialize Config for the MSP430F2xx Comparator_A+ *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>Comparator_Aplus_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* initialize Config for the MSP430 System Registers *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>System_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* initialize Config for the MSP430 WDT+ *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>WDTplus_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>while<\/strong>(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\u00a0\u00a0 <strong>if<\/strong> (CACTL2 &amp; CAOUT)<\/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\u00a0\u00a0 <strong>delay_ms<\/strong>(300);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 <strong>if<\/strong> (!(CACTL2 &amp; CAOUT))<\/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\u00a0\u00a0 <strong>delay_ms<\/strong>(100);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 P1OUT ^= BIT6;<\/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><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>BCSplus_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: BCSplus_graceInit_prologue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User initialization code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: BCSplus_graceInit_prologue) *\/<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Basic Clock System Control 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 * SELM_0 -- DCOCLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * DIVM_0 -- Divide by 1<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~SELS -- DCOCLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * DIVS_0 -- Divide by 1<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~DCOR -- DCO uses internal resistor<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 BCSCTL2 = SELM_0 | DIVM_0 | DIVS_0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>if<\/strong> (CALBC1_8MHZ != 0xFF) {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/* Adjust this accordingly to your VCC rise time *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>__delay_cycles<\/strong>(100000);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Follow recommended flow. First, clear all DCOx and MODx bits. Then<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ apply new RSELx values. Finally, apply new DCOx and MODx bit values.<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 DCOCTL = 0x00;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 BCSCTL1 = CALBC1_8MHZ;\u00a0\u00a0\u00a0\u00a0\u00a0 \/* Set DCO to 8MHz *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 DCOCTL = CALDCO_8MHZ;<\/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 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Basic Clock System Control 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 * XT2OFF -- Disable XT2CLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~XTS -- Low Frequency<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * DIVA_0 -- Divide by 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 * Note: ~XTS indicates that XTS has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 BCSCTL1 |= XT2OFF | DIVA_0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Basic Clock System Control 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 * XT2S_0 -- 0.4 - 1 MHz<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * LFXT1S_0 -- If XTS = 0, XT1 = 32768kHz Crystal ; If XTS = 1, XT1 = 0.4 - 1-MHz crystal or resonator<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * XCAP_1 -- ~6 pF<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 BCSCTL3 = XT2S_0 | LFXT1S_0 | XCAP_1;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: BCSplus_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: BCSplus_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>GPIO_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: GPIO_graceInit_prologue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User initialization code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: GPIO_graceInit_prologue) *\/<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Output Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1OUT = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Port Select Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1SEL = BIT7;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Direction Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1DIR = BIT0 | BIT6 | BIT7;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Interrupt Edge Select Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1IES = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Interrupt Flag Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1IFG = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Output Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2OUT = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Port Select Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2SEL &amp;= ~(BIT6 | BIT7);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Direction Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2DIR = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Interrupt Edge Select Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2IES = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Interrupt Flag Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2IFG = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 3 Output Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P3OUT = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 3 Direction Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P3DIR = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: GPIO_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: GPIO_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>Comparator_Aplus_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: Comparator_Aplus_graceInit_prologue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User initialization code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: Comparator_Aplus_graceInit_prologue) *\/<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* CACTL1 Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 CACTL1 = CAREF_2 | CAON | CAIES | CAIE;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* CACTL2 Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 CACTL2 = P2CA3 | P2CA1;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* CAPD, Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 CAPD = CAPD5;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: Comparator_Aplus_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: Comparator_Aplus_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>System_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: System_graceInit_prologue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User initialization code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: System_graceInit_prologue) *\/<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * SR, Status Register<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~SCG1 -- Disable System clock generator 1<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~SCG0 -- Disable System clock generator 0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~OSCOFF -- Oscillator On<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~CPUOFF -- CPU On<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * GIE -- General interrupt enable<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>__bis_SR_register<\/strong>(GIE);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: System_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: System_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>WDTplus_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: RTC_B_graceInit_prologue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User initialization code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: RTC_B_graceInit_prologue) *\/<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * WDTCTL, Watchdog Timer+ Register<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * WDTPW -- Watchdog password<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * WDTHOLD -- Watchdog timer+ is stopped<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTNMIES -- NMI on rising edge<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTNMI -- Reset function<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTTMSEL -- Watchdog mode<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTCNTCL -- No action<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTSSEL -- SMCLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTIS0 -- Watchdog clock source bit0 disabled<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTIS1 -- Watchdog clock source bit1 disabled<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 WDTCTL = WDTPW | WDTHOLD;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: RTC_B_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: RTC_B_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<p>&nbsp;<\/p>\n<h2>Simulation<\/h2>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Comp_A-Simulation.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13800\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Comp_A-Simulation-580x199.png\" alt=\"Comp_A+ Simulation\" width=\"580\" height=\"199\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Comp_A-Simulation-580x199.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Comp_A-Simulation.png 940w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<h2>Explanation<\/h2>\n<p style=\"text-align: justify\">For this demo, a MSP430G2553 is used. Launchpad board\u2019s LEDs are used and additionally a potentiometer (pot) is tied to P1.5. The pot has its ends connected to VDD and GND. The internal connection is as shown below:<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Grace-Settings1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-13801\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Grace-Settings1.png\" alt=\"Grace Settings\" width=\"366\" height=\"155\" \/><\/a><\/p>\n<p style=\"text-align: justify\">The code dictates that Comp A+ interrupt will occur on falling edges only. Now the question is whose falling edge? Certainly not the inputs. Interrupt will only occur when there is a logic high-low transition on <strong><em>CAOUT<\/em><\/strong> pin \u2013 P1.7 here. According to the simplified comparator internal connection shown above, this transition will occur only when P1.5\u2019s voltage exceeds that of the 1.8V reference. When a Comp_A+ interrupt occurs, P1.0\u2019s logic state is toggled.<\/p>\n<p style=\"text-align: justify\">One thing to note here is the fact that though it is not mandatory to clear comparator interrupt flag, it is wise to clear it after processing the interrupt request.<\/p>\n<h2>Demo<\/h2>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Comp_A-1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13802\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Comp_A-1-580x478.jpg\" alt=\"Comp_A+ (1)\" width=\"580\" height=\"478\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Comp_A-1-580x478.jpg 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Comp_A-1.jpg 590w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a> <a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Comp_A-2.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13803\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Comp_A-2-580x488.jpg\" alt=\"Comp_A+ (2)\" width=\"580\" height=\"488\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Comp_A-2-580x488.jpg 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Comp_A-2.jpg 590w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p style=\"text-align: justify\">Demo video: <a href=\"https:\/\/www.youtube.com\/watch?v=XIzvhpWopiI\">https:\/\/www.youtube.com\/watch?v=XIzvhpWopiI<\/a>.<\/p>\n<p style=\"text-align: justify\"><!--more--><\/p>\n<h1 style=\"text-align: justify\">ADC10<\/h1>\n<p style=\"text-align: justify\">In most value-line devices (VLD) like MSP430G2553 and MSP430G2452, ADC12 is not present and the ADC tasks are accomplished with ADC10 modules.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/ADC10-Block-Diagram.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-13806\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/ADC10-Block-Diagram.png\" alt=\"ADC10 Block Diagram\" width=\"465\" height=\"520\" \/><\/a><\/p>\n<p>Shown above is the simple block diagram of the ADC10 module. ADC10 is a SAR ADC. Highlighted segments include:<\/p>\n<ul>\n<li>ADC channel selector (light blue area) \u2013 select the highest channel from where the first ADC conversion starts. An internal counter counts down from this channel all the way down to A0.<\/li>\n<li>DMA and ADC output (red area) \u2013 here we get AD conversion results and can optionally do a peripheral to memory DMA transfer.<\/li>\n<li>ADC clock source (purple area) \u2013 this is the clock source that runs the ADC.<\/li>\n<li>ADC trigger source (light green area) \u2013 selects what triggers the ADC to start a conversion.<\/li>\n<li>Reference selectors (orange boxes) \u2013 selects ADC\u2019s positive and negative references.<\/li>\n<li>Built-in signal sources (Deep red area) \u2013 includes on-chip temperature sensor, supply voltage sensing voltage divider, etc.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h2>Code Example<\/h2>\n<pre><span style=\"color: #0000ff\"><strong>#include<\/strong> &lt;msp430.h&gt;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#include<\/strong> \"delay.h\"<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#include<\/strong> \"lcd.h\"<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>T_offset<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 -18<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>BCSplus_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>GPIO_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>ADC10_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>System_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>WDTplus_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>unsigned<\/strong> <strong>int<\/strong> <strong>get_ADC<\/strong>(<strong>unsigned<\/strong> <strong>int<\/strong> channel);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>unsigned<\/strong> <strong>int<\/strong> <strong>get_volt<\/strong>(<strong>unsigned<\/strong> <strong>int<\/strong> value);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>unsigned<\/strong> <strong>int<\/strong> <strong>get_temp<\/strong>(<strong>unsigned<\/strong> <strong>int<\/strong> value);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>lcd_print<\/strong>(<strong>unsigned<\/strong> <strong>char<\/strong> x_pos, <strong>unsigned<\/strong> <strong>char<\/strong> y_pos, <strong>unsigned<\/strong> <strong>int<\/strong> value);<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>main<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>unsigned<\/strong> <strong>char<\/strong> n = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>unsigned<\/strong> <strong>int<\/strong> res = 0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>unsigned<\/strong> <strong>int<\/strong> ADC_Value = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Stop watchdog timer from timing out during initial start-up. *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 WDTCTL = WDTPW | WDTHOLD;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* initialize Config for the MSP430 GPIO *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>GPIO_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* initialize Config for the MSP430 2xx family clock systems (BCS) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>BCSplus_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* initialize Config for the MSP430 10-bit Analog to Digital Converter (ADC) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>ADC10_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* initialize Config for the MSP430 System Registers *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>System_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* initialize Config for the MSP430 WDT+ *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>WDTplus_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_init<\/strong>();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_clear_home<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>while<\/strong>(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\u00a0\u00a0 <strong>if<\/strong>((P1IN &amp; BIT3) == 0)<\/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\u00a0\u00a0 P1OUT |= BIT6;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>while<\/strong>((P1IN &amp; BIT3) == 0);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 n++;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>if<\/strong>(n &gt; 2)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 n = 0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 P1OUT &amp;= ~BIT6;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 <strong>switch<\/strong>(n)<\/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\u00a0\u00a0 <strong>case<\/strong> 1:<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 ADC_Value = <strong>get_ADC<\/strong>(INCH_1);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 res = <strong>get_volt<\/strong>(ADC_Value);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>LCD_goto<\/strong>(0, 0);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>LCD_putstr<\/strong>(\"ADC Ch01:\");<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>LCD_goto<\/strong>(0, 1);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>LCD_putstr<\/strong>(\"Volts\/mV:\");<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>break<\/strong>;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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<strong>case<\/strong> 2:<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 ADC_Value = <strong>get_ADC<\/strong>(INCH_2);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 res = <strong>get_volt<\/strong>(ADC_Value);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>LCD_goto<\/strong>(0, 0);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>LCD_putstr<\/strong>(\"ADC Ch02:\");<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>LCD_goto<\/strong>(0, 1);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>LCD_putstr<\/strong>(\"Volts\/mV:\");<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>break<\/strong>;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 <strong>default<\/strong>:<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 ADC_Value = <strong>get_ADC<\/strong>(INCH_10);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 res = <strong>get_temp<\/strong>(<strong>get_volt<\/strong>(ADC_Value));<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>LCD_goto<\/strong>(0, 0);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>LCD_putstr<\/strong>(\"ADC Ch10:\");<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<strong>LCD_goto<\/strong>(0, 1);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>LCD_putstr<\/strong>(\"TC\/Deg.C:\");<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>break<\/strong>;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 }<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>lcd_print<\/strong>(12, 0, ADC_Value);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>lcd_print<\/strong>(12, 1, res);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>delay_ms<\/strong>(200);<\/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><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>GPIO_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: GPIO_graceInit_prologue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User initialization code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: GPIO_graceInit_prologue) *\/<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Output Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1OUT = BIT3;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Direction Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1DIR = BIT0 | BIT6;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Resistor Enable Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1REN = BIT3;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Interrupt Edge Select Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1IES = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Interrupt Flag Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1IFG = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Output Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2OUT = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Port Select Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2SEL &amp;= ~(BIT6 | BIT7);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Direction Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2DIR = BIT0 | BIT1 | BIT2 | BIT3 | BIT4 | BIT5;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Interrupt Edge Select Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2IES = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Interrupt Flag Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2IFG = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 3 Output Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P3OUT = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 3 Direction Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P3DIR = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: GPIO_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: GPIO_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>BCSplus_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: BCSplus_graceInit_prologue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User initialization code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: BCSplus_graceInit_prologue) *\/<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Basic Clock System Control 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 * SELM_0 -- DCOCLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * DIVM_0 -- Divide by 1<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~SELS -- DCOCLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * DIVS_0 -- Divide by 1<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~DCOR -- DCO uses internal resistor<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 BCSCTL2 = SELM_0 | DIVM_0 | DIVS_0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>if<\/strong> (CALBC1_1MHZ != 0xFF) {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/* Follow recommended flow. First, clear all DCOx and MODx bits. Then<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 * apply new RSELx values. Finally, apply new DCOx and MODx bit values.<\/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 DCOCTL = 0x00;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 BCSCTL1 = CALBC1_1MHZ;\u00a0\u00a0\u00a0\u00a0\u00a0 \/* Set DCO to 1MHz *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 DCOCTL = CALDCO_1MHZ;<\/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 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Basic Clock System Control 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 * XT2OFF -- Disable XT2CLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~XTS -- Low Frequency<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * DIVA_0 -- Divide by 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 * Note: ~XTS indicates that XTS has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 BCSCTL1 |= XT2OFF | DIVA_0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Basic Clock System Control 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 * XT2S_0 -- 0.4 - 1 MHz<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * LFXT1S_0 -- If XTS = 0, XT1 = 32768kHz Crystal ; If XTS = 1, XT1 = 0.4 - 1-MHz crystal or resonator<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * XCAP_1 -- ~6 pF<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 BCSCTL3 = XT2S_0 | LFXT1S_0 | XCAP_1;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: BCSplus_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: BCSplus_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>ADC10_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: ADC10_graceInit_prologue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User initialization code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: ADC10_graceInit_prologue) *\/<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* disable ADC10 during initialization *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 ADC10CTL0 &amp;= ~ENC;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Control Register 0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~ADC10SC -- No conversion<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~ENC -- Disable ADC<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~ADC10IFG -- Clear ADC interrupt flag<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~ADC10IE -- Disable ADC interrupt<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ADC10ON -- Switch On ADC10<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~REFON -- Disable ADC reference generator<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~REF2_5V -- Set reference voltage generator = 1.5V<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 \u00a0\u00a0* ~MSC -- Disable multiple sample and conversion<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~REFBURST -- Reference buffer on continuously<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~REFOUT -- Reference output off<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~ADC10SR -- Reference buffer supports up to ~200 ksps<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ADC10SHT_3 -- 64 x ADC10CLKs<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * SREF_0 -- VR+ = VCC and VR- = VSS<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 ADC10CTL0 = ADC10ON | ADC10SHT_3 | SREF_0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Control Register 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 * ~ADC10BUSY -- No operation is active<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * CONSEQ_2 -- Repeat single channel<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ADC10SSEL_3 -- SMCLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ADC10DIV_3 -- Divide by 4<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~ISSH -- Input signal not inverted<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~ADC10DF -- ADC10 Data Format as binary<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * SHS_0 -- ADC10SC<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * INCH_10 -- Temperature Sensor<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 ADC10CTL1 = CONSEQ_2 | ADC10SSEL_3 | ADC10DIV_3 | SHS_0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Analog (Input) Enable Control Register 0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 ADC10AE0 = 0x6;<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* enable ADC10 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 ADC10CTL0 |= ENC;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: ADC10_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: ADC10_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>System_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: System_graceInit_prologue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User initialization code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: System_graceInit_prologue) *\/<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * SR, Status Register<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~SCG1 -- Disable System clock generator 1<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~SCG0 -- Disable System clock generator 0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~OSCOFF -- Oscillator On<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~CPUOFF -- CPU On<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * GIE -- General interrupt enable<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>__bis_SR_register<\/strong>(GIE);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: System_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: System_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>WDTplus_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: RTC_B_graceInit_prologue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User initialization code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: RTC_B_graceInit_prologue) *\/<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * WDTCTL, Watchdog Timer+ Register<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * WDTPW -- Watchdog password<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * WDTHOLD -- Watchdog timer+ is stopped<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTNMIES -- NMI on rising edge<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTNMI -- Reset function<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTTMSEL -- Watchdog mode<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTCNTCL -- No action<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTSSEL -- SMCLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTIS0 -- Watchdog clock source bit0 disabled<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTIS1 -- Watchdog clock source bit1 disabled<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 WDTCTL = WDTPW | WDTHOLD;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: RTC_B_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: RTC_B_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>unsigned<\/strong> <strong>int<\/strong> <strong>get_ADC<\/strong>(<strong>unsigned<\/strong> <strong>int<\/strong> channel)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1OUT ^= BIT0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 ADC10CTL0 &amp;= ~ENC;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 ADC10CTL1 &amp;= ~(0xF000);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 ADC10CTL1 |= channel;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 ADC10CTL0 |= ENC;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 \u00a0ADC10CTL0 |= ADC10SC;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>while<\/strong> ((ADC10CTL0 &amp; ADC10IFG) == 0);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>return<\/strong> ADC10MEM;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>unsigned<\/strong> <strong>int<\/strong> <strong>get_volt<\/strong>(<strong>unsigned<\/strong> <strong>int<\/strong> value)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>return<\/strong> (<strong>unsigned<\/strong> <strong>int<\/strong>)((value * 3600.0) \/ 1023.0);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>unsigned<\/strong> <strong>int<\/strong> <strong>get_temp<\/strong>(<strong>unsigned<\/strong> <strong>int<\/strong> value)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>return<\/strong> (<strong>unsigned<\/strong> <strong>int<\/strong>)((((value \/ 1000.0) - 0.986) \/ 0.00355) + T_offset);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>lcd_print<\/strong>(<strong>unsigned<\/strong> <strong>char<\/strong> x_pos, <strong>unsigned<\/strong> <strong>char<\/strong> y_pos, <strong>unsigned<\/strong> <strong>int<\/strong> value)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>char<\/strong> chr = 0x00;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 chr = ((value \/ 1000) + 0x30);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_goto<\/strong>(x_pos, y_pos);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_putchar<\/strong>(chr);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 chr = (((value \/ 100) % 10) + 0x30);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_goto<\/strong>((x_pos + 1), y_pos);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_putchar<\/strong>(chr);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 chr = (((value \/ 10) % 10) + 0x30);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_goto<\/strong>((x_pos + 2), y_pos);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_putchar<\/strong>(chr);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 chr = ((value % 10) + 0x30);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_goto<\/strong>((x_pos + 3), y_pos);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_putchar<\/strong>(chr);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<p>&nbsp;<\/p>\n<h2>Simulation<\/h2>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/ADC10-Sim.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13810\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/ADC10-Sim-580x261.png\" alt=\"ADC10 Simulation\" width=\"580\" height=\"261\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/ADC10-Sim-580x261.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/ADC10-Sim-1024x460.png 1024w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/ADC10-Sim.png 1106w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<h2>Explanation<\/h2>\n<p>ADC10 is set up initially as depicted below:<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/ADC10-Setup.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13809\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/ADC10-Setup-580x510.png\" alt=\"ADC10 Grace Setup\" width=\"580\" height=\"510\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/ADC10-Setup-580x510.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/ADC10-Setup.png 785w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p style=\"text-align: justify\">Note that we are not using ADC10 interrupt and enabled two external channels (A1 and A2) although only the temperature sensor channel is selected for conversion. The internal temperature sensor has the highest channel number (channel 10) after VDD measurement channel (channel 11). If we also want to sense A1 and A2 along with the internal temperature sensor, we have to select all channels from channel 0 to channel 10 even when we don\u2019t want the others. This is so because in sequence scan mode conversion, all channels are scanned from the highest channel to channel 0. The other way to sense only the desired channels is to sense them one at a time i.e. as single channels. The latter method is used here.<\/p>\n<pre><span style=\"color: #0000ff\"><strong>unsigned<\/strong> <strong>int<\/strong> <strong>get_ADC<\/strong>(<strong>unsigned<\/strong> <strong>int<\/strong> channel)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1OUT ^= BIT0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 ADC10CTL0 &amp;= ~ENC;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 ADC10CTL1 &amp;= ~(0xF000);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 ADC10CTL1 |= channel;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 ADC10CTL0 |= ENC;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 ADC10CTL0 |= ADC10SC;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>while<\/strong> ((ADC10CTL0 &amp; ADC10IFG) == 0);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>return<\/strong> ADC10MEM;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<p>The function above first disables the momentarily and clears channel number or count. Then the desired channel is chosen. AD conversion is started following ADC restart and we wait for AD conversion completion. At the end of the conversion ADC result is extracted and returned.<\/p>\n<p>In the main loop, we simply select channel using the Launchpad user button and display ADC data on a LCD. For ease, I demoed ADC10 using two external channels (A1 and A2) and one internal ADC channel \u2013 the internal temperature sensor.<\/p>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>switch<\/strong>(n)<\/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\u00a0\u00a0 <strong>case<\/strong> 1:<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 ADC_Value = <strong>get_ADC<\/strong>(INCH_1);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 res = <strong>get_volt<\/strong>(ADC_Value);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>LCD_goto<\/strong>(0, 0);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<strong>LCD_putstr<\/strong>(\"ADC Ch01:\");<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>LCD_goto<\/strong>(0, 1);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>LCD_putstr<\/strong>(\"Volts\/mV:\");<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>break<\/strong>;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 <strong>case<\/strong> 2:<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 ADC_Value = <strong>get_ADC<\/strong>(INCH_2);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 res = <strong>get_volt<\/strong>(ADC_Value);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>LCD_goto<\/strong>(0, 0);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>LCD_putstr<\/strong>(\"ADC Ch02:\");<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>LCD_goto<\/strong>(0, 1);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>LCD_putstr<\/strong>(\"Volts\/mV:\");<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>break<\/strong>;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 <strong>default<\/strong>:<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 ADC_Value = <strong>get_ADC<\/strong>(INCH_10);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 res = <strong>get_temp<\/strong>(<strong>get_volt<\/strong>(ADC_Value));<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>LCD_goto<\/strong>(0, 0);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>LCD_putstr<\/strong>(\"ADC Ch10:\");<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>LCD_goto<\/strong>(0, 1);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>LCD_putstr<\/strong>(\"TC\/Deg.C:\");<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>break<\/strong>;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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}<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>lcd_print<\/strong>(12, 0, ADC_Value);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>lcd_print<\/strong>(12, 1, res);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>delay_ms<\/strong>(200);<\/span><\/pre>\n<p>&nbsp;<\/p>\n<h2>Demo<\/h2>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/ADC10-1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-13813\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/ADC10-1.jpg\" alt=\"ADC10 (1)\" width=\"578\" height=\"590\" \/><\/a> <a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/ADC10-2.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13814\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/ADC10-2-580x565.jpg\" alt=\"ADC10 (2)\" width=\"580\" height=\"565\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/ADC10-2-580x565.jpg 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/ADC10-2.jpg 590w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a> <a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/ADC10-3.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13815\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/ADC10-3-580x586.jpg\" alt=\"ADC10 (3)\" width=\"580\" height=\"586\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/ADC10-3-580x586.jpg 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/ADC10-3-90x90.jpg 90w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/ADC10-3.jpg 584w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p>Demo video: <a href=\"https:\/\/www.youtube.com\/watch?v=M_txQs2ajdk\">https:\/\/www.youtube.com\/watch?v=M_txQs2ajdk<\/a>.<\/p>\n<p><!--more--><\/p>\n<h1>ADC10 Interrupt<\/h1>\n<p style=\"text-align: justify\">In previous example, we didn\u2019t use ADC10 interrupt and the code was based on polling. ADC interrupts are as important as timer interrupts. We can start an ADC and extract conversion data in an orderly manner when conversion is complete. No other process waits for the ADC, freeing up the CPU.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Temperature-Sensor-Transfer-Function.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-13811\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Temperature-Sensor-Transfer-Function.png\" alt=\"Temperature Sensor Transfer Function\" width=\"454\" height=\"498\" \/><\/a><\/p>\n<p style=\"text-align: justify\">Many present-day microcontrollers have on-chip temperature sensors. While many people think they are just mere additions, they are not. Such sensors have a number of applications \u2013 most notably correction of ADC readings with temperature drift. Other applications include thermal protection, temperature comphensations, temperature reference, etc. However, these sensors are not meant to be as precise and accurate as dedicated temperature sensor chips like LM35 and DS18B20. This makes them highly unsuitable for measurements of wide range of temperatures and unsuitable for reliable readings. Shown above is the typical voltage output vs temperature graph of MSP430G2xx devices\u2019 internal temperature sensor. Though it is linear, the word <em>\u201ctypical\u201d <\/em>is indirectly telling us that there can be some deviations.<\/p>\n<p style=\"text-align: justify\">In this example, we will see how to read the internal temperature sensor of a MSP430G2xx micro using ADC10 interrupt.<\/p>\n<h2>Code Example<\/h2>\n<pre><span style=\"color: #0000ff\"><strong>#include<\/strong> &lt;msp430.h&gt;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#include<\/strong> \"delay.h\"<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#include<\/strong> \"lcd.h\"<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>T_offset<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 -18<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>unsigned<\/strong> <strong>int<\/strong> ADC_Value = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>GPIO_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>BCSplus_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>ADC10_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>System_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>WDTplus_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>unsigned<\/strong> <strong>int<\/strong> <strong>get_volt<\/strong>(<strong>unsigned<\/strong> <strong>int<\/strong> value);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>unsigned<\/strong> <strong>int<\/strong> <strong>get_temp<\/strong>(<strong>unsigned<\/strong> <strong>int<\/strong> value);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>lcd_print<\/strong>(<strong>unsigned<\/strong> <strong>char<\/strong> x_pos, <strong>unsigned<\/strong> <strong>char<\/strong> y_pos, <strong>unsigned<\/strong> <strong>int<\/strong> value);<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#pragma<\/strong> vector = ADC10_VECTOR<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>__interrupt<\/strong> <strong>void<\/strong> <strong>ADC10_ISR_HOOK<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1OUT ^= BIT0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 ADC_Value = ADC10MEM;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 ADC10CTL0 &amp;= ~ADC10IFG;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>main<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>signed<\/strong> <strong>int<\/strong> t = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 \u00a0\/* Stop watchdog timer from timing out during initial start-up. *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 WDTCTL = WDTPW | WDTHOLD;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* initialize Config for the MSP430 GPIO *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>GPIO_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* initialize Config for the MSP430 2xx family clock systems (BCS) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>BCSplus_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* initialize Config for the MSP430 10-bit Analog to Digital Converter (ADC) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>ADC10_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* initialize Config for the MSP430 System Registers *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>System_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* initialize Config for the MSP430 WDT+ *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>WDTplus_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_init<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_clear_home<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_goto<\/strong>(0, 0);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_putstr<\/strong>(\"ADC Value:\");<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_goto<\/strong>(0, 1);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_putstr<\/strong>(\"Tmp\/Deg.C:\");<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>while<\/strong>(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\u00a0\u00a0 \/\/ ADC Start Conversion - Software trigger<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 ADC10CTL0 |= ADC10SC;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 P1OUT ^= BIT6;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 t = <strong>get_volt<\/strong>(ADC_Value);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 t = <strong>get_temp<\/strong>(t);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>lcd_print<\/strong>(12, 0, ADC_Value);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>lcd_print<\/strong>(12, 1, t);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>delay_ms<\/strong>(200);<\/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><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>GPIO_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Output Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1OUT = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Direction Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1DIR = BIT0 | BIT6;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Interrupt Edge Select Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1IES = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Interrupt Flag Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1IFG = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Output Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2OUT = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Port Select Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2SEL &amp;= ~(BIT6 | BIT7);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Direction Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2DIR = BIT0 | BIT1 | BIT2 | BIT3 | BIT4 | BIT5;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Interrupt Edge Select Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2IES = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Interrupt Flag Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 \u00a0\u00a0P2IFG = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 3 Output Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P3OUT = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 3 Direction Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P3DIR = 0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>BCSplus_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Basic Clock System Control 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 * SELM_0 -- DCOCLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * DIVM_0 -- Divide by 1<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \u00a0* ~SELS -- DCOCLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * DIVS_0 -- Divide by 1<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~DCOR -- DCO uses internal resistor<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 BCSCTL2 = SELM_0 | DIVM_0 | DIVS_0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>if<\/strong> (CALBC1_12MHZ != 0xFF)<\/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 \/* Adjust this accordingly to your VCC rise time *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>__delay_cycles<\/strong>(1000);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/* Follow recommended flow. First, clear all DCOx and MODx bits. Then<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 * apply new RSELx values. Finally, apply new DCOx and MODx bit values.<\/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 DCOCTL = 0x00;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 BCSCTL1 = CALBC1_12MHZ;\u00a0\u00a0\u00a0\u00a0 \/* Set DCO to 12MHz *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 DCOCTL = CALDCO_12MHZ;<\/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 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Basic Clock System Control 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 * XT2OFF -- Disable XT2CLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~XTS -- Low Frequency<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * DIVA_0 -- Divide by 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 * Note: ~XTS indicates that XTS has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 BCSCTL1 |= XT2OFF | DIVA_0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Basic Clock System Control 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 * XT2S_0 -- 0.4 - 1 MHz<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * LFXT1S_2 -- If XTS = 0, XT1 = VLOCLK ; If XTS = 1, XT1 = 3 - 16-MHz crystal or resonator<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * XCAP_1 -- ~6 pF<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 BCSCTL3 = XT2S_0 | LFXT1S_2 | XCAP_1;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>ADC10_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* disable ADC10 during initialization *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 ADC10CTL0 &amp;= ~ENC;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Control Register 0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~ADC10SC -- No conversion<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~ENC -- Disable ADC<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~ADC10IFG -- Clear ADC interrupt flag<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ADC10IE -- Enable ADC interrupt<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ADC10ON -- Switch On ADC10<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~REFON -- Disable ADC reference generator<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~REF2_5V -- Set reference voltage generator = 1.5V<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * MSC -- Enable multiple sample and conversion<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~REFBURST -- Reference buffer on continuously<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~REFOUT -- Reference output off<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ADC10SR -- Reference buffer supports up to ~50 ksps<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ADC10SHT_3 -- 64 x ADC10CLKs<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * SREF_0 -- VR+ = VCC and VR- = VSS<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 ADC10CTL0 = ADC10IE | ADC10ON | MSC | ADC10SR | ADC10SHT_3 | SREF_0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Control Register 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 * ~ADC10BUSY -- No operation is active<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 \u00a0\u00a0* CONSEQ_2 -- Repeat single channel<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ADC10SSEL_3 -- SMCLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ADC10DIV_7 -- Divide by 8<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~ISSH -- Input signal not inverted<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~ADC10DF -- ADC10 Data Format as binary<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * SHS_0 -- ADC10SC<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * INCH_10 -- Temperature Sensor<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 ADC10CTL1 = CONSEQ_2 | ADC10SSEL_3 | ADC10DIV_7 | SHS_0 | INCH_10;<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* enable ADC10 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 ADC10CTL0 |= ENC;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>System_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Clear oscillator fault flag with software delay *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>do<\/strong><\/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 \/\/ Clear OSC fault flag<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 IFG1 &amp;= ~OFIFG;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ 50us delay<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>__delay_cycles<\/strong>(600);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 } <strong>while<\/strong> (IFG1 &amp; OFIFG);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * SR, Status Register<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~SCG1 -- Disable System clock generator 1<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~SCG0 -- Disable System clock generator 0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~OSCOFF -- Oscillator On<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~CPUOFF -- CPU On<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * GIE -- General interrupt enable<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>__bis_SR_register<\/strong>(GIE);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>WDTplus_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * WDTCTL, Watchdog Timer+ Register<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * WDTPW -- Watchdog password<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * WDTHOLD -- Watchdog timer+ is stopped<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTNMIES -- NMI on rising edge<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTNMI -- Reset function<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTTMSEL -- Watchdog mode<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTCNTCL -- No action<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTSSEL -- SMCLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTIS0 -- Watchdog clock source bit0 disabled<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTIS1 -- Watchdog clock source bit1 disabled<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 WDTCTL = WDTPW | WDTHOLD;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>unsigned<\/strong> <strong>int<\/strong> <strong>get_volt<\/strong>(<strong>unsigned<\/strong> <strong>int<\/strong> value)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>return<\/strong> (<strong>unsigned<\/strong> <strong>int<\/strong>)((value * 3600.0) \/ 1023.0);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>unsigned<\/strong> <strong>int<\/strong> <strong>get_temp<\/strong>(<strong>unsigned<\/strong> <strong>int<\/strong> value)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>return<\/strong> (<strong>unsigned<\/strong> <strong>int<\/strong>)((((value \/ 1000.0) - 0.986) \/ 0.00355) + T_offset);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>lcd_print<\/strong>(<strong>unsigned<\/strong> <strong>char<\/strong> x_pos, <strong>unsigned<\/strong> <strong>char<\/strong> y_pos, <strong>unsigned<\/strong> <strong>int<\/strong> value)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>char<\/strong> chr = 0x00;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 chr = ((value \/ 1000) + 0x30);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_goto<\/strong>(x_pos, y_pos);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_putchar<\/strong>(chr);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 chr = (((value \/ 100) % 10) + 0x30);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_goto<\/strong>((x_pos + 1), y_pos);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 \u00a0\u00a0<strong>LCD_putchar<\/strong>(chr);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 chr = (((value \/ 10) % 10) + 0x30);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_goto<\/strong>((x_pos + 2), y_pos);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_putchar<\/strong>(chr);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 chr = ((value % 10) + 0x30);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_goto<\/strong>((x_pos + 3), y_pos);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_putchar<\/strong>(chr);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<p>&nbsp;<\/p>\n<h2>Simulation<\/h2>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/ADC10-ISR-Sim.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13808\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/ADC10-ISR-Sim-580x199.png\" alt=\"ADC10 ISR Simulation\" width=\"580\" height=\"199\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/ADC10-ISR-Sim-580x199.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/ADC10-ISR-Sim-1024x352.png 1024w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/ADC10-ISR-Sim.png 1068w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p><em>*Note Proteus VSM cannot simulate internal temperature sensor. The simulation here only shows connections. The readouts are misleading.<\/em><\/p>\n<p>&nbsp;<\/p>\n<h2>Explanation<\/h2>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/ADC10-ISR-Setup.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13807\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/ADC10-ISR-Setup-580x502.png\" alt=\"ADC10 ISR Grace Setup\" width=\"580\" height=\"502\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/ADC10-ISR-Setup-580x502.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/ADC10-ISR-Setup.png 795w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p style=\"text-align: justify\">ADC10 setup is similar to the setup we have seen in the last example. However, this time we enabled ADC10 interrupt as this is an interrupt-based demo.<\/p>\n<p style=\"text-align: justify\">The ADC ISR is simple. Here we are just toggling P1.0 LED to indicate ADC conversion completion and clearing ADC interrupt flag after reading ADC data value.<\/p>\n<pre><span style=\"color: #0000ff\"><strong>#pragma<\/strong> vector = ADC10_VECTOR<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>__interrupt<\/strong> <strong>void<\/strong> <strong>ADC10_ISR_HOOK<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1OUT ^= BIT0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 ADC_Value = ADC10MEM;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 ADC10CTL0 &amp;= ~ADC10IFG;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<p style=\"text-align: justify\">In the main loop, AD conversion process starts using software trigger. The result of this conversion is extracted from the ADC ISR shown above. The ADC data obtained from the ISR is shown on a LCD. Two values are shown \u2013 actual ADC count and the corresponding temperature against that ADC readout.<\/p>\n<pre><span style=\"color: #0000ff\"><em>\u00a0<\/em>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ ADC Start Conversion - Software trigger<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 ADC10CTL0 |= ADC10SC;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 P1OUT ^= BIT6;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 t = <strong>get_volt<\/strong>(ADC_Value);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0t = <strong>get_temp<\/strong>(t);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>lcd_print<\/strong>(12, 0, ADC_Value);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>lcd_print<\/strong>(12, 1, t);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>delay_ms<\/strong>(200);<\/span><\/pre>\n<p style=\"text-align: justify\">To extract temperature in degree Celsius, first the ADC count is converted to millivolts and then this voltage value is translated to temperature value using the voltage vs temperature transfer function shown earlier. <em>T_offset <\/em>is an optional constant that is used to negate any offset in temperature readout.<\/p>\n<pre><span style=\"color: #0000ff\"><strong>unsigned<\/strong> <strong>int<\/strong> <strong>get_volt<\/strong>(<strong>unsigned<\/strong> <strong>int<\/strong> value)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>return<\/strong> (<strong>unsigned<\/strong> <strong>int<\/strong>)((value * 3600.0) \/ 1023.0);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>unsigned<\/strong> <strong>int<\/strong> <strong>get_temp<\/strong>(<strong>unsigned<\/strong> <strong>int<\/strong> value)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>return<\/strong> (<strong>unsigned<\/strong> <strong>int<\/strong>)((((value \/ 1000.0) - 0.986) \/ 0.00355) + T_offset);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<p>&nbsp;<\/p>\n<h2>Demo<a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/ADC-ISR.jpg\"><br \/>\n<\/a><\/h2>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/ADC-ISR.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13812\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/ADC-ISR-580x547.jpg\" alt=\"ADC ISR\" width=\"580\" height=\"547\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/ADC-ISR-580x547.jpg 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/ADC-ISR.jpg 590w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p style=\"text-align: justify\">Demo video: <a href=\"https:\/\/www.youtube.com\/watch?v=XOlI3Vfjfb8\">https:\/\/www.youtube.com\/watch?v=XOlI3Vfjfb8<\/a>.<\/p>\n<p style=\"text-align: justify\"><!--more--><\/p>\n<h1>Communication Overview<\/h1>\n<p style=\"text-align: justify\">Most MSP430 micros are equipped with <strong><em>Universal Serial Interface (USI) <\/em><\/strong>and<strong><em> Universal Serial Communication Interface (USCI) <\/em><\/strong>modules. Some devices have <strong><em>Universal Asynchronous Receiver-Transmitter (UART) <\/em><\/strong>modules. These interfaces are needed to communicate with external devices like sensors, actuators, drives, other microcontrollers or onboard devices, etc and are responsible for handling the most commonly used serial communications like <strong><em>Universal Asynchronous Receiver-Transmitter (UART)<\/em><\/strong>, <strong><em>Serial Peripheral Interface (SPI)<\/em><\/strong> and <strong><em>Inter-Integrated Circuit (I2C)<\/em><\/strong>. Also, there are other additional more robust communication interfaces like <strong><em>Controller Area Network (CAN)<\/em><\/strong>,<strong><em> Local Interconnect Network (LIN)<\/em><\/strong>,<strong><em> Infrared Data Association (IrDA) <\/em><\/strong>and<strong><em> RS-485<\/em><\/strong>. The latter communications will not be discussed here as they are advanced and are basically extension of the aforementioned serial communications. Each method communication has its own advantages and disadvantages. In the table below, some individual basics of various methods of communications are shown:<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Communication-Methods.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13841\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Communication-Methods-580x510.png\" alt=\"Communication Methods\" width=\"580\" height=\"510\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Communication-Methods-580x510.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Communication-Methods.png 607w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p style=\"text-align: justify\">We can also use software-based methods instead of using dedicated hardware to replicate some of these communications but these methods are not efficient as they consume resources like clock cycles, memories and often employ polling strategies. However, in the absence of dedicated hardware, software methods are the last resorts.<\/p>\n<h2>USI vs USCI &#8211; Which one is better?<\/h2>\n<p style=\"text-align: justify\">USI and USCI are both hardware-based serial communication handlers but the question which one is better lurks in every beginner\u2019s mind. Although both modules do same tasks, they are not identical.<\/p>\n<ul style=\"text-align: justify\">\n<li><span style=\"color: #333399\"><strong><em><u>Universal Serial Interface (USI)<br \/>\n<\/u><\/em><\/strong><\/span>Mainly USI is intended for I2C and SPI communications. Technically speaking, USI is a pumped-up shift register that does all the bit-banging in hardware that a programmer would have done in software end. Apart from its aforementioned shift register, it has clock generator, bit counter and few extra assists for I2C communications.<br \/>\nIn the firmware end, we need to load the bit counter with the number of bits to transfer. This bit counter always counts down to zero. During transmission, the shift register is loaded with the value that is to be transmitted while during reception, the shift register is read back once the bit counter hits zero.<\/li>\n<\/ul>\n<ul>\n<li style=\"text-align: justify\"><span style=\"color: #333399\"><strong><em><u>Universal Serial Communication Interface (USCI)<br \/>\n<\/u><\/em><\/strong><\/span>USCI, on the other hand, is a highly sophisticated module that is intended for most forms of serial communications. USCI is more advanced than USI in terms of hardware. It has a one-byte I\/O buffer and DMA transfer capability for higher throughput. USCI can be subcategorized in two types:<br \/>\n<span style=\"color: #00ccff\"><strong><em><u>Asynchronous USCI (USCI_A)<br \/>\n<\/u><\/em><\/strong><\/span>It is used for UART, SPI, LIN and IRDA as it can detect the baud rate of an incoming signal. This type is most common.<br \/>\n<span style=\"color: #00ccff\"><strong><em><u>Synchronous USCI (USCI_B)<br \/>\n<\/u><\/em><\/strong><\/span>This type can handle synchronous communications like SPI and I2C that need a clock signal. The coolest part is the fact that the full I2C communication protocol as per NXP is implemented in I2C mode with an in-built I2C state machine.<\/li>\n<\/ul>\n<p style=\"text-align: justify\">Both USI and USCI support master-slave modes although most of the times we don\u2019t need slave modes. This is because most of the times we do not need communicate with multiple microcontrollers on board. I2C and SPI are usually used to establish communication between external devices (sensors and drivers) and a host microcontroller. UART, on the other hand, is mainly used to communicate with a computer. It is also used for long-distance communications like RS232, RS485, LIN and IrDA.<\/p>\n<h2>Software-based Communication<\/h2>\n<p style=\"text-align: justify\">When USI\/USCI are absent or used up, we have to stick to software-based methods to emulate SPI, I2C and UART. They are slow and may require the aid of other hardware like timers and external interrupts. Software methods add considerable amounts of coding overhead and consume both CPU cycles as well as memories. We can\u2019t also use them for communications that are more complex than I2C, SPI or UART. It is better to avoid them whenever possible. Still however, these methods help in learning the details of UART, I2C and SPI communications to a great extent. A good thing about software solutions is the fact that we can virtually create unlimited amounts of communication ports.<\/p>\n<p style=\"text-align: justify\"><!--more--><\/p>\n<h1>Hardware SPI &#8211; USI<\/h1>\n<p style=\"text-align: justify\">SPI communication is an onboard synchronous communication method. It is used for communicating with a number of devices including sensors, TFT displays, port expanders, PWM controller ICs, memory chips, addon support devices and even other microcontrollers.<\/p>\n<p style=\"text-align: justify\">In a SPI communication bus, there is always one master device which generates clock and select slave(s). Master sends commands to slave(s). Slave(s) responds to commands sent by the master. The number of slaves in a SPI bus is virtually unlimited provided that there is no issue with slave selection hardware and bus speed. Except the chip selection pin, all SPI devices in a bus can share the same clock and data pins.<\/p>\n<p style=\"text-align: justify\">Typical full-duplex SPI bus requires four basic I\/O pins:<\/p>\n<ul style=\"text-align: justify\">\n<li><span style=\"color: #ff0000\"><strong><em>Master-Out-Slave-In (MOSI)<\/em><\/strong> <\/span>connected to <span style=\"color: #339966\"><strong><em>Slave-Data-In (SDI)<\/em><\/strong><\/span>.<\/li>\n<li><span style=\"color: #ff0000\"><strong><em>Master-In-Slave-Out (MIS0)<\/em><\/strong><\/span> connected to <span style=\"color: #339966\"><strong><em>Slave-Data-Out (SDO)<\/em><\/strong><\/span>.<\/li>\n<li><span style=\"color: #ff0000\"><strong><em>Serial Clock (SCLK)<\/em><\/strong> <\/span>connected to <span style=\"color: #339966\"><strong><em>Slave Clock (SCK)<\/em><\/strong><\/span>.<\/li>\n<li><span style=\"color: #ff0000\"><strong><em>Slave Select (SS)<\/em><\/strong><\/span> connected to <span style=\"color: #339966\"><strong><em>Chip Select (CS)<\/em><\/strong><\/span>.<\/li>\n<\/ul>\n<p style=\"text-align: justify\">The diagram below illustrates SPI communication with a MSP430 micro. The green labels are for slaves while the red ones are for the master or host MSP430 micro.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/SPI-Comms1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13854\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/SPI-Comms1-580x366.png\" alt=\"SPI Comms\" width=\"580\" height=\"366\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/SPI-Comms1-580x366.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/SPI-Comms1-1024x646.png 1024w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/SPI-Comms1.png 1134w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p>In general, if you wish to know more about SPI bus here are some cool links:<\/p>\n<ul>\n<li><a href=\"https:\/\/learn.mikroe.com\/spi-bus\/\">https:\/\/learn.mikroe.com\/spi-bus\/<\/a><\/li>\n<li><a href=\"https:\/\/learn.sparkfun.com\/tutorials\/serial-peripheral-interface-spi\">https:\/\/learn.sparkfun.com\/tutorials\/serial-peripheral-interface-spi<\/a><\/li>\n<li><a href=\"http:\/\/ww1.microchip.com\/downloads\/en\/devicedoc\/spi.pdf\">http:\/\/ww1.microchip.com\/downloads\/en\/devicedoc\/spi.pdf<\/a><\/li>\n<li><a href=\"http:\/\/tronixstuff.com\/2011\/05\/13\/tutorial-arduino-and-the-spi-bus\/\">http:\/\/tronixstuff.com\/2011\/05\/13\/tutorial-arduino-and-the-spi-bus\/<\/a><\/li>\n<li><a href=\"https:\/\/embeddedmicro.com\/tutorials\/mojo\/serial-peripheral-interface-spi\">https:\/\/embeddedmicro.com\/tutorials\/mojo\/serial-peripheral-interface-spi<\/a><\/li>\n<li><a href=\"http:\/\/www.circuitbasics.com\/basics-of-the-spi-communication-protocol\/\">http:\/\/www.circuitbasics.com\/basics-of-the-spi-communication-protocol\/<\/a><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h2>Code Example<\/h2>\n<p>&nbsp;<\/p>\n<p><strong><em>SPI.h<\/em><\/strong><\/p>\n<pre><span style=\"color: #0000ff\"><strong>#include<\/strong> &lt;msp430.h&gt;<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>unsigned<\/strong> <strong>char<\/strong> <strong>SPI_transfer<\/strong>(<strong>unsigned<\/strong> <strong>char<\/strong> data_out);<\/span><\/pre>\n<p>&nbsp;<\/p>\n<p><strong><em>SPI.c<\/em><\/strong><\/p>\n<pre><span style=\"color: #0000ff\"><strong>#include<\/strong> \"SPI.h\"<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>unsigned<\/strong> <strong>char<\/strong> <strong>SPI_transfer<\/strong>(<strong>unsigned<\/strong> <strong>char<\/strong> data_out)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>unsigned<\/strong> <strong>char<\/strong> data_in = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 USISRL = data_out;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Load shift register with data byte to be TXed<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 USICNT = 8;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Load bit-counter to send\/receive data byte<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>while<\/strong> (!(USIIFG &amp; USICTL1));\u00a0 \/\/ Loop until data byte transmitted<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 data_in = USISRL;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\/\/ Read out the received data<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>return<\/strong> data_in;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<p>&nbsp;<\/p>\n<p><strong><em>MAX72xx.h<\/em><\/strong><\/p>\n<pre><span style=\"color: #0000ff\"><strong>#include<\/strong> &lt;MSP430.h&gt;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#include<\/strong> \"delay.h\"<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#include<\/strong> \"SPI.h\"<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>HW_SPI_DIR<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 P1DIR<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>HW_SPI_OUT<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 P1OUT<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>HW_SPI_IN<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 P1IN<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>CS_pin<\/strong>\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0BIT4<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>CS_DIR_OUT<\/strong>()\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>do<\/strong>{HW_SPI_DIR |= CS_pin;}<strong>while<\/strong>(0)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>CS_DIR_IN<\/strong>()\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>do<\/strong>{HW_SPI_DIR &amp;= ~CS_pin;}<strong>while<\/strong>(0)<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>CS_HIGH<\/strong>()\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>do<\/strong>{HW_SPI_OUT |= CS_pin;}<strong>while<\/strong>(0)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>CS_LOW<\/strong>()\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>do<\/strong>{HW_SPI_OUT &amp;= ~CS_pin;}<strong>while<\/strong>(0)<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>NOP<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0x00<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>DIG0<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0x01<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>DIG1<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0x02<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>DIG2<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0x03<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>DIG3<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0x04<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>DIG4<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0x05<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>DIG5<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0x06<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>DIG6<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0x07<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>DIG7<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0x08<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>decode_mode_reg<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0x09<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>intensity_reg<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0x0A<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>scan_limit_reg<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0x0B<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>shutdown_reg<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0x0C<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>display_test_reg<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0x0F<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>shutdown_cmd<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0x00<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>run_cmd<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0x01<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>no_test_cmd<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0x00<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>test_cmd<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0x01<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>MAX72xx_init<\/strong>(void);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>MAX72xx_write<\/strong>(<strong>unsigned<\/strong> <strong>char<\/strong> address, <strong>unsigned<\/strong> <strong>char<\/strong> value);<\/span><\/pre>\n<p>&nbsp;<\/p>\n<p><strong><em>MAX72xx.c<\/em><\/strong><\/p>\n<pre><span style=\"color: #0000ff\"><strong>#include<\/strong> \"MAX72xx.h\"<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>MAX72xx_init<\/strong>(void)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 CS_DIR_OUT();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 CS_HIGH();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>MAX72xx_write<\/strong>(shutdown_reg, run_cmd);\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 <strong>MAX72xx_write<\/strong>(decode_mode_reg, 0x00);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>MAX72xx_write<\/strong>(scan_limit_reg, 0x07);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 \u00a0<strong>MAX72xx_write<\/strong>(intensity_reg, 0x04);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>MAX72xx_write<\/strong>(display_test_reg, test_cmd);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>delay_ms<\/strong>(100);\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>MAX72xx_write<\/strong>(display_test_reg, no_test_cmd);\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\"><strong>void<\/strong> <strong>MAX72xx_write<\/strong>(<strong>unsigned<\/strong> <strong>char<\/strong> address, <strong>unsigned<\/strong> <strong>char<\/strong> value)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 CS_LOW();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 \u00a0\u00a0<strong>SPI_transfer<\/strong>(address);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>SPI_transfer<\/strong>(value);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 CS_HIGH();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<p>&nbsp;<\/p>\n<p><strong><em>main.c<\/em><\/strong><\/p>\n<pre><span style=\"color: #0000ff\"><strong>#include<\/strong> &lt;msp430.h&gt;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#include<\/strong> &lt;string.h&gt;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#include<\/strong> \"delay.h\"<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#include<\/strong> \"SPI.h\"<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#include<\/strong> \"MAX72xx.h\"<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>GPIO_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>BCSplus_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>USI_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>System_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>WDTplus_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>main<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>unsigned<\/strong> <strong>char<\/strong> i = 0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>unsigned<\/strong> <strong>char<\/strong> j = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>unsigned<\/strong> <strong>char<\/strong> temp[8];<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>const<\/strong> <strong>unsigned<\/strong> <strong>char<\/strong> text[80] =<\/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 0x00, 0x7E, 0x04, 0x08, 0x08, 0x04, 0x7E, 0x00,\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/M<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0x00, 0x42, 0x42, 0x7E, 0x7E, 0x42, 0x42, 0x00,\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/I<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0x00, 0x3C, 0x42, 0x42, 0x42, 0x42, 0x24, 0x00,\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/C<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0x00, 0x7E, 0x1A, 0x1A, 0x1A, 0x2A, 0x44, 0x00,\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/R<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0x00, 0x3C, 0x42, 0x42, 0x42, 0x42, 0x3C, 0x00,\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/O<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0x00, 0x7C, 0x12, 0x12, 0x12, 0x12, 0x7C, 0x00,\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/A<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0x00, 0x7E, 0x1A, 0x1A, 0x1A, 0x2A, 0x44, 0x00,\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/R<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0x00, 0x7E, 0x7E, 0x4A, 0x4A, 0x4A, 0x42, 0x00,\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/E<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0x00, 0x7E, 0x04, 0x08, 0x10, 0x20, 0x7E, 0x00,\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/N<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0x00, 0x7C, 0x12, 0x12, 0x12, 0x12, 0x7C, 0x00\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/A<\/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 <strong>const<\/strong> <strong>unsigned<\/strong> <strong>char<\/strong> symbols[56] =<\/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 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0x3C, 0x42, 0x95, 0xA1, 0xA1, 0x95, 0x42, 0x3C,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0xFF, 0xC3, 0xBD, 0xA5, 0xA5, 0xBD, 0xC3, 0xFF,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0x99, 0x5A, 0x3C, 0xFF, 0xFF, 0x3C, 0x5A, 0x99,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0x1C, 0x22, 0x41, 0x86, 0x86, 0x41, 0x22, 0x1C,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0xDF, 0xDF, 0xD8, 0xFF, 0xFF, 0x1B, 0xFB, 0xFB,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55<\/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 <strong>memset<\/strong>(temp, 0x00, <strong>sizeof<\/strong>(temp));<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Stop watchdog timer from timing out during initial start-up. *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 WDTCTL = WDTPW | WDTHOLD;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* initialize Config for the MSP430 GPIO *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>GPIO_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* initialize Config for the MSP430 2xx family clock systems (BCS) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>BCSplus_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* initialize Config for the MSP430 USI *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>USI_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* initialize Config for the MSP430 System Registers *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>System_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* initialize Config for the MSP430 WDT+ *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>WDTplus_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>MAX72xx_init<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>while<\/strong>(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\u00a0\u00a0 <strong>for<\/strong>(i = 0; i &lt; 80; i++)<\/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\u00a0 <strong>for<\/strong>(j = 0; j &lt; 8; j++)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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\u00a0 temp[j] = text[(i + j)];<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>MAX72xx_write<\/strong>((j + 1), temp[j]);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>delay_ms<\/strong>(6);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 }<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>for<\/strong>(j = 0; j &lt; 56; j = (j + 8))<\/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\u00a0\u00a0 <strong>for<\/strong>(i = 0; i &lt; 8; i++)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 <strong>MAX72xx_write<\/strong>((i + 1), symbols[(i + j)]);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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\u00a0\u00a0\u00a0 \u00a0<strong>delay_ms<\/strong>(2000);<\/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 };<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>GPIO_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: GPIO_graceInit_prologue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User initialization code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: GPIO_graceInit_prologue) *\/<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Output Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1OUT = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Port Select Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1SEL = BIT5 | BIT6;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Direction Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1DIR = BIT4;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Interrupt Edge Select Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1IES = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Interrupt Flag Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1IFG = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Output Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2OUT = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Direction Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2DIR = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Interrupt Edge Select Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2IES = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Interrupt Flag Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2IFG = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: GPIO_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: GPIO_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>BCSplus_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: BCSplus_graceInit_prologue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User initialization code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: BCSplus_graceInit_prologue) *\/<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Basic Clock System Control 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 * SELM_0 -- DCOCLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * DIVM_0 -- Divide by 1<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~SELS -- DCOCLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * DIVS_0 -- Divide by 1<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~DCOR -- DCO uses internal resistor<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 BCSCTL2 = SELM_0 | DIVM_0 | DIVS_0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>if<\/strong> (CALBC1_1MHZ != 0xFF) {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/* Follow recommended flow. First, clear all DCOx and MODx bits. Then<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 * apply new RSELx values. Finally, apply new DCOx and MODx bit values.<\/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 DCOCTL = 0x00;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 BCSCTL1 = CALBC1_1MHZ;\u00a0\u00a0\u00a0\u00a0\u00a0 \/* Set DCO to 1MHz *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 DCOCTL = CALDCO_1MHZ;<\/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 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Basic Clock System Control 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 * XT2OFF -- Disable XT2CLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~XTS -- Low Frequency<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * DIVA_0 -- Divide by 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 * Note: ~XTS indicates that XTS has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 BCSCTL1 |= XT2OFF | DIVA_0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Basic Clock System Control 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 * XT2S_0 -- 0.4 - 1 MHz<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * LFXT1S_2 -- If XTS = 0, XT1 = VLOCLK ; If XTS = 1, XT1 = 3 - 16-MHz crystal or resonator<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * XCAP_1 -- ~6 pF<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 BCSCTL3 = XT2S_0 | LFXT1S_2 | XCAP_1;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: BCSplus_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: BCSplus_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>USI_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: USI_graceInit_prologue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User initialization code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: USI_graceInit_prologue) *\/<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Disable USI *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 USICTL0 |= USISWRST;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * USI Control Register 0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~USIPE7 -- USI function disabled<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * USIPE6 -- USI function enabled<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * USIPE5 -- USI function enabled<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~USILSB -- MSB first<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * USIMST -- Master mode<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~USIGE -- Output latch enable depends on shift clock<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * USIOE -- Output enabled<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * USISWRST -- USI logic held in reset state<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 USICTL0 = USIPE6 | USIPE5 | USIMST | USIOE | USISWRST;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * USI Clock Control Register<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * USIDIV_3 -- Divide by 8<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * USISSEL_2 -- SMCLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * USICKPL -- Inactive state is high<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~USISWCLK -- Input clock is low<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~USISWCLK indicates that USISWCLK has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 USICKCTL = USIDIV_3 | USISSEL_2 | USICKPL;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Enable USI *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 USICTL0 &amp;= ~USISWRST;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: USI_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: USI_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>System_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: System_graceInit_prologue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User initialization code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: System_graceInit_prologue) *\/<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Clear oscillator fault flag with software delay *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>do<\/strong><\/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 \/\/ Clear OSC fault flag<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 IFG1 &amp;= ~OFIFG;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ 50us delay<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>__delay_cycles<\/strong>(50);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 } <strong>while<\/strong> (IFG1 &amp; OFIFG);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * SR, Status Register<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~SCG1 -- Disable System clock generator 1<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~SCG0 -- Disable System clock generator 0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~OSCOFF -- Oscillator On<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~CPUOFF -- CPU On<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * GIE -- General interrupt enable<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>__bis_SR_register<\/strong>(GIE);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: System_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 \u00a0\u00a0\/* User code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: System_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>WDTplus_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: RTC_B_graceInit_prologue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User initialization code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: RTC_B_graceInit_prologue) *\/<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * WDTCTL, Watchdog Timer+ Register<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * WDTPW -- Watchdog password<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * WDTHOLD -- Watchdog timer+ is stopped<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTNMIES -- NMI on rising edge<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTNMI -- Reset function<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTTMSEL -- Watchdog mode<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTCNTCL -- No action<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTSSEL -- SMCLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTIS0 -- Watchdog clock source bit0 disabled<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTIS1 -- Watchdog clock source bit1 disabled<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 WDTCTL = WDTPW | WDTHOLD;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: RTC_B_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: RTC_B_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<p>&nbsp;<\/p>\n<h2>Simulation<\/h2>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/USI-SPI-Sim1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13865\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/USI-SPI-Sim1-580x233.png\" alt=\"USI-SPI-Sim\" width=\"580\" height=\"233\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/USI-SPI-Sim1-580x233.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/USI-SPI-Sim1-1024x411.png 1024w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/USI-SPI-Sim1.png 1748w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<h2>Explanation<\/h2>\n<p style=\"text-align: justify\">USI-based SPI is best realized with a MAX7219-based dot-matrix display and this demo is based on it.\u00a0 USI is setup for SPI mode. Every time USI is initialized it is disabled first just like other hardware. We, then, proceed to setup the SPI data transfer properties like SPI clock speed, device role, mode of operation, etc. Finally, USI is enabled for data transfer over SPI bus.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/USI-SPI1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-13864\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/USI-SPI1.png\" alt=\"USI-SPI\" width=\"536\" height=\"536\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/USI-SPI1.png 536w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/USI-SPI1-150x150.png 150w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/USI-SPI1-90x90.png 90w\" sizes=\"auto, (max-width: 536px) 100vw, 536px\" \/><\/a><\/p>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Disable USI *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 USICTL0 |= USISWRST;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * USI Control Register 0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~USIPE7 -- USI function disabled<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * USIPE6 -- USI function enabled<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * USIPE5 -- USI function enabled<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~USILSB -- MSB first<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * USIMST -- Master mode<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~USIGE -- Output latch enable depends on shift clock<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * USIOE -- Output enabled<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * USISWRST -- USI logic held in reset state<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 USICTL0 = USIPE6 | USIPE5 | USIMST | USIOE | USISWRST;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * USI Clock Control Register<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * USIDIV_3 -- Divide by 8<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * USISSEL_2 -- SMCLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * USICKPL -- Inactive state is high<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~USISWCLK -- Input clock is low<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~USISWCLK indicates that USISWCLK has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 USICKCTL = USIDIV_3 | USISSEL_2 | USICKPL;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Enable USI *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 USICTL0 &amp;= ~USISWRST;<\/span><\/pre>\n<pre><\/pre>\n<p>The function below does the actual SPI data transfer. It both transmits and receives data over SPI bus. Data to be transmitted is loaded in USISRL register. USI bit counter is loaded with the number of bits to transfer, here 8. In the hardware end, this counter is decremented with data being shifted and clock signal being generated on each decrement. Since SPI bus is a circular bus, reading USISRL register back returns received data. Note slave select pin is not used here as it is used in MAX7219 source file.<\/p>\n<pre><span style=\"color: #0000ff\">unsigned char SPI_transfer(unsigned char data_out)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 unsigned char data_in = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 USISRL = data_out;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Load shift register with data byte to be TXed<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 USICNT = 8;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Load bit-counter to send\/receive data byte<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 while (!(USIIFG &amp; USICTL1));\u00a0 \/\/ Loop until data byte transmitted<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 data_in = USISRL;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Read out the received data<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 return data_in;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<p style=\"text-align: justify\">The rest of the code is the implementation of MAX7219 driver and how to use it to create patterns in the dot-matrix display. When started, the display scrolls the letters of the word \u201c<a href=\"https:\/\/www.facebook.com\/MicroArena\/\" target=\"_blank\">MICROARENA<\/a>\u201d &#8211;\u00a0 the name of my Facebook page followed by some symbols. I assume that readers will understand what I have done here.<\/p>\n<h2>Demo<\/h2>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/USI-SPI.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13876\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/USI-SPI-580x448.jpg\" alt=\"USI-SPI\" width=\"580\" height=\"448\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/USI-SPI-580x448.jpg 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/USI-SPI.jpg 590w\" 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\/2017\/08\/MAX7219-Display1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13851\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/MAX7219-Display1-580x559.png\" alt=\"MAX7219 Display\" width=\"580\" height=\"559\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/MAX7219-Display1-580x559.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/MAX7219-Display1.png 630w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p>Demo video: <a href=\"https:\/\/www.youtube.com\/watch?v=FY3jtRD8FCA\">https:\/\/www.youtube.com\/watch?v=FY3jtRD8FCA<\/a>.<\/p>\n<p><!--more--><\/p>\n<h1>Software SPI<\/h1>\n<p style=\"text-align: justify\">Software SPI is the only solution in absence of USI\/USCI modules. We need to code every step after studying device datasheet since there are four modes of SPI communication and we need to be sure which modes are supported by the device we are trying to communicate with. It should be noted that software SPI is not as fast as hardware-based SPI and this become more evident when software SPI is used to drive displays like TFTs, OLEDs, dot-matrix displays and monochrome displays like the one shown below. Software SPI may encounter issues due to glitches if improperly coded.<a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/PIC18F252-Nokia-LCD-Mini-Digital-Oscilloscope-41.jpg\"><br \/>\n<\/a><\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/PIC18F252-Nokia-LCD-Mini-Digital-Oscilloscope-41.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13853\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/PIC18F252-Nokia-LCD-Mini-Digital-Oscilloscope-41-580x546.jpg\" alt=\"PIC18F252 Nokia LCD Mini Digital Oscilloscope (4)\" width=\"580\" height=\"546\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/PIC18F252-Nokia-LCD-Mini-Digital-Oscilloscope-41-580x546.jpg 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/PIC18F252-Nokia-LCD-Mini-Digital-Oscilloscope-41.jpg 590w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p style=\"text-align: justify\">However, for a beginner, software-based SPI is good for understanding the concept behind SPI communication. Shown below is a typical SPI bus timing diagram. As you see it is simply a pattern of ones and zeroes. Digital I\/O can generate these patterns if coded.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/SPI1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13855\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/SPI1-580x206.png\" alt=\"SPI\" width=\"580\" height=\"206\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/SPI1-580x206.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/SPI1.png 927w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<h2>Code Example<\/h2>\n<p>&nbsp;<\/p>\n<p><strong><em>MCP4921.h<\/em><\/strong><\/p>\n<p>&nbsp;<\/p>\n<pre><span style=\"color: #0000ff\"><strong>#include<\/strong> &lt;msp430.h&gt;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#include<\/strong> \"delay.h\"<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>SW_SPI_DIR<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 P2DIR<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>SW_SPI_OUT<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 P2OUT<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>SW_SPI_IN<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 P2IN<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>SCK_pin<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 BIT0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>CS_pin<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 BIT1<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>SDI_pin<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 BIT2<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>LDAC_pin<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 BIT3<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>SCK_DIR_OUT<\/strong>()\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>do<\/strong>{SW_SPI_DIR |= SCK_pin;}<strong>while<\/strong>(0)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>SCK_DIR_IN<\/strong>()\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>do<\/strong>{SW_SPI_DIR &amp;= ~SCK_pin;}<strong>while<\/strong>(0)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>CS_DIR_OUT<\/strong>()\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>do<\/strong>{SW_SPI_DIR |= CS_pin;}<strong>while<\/strong>(0)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>CS_DIR_IN<\/strong>()\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>do<\/strong>{SW_SPI_DIR &amp;= ~CS_pin;}<strong>while<\/strong>(0)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>SDI_DIR_OUT<\/strong>()\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>do<\/strong>{SW_SPI_DIR |= SDI_pin;}<strong>while<\/strong>(0)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>SDI_DIR_IN<\/strong>()\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>do<\/strong>{SW_SPI_DIR &amp;= ~SDI_pin;}<strong>while<\/strong>(0)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>LDAC_DIR_OUT<\/strong>()\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>do<\/strong>{SW_SPI_DIR |= LDAC_pin;}<strong>while<\/strong>(0)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>LDAC_DIR_IN<\/strong>()\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>do<\/strong>{SW_SPI_DIR &amp;= ~LDAC_pin;}<strong>while<\/strong>(0)<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>SCK_HIGH<\/strong>()\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>do<\/strong>{SW_SPI_OUT |= SCK_pin;}<strong>while<\/strong>(0)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>SCK_LOW<\/strong>()\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>do<\/strong>{SW_SPI_OUT &amp;= ~SCK_pin;}<strong>while<\/strong>(0)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>CS_HIGH<\/strong>()\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>do<\/strong>{SW_SPI_OUT |= CS_pin;}<strong>while<\/strong>(0)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>CS_LOW<\/strong>()\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>do<\/strong>{SW_SPI_OUT &amp;= ~CS_pin;}<strong>while<\/strong>(0)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>SDI_HIGH<\/strong>()\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>do<\/strong>{SW_SPI_OUT |= SDI_pin;}<strong>while<\/strong>(0)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>SDI_LOW<\/strong>()\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>do<\/strong>{SW_SPI_OUT &amp;= ~SDI_pin;}<strong>while<\/strong>(0)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>LDAC_HIGH<\/strong>()\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>do<\/strong>{SW_SPI_OUT |= LDAC_pin;}<strong>while<\/strong>(0)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>LDAC_LOW<\/strong>()\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>do<\/strong>{SW_SPI_OUT &amp;= ~LDAC_pin;}<strong>while<\/strong>(0)<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>ignore_cmd<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0x80<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>DAC_write_cmd<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0x00<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>Buffer_on<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0x40<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>Buffer_off<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0x00<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>Gain_1X<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a00x20<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>Gain_2X<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0x00<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>Run_cmd<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0x10<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>Shutdown<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0x00<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>MCP4921_init<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>MCP4921_write<\/strong>(<strong>unsigned<\/strong> <strong>char<\/strong> cmd, <strong>unsigned<\/strong> <strong>int<\/strong> dac_value);<\/span><\/pre>\n<p>&nbsp;<\/p>\n<p><strong><em>MCP4921.c<\/em><\/strong><\/p>\n<p>&nbsp;<\/p>\n<pre><span style=\"color: #0000ff\"><strong>#include<\/strong> \"MCP4921.h\"<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>MCP4921_init<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 CS_DIR_OUT();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 SCK_DIR_OUT();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 SDI_DIR_OUT();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 LDAC_DIR_OUT();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 CS_HIGH();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 LDAC_HIGH();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 SCK_HIGH();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 SDI_HIGH();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>MCP4921_write<\/strong>(<strong>unsigned<\/strong> <strong>char<\/strong> cmd, <strong>unsigned<\/strong> <strong>int<\/strong> dac_value)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>unsigned<\/strong> <strong>char<\/strong> s = 16;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>unsigned<\/strong> <strong>int<\/strong> value = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 value = cmd;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 value &lt;&lt;= 8;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 value |= (dac_value &amp; 0x0FFF);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 CS_LOW();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>while<\/strong>(s &gt; 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\u00a0\u00a0 <strong>if<\/strong>((value &amp; 0x8000) != 0)<\/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\u00a0\u00a0 SDI_HIGH();<\/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 <strong>else<\/strong><\/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\u00a0\u00a0 SDI_LOW();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 SCK_LOW();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 SCK_HIGH();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 value &lt;&lt;= 1;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 s--;<\/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 LDAC_LOW();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 CS_HIGH();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>delay_us<\/strong>(10);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 LDAC_HIGH();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<p>&nbsp;<\/p>\n<p><strong><em>main.c<\/em><\/strong><\/p>\n<p>&nbsp;<\/p>\n<pre><span style=\"color: #0000ff\"><strong>#include<\/strong> &lt;msp430.h&gt;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#include<\/strong> \"delay.h\"<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#include<\/strong> \"MCP4921.h\"<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>GPIO_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>BCSplus_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>System_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>WDTplus_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>const<\/strong> <strong>unsigned<\/strong> <strong>int<\/strong> sine_table[33] =<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 0,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 100,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 201,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 301,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 400,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 498,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 595,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 690,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 784,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 876,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 965,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 1053,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 1138,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 1220,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 1299,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 1375,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 1448,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 1517,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 1583,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 1645,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 1703,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 1757,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 1806,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 1851,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 1892,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 1928,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 1960,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 1987,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 2026,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 2038,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 2046,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 2047<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">};<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>const<\/strong> <strong>unsigned<\/strong> <strong>int<\/strong> triangle_table[33] =<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 0,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 64,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 128,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 192,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 256,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 320,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 384,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 448,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 512,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 576,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 640,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 704,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 768,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 832,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 896,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 960,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 1024,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 1088,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 1152,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 1216,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 1280,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 1344,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 1408,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 1472,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 1536,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 1600,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 1664,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 1728,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 1792,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 1856,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 1920,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 1984,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 2047<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">};<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>const<\/strong> <strong>unsigned<\/strong> <strong>int<\/strong> square_table[33] =<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 2047,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 2047,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 2047,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 2047,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 2047,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 2047,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 2047,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 2047,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 2047,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 2047,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 2047,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 2047,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 2047,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 2047,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 2047,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 2047,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 2047,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 2047,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 2047,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 2047,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 2047,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 2047,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 2047,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 2047,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 2047,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 2047,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 2047,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 2047,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 2047,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 2047,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 2047,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 2047,<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 2047<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">};<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>main<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>unsigned<\/strong> <strong>char<\/strong> s = 0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>unsigned<\/strong> <strong>char<\/strong> wave = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Stop watchdog timer from timing out during initial start-up. *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 WDTCTL = WDTPW | WDTHOLD;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* initialize Config for the MSP430 GPIO *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>GPIO_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* initialize Config for the MSP430 2xx family clock systems (BCS) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>BCSplus_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* initialize Config for the MSP430 System Registers *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>System_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* initialize Config for the MSP430 WDT+ *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>WDTplus_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>MCP4921_init<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>while<\/strong>(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\u00a0\u00a0 <strong>if<\/strong>((P1IN &amp; BIT3) == 0)<\/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\u00a0\u00a0 P1OUT |= BIT0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>while<\/strong>((P1IN &amp; BIT3) == 0);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 wave++;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>if<\/strong>(wave &gt; 2)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 wave = 0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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\u00a0\u00a0\u00a0\u00a0 P1OUT &amp;= ~BIT0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 <strong>else<\/strong><\/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\u00a0\u00a0 <strong>switch<\/strong>(wave)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 <strong>case<\/strong> 1:<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>for<\/strong>(s = 0; s &lt; 32; s++)<\/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 {<\/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\u00a0\u00a0<strong>MCP4921_write<\/strong>((DAC_write_cmd | Buffer_on | Gain_1X | Run_cmd), (2047 + square_table[s]));<\/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\u00a0\u00a0 <strong>delay_ms<\/strong>(10);<\/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 }<\/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 <strong>for<\/strong>(s = 31; s &gt; 0; s--)<\/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 {<\/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\u00a0\u00a0<strong>MCP4921_write<\/strong>((DAC_write_cmd | Buffer_on | Gain_1X | Run_cmd), (2047 + square_table[s]));<\/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\u00a0\u00a0 <strong>delay_ms<\/strong>(10);<\/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 }<\/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 <strong>for<\/strong>(s = 0; s &lt; 32; s++)<\/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 {<\/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\u00a0\u00a0 <strong>MCP4921_write<\/strong>((DAC_write_cmd | Buffer_on | Gain_1X | Run_cmd), (2047 - square_table[s]));<\/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\u00a0\u00a0 <strong>delay_ms<\/strong>(10);<\/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 }<\/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 <strong>for<\/strong>(s = 31; s &gt; 0; s--)<\/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 {<\/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\u00a0\u00a0 <strong>MCP4921_write<\/strong>((DAC_write_cmd | Buffer_on | Gain_1X | Run_cmd), (2047 - square_table[s]));<\/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\u00a0\u00a0 <strong>delay_ms<\/strong>(10);<\/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 }<\/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 <strong>break<\/strong>;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>case<\/strong> 2:<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>for<\/strong>(s = 0; s &lt; 32; s++)<\/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{<\/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\u00a0\u00a0 <strong>MCP4921_write<\/strong>((DAC_write_cmd | Buffer_on | Gain_1X | Run_cmd), (2047 + triangle_table[s]));<\/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\u00a0\u00a0 <strong>delay_ms<\/strong>(10);<\/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 }<\/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 <strong>for<\/strong>(s = 31; s &gt; 0; s--)<\/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{<\/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\u00a0\u00a0 <strong>MCP4921_write<\/strong>((DAC_write_cmd | Buffer_on | Gain_1X | Run_cmd), (2047 + triangle_table[s]));<\/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\u00a0\u00a0 <strong>delay_ms<\/strong>(10);<\/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 }<\/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 <strong>for<\/strong>(s = 0; s &lt; 32; s++)<\/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 {<\/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\u00a0\u00a0<strong>MCP4921_write<\/strong>((DAC_write_cmd | Buffer_on | Gain_1X | Run_cmd), (2047 - triangle_table[s]));<\/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\u00a0\u00a0 <strong>delay_ms<\/strong>(10);<\/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 }<\/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 <strong>for<\/strong>(s = 31; s &gt; 0; s--)<\/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 {<\/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\u00a0 \u00a0<strong>MCP4921_write<\/strong>((DAC_write_cmd | Buffer_on | Gain_1X | Run_cmd), (2047 - triangle_table[s]));<\/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\u00a0\u00a0 <strong>delay_ms<\/strong>(10);<\/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 }<\/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 <strong>break<\/strong>;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>default<\/strong>:<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0<strong>for<\/strong>(s = 0; s &lt; 32; s++)<\/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 {<\/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\u00a0\u00a0 <strong>MCP4921_write<\/strong>((DAC_write_cmd | Buffer_on | Gain_1X | Run_cmd), (2047 + sine_table[s]));<\/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\u00a0\u00a0 <strong>delay_ms<\/strong>(10);<\/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 }<\/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 <strong>for<\/strong>(s = 31; s &gt; 0; s--)<\/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 {<\/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\u00a0\u00a0 <strong>MCP4921_write<\/strong>((DAC_write_cmd | Buffer_on | Gain_1X | Run_cmd), (2047 + sine_table[s]));<\/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\u00a0\u00a0 <strong>delay_ms<\/strong>(10);<\/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 }<\/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 <strong>for<\/strong>(s = 0; s &lt; 32; s++)<\/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{<\/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\u00a0\u00a0 <strong>MCP4921_write<\/strong>((DAC_write_cmd | Buffer_on | Gain_1X | Run_cmd), (2047 - sine_table[s]));<\/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\u00a0\u00a0 <strong>delay_ms<\/strong>(10);<\/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 }<\/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 <strong>for<\/strong>(s = 31; s &gt; 0; s--)<\/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 {<\/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\u00a0\u00a0<strong>MCP4921_write<\/strong>((DAC_write_cmd | Buffer_on | Gain_1X | Run_cmd), (2047 - sine_table[s]));<\/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\u00a0\u00a0 <strong>delay_ms<\/strong>(10);<\/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 }<\/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 <strong>break<\/strong>;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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\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 };<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>GPIO_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: GPIO_graceInit_prologue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User initialization code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: GPIO_graceInit_prologue) *\/<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Output Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1OUT = BIT3;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Direction Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1DIR = BIT0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Resistor Enable Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1REN = BIT3;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Interrupt Edge Select Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1IES = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Interrupt Flag Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1IFG = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Output Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2OUT = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Port Select Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2SEL &amp;= ~(BIT6 | BIT7);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Direction Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2DIR = BIT0 | BIT1 | BIT2 | BIT3;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Interrupt Edge Select Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2IES = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Interrupt Flag Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2IFG = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: GPIO_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: GPIO_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>BCSplus_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: BCSplus_graceInit_prologue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User initialization code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: BCSplus_graceInit_prologue) *\/<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Basic Clock System Control 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 * SELM_0 -- DCOCLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * DIVM_0 -- Divide by 1<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~SELS -- DCOCLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * DIVS_0 -- Divide by 1<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~DCOR -- DCO uses internal resistor<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 BCSCTL2 = SELM_0 | DIVM_0 | DIVS_0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>if<\/strong> (CALBC1_1MHZ != 0xFF) {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/* Follow recommended flow. First, clear all DCOx and MODx bits. Then<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 * apply new RSELx values. Finally, apply new DCOx and MODx bit values.<\/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 DCOCTL = 0x00;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 BCSCTL1 = CALBC1_1MHZ;\u00a0\u00a0\u00a0\u00a0\u00a0 \/* Set DCO to 1MHz *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 DCOCTL = CALDCO_1MHZ;<\/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 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Basic Clock System Control 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 * XT2OFF -- Disable XT2CLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~XTS -- Low Frequency<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * DIVA_0 -- Divide by 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 * Note: ~XTS indicates that XTS has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 BCSCTL1 |= XT2OFF | DIVA_0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Basic Clock System Control 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 * XT2S_0 -- 0.4 - 1 MHz<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * LFXT1S_0 -- If XTS = 0, XT1 = 32768kHz Crystal ; If XTS = 1, XT1 = 0.4 - 1-MHz crystal or resonator<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * XCAP_1 -- ~6 pF<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 BCSCTL3 = XT2S_0 | LFXT1S_0 | XCAP_1;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: BCSplus_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: BCSplus_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>System_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: System_graceInit_prologue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User initialization code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: System_graceInit_prologue) *\/<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * SR, Status Register<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~SCG1 -- Disable System clock generator 1<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~SCG0 -- Disable System clock generator 0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~OSCOFF -- Oscillator On<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~CPUOFF -- CPU On<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * GIE -- General interrupt enable<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>__bis_SR_register<\/strong>(GIE);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: System_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: System_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>WDTplus_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: RTC_B_graceInit_prologue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User initialization code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: RTC_B_graceInit_prologue) *\/<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * WDTCTL, Watchdog Timer+ Register<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * WDTPW -- Watchdog password<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * WDTHOLD -- Watchdog timer+ is stopped<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTNMIES -- NMI on rising edge<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTNMI -- Reset function<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTTMSEL -- Watchdog mode<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTCNTCL -- No action<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTSSEL -- SMCLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTIS0 -- Watchdog clock source bit0 disabled<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTIS1 -- Watchdog clock source bit1 disabled<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 WDTCTL = WDTPW | WDTHOLD;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: RTC_B_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: RTC_B_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<p>&nbsp;<\/p>\n<h2>Simulation<\/h2>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/MCP49211.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13852\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/MCP49211-580x431.png\" alt=\"MCP4921\" width=\"580\" height=\"431\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/MCP49211-580x431.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/MCP49211.png 691w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<h2>Explanation<\/h2>\n<p style=\"text-align: justify\">Software SPI requires digital I\/Os only. Software SPI is achieved using bit-banging technique. For this demo, a MCP4921 12-bit DAC is used. This DAC communicates with a host micro using SPI bus. Check the function below. Here from the data bit to the clock signal, everything is controlled by changing the logic states of digital I\/Os. The changes are done in such a way that they do exactly the same thing a hardware SPI would have done. If we used hardware-based SPI as in the last example, it would have taken one or two lines of code unlike the long listing given below. This extra coding along with some hardware limitations reduces SPI clock speed.<\/p>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>MCP4921_write<\/strong>(<strong>unsigned<\/strong> <strong>char<\/strong> cmd, <strong>unsigned<\/strong> <strong>int<\/strong> dac_value)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>unsigned<\/strong> <strong>char<\/strong> s = 16;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>unsigned<\/strong> <strong>int<\/strong> value = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 value = cmd;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 value &lt;&lt;= 8;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 value |= (dac_value &amp; 0x0FFF);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 CS_LOW();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>while<\/strong>(s &gt; 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\u00a0\u00a0 <strong>if<\/strong>((value &amp; 0x8000) != 0)<\/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\u00a0\u00a0 SDI_HIGH();<\/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 <strong>else<\/strong><\/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\u00a0\u00a0 SDI_LOW();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 SCK_LOW();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 SCK_HIGH();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 value &lt;&lt;= 1;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 s--;<\/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 LDAC_LOW();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 CS_HIGH();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>delay_us<\/strong>(10);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 \u00a0\u00a0LDAC_HIGH();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<p style=\"text-align: justify\">The rest of the code simply does the work of a <strong><em>Digital Signal Synthesizer (DSS)<\/em><\/strong> <em>a.k.a<\/em> waveform generator. It generates three types of waves using wave tables and some basic mathematics.<\/p>\n<h2>Demo<\/h2>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/SW-SPI.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13873\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/SW-SPI-580x328.jpg\" alt=\"SW-SPI\" width=\"580\" height=\"328\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/SW-SPI-580x328.jpg 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/SW-SPI.jpg 590w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p>Demo video: <a href=\"https:\/\/www.youtube.com\/watch?v=gPZJyL9LWQc\">https:\/\/www.youtube.com\/watch?v=gPZJyL9LWQc<\/a>.<\/p>\n<p><!--more--><\/p>\n<h1>LCD using DIO Bit-Banging<\/h1>\n<p style=\"text-align: justify\">Some MSP430 devices, especially the 14-pin parts have limited number of pins and so it is wise to use port expanders like MCP23S17 or MAX7300, serial LCDs and shift register-based LCDs. Everything is same here just as in the LCD example. The only exception is the method of handling the device responsible for port-expansion task. In some cases, it may be necessary to use additional hardware like UART\/I2C\/SPI or emulate these methods using software when such dedicated hardware is either unavailable or used up for some other jobs. Thus, in such cases, things are no longer as simple as with digital I\/Os. In the software end, we will also need to code for the additional interface too.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/I2C-SPI-LCD.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13881\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/I2C-SPI-LCD-580x282.jpg\" alt=\"I2C SPI LCD\" width=\"580\" height=\"282\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/I2C-SPI-LCD-580x282.jpg 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/I2C-SPI-LCD.jpg 590w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p style=\"text-align: justify\">Everything that has an advantage must also have a disadvantage. The primary disadvantages that we are left with when using port-expanded LCDs are slower displays and vulnerability to EMI. Noise and glitches are big issues when your device is working in a harsh industrial environment surrounded by electromagnetics. Likewise, if the wires connecting the LCD pack with the host MCU are too long, it is highly likely to fail or show garbage characters after some time. The simplest solution to these issues are to use shorter connection wires, slower communication speed and frequent but periodic reinitialization of the LCD pack. Again, all these lead to slow functioning. Thus, a careful system design is an absolute must.<\/p>\n<p style=\"text-align: justify\">The most popular methods of driving alphanumeric LCDs with fewer wires include:<\/p>\n<ul style=\"text-align: justify\">\n<li>SPI-based solutions using shift registers like 74HC595 and CD4094B<\/li>\n<li>I2C-based solutions using I2C port expander ICs like PCF8574 and MCP23S17.<\/li>\n<\/ul>\n<p style=\"text-align: justify\">We can use either hardware-based SPI\/I2C modules or emulate these in software using bit-banging methods. The former adds some much-needed processing speed which is impossible with bit-banging.<\/p>\n<p style=\"text-align: justify\">In this segment, we will see how to use a CD4094B CMOS shift-register with software SPI to drive an alphanumeric LCD.<\/p>\n<h2>Code Example<\/h2>\n<p>&nbsp;<\/p>\n<p><strong><em>lcd.h<\/em><\/strong><\/p>\n<p>&nbsp;<\/p>\n<pre><span style=\"color: #0000ff\"><strong>#include<\/strong> &lt;msp430.h&gt;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#include<\/strong> &lt;delay.h&gt;<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>LCD_PORT<\/strong>\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 P2OUT<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>SDO<\/strong>\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 BIT0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>SCK<\/strong>\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 BIT1<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>STB<\/strong>\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 BIT2<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>SDO_HIGH<\/strong>\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 LCD_PORT |= SDO<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>SDO_LOW<\/strong>\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 LCD_PORT &amp;= ~SDO<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>SCK_HIGH<\/strong>\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 LCD_PORT |= SCK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>SCK_LOW<\/strong>\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 LCD_PORT &amp;= ~SCK<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>STB_HIGH<\/strong>\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 LCD_PORT |= STB<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>STB_LOW<\/strong>\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 LCD_PORT &amp;= ~STB<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>clear_display<\/strong>\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 0x01<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>goto_home<\/strong>\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 0x02<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>cursor_direction_inc<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 (0x04 | 0x02)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>cursor_direction_dec<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 (0x04 | 0x00)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>display_shift<\/strong>\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 (0x04 | 0x01)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>display_no_shift<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 (0x04 | 0x00)<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>display_on<\/strong>\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 (0x08 | 0x04)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>display_off<\/strong>\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 (0x08 | 0x02)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>cursor_on<\/strong>\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 (0x08 | 0x02)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>cursor_off<\/strong>\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 (0x08 | 0x00)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>blink_on<\/strong>\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 (0x08 | 0x01)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>blink_off<\/strong>\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 (0x08 | 0x00)<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>_8_pin_interface<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 (0x20 | 0x10)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>_4_pin_interface<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 (0x20 | 0x00)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>_2_row_display<\/strong>\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 (0x20 | 0x08)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>_1_row_display<\/strong>\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 (0x20 | 0x00)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>_5x10_dots<\/strong>\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 (0x20 | 0x40)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>_5x7_dots<\/strong>\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 (0x20 | 0x00)<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>dly<\/strong>\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\u00a01<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>unsigned<\/strong> <strong>char<\/strong> data_value;<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>SIPO<\/strong>(void);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>LCD_init<\/strong>(void);\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>LCD_command<\/strong>(<strong>unsigned<\/strong> <strong>char<\/strong> value);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>LCD_send_data<\/strong>(<strong>unsigned<\/strong> <strong>char<\/strong> value);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>LCD_4bit_send<\/strong>(<strong>unsigned<\/strong> <strong>char<\/strong> lcd_data);\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\"><strong>void<\/strong> <strong>LCD_putstr<\/strong>(<strong>char<\/strong> *lcd_string);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>LCD_putchar<\/strong>(<strong>char<\/strong> char_data);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>LCD_clear_home<\/strong>(void);\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>LCD_goto<\/strong>(<strong>unsigned<\/strong> <strong>char<\/strong> x_pos, <strong>unsigned<\/strong> <strong>char<\/strong> y_pos);<\/span><\/pre>\n<p>&nbsp;<\/p>\n<p><strong><em>lcd.c<\/em><\/strong><\/p>\n<p>&nbsp;<\/p>\n<pre><span style=\"color: #0000ff\"><strong>#include<\/strong> \"lcd.h\"<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>SIPO<\/strong>(void)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>unsigned<\/strong> <strong>char<\/strong> bit = 0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>unsigned<\/strong> <strong>char<\/strong> clk = 8;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>unsigned<\/strong> <strong>char<\/strong> temp = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 temp = data_value;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 STB_LOW;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>while<\/strong>(clk &gt; 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\u00a0\u00a0 bit = ((temp &amp; 0x80) &gt;&gt; 0x07);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 bit &amp;= 0x01;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>switch<\/strong>(bit)<\/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\u00a0\u00a0 <strong>case<\/strong> 0:<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 SDO_LOW;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>break<\/strong>;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 <strong>default<\/strong>:<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 SDO_HIGH;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>break<\/strong>;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 }<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 SCK_HIGH;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 temp &lt;&lt;= 1;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 clk--;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 SCK_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 STB_HIGH;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>LCD_init<\/strong>(void)<\/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\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 <strong>unsigned<\/strong> <strong>char<\/strong> t = 0x0A;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 data_value = 0x08;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>SIPO<\/strong>();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>while<\/strong>(t &gt; 0x00)<\/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\u00a0\u00a0 <strong>delay_ms<\/strong>(dly);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 t--;<\/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 data_value = 0x30;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>SIPO<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 data_value |= 0x08;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>SIPO<\/strong>();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>delay_ms<\/strong>(dly);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 data_value &amp;= 0xF7;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>SIPO<\/strong>();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>delay_ms<\/strong>(dly);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 data_value = 0x30;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>SIPO<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 data_value |= 0x08;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>SIPO<\/strong>();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>delay_ms<\/strong>(dly);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 data_value &amp;= 0xF7;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>SIPO<\/strong>();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>delay_ms<\/strong>(dly);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 data_value = 0x30;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>SIPO<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 data_value |= 0x08;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>SIPO<\/strong>();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>delay_ms<\/strong>(dly);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 data_value &amp;= 0xF7;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>SIPO<\/strong>();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>delay_ms<\/strong>(dly);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 data_value = 0x20;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>SIPO<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 data_value |= 0x08;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>SIPO<\/strong>();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>delay_ms<\/strong>(dly);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 data_value &amp;= 0xF7;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>SIPO<\/strong>();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>delay_ms<\/strong>(dly);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_command<\/strong>(_4_pin_interface | _2_row_display | _5x7_dots);\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_command<\/strong>(display_on | cursor_off | blink_off);\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_command<\/strong>(clear_display);\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_command<\/strong>(cursor_direction_inc | display_no_shift);\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}\u00a0\u00a0<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>LCD_command<\/strong>(<strong>unsigned<\/strong> <strong>char<\/strong> value)<\/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\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 data_value &amp;= 0xFB;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>SIPO<\/strong>();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_4bit_send<\/strong>(value);\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\"><strong>void<\/strong> <strong>LCD_send_data<\/strong>(<strong>unsigned<\/strong> <strong>char<\/strong> value)<\/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\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 data_value |= 0x04;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>SIPO<\/strong>();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_4bit_send<\/strong>(value);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}\u00a0<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>LCD_4bit_send<\/strong>(<strong>unsigned<\/strong> <strong>char<\/strong> lcd_data)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>unsigned<\/strong> <strong>char<\/strong> temp = 0x00;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 temp = (lcd_data &amp; 0xF0);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 data_value &amp;= 0x0F;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 data_value |= temp;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>SIPO<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 data_value |= 0x08;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>SIPO<\/strong>();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>delay_ms<\/strong>(dly);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 data_value &amp;= 0xF7;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>SIPO<\/strong>();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>delay_ms<\/strong>(dly);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 temp = (lcd_data &amp; 0x0F);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 temp &lt;&lt;= 0x04;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 data_value &amp;= 0x0F;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 data_value |= temp;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>SIPO<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 data_value |= 0x08;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>SIPO<\/strong>();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>delay_ms<\/strong>(dly);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 data_value &amp;= 0xF7;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>SIPO<\/strong>();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>delay_ms<\/strong>(dly);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}\u00a0<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>LCD_putstr<\/strong>(<strong>char<\/strong> *lcd_string)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>while<\/strong> (*lcd_string != '\\0')\u00a0\u00a0<\/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 <strong>LCD_send_data<\/strong>(*lcd_string);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 lcd_string++;<\/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><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>LCD_putchar<\/strong>(<strong>char<\/strong> char_data)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_send_data<\/strong>(char_data);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>LCD_clear_home<\/strong>(void)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_command<\/strong>(clear_display);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_command<\/strong>(goto_home);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>LCD_goto<\/strong>(<strong>unsigned<\/strong> <strong>char<\/strong> x_pos,<strong>unsigned<\/strong> <strong>char<\/strong> y_pos)<\/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\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 <strong>if<\/strong>(y_pos == 0)\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\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\u00a0\u00a0 <strong>LCD_command<\/strong>(0x80 | x_pos);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 }<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>else<\/strong><\/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\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\u00a0\u00a0\u00a0\u00a0 <strong>LCD_command<\/strong>(0x80 | 0x40 | x_pos);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 }<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<p>&nbsp;<\/p>\n<p><strong><em>main.c<\/em><\/strong><\/p>\n<p>&nbsp;<\/p>\n<pre><span style=\"color: #0000ff\"><strong>#include<\/strong> &lt;msp430.h&gt;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#include<\/strong> \"delay.h\"<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#include<\/strong> \"lcd.h\"<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>BCSplus_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>GPIO_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>System_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0* main.c<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0*\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>main<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>unsigned<\/strong> <strong>char<\/strong> s = 0x00;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>const<\/strong> <strong>char<\/strong> txt1[] = {\"MICROARENA\"};<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>const<\/strong> <strong>char<\/strong> txt2[] = {\"SShahryiar\"};<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>const<\/strong> <strong>char<\/strong> txt3[] = {\"MSP-EXP430G2\"};<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>const<\/strong> <strong>char<\/strong> txt4[] = {\"Launchpad!\"};<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>BCSplus_graceInit<\/strong>();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>GPIO_graceInit<\/strong>();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>System_graceInit<\/strong>();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_init<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_clear_home<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_goto<\/strong>(3, 0);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_putstr<\/strong>(txt1);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_goto<\/strong>(3, 1);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_putstr<\/strong>(txt2);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>delay_ms<\/strong>(2600);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_clear_home<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>for<\/strong>(s = 0; s &lt; 12; 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\u00a0\u00a0 <strong>LCD_goto<\/strong>((2 + s), 0);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>LCD_putchar<\/strong>(txt3[s]);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>delay_ms<\/strong>(60);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 }<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>for<\/strong>(s = 0; s &lt; 10; 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\u00a0\u00a0 <strong>LCD_goto<\/strong>((3 + s), 1);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>LCD_putchar<\/strong>(txt4[s]);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0<strong>delay_ms<\/strong>(60);<\/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 s = 0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_clear_home<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_goto<\/strong>(3, 0);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_putstr<\/strong>(txt1);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>while<\/strong>(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\u00a0\u00a0 <strong>show_value<\/strong>(s);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 s++;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>delay_ms<\/strong>(200);<\/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><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>BCSplus_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Basic Clock System Control 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 * SELM_0 -- DCOCLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * DIVM_0 -- Divide by 1<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~SELS -- DCOCLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * DIVS_0 -- Divide by 1<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~DCOR -- DCO uses internal resistor<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 BCSCTL2 = SELM_0 | DIVM_0 | DIVS_0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>if<\/strong> (CALBC1_16MHZ != 0xFF)<\/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 \/* Adjust this accordingly to your VCC rise time *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>__delay_cycles<\/strong>(1000);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/* Follow recommended flow. First, clear all DCOx and MODx bits. Then<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 * apply new RSELx values. Finally, apply new DCOx and MODx bit values.<\/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 DCOCTL = 0x00;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 BCSCTL1 = CALBC1_16MHZ;\u00a0\u00a0\u00a0\u00a0 \/* Set DCO to 16MHz *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 DCOCTL = CALDCO_16MHZ;<\/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 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Basic Clock System Control 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 * XT2OFF -- Disable XT2CLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~XTS -- Low Frequency<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * DIVA_0 -- Divide by 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 * Note: ~XTS indicates that XTS has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 BCSCTL1 |= XT2OFF | DIVA_0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Basic Clock System Control 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 * XT2S_0 -- 0.4 - 1 MHz<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * LFXT1S_0 -- If XTS = 0, XT1 = 32768kHz Crystal ; If XTS = 1, XT1 = 0.4 - 1-MHz crystal or resonator<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * XCAP_1 -- ~6 pF<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 BCSCTL3 = XT2S_0 | LFXT1S_0 | XCAP_1;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>GPIO_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Output Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1OUT = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Direction Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1DIR = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Interrupt Edge Select Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1IES = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Interrupt Flag Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1IFG = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Output Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2OUT = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Port Select Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 \u00a0P2SEL &amp;= ~(BIT6 | BIT7);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Direction Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2DIR = BIT0 | BIT1 | BIT2;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Interrupt Edge Select Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2IES = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Interrupt Flag Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2IFG = 0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>System_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 WDTCTL = WDTPW | WDTHOLD;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>show_value<\/strong>(<strong>unsigned<\/strong> <strong>char<\/strong> value)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 <strong>unsigned<\/strong> <strong>char<\/strong> ch = 0x00;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 ch = ((value \/ 100) + 0x30);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 <strong>LCD_goto<\/strong>(6, 1);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 <strong>LCD_putchar<\/strong>(ch);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 ch = (((value \/ 10) % 10) + 0x30);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 <strong>LCD_goto<\/strong>(7, 1);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 <strong>LCD_putchar<\/strong>(ch);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 ch = ((value % 10) + 0x30);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 <strong>LCD_goto<\/strong>(8, 1);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 <strong>LCD_putchar<\/strong>(ch);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<p>&nbsp;<\/p>\n<h2>Simulation<\/h2>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Simulation-3.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13883\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Simulation-3-580x230.png\" alt=\"Simulation 3\" width=\"580\" height=\"230\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Simulation-3-580x230.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Simulation-3-1024x406.png 1024w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Simulation-3.png 1091w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<h2>Explanation<\/h2>\n<p style=\"text-align: justify\">Basically here, the same LCD library as in the LCD example has been used. The only difference is the <strong><em>Serial-In-Parallel-Out (SIPO)<\/em><\/strong> part. I used a CD4094B CMOS SIPO shift register between the LCD and the host MSP430 micro to achieve the port-expansion task. Only three pins of the host micro are needed to drive the LCD. In the LCD library, you can see SIPO function on the top. This part translates serial inputs to parallel outputs. Note that at every level the stored value in the SIPO is either <strong><em>ORed<\/em><\/strong> or inverse <strong><em>ANDed<\/em><\/strong> instead of entirely changing the SIPO\u2019s current content. In simple terms, it is like read-modification-write. \u00a0This is to ensure that only the target bits are altered, not the entire content of the SIPO. In this way to some extent, data corruption is prevented.<\/p>\n<h2>Demo<a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/SPI-LCD-2.jpg\"><br \/>\n<\/a><\/h2>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/SPI-LCD-1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13868\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/SPI-LCD-1-580x350.jpg\" alt=\"SPI LCD (1)\" width=\"580\" height=\"350\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/SPI-LCD-1-580x350.jpg 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/SPI-LCD-1.jpg 590w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a> <a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/SPI-LCD-2.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13869\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/SPI-LCD-2-580x336.jpg\" alt=\"SPI LCD (2)\" width=\"580\" height=\"336\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/SPI-LCD-2-580x336.jpg 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/SPI-LCD-2.jpg 590w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p style=\"text-align: justify\">Demo video: <a href=\"https:\/\/www.youtube.com\/watch?v=NBfBqtjZ35s\">https:\/\/www.youtube.com\/watch?v=NBfBqtjZ35s<\/a>.<\/p>\n<p style=\"text-align: justify\"><!--more--><\/p>\n<h1>Hardware I2C &#8211; USI<\/h1>\n<p style=\"text-align: justify\">I2C is another popular form of on board synchronous serial communication developed by NXP. It just uses two wires for communication and so it is also referred as <strong><em>Two Wire Interface (TWI)<\/em><\/strong>. Just like SPI, I2C is widely used in interfacing real-time clocks (RTC), digital sensors, memory chips and so on. It rivals SPI but compared to SPI it is slower and have some limitations. Typical bus speed ranges from a few kilohertz to 400kHz. Up to 127 devices can coexist in an I2C bus. In an I2C bus it is not possible, by conventional means to interface devices with same device IDs or devices with different logic voltage levels without logic level converters and so on. Still however, I2C is very popular because these issues rarely arise and because of its simplicity. Unlike other communications, there\u2019s no pin\/wire swapping as two wires connect straight to the bus \u2013 SDA to SDA and SCL to SCL.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/I2C-Comms1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13848\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/I2C-Comms1-580x363.png\" alt=\"I2C Comms\" width=\"580\" height=\"363\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/I2C-Comms1-580x363.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/I2C-Comms1-1024x641.png 1024w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/I2C-Comms1.png 1143w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p style=\"text-align: justify\">As with SPI, an I2C bus must contain one master and one or more slaves. The master is solely responsible for generating clock signals and initiating communication. Communication starts when master sends out a slave\u2019s ID with read\/write command and request. The slave reacts to this command by processing the request from the master and sending out data or processing it. I2C bus is always pulled-up using pull-up resistors. Without these pull-up resistors, I2C bus may not function properly.<\/p>\n<p style=\"text-align: justify\">To know more about I2C interface visit the following links:<\/p>\n<ul style=\"text-align: justify\">\n<li><a href=\"https:\/\/learn.mikroe.com\/i2c-everything-need-know\/\">https:\/\/learn.mikroe.com\/i2c-everything-need-know\/<\/a><\/li>\n<li><a href=\"https:\/\/learn.sparkfun.com\/tutorials\/i2c\">https:\/\/learn.sparkfun.com\/tutorials\/i2c<\/a><\/li>\n<li><a href=\"http:\/\/www.ti.com\/lsds\/ti\/interface\/i2c-overview.page\">http:\/\/www.ti.com\/lsds\/ti\/interface\/i2c-overview.page<\/a><\/li>\n<li><a href=\"http:\/\/www.robot-electronics.co.uk\/i2c-tutorial\">http:\/\/www.robot-electronics.co.uk\/i2c-tutorial<\/a><\/li>\n<li><a href=\"https:\/\/www.i2c-bus.org\/i2c-bus\/\">https:\/\/www.i2c-bus.org\/i2c-bus\/<\/a><\/li>\n<li><a href=\"http:\/\/i2c.info\/\">http:\/\/i2c.info\/<\/a><\/li>\n<\/ul>\n<p style=\"text-align: justify\">Other protocols like SMBus and I2S have similarities with I2C and so learning about I2C advances learning these too.<\/p>\n<h2>Code Example<\/h2>\n<p>&nbsp;<\/p>\n<p><strong><em>I2C.h<\/em><\/strong><\/p>\n<p>&nbsp;<\/p>\n<pre><span style=\"color: #0000ff\"><strong>#include<\/strong> &lt;msp430.h&gt;<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>FALSE<\/strong>\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 0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>TRUE<\/strong>\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 1<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>wr<\/strong>\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 FALSE<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>rd<\/strong>\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 TRUE<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>SET_SDA_AS_OUTPUT<\/strong>()\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 (USICTL0 |= USIOE)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>SET_SDA_AS_INPUT<\/strong>()\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 (USICTL0 &amp;= ~USIOE)<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>FORCING_SDA_HIGH<\/strong>()\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\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\u00a0\u00a0\u00a0\u00a0 USISRL = 0xFF;\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\u00a0\u00a0\u00a0\u00a0 USICTL0 |= USIGE;\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 USICTL0 &amp;= ~(USIGE + USIOE); \\<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>FORCING_SDA_LOW<\/strong>()\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\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\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 USISRL = 0x00;\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\u00a0\u00a0\u00a0\u00a0 USICTL0 |= (USIGE + USIOE);\u00a0 \\<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 USICTL0 &amp;= ~USIGE;\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 }<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>i2c_usi_mst_gen_start<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>i2c_usi_mst_gen_repeated_start<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>i2c_usi_mst_gen_stop<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>i2c_usi_mst_wait_usi_cnt_flag<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>unsigned<\/strong> <strong>char<\/strong> <strong>i2c_usi_mst_send_byte<\/strong>(<strong>unsigned<\/strong> <strong>char<\/strong> value);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>unsigned<\/strong> <strong>char<\/strong> <strong>i2c_usi_mst_read_byte<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>i2c_usi_mst_send_n_ack<\/strong>(<strong>unsigned<\/strong> <strong>char<\/strong> ack);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>unsigned<\/strong> <strong>char<\/strong> <strong>i2c_usi_mst_send_address<\/strong>(<strong>unsigned<\/strong> <strong>char<\/strong> addr, <strong>unsigned<\/strong> <strong>char<\/strong> r_w);<\/span><\/pre>\n<p>&nbsp;<\/p>\n<p><strong><em>I2C.c<\/em><\/strong><\/p>\n<p>&nbsp;<\/p>\n<pre><span style=\"color: #0000ff\"><strong>#include<\/strong> \"I2C.h\"<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>static<\/strong> <strong>unsigned<\/strong> <strong>char<\/strong> usi_cnt_flag = FALSE;<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\/\/ function to generate I2C START condition<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>i2c_usi_mst_gen_start<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 \/\/ make sure SDA line is in HIGH level<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 FORCING_SDA_HIGH();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 \/\/ small delay<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 _delay_cycles(100);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 \/\/ pull down SDA to create START condition<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 FORCING_SDA_LOW();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\/\/ function to generate I2C REPEATED START condition<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>i2c_usi_mst_gen_repeated_start<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 USICTL0 |= USIOE;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 USISRL = 0xFF;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 USICNT = 1;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 \/\/ wait for USIIFG is set<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 <strong>i2c_usi_mst_wait_usi_cnt_flag<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 \/\/ small delay<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 _delay_cycles(100);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 \/\/ pull down SDA to create START condition<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 FORCING_SDA_LOW();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 \/\/ small delay<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 _delay_cycles(100);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\/\/ function to generate I2C STOP condition<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>i2c_usi_mst_gen_stop<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 USICTL0 |= USIOE;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 USISRL = 0x00;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 USICNT = 1;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 \/\/ wait for USIIFG is set<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 <strong>i2c_usi_mst_wait_usi_cnt_flag<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 FORCING_SDA_HIGH();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\/\/ function to wait for I2C counter flag condition<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>i2c_usi_mst_wait_usi_cnt_flag<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 <strong>while<\/strong>(usi_cnt_flag == FALSE)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/\/__bis_SR_register(LPM0_bits);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 }<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 \/\/ reset flag<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 usi_cnt_flag = FALSE;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\/\/ function to send a byte<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>unsigned<\/strong> <strong>char<\/strong> <strong>i2c_usi_mst_send_byte<\/strong>(<strong>unsigned<\/strong> <strong>char<\/strong> data_byte)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 \/\/ send address and R\/W bit<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 SET_SDA_AS_OUTPUT();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 USISRL = data_byte;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 USICNT = (USICNT &amp; 0xE0) + 8;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 \/\/ wait until USIIFG is set<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 <strong>i2c_usi_mst_wait_usi_cnt_flag<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 \/\/ check NACK\/ACK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 SET_SDA_AS_INPUT();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 USICNT = (USICNT &amp; 0xE0) + 1;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 \/\/ wait for USIIFG is set<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 <strong>i2c_usi_mst_wait_usi_cnt_flag<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 <strong>if<\/strong>(USISRL &amp; 0x01)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/\/ NACK received returns FALSE<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>return<\/strong> FALSE;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 }<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 <strong>return<\/strong> TRUE;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\/\/ function to read a byte<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>unsigned<\/strong> <strong>char<\/strong> <strong>i2c_usi_mst_read_byte<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 SET_SDA_AS_INPUT();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 USICNT = (USICNT &amp; 0xE0) + 8;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 \/\/ wait for USIIFG is set<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 <strong>i2c_usi_mst_wait_usi_cnt_flag<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 <strong>return<\/strong> USISRL;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\/\/ function to send (N)ACK bit<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>i2c_usi_mst_send_n_ack<\/strong>(<strong>unsigned<\/strong> <strong>char<\/strong> ack)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 \/\/ send (N)ack bit<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 SET_SDA_AS_OUTPUT();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 <strong>if<\/strong>(ack)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 USISRL = 0x00;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 }<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 <strong>else<\/strong><\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 USISRL = 0xFF;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 }<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 USICNT = (USICNT &amp; 0xE0) + 1;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 \/\/ wait until USIIFG is set<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 <strong>i2c_usi_mst_wait_usi_cnt_flag<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 \/\/ set SDA as input<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 SET_SDA_AS_INPUT();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\/\/ function to send I2C address with R\/W bit<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>unsigned<\/strong> <strong>char<\/strong> <strong>i2c_usi_mst_send_address<\/strong>(<strong>unsigned<\/strong> <strong>char<\/strong> addr, <strong>unsigned<\/strong> <strong>char<\/strong> r_w)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 addr &lt;&lt;= 1;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 <strong>if<\/strong>(r_w)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 addr |= 0x01;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 }<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 <strong>return<\/strong>(<strong>i2c_usi_mst_send_byte<\/strong>(addr));<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\/\/ USI I2C ISR function<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#pragma<\/strong> vector=USI_VECTOR<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>__interrupt<\/strong> <strong>void<\/strong> <strong>USI_ISR<\/strong> (<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 <strong>if<\/strong>(USICTL1 &amp; USISTTIFG)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/\/ do something if necessary<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/\/ clear flag<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 USICTL1 &amp;= ~USISTTIFG;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 }<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 <strong>if<\/strong>(USICTL1 &amp; USIIFG)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/\/ USI counter interrupt flag<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 \u00a0usi_cnt_flag = TRUE;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/\/ clear flag<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 USICTL1 &amp;= ~USIIFG;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 }<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 \/\/__bic_SR_register_on_exit(LPM0_bits);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<p>&nbsp;<\/p>\n<p><strong><em>PCF8574.h<\/em><\/strong><\/p>\n<p>&nbsp;<\/p>\n<pre><span style=\"color: #0000ff\"><strong>#include<\/strong> \"I2C.h\"<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>PCF8574_address<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0x20<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>unsigned<\/strong> <strong>char<\/strong> <strong>PCF8574_read<\/strong>(void);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>PCF8574_write<\/strong>(<strong>unsigned<\/strong> <strong>char<\/strong> data_byte);<\/span><\/pre>\n<p>&nbsp;<\/p>\n<p><strong><em>PCF8574.c<\/em><\/strong><\/p>\n<p>&nbsp;<\/p>\n<pre><span style=\"color: #0000ff\"><strong>#include<\/strong> \"PCF8574.h\"<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>unsigned<\/strong> <strong>char<\/strong> <strong>PCF8574_read<\/strong>(void)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>unsigned<\/strong> <strong>char<\/strong> port_byte = 0x00;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>i2c_usi_mst_gen_start<\/strong>();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>i2c_usi_mst_send_address<\/strong>(PCF8574_address, rd);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 port_byte = <strong>i2c_usi_mst_read_byte<\/strong>();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>i2c_usi_mst_send_n_ack<\/strong>(0);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>i2c_usi_mst_gen_stop<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>return<\/strong> port_byte;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>PCF8574_write<\/strong>(<strong>unsigned<\/strong> <strong>char<\/strong> data_byte)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>i2c_usi_mst_gen_start<\/strong>();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>i2c_usi_mst_send_address<\/strong>(PCF8574_address, wr);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>i2c_usi_mst_send_byte<\/strong>(data_byte);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>i2c_usi_mst_gen_stop<\/strong>();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<p>&nbsp;<\/p>\n<p><strong><em>main.c<\/em><\/strong><\/p>\n<p>&nbsp;<\/p>\n<pre><span style=\"color: #0000ff\"><strong>#include<\/strong> &lt;msp430.h&gt;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#include<\/strong> \"delay.h\"<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#include<\/strong> \"I2C.h\"<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#include<\/strong> \"PCF8574.h\"<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>GPIO_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>BCSplus_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>USI_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>System_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>WDTplus_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>main<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 \u00a0<strong>unsigned<\/strong> <strong>char<\/strong> i = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Stop watchdog timer from timing out during initial start-up. *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 WDTCTL = WDTPW | WDTHOLD;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* initialize Config for the MSP430 GPIO *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>GPIO_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* initialize Config for the MSP430 2xx family clock systems (BCS) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>BCSplus_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* initialize Config for the MSP430 USI *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>USI_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* initialize Config for the MSP430 System Registers *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>System_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* initialize Config for the MSP430 WDT+ *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>WDTplus_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>while<\/strong>(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\u00a0\u00a0 <strong>for<\/strong>(i = 1; i &lt; 128; i &lt;&lt;= 1)<\/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\u00a0\u00a0 <strong>PCF8574_write<\/strong>(i);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>delay_ms<\/strong>(200);<\/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 <strong>for<\/strong>(i = 128; i &gt; 1; i &gt;&gt;= 1)<\/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\u00a0\u00a0 <strong>PCF8574_write<\/strong>(i);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>delay_ms<\/strong>(200);<\/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 };<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>GPIO_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: GPIO_graceInit_prologue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User initialization code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: GPIO_graceInit_prologue) *\/<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Output Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1OUT = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Port Select Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1SEL = BIT6 | BIT7;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Direction Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1DIR = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Interrupt Edge Select Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1IES = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Interrupt Flag Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1IFG = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Output Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2OUT = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Direction Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2DIR = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Interrupt Edge Select Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2IES = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Interrupt Flag Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2IFG = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: GPIO_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: GPIO_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>BCSplus_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: BCSplus_graceInit_prologue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User initialization code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: BCSplus_graceInit_prologue) *\/<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Basic Clock System Control 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 * SELM_0 -- DCOCLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * DIVM_0 -- Divide by 1<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~SELS -- DCOCLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * DIVS_0 -- Divide by 1<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~DCOR -- DCO uses internal resistor<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 BCSCTL2 = SELM_0 | DIVM_0 | DIVS_0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>if<\/strong> (CALBC1_1MHZ != 0xFF) {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/* Follow recommended flow. First, clear all DCOx and MODx bits. Then<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 * apply new RSELx values. Finally, apply new DCOx and MODx bit values.<\/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 DCOCTL = 0x00;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 BCSCTL1 = CALBC1_1MHZ;\u00a0\u00a0\u00a0\u00a0\u00a0 \/* Set DCO to 1MHz *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 DCOCTL = CALDCO_1MHZ;<\/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 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Basic Clock System Control 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 * XT2OFF -- Disable XT2CLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~XTS -- Low Frequency<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * DIVA_0 -- Divide by 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 * Note: ~XTS indicates that XTS has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 BCSCTL1 |= XT2OFF | DIVA_0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Basic Clock System Control 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 * XT2S_0 -- 0.4 - 1 MHz<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * LFXT1S_2 -- If XTS = 0, XT1 = VLOCLK ; If XTS = 1, XT1 = 3 - 16-MHz crystal or resonator<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * XCAP_1 -- ~6 pF<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 BCSCTL3 = XT2S_0 | LFXT1S_2 | XCAP_1;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: BCSplus_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: BCSplus_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>USI_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: USI_graceInit_prologue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User initialization code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: USI_graceInit_prologue) *\/<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Disable USI *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 USICTL0 |= USISWRST;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * USI Control Register 0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * USIPE7 -- USI function enabled<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * USIPE6 -- USI function enabled<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~USIPE5 -- USI function disabled<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~USILSB -- MSB first<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * USIMST -- Master mode<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~USIGE -- Output latch enable depends on shift clock<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~USIOE -- Output disabled<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * USISWRST -- USI logic held in reset state<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 USICTL0 = USIPE7 | USIPE6 | USIMST | USISWRST;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * USI Control Register 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 * ~USICKPH -- Data is changed on the first SCLK edge and captured on the following edge<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * USII2C -- I2C mode enabled<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~USISTTIE -- Interrupt on START condition disabled<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * USIIE -- Interrupt enabled<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~USIAL -- No arbitration lost condition<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~USISTP -- No STOP condition received<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~USISTTIFG -- No START condition received. No interrupt pending<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * USIIFG -- Interrupt pending<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 USICTL1 = USII2C | USIIE | USIIFG;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * USI Clock Control Register<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * USIDIV_4 -- Divide by 16<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * USISSEL_2 -- SMCLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * USICKPL -- Inactive state is high<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~USISWCLK -- Input clock is low<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~USISWCLK indicates that USISWCLK has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 USICKCTL = USIDIV_4 | USISSEL_2 | USICKPL;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * USI Bit Counter Register<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~USISCLREL -- SCL line is held low if USIIFG is set<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~USI16B -- 8-bit shift register mode. Low byte register USISRL is used<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * USIIFGCC -- USIIFG is not cleared automatically<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~USICNT4 -- USI bit count<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~USICNT3 -- USI bit count<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~USICNT2 -- USI bit count<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~USICNT1 -- USI bit count<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~USICNT0 -- USI bit count<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 USICNT = USIIFGCC;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Enable USI *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 USICTL0 &amp;= ~USISWRST;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Clear pending flag *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 USICTL1 &amp;= ~(USIIFG + USISTTIFG);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: USI_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: USI_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>System_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: System_graceInit_prologue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User initialization code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: System_graceInit_prologue) *\/<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Clear oscillator fault flag with software delay *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>do<\/strong><\/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 \/\/ Clear OSC fault flag<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 IFG1 &amp;= ~OFIFG;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ 50us delay<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>__delay_cycles<\/strong>(50);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 } <strong>while<\/strong> (IFG1 &amp; OFIFG);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * SR, Status Register<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~SCG1 -- Disable System clock generator 1<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~SCG0 -- Disable System clock generator 0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~OSCOFF -- Oscillator On<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~CPUOFF -- CPU On<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * GIE -- General interrupt enable<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>__bis_SR_register<\/strong>(GIE);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: System_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: System_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>WDTplus_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: RTC_B_graceInit_prologue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User initialization code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: RTC_B_graceInit_prologue) *\/<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * WDTCTL, Watchdog Timer+ Register<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * WDTPW -- Watchdog password<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * WDTHOLD -- Watchdog timer+ is stopped<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTNMIES -- NMI on rising edge<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTNMI -- Reset function<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTTMSEL -- Watchdog mode<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTCNTCL -- No action<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTSSEL -- SMCLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTIS0 -- Watchdog clock source bit0 disabled<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTIS1 -- Watchdog clock source bit1 disabled<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 WDTCTL = WDTPW | WDTHOLD;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: RTC_B_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: RTC_B_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<p>&nbsp;<\/p>\n<h2>Simulation<\/h2>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/USI-I2C-Sim1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13862\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/USI-I2C-Sim1-580x356.png\" alt=\"USI-I2C Sim\" width=\"580\" height=\"356\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/USI-I2C-Sim1-580x356.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/USI-I2C-Sim1.png 809w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<h2>Explanation<\/h2>\n<p style=\"text-align: justify\">To keep things simple, I demoed USI-based I2C using PCF8574 8-bit I2C GPIO expander. Here the GPIOs of PCF8574 are used to create a running LED light pattern similar to <em>Kitt Scan<\/em> from the popular series <em>Knight Rider<\/em>. The code for USI-based I2C implementation is obtained from TI\u2019s wiki page:\u00a0<a href=\"http:\/\/processors.wiki.ti.com\/index.php\/I2C_Communication_with_USI_Module\">http:\/\/processors.wiki.ti.com\/index.php\/I2C_Communication_with_USI_Module<\/a>. \u00a0I did some minor modifications on it. The code is self-explanatory with detailed documentation on the wiki page and so I won\u2019t be discussing it. The rest of the code is the implementation of PCF8574 driver and actual demo code.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/USI-I2C1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-13863\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/USI-I2C1.png\" alt=\"USI-I2C\" width=\"566\" height=\"471\" \/><\/a><\/p>\n<h2>Demo<\/h2>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/USI-I2C.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13875\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/USI-I2C-580x389.jpg\" alt=\"USI-I2C\" width=\"580\" height=\"389\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/USI-I2C-580x389.jpg 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/USI-I2C.jpg 590w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p>Demo video: <a href=\"https:\/\/www.youtube.com\/watch?v=9svi-0cd2gk\">https:\/\/www.youtube.com\/watch?v=9svi-0cd2gk<\/a>.<\/p>\n<p><!--more--><\/p>\n<h1>Software I2C<\/h1>\n<p style=\"text-align: justify\">Software I2C implementation is a bit complex compared to software SPI. This is because there are start-stop conditions and acknowledgments that are also needed to be taken care off along with data transactions. However, it becomes the only option when USI\/USCI are absent. Unlike SPI, I2C is slow and so software implementation doesn\u2019t affect performance significantly.<a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/ATMega328P-Weather-Station1.jpg\"><br \/>\n<\/a><\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/ATMega328P-Weather-Station1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13846\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/ATMega328P-Weather-Station1-580x576.jpg\" alt=\"ATMega328P Weather Station\" width=\"580\" height=\"576\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/ATMega328P-Weather-Station1-580x576.jpg 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/ATMega328P-Weather-Station1-150x150.jpg 150w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/ATMega328P-Weather-Station1-90x90.jpg 90w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/ATMega328P-Weather-Station1.jpg 590w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p style=\"text-align: justify\">Shown above is an I2C-based SSD1306 OLED display. This was the display for my weather station. The weather station was based on a <a href=\"https:\/\/www.sparkfun.com\/products\/retired\/10586\">Sparkfun Weather Board<\/a>. \u00a0During that project, I was encountering an issue with the on board SHT15 relative humidity-temperature sensor chip and I could not find out the issue for quite some time. I thought that the MCU\u2019s TWI module was malfunctioning and so I took the help of software I2C for rooting out the cause. Cases like these make software I2C-based testing really useful and interesting.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/I2C1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13849\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/I2C1-580x104.png\" alt=\"I2C\" width=\"580\" height=\"104\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/I2C1-580x104.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/I2C1.png 927w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p style=\"text-align: justify\">Shown above is the typical timing diagram for a I2C bus. We can expect the same patterns in both hardware I2C and software I2C but bus speed may differ. I2C bus speed become critical in some cases. For example, consider the case of the OLED display above.<\/p>\n<h2 style=\"text-align: justify\">Code Example<\/h2>\n<p>&nbsp;<\/p>\n<p><strong><em>SW_I2C.h<\/em><\/strong><\/p>\n<p>&nbsp;<\/p>\n<pre><span style=\"color: #0000ff\"><strong>#include<\/strong> &lt;msp430.h&gt;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#include<\/strong> \"delay.h\"<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>SW_I2C_DIR<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0 P1DIR<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>SW_I2C_OUT<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0 P1OUT<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>SW_I2C_IN<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 P1IN<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>SDA_pin<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 BIT7<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>SCL_pin<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 BIT6<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>SDA_DIR_OUT<\/strong>()\u00a0\u00a0 <strong>do<\/strong>{SW_I2C_DIR |= SDA_pin;}<strong>while<\/strong>(0)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>SDA_DIR_IN<\/strong>()\u00a0\u00a0\u00a0 <strong>do<\/strong>{SW_I2C_DIR &amp;= ~SDA_pin;}<strong>while<\/strong>(0)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>SCL_DIR_OUT<\/strong>()\u00a0\u00a0 <strong>do<\/strong>{SW_I2C_DIR |= SCL_pin;}<strong>while<\/strong>(0)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>SCL_DIR_IN<\/strong>()\u00a0\u00a0\u00a0 <strong>do<\/strong>{SW_I2C_DIR &amp;= ~SCL_pin;}<strong>while<\/strong>(0)<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>SDA_HIGH<\/strong>()\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>do<\/strong>{SW_I2C_OUT |= SDA_pin;}<strong>while<\/strong>(0)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>SDA_LOW<\/strong>()\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>do<\/strong>{SW_I2C_OUT &amp;= ~SDA_pin;}<strong>while<\/strong>(0)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>SCL_HIGH<\/strong>()\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>do<\/strong>{SW_I2C_OUT |= SCL_pin;}<strong>while<\/strong>(0)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>SCL_LOW<\/strong>()\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>do<\/strong>{SW_I2C_OUT &amp;= ~SCL_pin;}<strong>while<\/strong>(0)<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>SDA_IN<\/strong>()\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 (SW_I2C_IN &amp; SDA_pin)<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>I2C_ACK<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0xFF<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>I2C_NACK<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0x00<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>I2C_timeout<\/strong>\u00a0\u00a0\u00a0\u00a0 1000<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>SW_I2C_init<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>SW_I2C_start<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>SW_I2C_stop<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>unsigned<\/strong> <strong>char<\/strong> <strong>SW_I2C_read<\/strong>(<strong>unsigned<\/strong> <strong>char<\/strong> ack);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>SW_I2C_write<\/strong>(<strong>unsigned<\/strong> <strong>char<\/strong> value);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>SW_I2C_ACK_NACK<\/strong>(<strong>unsigned<\/strong> <strong>char<\/strong> mode);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>unsigned<\/strong> <strong>char<\/strong> <strong>SW_I2C_wait_ACK<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<p>&nbsp;<\/p>\n<p><strong><em>SW_I2C.c<\/em><\/strong><\/p>\n<p>&nbsp;<\/p>\n<pre><span style=\"color: #0000ff\"><strong>#include<\/strong> \"SW_I2C.h\"<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>SW_I2C_init<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 SDA_DIR_OUT();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 SCL_DIR_OUT();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>delay_ms<\/strong>(1);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 SDA_HIGH();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 SCL_HIGH();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>SW_I2C_start<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 SDA_DIR_OUT();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 SDA_HIGH();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 SCL_HIGH();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>delay_us<\/strong>(4);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 SDA_LOW();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>delay_us<\/strong>(4);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 SCL_LOW();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>SW_I2C_stop<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 SDA_DIR_OUT();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 SDA_LOW();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 SCL_LOW();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>delay_us<\/strong>(4);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 SDA_HIGH();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 SCL_HIGH();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>delay_us<\/strong>(4);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>unsigned<\/strong> <strong>char<\/strong> <strong>SW_I2C_read<\/strong>(<strong>unsigned<\/strong> <strong>char<\/strong> ack)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>unsigned<\/strong> <strong>char<\/strong> i = 8;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>unsigned<\/strong> <strong>char<\/strong> j = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 SDA_DIR_IN();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>while<\/strong>(i &gt; 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\u00a0\u00a0 SCL_LOW();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>delay_us<\/strong>(2);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 SCL_HIGH();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>delay_us<\/strong>(2);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 j &lt;&lt;= 1;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>if<\/strong>(SDA_IN() != 0x00)<\/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\u00a0\u00a0 j++;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 <strong>delay_us<\/strong>(1);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 i--;<\/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 <strong>switch<\/strong>(ack)<\/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 <strong>case<\/strong> I2C_ACK:<\/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\u00a0\u00a0 <strong>SW_I2C_ACK_NACK<\/strong>(I2C_ACK);;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>break<\/strong>;<\/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 <strong>default<\/strong>:<\/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\u00a0\u00a0 <strong>SW_I2C_ACK_NACK<\/strong>(I2C_NACK);;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>break<\/strong>;<\/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 }<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>return<\/strong> j;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>SW_I2C_write<\/strong>(<strong>unsigned<\/strong> <strong>char<\/strong> value)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>unsigned<\/strong> <strong>char<\/strong> i = 8;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 SDA_DIR_OUT();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 SCL_LOW();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>while<\/strong>(i &gt; 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\u00a0\u00a0\u00a0\u00a0 <strong>if<\/strong>(((value &amp; 0x80) &gt;&gt; 7) != 0x00)<\/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\u00a0\u00a0 SDA_HIGH();<\/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 <strong>else<\/strong><\/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\u00a0\u00a0 SDA_LOW();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 value &lt;&lt;= 1;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>delay_us<\/strong>(2);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 SCL_HIGH();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>delay_us<\/strong>(2);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 SCL_LOW();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>delay_us<\/strong>(2);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 i--;<\/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><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>SW_I2C_ACK_NACK<\/strong>(<strong>unsigned<\/strong> <strong>char<\/strong> mode)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 SCL_LOW();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 SDA_DIR_OUT();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>switch<\/strong>(mode)<\/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 <strong>case<\/strong> I2C_ACK:<\/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\u00a0\u00a0 SDA_LOW();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>break<\/strong>;<\/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 <strong>default<\/strong>:<\/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\u00a0\u00a0 SDA_HIGH();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>break<\/strong>;<\/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 }<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>delay_us<\/strong>(2);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 SCL_HIGH();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>delay_us<\/strong>(2);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 SCL_LOW();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>unsigned<\/strong> <strong>char<\/strong> <strong>SW_I2C_wait_ACK<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 \u00a0\u00a0<strong>signed<\/strong> <strong>int<\/strong> timeout = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 SDA_DIR_IN();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 SDA_HIGH();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>delay_us<\/strong>(1);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 SCL_HIGH();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>delay_us<\/strong>(1);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>while<\/strong>(SDA_IN() != 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 timeout++;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>if<\/strong>(timeout &gt; I2C_timeout)<\/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\u00a0\u00a0 <strong>SW_I2C_stop<\/strong>();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>return<\/strong> 1;<\/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 };<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 SCL_LOW();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>return<\/strong> 0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<p>&nbsp;<\/p>\n<p><strong><em>PCF8591.h<\/em><\/strong><\/p>\n<p>&nbsp;<\/p>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>PCF8591_address<\/strong>\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 0x90<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>PCF8591_read_cmd<\/strong>\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 (PCF8591_address | 0x01)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>PCF8591_write_cmd<\/strong>\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 PCF8591_address<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>AIN0<\/strong>\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 0x00<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>AIN1<\/strong>\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 0x01<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>AIN2<\/strong>\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 0x02<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>AIN3<\/strong>\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 0x03<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>Auto_Increment_Enable<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0x04<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>Auto_Increment_Disable<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0x00\r\n\r\n<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>Four_Channel_ADC<\/strong>\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\u00a00x00<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>Three_differential_Inputs<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0x10<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>AIN0_and_1_Single_AIN2_and_AIN3_Differential<\/strong>\u00a0 0x20<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>All_Differential<\/strong>\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 0x30<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>AOut_enable<\/strong>\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 0x40<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>AOut_disable<\/strong>\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 0x00<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>PCF8591_write<\/strong>(<strong>unsigned<\/strong> <strong>char<\/strong> control_value, <strong>unsigned<\/strong> <strong>char<\/strong> data_value);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>unsigned<\/strong> <strong>char<\/strong> <strong>PCF8591_read<\/strong>(<strong>unsigned<\/strong> <strong>char<\/strong> control_value);<\/span><\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><strong><em>PCF8591.c<\/em><\/strong><\/p>\n<p><strong><em>\u00a0<\/em><\/strong><\/p>\n<pre><span style=\"color: #0000ff\"><strong>#include<\/strong> \"PCF8591.h\"<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>PCF8591_write<\/strong>(<strong>unsigned<\/strong> <strong>char<\/strong> control_value, <strong>unsigned<\/strong> <strong>char<\/strong> data_value)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 <strong>SW_I2C_start<\/strong>();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 <strong>SW_I2C_write<\/strong>(PCF8591_write_cmd);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 <strong>SW_I2C_wait_ACK<\/strong>();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 <strong>SW_I2C_write<\/strong>((control_value &amp; 0xFF));<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 <strong>SW_I2C_wait_ACK<\/strong>();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 <strong>SW_I2C_write<\/strong>(data_value);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 <strong>SW_I2C_wait_ACK<\/strong>();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 <strong>SW_I2C_stop<\/strong>();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>unsigned<\/strong> <strong>char<\/strong> <strong>PCF8591_read<\/strong>(<strong>unsigned<\/strong> <strong>char<\/strong> control_value)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 <strong>unsigned<\/strong> <strong>char<\/strong> value = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 <strong>SW_I2C_start<\/strong>();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 <strong>SW_I2C_write<\/strong>(PCF8591_write_cmd);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 <strong>SW_I2C_wait_ACK<\/strong>();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 <strong>SW_I2C_write<\/strong>((control_value &amp; 0xFF));<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 \u00a0\u00a0\u00a0<strong>SW_I2C_ACK_NACK<\/strong>(I2C_ACK);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 <strong>SW_I2C_stop<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 <strong>SW_I2C_start<\/strong>();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 <strong>SW_I2C_write<\/strong>(PCF8591_read_cmd);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 <strong>SW_I2C_wait_ACK<\/strong>();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 value = <strong>SW_I2C_read<\/strong>(0);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 <strong>SW_I2C_wait_ACK<\/strong>();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 <strong>SW_I2C_stop<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 <strong>return<\/strong> value;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<p>&nbsp;<\/p>\n<p><strong><em>main.c<\/em><\/strong><\/p>\n<p>&nbsp;<\/p>\n<pre><span style=\"color: #0000ff\"><strong>#include<\/strong> &lt;msp430.h&gt;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#include<\/strong> \"delay.h\"<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#include<\/strong> \"SW_I2C.h\"<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#include<\/strong> \"PCF8591.h\"<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#include<\/strong> \"lcd.h\"<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>GPIO_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>BCSplus_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>System_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>WDTplus_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>lcd_print<\/strong>(<strong>unsigned<\/strong> <strong>char<\/strong> x_pos, <strong>unsigned<\/strong> <strong>char<\/strong> y_pos, <strong>unsigned<\/strong> <strong>char<\/strong> value);<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>main<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>unsigned<\/strong> <strong>char<\/strong> adc0 = 0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>unsigned<\/strong> <strong>char<\/strong> adc1 = 0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>unsigned<\/strong> <strong>char<\/strong> adc2 = 0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>unsigned<\/strong> <strong>char<\/strong> adc3 = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 WDTCTL = WDTPW | WDTHOLD;\u00a0\u00a0\u00a0 \/\/ Stop watchdog timer<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* initialize Config for the MSP430 GPIO *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 \u00a0\u00a0<strong>GPIO_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* initialize Config for the MSP430 2xx family clock systems (BCS) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>BCSplus_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* initialize Config for the MSP430 System Registers *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>System_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* initialize Config for the MSP430 WDT+ *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 \u00a0\u00a0<strong>WDTplus_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>SW_I2C_init<\/strong>();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_init<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_goto<\/strong>(0, 0);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_putstr<\/strong>(\"A0:\");<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_goto<\/strong>(9, 0);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_putstr<\/strong>(\"A1:\");<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_goto<\/strong>(0, 1);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_putstr<\/strong>(\"A2:\");<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_goto<\/strong>(9, 1);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_putstr<\/strong>(\"A3:\");<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>while<\/strong>(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\u00a0\u00a0 adc0 = <strong>PCF8591_read<\/strong>(AOut_enable | Four_Channel_ADC | Auto_Increment_Disable | AIN0);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>lcd_print<\/strong>(4, 0, adc0);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 adc1 = <strong>PCF8591_read<\/strong>(AOut_enable | Four_Channel_ADC | Auto_Increment_Disable | AIN1);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>lcd_print<\/strong>(13, 0, adc1);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0adc2 = <strong>PCF8591_read<\/strong>(AOut_enable | Four_Channel_ADC | Auto_Increment_Disable | AIN2);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>lcd_print<\/strong>(4, 1, adc2);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 adc3 = <strong>PCF8591_read<\/strong>(AOut_enable | Four_Channel_ADC | Auto_Increment_Disable | AIN3);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>lcd_print<\/strong>(13, 1, adc3);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>PCF8591_write<\/strong>(AOut_enable, adc0);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>delay_ms<\/strong>(400);<\/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><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>GPIO_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: GPIO_graceInit_prologue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User initialization code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: GPIO_graceInit_prologue) *\/<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Output Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1OUT = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Direction Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1DIR = BIT0 | BIT6 | BIT7;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Interrupt Edge Select Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1IES = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Interrupt Flag Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1IFG = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Output Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2OUT = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Port Select Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2SEL &amp;= ~(BIT6 | BIT7);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Direction Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2DIR = BIT0 | BIT1 | BIT2;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Interrupt Edge Select Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2IES = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Interrupt Flag Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2IFG = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: GPIO_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: GPIO_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>BCSplus_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: BCSplus_graceInit_prologue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User initialization code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: BCSplus_graceInit_prologue) *\/<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Basic Clock System Control 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 * SELM_0 -- DCOCLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * DIVM_0 -- Divide by 1<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~SELS -- DCOCLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * DIVS_0 -- Divide by 1<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~DCOR -- DCO uses internal resistor<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 BCSCTL2 = SELM_0 | DIVM_0 | DIVS_0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>if<\/strong> (CALBC1_1MHZ != 0xFF) {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/* Follow recommended flow. First, clear all DCOx and MODx bits. Then<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 * apply new RSELx values. Finally, apply new DCOx and MODx bit values.<\/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 DCOCTL = 0x00;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 BCSCTL1 = CALBC1_1MHZ;\u00a0\u00a0\u00a0\u00a0\u00a0 \/* Set DCO to 1MHz *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 DCOCTL = CALDCO_1MHZ;<\/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 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Basic Clock System Control 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 * XT2OFF -- Disable XT2CLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~XTS -- Low Frequency<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * DIVA_0 -- Divide by 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 * Note: ~XTS indicates that XTS has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 BCSCTL1 |= XT2OFF | DIVA_0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Basic Clock System Control 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 * XT2S_0 -- 0.4 - 1 MHz<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * LFXT1S_0 -- If XTS = 0, XT1 = 32768kHz Crystal ; If XTS = 1, XT1 = 0.4 - 1-MHz crystal or resonator<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * XCAP_1 -- ~6 pF<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 BCSCTL3 = XT2S_0 | LFXT1S_0 | XCAP_1;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: BCSplus_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: BCSplus_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>System_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: System_graceInit_prologue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User initialization code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: System_graceInit_prologue) *\/<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * SR, Status Register<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~SCG1 -- Disable System clock generator 1<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~SCG0 -- Disable System clock generator 0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~OSCOFF -- Oscillator On<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~CPUOFF -- CPU On<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * GIE -- General interrupt enable<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>__bis_SR_register<\/strong>(GIE);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: System_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: System_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>WDTplus_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: RTC_B_graceInit_prologue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User initialization code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: RTC_B_graceInit_prologue) *\/<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * WDTCTL, Watchdog Timer+ Register<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * WDTPW -- Watchdog password<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * WDTHOLD -- Watchdog timer+ is stopped<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTNMIES -- NMI on rising edge<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTNMI -- Reset function<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTTMSEL -- Watchdog mode<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTCNTCL -- No action<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTSSEL -- SMCLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTIS0 -- Watchdog clock source bit0 disabled<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTIS1 -- Watchdog clock source bit1 disabled<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 WDTCTL = WDTPW | WDTHOLD;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: RTC_B_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: RTC_B_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>lcd_print<\/strong>(<strong>unsigned<\/strong> <strong>char<\/strong> x_pos, <strong>unsigned<\/strong> <strong>char<\/strong> y_pos, <strong>unsigned<\/strong> <strong>char<\/strong> value)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 <strong>unsigned<\/strong> <strong>char<\/strong> ch = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 ch = ((value \/ 100) + 0x30);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 <strong>LCD_goto<\/strong>(x_pos, y_pos);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 <strong>LCD_putchar<\/strong>(ch);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 ch = (((value \/ 10) % 10) + 0x30);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 <strong>LCD_goto<\/strong>((x_pos + 1), y_pos);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 <strong>LCD_putchar<\/strong>(ch);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 ch = ((value % 10) + 0x30);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 <strong>LCD_goto<\/strong>((x_pos + 2), y_pos);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 <strong>LCD_putchar<\/strong>(ch);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<p>&nbsp;<\/p>\n<h2>Simulation<\/h2>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/SW-I2C-Sim1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13857\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/SW-I2C-Sim1-580x310.png\" alt=\"SW-I2C Sim\" width=\"580\" height=\"310\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/SW-I2C-Sim1-580x310.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/SW-I2C-Sim1-1024x547.png 1024w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/SW-I2C-Sim1.png 1087w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<h2>Explanation<\/h2>\n<p style=\"text-align: justify\">Just like software-based SPI, software-based I2C employs bit-banging ordinary digital I\/Os. The whole functioning of I2C protocol is implemented in software. Again, the software I2C codes are self-explanatory. This software I2C implementation can be used for any I2C-based device. If you have gone through the pages recommended earlier, you will understand how it is working.<\/p>\n<p style=\"text-align: justify\">For this demo, a PCF8591 8-bit ADC-DAC module is used. This ready-made board hosts four 8-bit ADC input channels and an 8-bit DAC output channel. Off the shelf, it contains a thermistor for sensing ambient temperature, a light-dependent resistor (LDR) for detecting light level, a potentiometer connected across the power rail and a free ADC input. The ADC inputs can also be used in various ways. Check the device\u2019s datasheet for details. I did a very raw level coding for the demo, and so I just read and showed the ADC values. The DAC is operated using the value of ADC channel 0.<\/p>\n<h2>Demo<\/h2>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/SW-I2C.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13872\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/SW-I2C-580x389.jpg\" alt=\"SW-I2C\" width=\"580\" height=\"389\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/SW-I2C-580x389.jpg 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/SW-I2C.jpg 590w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p style=\"text-align: justify\">Demo video: <a href=\"https:\/\/www.youtube.com\/watch?v=ElRvBRv7YY4\">https:\/\/www.youtube.com\/watch?v=ElRvBRv7YY4<\/a>.<\/p>\n<p style=\"text-align: justify\"><!--more--><\/p>\n<h1>Two Wire LCD<\/h1>\n<p style=\"text-align: justify\">This segment is basically the repetition of the bit-banging-based LCD example shown earlier. In that example, we saw software SPI-based LCD driving technique. Here we will see the same but with USI-based I2C. We can also use software I2C for the same purpose.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/I2C-LCD.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13880\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/I2C-LCD-580x276.jpg\" alt=\"I2C LCD\" width=\"580\" height=\"276\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/I2C-LCD-580x276.jpg 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/I2C-LCD.jpg 590w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p style=\"text-align: justify\">In the embedded system world, there is a cheap and popular 2-wire LCD module based on PCF8574T.<a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/i2c_backpack_01-1024x527.jpg\"><br \/>\n<\/a><\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/i2c_backpack_01-1024x527.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13882\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/i2c_backpack_01-1024x527-580x268.jpg\" alt=\"i2c_backpack_01-1024x527\" width=\"580\" height=\"268\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/i2c_backpack_01-1024x527-580x268.jpg 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/i2c_backpack_01-1024x527.jpg 590w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p style=\"text-align: justify\">There are a few advantages of this module. Firstly, it is based on a PCF8574T chip that is made by NXP (<em>a.k.a<\/em> Philips). NXP happens to be the founder of I2C communication and so the chip is well documented in terms of I2C communication. Secondly, there are three external address selection bits which can be used to address multiple LCDs existing on the same I2C bus. Lastly the module is compact and readily plug-and-playable.<\/p>\n<h2>Code Example<\/h2>\n<p>&nbsp;<\/p>\n<p><strong><em>lcd.h<\/em><\/strong><\/p>\n<p>&nbsp;<\/p>\n<pre><span style=\"color: #0000ff\"><strong>#include<\/strong> &lt;msp430.h&gt;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#include<\/strong> \"PCF8574.h\"<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#include<\/strong> \"delay.h\"<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>clear_display<\/strong>\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 0x01<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>goto_home<\/strong>\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 0x02<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>cursor_direction_inc<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0(0x04 | 0x02)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>cursor_direction_dec<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 (0x04 | 0x00)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>display_shift<\/strong>\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 (0x04 | 0x01)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>display_no_shift<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 (0x04 | 0x00)<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>display_on<\/strong>\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 (0x08 | 0x04)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>display_off<\/strong>\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 (0x08 | 0x02)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>cursor_on<\/strong>\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 (0x08 | 0x02)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>cursor_off<\/strong>\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 (0x08 | 0x00)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>blink_on<\/strong>\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 (0x08 | 0x01)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>blink_off<\/strong>\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 (0x08 | 0x00)<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>_8_pin_interface<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 (0x20 | 0x10)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>_4_pin_interface<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 (0x20 | 0x00)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>_2_row_display<\/strong>\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 (0x20 | 0x08)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>_1_row_display<\/strong>\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 (0x20 | 0x00)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>_5x10_dots<\/strong>\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 (0x20 | 0x40)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>_5x7_dots<\/strong>\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 (0x20 | 0x00)<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>dly<\/strong>\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 1<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>CMD<\/strong>\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\u00a00<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>DAT<\/strong>\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 1<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>BL_ON<\/strong>\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 1<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#define<\/strong> <strong>BL_OFF<\/strong>\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 0<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>unsigned<\/strong> <strong>char<\/strong> bl_state;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>unsigned<\/strong> <strong>char<\/strong> data_value;<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>LCD_init<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>LCD_send<\/strong>(<strong>unsigned<\/strong> <strong>char<\/strong> value, <strong>unsigned<\/strong> <strong>char<\/strong> control_type);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>LCD_4bit_send<\/strong>(<strong>unsigned<\/strong> <strong>char<\/strong> lcd_data);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>LCD_putstr<\/strong>(<strong>char<\/strong> *lcd_string);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>LCD_putchar<\/strong>(<strong>char<\/strong> char_data);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>LCD_clear_home<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>LCD_goto<\/strong>(<strong>unsigned<\/strong> <strong>char<\/strong> x_pos, <strong>unsigned<\/strong> <strong>char<\/strong> y_pos);<\/span><\/pre>\n<p>&nbsp;<\/p>\n<p><strong><em>lcd.c<\/em><\/strong><\/p>\n<p><strong><em>\u00a0<\/em><\/strong><\/p>\n<pre><span style=\"color: #0000ff\"><strong>#include<\/strong> \"lcd.h\"<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>extern<\/strong> <strong>unsigned<\/strong> <strong>char<\/strong> data_value;<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>LCD_init<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 bl_state = BL_ON;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 data_value = 0x04;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>PCF8574_write<\/strong>(data_value);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>delay_ms<\/strong>(10);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 data_value = 0x30;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>PCF8574_write<\/strong>(data_value);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 data_value |= 0x04;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>PCF8574_write<\/strong>(data_value);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>delay_ms<\/strong>(dly);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 data_value &amp;= 0xF1;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>PCF8574_write<\/strong>(data_value);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>delay_ms<\/strong>(dly);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 data_value = 0x30;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>PCF8574_write<\/strong>(data_value);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 data_value |= 0x04;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>PCF8574_write<\/strong>(data_value);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>delay_ms<\/strong>(dly);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 data_value &amp;= 0xF1;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>PCF8574_write<\/strong>(data_value);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>delay_ms<\/strong>(dly);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 data_value = 0x30;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>PCF8574_write<\/strong>(data_value);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 data_value |= 0x04;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>PCF8574_write<\/strong>(data_value);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>delay_ms<\/strong>(dly);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 data_value &amp;= 0xF1;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>PCF8574_write<\/strong>(data_value);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>delay_ms<\/strong>(dly);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 data_value = 0x20;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>PCF8574_write<\/strong>(data_value);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 data_value |= 0x04;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>PCF8574_write<\/strong>(data_value);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>delay_ms<\/strong>(dly);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 data_value &amp;= 0xF1;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>PCF8574_write<\/strong>(data_value);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>delay_ms<\/strong>(dly);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_send<\/strong>((_4_pin_interface | _2_row_display | _5x7_dots), CMD);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_send<\/strong>((display_on | cursor_off | blink_off), CMD);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_send<\/strong>((clear_display), CMD);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_send<\/strong>((cursor_direction_inc | display_no_shift), CMD);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>LCD_send<\/strong>(<strong>unsigned<\/strong> <strong>char<\/strong> value, <strong>unsigned<\/strong> <strong>char<\/strong> control_type)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>switch<\/strong>(control_type)<\/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 <strong>case<\/strong> CMD:<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 data_value &amp;= 0xF4;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>break<\/strong>;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>case<\/strong> DAT:<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 data_value |= 0x01;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>break<\/strong>;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\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 <strong>switch<\/strong>(bl_state)<\/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 <strong>case<\/strong> BL_ON:<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 data_value |= 0x08;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>break<\/strong>;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>case<\/strong> BL_OFF:<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 data_value &amp;= 0xF7;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>break<\/strong>;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\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 <strong>PCF8574_write<\/strong>(data_value);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_4bit_send<\/strong>(value);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>delay_ms<\/strong>(10);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>LCD_4bit_send<\/strong>(<strong>unsigned<\/strong> <strong>char<\/strong> lcd_data)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>unsigned<\/strong> <strong>char<\/strong> temp = 0x00;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 temp = (lcd_data &amp; 0xF0);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 data_value &amp;= 0x0F;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 data_value |= temp;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>PCF8574_write<\/strong>(data_value);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 data_value |= 0x04;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>PCF8574_write<\/strong>(data_value);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>delay_ms<\/strong>(dly);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 data_value &amp;= 0xF9;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>PCF8574_write<\/strong>(data_value);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>delay_ms<\/strong>(dly);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 temp = (lcd_data &amp; 0x0F);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 temp &lt;&lt;= 0x04;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 data_value &amp;= 0x0F;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 data_value |= temp;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>PCF8574_write<\/strong>(data_value);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 data_value |= 0x04;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>PCF8574_write<\/strong>(data_value);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>delay_ms<\/strong>(dly);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 data_value &amp;= 0xF9;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>PCF8574_write<\/strong>(data_value);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>delay_ms<\/strong>(dly);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>LCD_putstr<\/strong>(<strong>char<\/strong> *lcd_string)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>while<\/strong>(*lcd_string != '\\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\u00a0\u00a0 <strong>LCD_send<\/strong>((*lcd_string++), DAT);<\/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><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>LCD_putchar<\/strong>(<strong>char<\/strong> char_data)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_send<\/strong>(char_data, DAT);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>LCD_clear_home<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_send<\/strong>(clear_display, CMD);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_send<\/strong>(goto_home, CMD);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>LCD_goto<\/strong>(<strong>unsigned<\/strong> <strong>char<\/strong> x_pos,<strong>unsigned<\/strong> <strong>char<\/strong> y_pos)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>if<\/strong>(y_pos == 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\u00a0\u00a0 <strong>LCD_send<\/strong>((0x80 | x_pos), CMD);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 }<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>else<\/strong><\/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 <strong>LCD_send<\/strong>((0x80 | 0x40 | x_pos), CMD);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 }<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<p>&nbsp;<\/p>\n<p><strong><em>main.c<\/em><\/strong><\/p>\n<p>&nbsp;<\/p>\n<pre><span style=\"color: #0000ff\"><strong>#include<\/strong> &lt;msp430.h&gt;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#include<\/strong> \"delay.h\"<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#include<\/strong> \"I2C.h\"<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#include<\/strong> \"PCF8574.h\"<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#include<\/strong> \"lcd.h\"<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>GPIO_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>BCSplus_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>USI_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>System_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>WDTplus_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>show_value<\/strong>(<strong>unsigned<\/strong> <strong>char<\/strong> value);<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>main<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>unsigned<\/strong> <strong>char<\/strong> s = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>char<\/strong> txt1[] = {\"MICROARENA\"};<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>char<\/strong> txt2[] = {\"SShahryiar\"};<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>char<\/strong> txt3[] = {\"MSP-EXP430G2\"};<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>char<\/strong> txt4[] = {\"Launchpad!\"};<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Stop watchdog timer from timing out during initial start-up. *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 WDTCTL = WDTPW | WDTHOLD;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* initialize Config for the MSP430 GPIO *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>GPIO_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* initialize Config for the MSP430 2xx family clock systems (BCS) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>BCSplus_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* initialize Config for the MSP430 USI *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>USI_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* initialize Config for the MSP430 System Registers *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>System_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* initialize Config for the MSP430 WDT+ *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>WDTplus_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_init<\/strong>();<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_clear_home<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_goto<\/strong>(3, 0);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_putstr<\/strong>(txt1);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_goto<\/strong>(3, 1);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_putstr<\/strong>(txt2);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>delay_ms<\/strong>(2600);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_clear_home<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>for<\/strong>(s = 0; s &lt; 12; 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\u00a0\u00a0 <strong>LCD_goto<\/strong>((2 + s), 0);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>LCD_putchar<\/strong>(txt3[s]);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>delay_ms<\/strong>(60);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 }<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>for<\/strong>(s = 0; s &lt; 10; 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\u00a0\u00a0 <strong>LCD_goto<\/strong>((3 + s), 1);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>LCD_putchar<\/strong>(txt4[s]);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>delay_ms<\/strong>(60);<\/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 <strong>delay_ms<\/strong>(2600);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 s = 0;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_clear_home<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_goto<\/strong>(3, 0);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_putstr<\/strong>(txt1);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>while<\/strong>(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\u00a0\u00a0 <strong>show_value<\/strong>(s);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 s++;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>delay_ms<\/strong>(200);<\/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><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>GPIO_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: GPIO_graceInit_prologue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User initialization code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: GPIO_graceInit_prologue) *\/<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Output Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1OUT = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Port Select Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1SEL = BIT6 | BIT7;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Direction Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1DIR = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Interrupt Edge Select Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1IES = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Interrupt Flag Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1IFG = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Output Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2OUT = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Direction Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2DIR = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Interrupt Edge Select Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2IES = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Interrupt Flag Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2IFG = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: GPIO_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: GPIO_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>BCSplus_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: BCSplus_graceInit_prologue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User initialization code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: BCSplus_graceInit_prologue) *\/<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Basic Clock System Control 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 * SELM_0 -- DCOCLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * DIVM_0 -- Divide by 1<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~SELS -- DCOCLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * DIVS_0 -- Divide by 1<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~DCOR -- DCO uses internal resistor<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 BCSCTL2 = SELM_0 | DIVM_0 | DIVS_0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>if<\/strong> (CALBC1_1MHZ != 0xFF) {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/* Follow recommended flow. First, clear all DCOx and MODx bits. Then<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 * apply new RSELx values. Finally, apply new DCOx and MODx bit values.<\/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 DCOCTL = 0x00;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 BCSCTL1 = CALBC1_1MHZ;\u00a0\u00a0\u00a0\u00a0\u00a0 \/* Set DCO to 1MHz *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 DCOCTL = CALDCO_1MHZ;<\/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 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Basic Clock System Control 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 * XT2OFF -- Disable XT2CLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~XTS -- Low Frequency<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * DIVA_0 -- Divide by 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 * Note: ~XTS indicates that XTS has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 BCSCTL1 |= XT2OFF | DIVA_0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Basic Clock System Control 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 * XT2S_0 -- 0.4 - 1 MHz<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * LFXT1S_2 -- If XTS = 0, XT1 = VLOCLK ; If XTS = 1, XT1 = 3 - 16-MHz crystal or resonator<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * XCAP_1 -- ~6 pF<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 BCSCTL3 = XT2S_0 | LFXT1S_2 | XCAP_1;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: BCSplus_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: BCSplus_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>USI_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: USI_graceInit_prologue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User initialization code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: USI_graceInit_prologue) *\/<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Disable USI *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 USICTL0 |= USISWRST;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * USI Control Register 0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * USIPE7 -- USI function enabled<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * USIPE6 -- USI function enabled<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~USIPE5 -- USI function disabled<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~USILSB -- MSB first<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * USIMST -- Master mode<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~USIGE -- Output latch enable depends on shift clock<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~USIOE -- Output disabled<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * USISWRST -- USI logic held in reset state<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 USICTL0 = USIPE7 | USIPE6 | USIMST | USISWRST;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * USI Control Register 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 * ~USICKPH -- Data is changed on the first SCLK edge and captured on the following edge<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * USII2C -- I2C mode enabled<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~USISTTIE -- Interrupt on START condition disabled<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * USIIE -- Interrupt enabled<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~USIAL -- No arbitration lost condition<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~USISTP -- No STOP condition received<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~USISTTIFG -- No START condition received. No interrupt pending<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * USIIFG -- Interrupt pending<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 USICTL1 = USII2C | USIIE | USIIFG;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * USI Clock Control Register<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * USIDIV_4 -- Divide by 16<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * USISSEL_2 -- SMCLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * USICKPL -- Inactive state is high<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~USISWCLK -- Input clock is low<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~USISWCLK indicates that USISWCLK has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 USICKCTL = USIDIV_7 | USISSEL_2 | USICKPL;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * USI Bit Counter Register<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~USISCLREL -- SCL line is held low if USIIFG is set<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~USI16B -- 8-bit shift register mode. Low byte register USISRL is used<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * USIIFGCC -- USIIFG is not cleared automatically<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~USICNT4 -- USI bit count<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~USICNT3 -- USI bit count<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~USICNT2 -- USI bit count<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~USICNT1 -- USI bit count<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~USICNT0 -- USI bit count<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 USICNT = USIIFGCC;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Enable USI *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 USICTL0 &amp;= ~USISWRST;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Clear pending flag *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 USICTL1 &amp;= ~(USIIFG + USISTTIFG);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: USI_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: USI_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>System_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: System_graceInit_prologue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User initialization code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: System_graceInit_prologue) *\/<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Clear oscillator fault flag with software delay *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>do<\/strong><\/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 \/\/ Clear OSC fault flag<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 IFG1 &amp;= ~OFIFG;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ 50us delay<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>__delay_cycles<\/strong>(50);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 } <strong>while<\/strong> (IFG1 &amp; OFIFG);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * SR, Status Register<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~SCG1 -- Disable System clock generator 1<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~SCG0 -- Disable System clock generator 0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~OSCOFF -- Oscillator On<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~CPUOFF -- CPU On<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * GIE -- General interrupt enable<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>__bis_SR_register<\/strong>(GIE);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: System_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: System_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>WDTplus_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: RTC_B_graceInit_prologue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User initialization code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: RTC_B_graceInit_prologue) *\/<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * WDTCTL, Watchdog Timer+ Register<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 \u00a0\u00a0\u00a0* WDTPW -- Watchdog password<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * WDTHOLD -- Watchdog timer+ is stopped<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTNMIES -- NMI on rising edge<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTNMI -- Reset function<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTTMSEL -- Watchdog mode<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTCNTCL -- No action<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTSSEL -- SMCLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTIS0 -- Watchdog clock source bit0 disabled<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTIS1 -- Watchdog clock source bit1 disabled<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 WDTCTL = WDTPW | WDTHOLD;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: RTC_B_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 \u00a0\u00a0\/* User code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: RTC_B_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>show_value<\/strong>(<strong>unsigned<\/strong> <strong>char<\/strong> value)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 <strong>unsigned<\/strong> <strong>char<\/strong> ch = 0x00;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 ch = ((value \/ 100) + 0x30);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 <strong>LCD_goto<\/strong>(6, 1);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 <strong>LCD_putchar<\/strong>(ch);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 ch = (((value \/ 10) % 10) + 0x30);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 <strong>LCD_goto<\/strong>(7, 1);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 <strong>LCD_putchar<\/strong>(ch);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 ch = ((value % 10) + 0x30);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 <strong>LCD_goto<\/strong>(8, 1);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 <strong>LCD_putchar<\/strong>(ch);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<p>&nbsp;<\/p>\n<h2>Simulation<\/h2>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/SW-I2C-LCD-Sim1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13856\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/SW-I2C-LCD-Sim1-580x378.png\" alt=\"I2C LCD Sim\" width=\"580\" height=\"378\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/SW-I2C-LCD-Sim1-580x378.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/SW-I2C-LCD-Sim1.png 603w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p><em>*Note Proteus VSM could not simulate I2C-based simulations. The simulation here only shows connections.<\/em><\/p>\n<p>&nbsp;<\/p>\n<h2>Explanation<\/h2>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/SW_I2C_LCD2.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13871\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/SW_I2C_LCD2-580x331.jpg\" alt=\"SW_I2C_LCD2\" width=\"580\" height=\"331\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/SW_I2C_LCD2-580x331.jpg 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/SW_I2C_LCD2.jpg 590w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a> <a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/SW_I2C_LCD1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13870\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/SW_I2C_LCD1-580x435.jpg\" alt=\"SW_I2C_LCD1\" width=\"580\" height=\"435\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/SW_I2C_LCD1-580x435.jpg 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/SW_I2C_LCD1.jpg 590w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p>Demo video: <a href=\"https:\/\/www.youtube.com\/watch?v=y6HQATVucNk\">https:\/\/www.youtube.com\/watch?v=y6HQATVucNk<\/a>.<\/p>\n<p><!--more--><\/p>\n<h1>UART &#8211; USCI<\/h1>\n<p style=\"text-align: justify\">Serial communication is perhaps the most widely used communication method for interfacing a PC or other machines with a micro and over long distances. With just two cross-connecting wires, we can achieve a full-duplex point-to-point communication. Owing to its simplicity, range and wide usage, it is the communication interface backbone that is used for GSM modems, RF modules, Zigbee devices like CC2530, BLE devices like CC2540, Wi-Fi devices like CC3200, etc. Other forms of advance serial communications have their lineages tracing back to it, for example, RS-485, LIN, IrDA, etc.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/UART-Comms1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13859\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/UART-Comms1-580x209.png\" alt=\"UART Comms\" width=\"580\" height=\"209\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/UART-Comms1-580x209.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/UART-Comms1.png 859w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p style=\"text-align: justify\">Shown below is a demo of HMC1022-based LED compass. To communicate with this chip to get compass heading, we need to send-receive data via UART.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/HMC10221.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13847\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/HMC10221-580x615.jpg\" alt=\"HMC1022\" width=\"580\" height=\"615\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/HMC10221-580x615.jpg 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/HMC10221-966x1024.jpg 966w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/HMC10221.jpg 1292w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p>To learn more about UART visit the following link:<\/p>\n<p><a href=\"https:\/\/learn.mikroe.com\/uart-serial-communication\/\">https:\/\/learn.mikroe.com\/uart-serial-communication\/<\/a><\/p>\n<h2>Code Example<\/h2>\n<p>&nbsp;<\/p>\n<pre><span style=\"color: #0000ff\"><strong>#include<\/strong> &lt;msp430.h&gt;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#include<\/strong> \"delay.h\"<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#include<\/strong> \"lcd.h\"<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>unsigned<\/strong> <strong>char<\/strong> rx = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>GPIO_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>BCSplus_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>USCI_A0_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>System_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>WDTplus_graceInit<\/strong>(<strong>void<\/strong>);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>UART_putc<\/strong>(<strong>char<\/strong> ch);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>UART_puts<\/strong>(<strong>char<\/strong> *str);<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>#pragma<\/strong> vector=USCIAB0RX_VECTOR<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>__interrupt<\/strong> <strong>void<\/strong> <strong>USCI0RX_ISR_HOOK<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 rx = UCA0RXBUF;\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\"><strong>void<\/strong> <strong>main<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>unsigned<\/strong> <strong>char<\/strong> tx = 0x20;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Stop watchdog timer from timing out during initial start-up. *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 WDTCTL = WDTPW | WDTHOLD;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* initialize Config for the MSP430 GPIO *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>GPIO_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* initialize Config for the MSP430 2xx family clock systems (BCS) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>BCSplus_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* initialize Config for the MSP430 USCI_A0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>USCI_A0_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* initialize Config for the MSP430 System Registers *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>System_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* initialize Config for the MSP430 WDT+ *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>WDTplus_graceInit<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>UART_puts<\/strong>(\"\\f\");<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>UART_puts<\/strong>(\"MSP430G2553 UART Demo\\n\");<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>UART_puts<\/strong>(\"Shawon Shahryiar\\n\");<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>UART_puts<\/strong>(\"https:\/\/www.facebook.com\/MicroArena\\n\");<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_init<\/strong>();<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_goto<\/strong>(0, 0);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_putstr<\/strong>(\"TXD:\");<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_goto<\/strong>(0, 1);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>LCD_putstr<\/strong>(\"RXD:\");<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>while<\/strong>(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\u00a0\u00a0 <strong>LCD_goto<\/strong>(15, 0);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>LCD_putchar<\/strong>(tx);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>UART_putc<\/strong>(tx);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 tx++;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>if<\/strong>(tx &gt; 0x7F)<\/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\u00a0\u00a0 tx = 0x20;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\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 <strong>LCD_goto<\/strong>(15, 1);<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>LCD_putchar<\/strong>(rx);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>delay_ms<\/strong>(400);<\/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><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>GPIO_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: GPIO_graceInit_prologue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User initialization code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: GPIO_graceInit_prologue) *\/<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Port Select 2 Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1SEL2 = BIT1 | BIT2;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 \u00a0\/* Port 1 Output Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1OUT = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Port Select Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1SEL = BIT1 | BIT2;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Direction Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1DIR = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Interrupt Edge Select Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1IES = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 1 Interrupt Flag Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P1IFG = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Output Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2OUT = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Direction Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2DIR = BIT0 | BIT1 | BIT2;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Interrupt Edge Select Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2IES = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 2 Interrupt Flag Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P2IFG = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 3 Output Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P3OUT = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Port 3 Direction Register *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 P3DIR = 0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: GPIO_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: GPIO_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>BCSplus_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: BCSplus_graceInit_prologue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User initialization code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: BCSplus_graceInit_prologue) *\/<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Basic Clock System Control 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 * SELM_0 -- DCOCLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * DIVM_0 -- Divide by 1<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~SELS -- DCOCLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * DIVS_0 -- Divide by 1<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~DCOR -- DCO uses internal resistor<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 BCSCTL2 = SELM_0 | DIVM_0 | DIVS_0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>if<\/strong> (CALBC1_1MHZ != 0xFF) {<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/* Follow recommended flow. First, clear all DCOx and MODx bits. Then<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 * apply new RSELx values. Finally, apply new DCOx and MODx bit values.<\/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 DCOCTL = 0x00;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 BCSCTL1 = CALBC1_1MHZ;\u00a0\u00a0\u00a0\u00a0\u00a0 \/* Set DCO to 1MHz *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 DCOCTL = CALDCO_1MHZ;<\/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 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Basic Clock System Control 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 * XT2OFF -- Disable XT2CLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~XTS -- Low Frequency<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * DIVA_0 -- Divide by 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 * Note: ~XTS indicates that XTS has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \u00a0*\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 BCSCTL1 |= XT2OFF | DIVA_0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Basic Clock System Control 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 * XT2S_0 -- 0.4 - 1 MHz<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * LFXT1S_0 -- If XTS = 0, XT1 = 32768kHz Crystal ; If XTS = 1, XT1 = 0.4 - 1-MHz crystal or resonator<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * XCAP_1 -- ~6 pF<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0 \u00a0\u00a0BCSCTL3 = XT2S_0 | LFXT1S_0 | XCAP_1;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: BCSplus_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: BCSplus_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>USCI_A0_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: USCI_A0_graceInit_prologue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User initialization code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: USCI_A0_graceInit_prologue) *\/<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Disable USCI *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 UCA0CTL1 |= UCSWRST;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Control Register 0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~UCPEN -- Parity Disabled<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * UCPAR -- Even parity<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~UCMSB -- LSB first<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~UC7BIT -- 8-bit<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~UCSPB -- One stop bit<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * UCMODE_0 -- UART Mode<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~UCSYNC -- Asynchronous mode<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 UCA0CTL0 = UCPAR | UCMODE_0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Control Register 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 * UCSSEL_2 -- SMCLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~UCRXEIE -- Erroneous characters rejected and UCAxRXIFG is not set<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~UCBRKIE -- Received break characters do not set UCAxRXIFG<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~UCDORM -- Not dormant. All received characters will set UCAxRXIFG<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~UCTXADDR -- Next frame transmitted is data<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~UCTXBRK -- Next frame transmitted is not a break<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * UCSWRST -- Enabled. USCI logic held in reset state<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 UCA0CTL1 = UCSSEL_2 | UCSWRST;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Modulation Control Register<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * UCBRF_0 -- First stage 0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * UCBRS_1 -- Second stage 1<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~UCOS16 -- Disabled<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~UCOS16 indicates that UCOS16 has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 UCA0MCTL = UCBRF_0 | UCBRS_1;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Baud rate control register 0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 UCA0BR0 = 104;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Enable USCI *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 UCA0CTL1 &amp;= ~UCSWRST;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: USCI_A0_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: USCI_A0_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>System_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: System_graceInit_prologue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User initialization code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: System_graceInit_prologue) *\/<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * IFG2, Interrupt Flag Register 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 * ~UCB0TXIFG -- No interrupt pending<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~UCB0RXIFG -- No interrupt pending<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~UCA0TXIFG -- No interrupt pending<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * UCA0RXIFG -- Interrupt pending<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 IFG2 &amp;= ~(UCA0RXIFG);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * IE2, Interrupt Enable Register 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 * ~UCB0TXIE -- Interrupt disabled<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~UCB0RXIE -- Interrupt disabled<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~UCA0TXIE -- Interrupt disabled<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * UCA0RXIE -- Interrupt enabled<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 \u00a0\u00a0*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 IE2 |= UCA0RXIE;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * SR, Status Register<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~SCG1 -- Disable System clock generator 1<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~SCG0 -- Disable System clock generator 0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~OSCOFF -- Oscillator On<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~CPUOFF -- CPU On<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * GIE -- General interrupt enable<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>__bis_SR_register<\/strong>(GIE);<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: System_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: System_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>WDTplus_graceInit<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: RTC_B_graceInit_prologue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User initialization code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: RTC_B_graceInit_prologue) *\/<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * WDTCTL, Watchdog Timer+ Register<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * WDTPW -- Watchdog password<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * WDTHOLD -- Watchdog timer+ is stopped<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTNMIES -- NMI on rising edge<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTNMI -- Reset function<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTTMSEL -- Watchdog mode<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTCNTCL -- No action<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTSSEL -- SMCLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTIS0 -- Watchdog clock source bit0 disabled<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~WDTIS1 -- Watchdog clock source bit1 disabled<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 WDTCTL = WDTPW | WDTHOLD;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE START (section: RTC_B_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* User code *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* USER CODE END (section: RTC_B_graceInit_epilogue) *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>UART_putc<\/strong>(<strong>char<\/strong> ch)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>while<\/strong>(!(IFG2 &amp; UCA0TXIFG));<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 UCA0TXBUF = ch;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>UART_puts<\/strong>(<strong>char<\/strong> *str)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>while<\/strong>(*str != 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\u00a0\u00a0 <strong>while<\/strong>(!(IFG2 &amp; UCA0TXIFG));<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 UCA0TXBUF = *str++;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 };<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<p>&nbsp;<\/p>\n<h2>Simulation<a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/UART_Sim1.png\"><br \/>\n<\/a><\/h2>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/UART_Sim1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13860\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/UART_Sim1-580x208.png\" alt=\"UART_Sim\" width=\"580\" height=\"208\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/UART_Sim1-580x208.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/UART_Sim1-1024x367.png 1024w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/UART_Sim1.png 1187w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<h2>Explanation<\/h2>\n<p style=\"text-align: justify\">MSP430G2553 is used for this demo since it has UART-supporting USCI module. An important thing to note before trying to use the UART is the jumper settings shown below:<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/jumpers1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13850\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/jumpers1-580x435.png\" alt=\"jumpers\" width=\"580\" height=\"435\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/jumpers1-580x435.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/jumpers1.png 587w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p style=\"text-align: justify\">The photo shows that the TX-RX jumpers are connected in way perpendicular to other jumpers. However, by default, jumpers are connected the other way around, i.e. software UART.\u00a0 If the jumpers are setup as shown, we can access the on-board USB-UART converter, i.e. hardware UART. We will need this piece of hardware for communicating with a computer.<\/p>\n<p style=\"text-align: justify\">Using GRACE, we setup the USCI module in asynchronous UART mode with typical settings like 9600 baudrate and 8-bit data transfer mode. GRACE also takes care of baud rate generation calculation. UART data reception interrupt is also used to quickly respond to incoming characters.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/USCI-UART1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-13861\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/USCI-UART1.png\" alt=\"USCI-UART\" width=\"568\" height=\"477\" \/><\/a><\/p>\n<pre>\u00a0\u00a0\u00a0<span style=\"color: #0000ff\"> \/* Disable USCI *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 UCA0CTL1 |= UCSWRST;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Control Register 0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~UCPEN -- Parity Disabled<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * UCPAR -- Even parity<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~UCMSB -- LSB first<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~UC7BIT -- 8-bit<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~UCSPB -- One stop bit<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * UCMODE_0 -- UART Mode<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~UCSYNC -- Asynchronous mode<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 UCA0CTL0 = UCPAR | UCMODE_0;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Control Register 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 * UCSSEL_2 -- SMCLK<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~UCRXEIE -- Erroneous characters rejected and UCAxRXIFG is not set<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~UCBRKIE -- Received break characters do not set UCAxRXIFG<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~UCDORM -- Not dormant. All received characters will set UCAxRXIFG<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~UCTXADDR -- Next frame transmitted is data<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~UCTXBRK -- Next frame transmitted is not a break<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * UCSWRST -- Enabled. USCI logic held in reset state<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0 \u00a0\u00a0* Note: ~&lt;BIT&gt; indicates that &lt;BIT&gt; has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 UCA0CTL1 = UCSSEL_2 | UCSWRST;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/*<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Modulation Control Register<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * UCBRF_0 -- First stage 0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * UCBRS_1 -- Second stage 1<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * ~UCOS16 -- Disabled<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 * Note: ~UCOS16 indicates that UCOS16 has value zero<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 UCA0MCTL = UCBRF_0 | UCBRS_1;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Baud rate control register 0 *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 UCA0BR0 = 104;<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 \/* Enable USCI *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 UCA0CTL1 &amp;= ~UCSWRST;<\/span><\/pre>\n<p>Right after initialization of all required hardware, the UART starts sending some strings.<\/p>\n<pre>\u00a0<span style=\"color: #0000ff\">\u00a0\u00a0 <strong>UART_puts<\/strong>(\"\\f\");<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>UART_puts<\/strong>(\"MSP430G2553 UART Demo\\n\");<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>UART_puts<\/strong>(\"Shawon Shahryiar\\n\");<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>UART_puts<\/strong>(\"https:\/\/www.facebook.com\/MicroArena\\n\");<\/span><\/pre>\n<p style=\"text-align: justify\">The following functions transmit data via UART. The first one can transmit one character at a time while the second can transmit a string of characters. In both cases, it is checked if the last character has been successfully sent before sending a new character.<\/p>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>UART_putc<\/strong>(<strong>char<\/strong> ch)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>while<\/strong>(!(IFG2 &amp; UCA0TXIFG));<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 UCA0TXBUF = ch;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<pre><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>void<\/strong> <strong>UART_puts<\/strong>(<strong>char<\/strong> *str)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 <strong>while<\/strong>(*str != 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\u00a0\u00a0 <strong>while<\/strong>(!(IFG2 &amp; UCA0TXIFG));<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 UCA0TXBUF = *str++;<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 };<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<p>Since data recption interrupt is used, data received is extracted from UART reception ISR.<\/p>\n<pre><span style=\"color: #0000ff\"><strong>#pragma<\/strong> vector=USCIAB0RX_VECTOR<\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><strong>__interrupt<\/strong> <strong>void<\/strong> <strong>USCI0RX_ISR_HOOK<\/strong>(<strong>void<\/strong>)<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">{<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">\u00a0\u00a0\u00a0 rx = UCA0RXBUF; \u00a0 \u00a0 \u00a0<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<p style=\"text-align: justify\">Whenever a new character is received by the UART, RX interrupt fires up and catches the sent character. The received character is shown on a LCD. In the main loop, the code sends out ASCII characters and the LCD shows what has been sent out.<\/p>\n<p style=\"text-align: justify\">CCS IDE comes with a built-in serial terminal interface. You can that or you can use third-party software like <strong><em>CoolTerm<\/em><\/strong>,<strong><em> Putty<\/em><\/strong>, <strong><em>Termite<\/em><\/strong>,<strong><em> Docklight<\/em><\/strong>,<strong><em> RealTerm<\/em><\/strong>, <strong><em>Tera Term<\/em><\/strong>, etc to check or monitor serial communication transactions.<\/p>\n<h2>Demo<\/h2>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/UART.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-13874\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/UART-580x278.jpg\" alt=\"UART\" width=\"580\" height=\"278\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/UART-580x278.jpg 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/UART.jpg 590w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p>Demo video: <a href=\"https:\/\/www.youtube.com\/watch?v=Mm2i4rVGyoo\">https:\/\/www.youtube.com\/watch?v=Mm2i4rVGyoo<\/a>.<\/p>\n<p><!--more--><\/p>\n<h1>Ending<\/h1>\n<p style=\"text-align: justify\">A microcontroller like the ones in MSP430G2xxx series may look tiny and less resourceful but it is impossible to show everything in full details. This write up is just a summary. Even at this stage after explaining and demoing multiple basic hardware of MSP430s, I have yet to cover more stuffs.<\/p>\n<p style=\"text-align: justify\">As an advice, it is imperative that readers study app notes, reference manuals and TI wiki pages to improve proficiency and in depth knowledge. There are some good books on MSP430s too. Books like <strong><em>MSP430 Microcontroller Basics<\/em><\/strong> by <strong><em>John Davies <\/em><\/strong>and <strong><em>Analog and Digital Circuits for Electronic Control System Applications Using the TI MSP430 Microcontroller <\/em><\/strong>by<strong><em> Jerry Luecke<\/em><\/strong> discuss MSP430 microcontrollers and their applications in really great details. For now, I ask readers to practice and experiment what I have shown so far. Try stuffs without using GRACE assist, try out different combinations and try out different MSP430 micros.<\/p>\n<p style=\"text-align: justify\">PDF version of this blog post can be downloaded from <a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2017\/08\/Introducing-TI-MSP430-Microcontrollers.pdf\">here<\/a>.<\/p>\n<p style=\"text-align: justify\">Code examples and Grace output files\u00a0can be found <a href=\"https:\/\/drive.google.com\/open?id=0Bx-UVKbCh-ssUmMyQWxDbFh6MmM\">here<\/a>.<\/p>\n<p>Happy coding.<\/p>\n<p>&nbsp;<\/p>\n<p><em>Author: Shawon M. Shahryiar<\/em><\/p>\n<p><a href=\"https:\/\/www.facebook.com\/MicroArena?ref=hl\"><em>https:\/\/www.facebook.com\/MicroArena<\/em><\/a><em>\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 25.08.2017<\/em><\/p>\n<p>&nbsp;<\/p>\n<p><em>\u00a0<\/em><\/p>\n<p><em>** Some images have been taken from the documents and webpages of Texas Instruments (TI) and Mikroelektronika. <\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Texas Instruments (TI) is a well-known US-based semiconductor manufacturer. TI is perhaps best known to many as the manufacturer of some of the fanciest scientific calculators in the market. Of the long list of electronic devices produced by TI, microcontrollers are on the top. TI manufactures some of the coolest and advanced microcontrollers of the market today. There are several categories of micros from TI. These include general purpose low power MCUs which mainly comprise of MSP430s, ARMs like TM4Cs, MSP432s, etc, micros for wireless communications like CC2xxx series, ARM + DSP micros, DSP-specialized micros like the TMS32xxx series and<\/p>\n","protected":false},"author":5,"featured_media":13632,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[379,933,955,1466],"tags":[1708,473,384,773,1707,1706,1705,1415,346,910],"class_list":["post-13631","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-msp430-launchpad","category-texas-instruments","category-ti-2","category-tutorials","tag-16-bit-microcontroller","tag-launchpad","tag-launchpad-kit","tag-msp430","tag-msp430g2-launchpad","tag-msp430g2452","tag-msp430g2553","tag-texas-instruments","tag-ti","tag-ti-launchpad"],"_links":{"self":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts\/13631","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=13631"}],"version-history":[{"count":69,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts\/13631\/revisions"}],"predecessor-version":[{"id":14136,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts\/13631\/revisions\/14136"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/media\/13632"}],"wp:attachment":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/media?parent=13631"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/categories?post=13631"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/tags?post=13631"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}