{"id":5517,"date":"2012-07-03T14:10:11","date_gmt":"2012-07-03T18:10:11","guid":{"rendered":"http:\/\/embedded-lab.com\/blog\/?p=5517"},"modified":"2012-11-06T13:40:21","modified_gmt":"2012-11-06T18:40:21","slug":"temperature-and-relative-humidity-display-with-adaptive-brightness-control","status":"publish","type":"post","link":"https:\/\/embedded-lab.com\/blog\/temperature-and-relative-humidity-display-with-adaptive-brightness-control\/","title":{"rendered":"Temperature and relative humidity display with adaptive brightness control"},"content":{"rendered":"<p>The objective of this project is to illustrate a technique of implementing adaptive brightness control to seven segment LED displays. It consists of a closed loop system that continuously assesses ambient light condition using an inexpensive light-dependent resistor (LDR) and uses that information to adjust the brightness of the display. For the proof of concept, the technique is applied to construct a digital temperature and relative humidity meter that adapts the brightness of the seven segment LED displays to the surrounding lighting conditions. There are 8 seven segment LED displays used in this project and they are all driven by one MAX7219 chip. The ambient temperature and relative humidity are measured using the DHT11 sensor. The microcontroller used in this project is PIC12F683, which is a low-pin-count (8) device from 8-bit PIC microcontroller family. Auto-adjusting the brightness of the seven-segment LED display with surrounding illumination enhances the readability in all ambient lighting conditions.<\/p>\n<div id=\"attachment_5536\" style=\"width: 575px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/07\/PIC12FTRTitle.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-5536\" class=\"size-full wp-image-5536\" title=\"PIC12FTRTitle\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/07\/PIC12FTRTitle.jpg\" alt=\"\" width=\"565\" height=\"458\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/07\/PIC12FTRTitle.jpg 565w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/07\/PIC12FTRTitle-550x445.jpg 550w\" sizes=\"auto, (max-width: 565px) 100vw, 565px\" \/><\/a><p id=\"caption-attachment-5536\" class=\"wp-caption-text\">Temperature and humidity display with auto-brightness control<\/p><\/div>\n<p><strong><!--more-->Theory and circuit setup<br \/>\n<\/strong><\/p>\n<p>Many smartphones, HDTVs, PDAs, tablets, and computer screens now come with an automatic brightness control that aims to conserve power as well as to make the display easier to see in a broad range of illumination conditions. This feature basically dims the display screen in a dark environment and brightens it when the ambient light level is higher. The brighter screen helps to counteract the effects of glare from ambient light and also takes into account for the decreased sensitivity of human eyes to brightness as the ambient light level increases.<\/p>\n<p>An automatic brightness adjustment is basically a closed loop system that has the capability to assess ambient light and adjust the brightness of the display accordingly. In our project, a general purpose LDR and a fixed value resistor (10K) are connected in series\u00a0between the power supply and ground pins to create a voltage dividing network, as shown in the circuit diagram below. The resistance of a typical LDR is less than 1 K? under bright lighting condition. Its resistance could go up to several hundred\u00a0K? under extremely dark condition. Therefore, the voltage across the 10K resistor increases proportionally with the surrounding illumination. For the given setup, the voltage across the 10K resistor can vary from 0.1V (under dark condition) to over 4.0V (under very bright illumination). The PIC12F683 microcontroller reads this analog voltage through its AN3 (GP4) ADC channel and then sends out appropriate signals to the MAX7219 display driver to adjust the brightness of the seven segment LED displays.<\/p>\n<div id=\"attachment_5528\" style=\"width: 583px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/07\/PIC12F_TRMETERCKT.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-5528\" class=\"size-full wp-image-5528\" title=\"PIC12F_TRMETERCKT\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/07\/PIC12F_TRMETERCKT.png\" alt=\"\" width=\"573\" height=\"491\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/07\/PIC12F_TRMETERCKT.png 573w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/07\/PIC12F_TRMETERCKT-550x471.png 550w\" sizes=\"auto, (max-width: 573px) 100vw, 573px\" \/><\/a><p id=\"caption-attachment-5528\" class=\"wp-caption-text\">Circuit diagram<\/p><\/div>\n<p>The MAX7219 chip provides a serial interface to drive 7-segment LED displays (common-cathode type) up to 8 digits and requires only 3 I\/O pins from microcontroller. Included on the chip are a BCD decoder, multiplex scan circuitry, segment and digit\u00a0drivers, and an 8\u00d78 static RAM to store the digit values. The segment current for all LEDs is set through only one external resistor connected between the ISET pin and power supply. However, the device also provides a digital control of the display brightness (16 steps from minimum to maximum) through an internal pulse-width modulator. To learn more about the MAX7219 device, read my previous article\u00a0<a href=\"http:\/\/embedded-lab.com\/blog\/?p=4935\" target=\"_blank\">Serial 4-digit seven-segment LED display<\/a>. In this project, the GP0, GP1, and GP2 I\/O pins of PIC12F683 are used to drive LOAD, DIN, and CLK signal lines of the MAX7219.<\/p>\n<p>In order to measure the temperature and relative humidity, the DHT11 sensor is used. It can measure temperature from 0-50 \u00b0C with an accuracy of \u00b12\u00b0C and relative humidity ranging from 20-95% with an accuracy of \u00a0\u00b15%. The sensor provides fully calibrated digital outputs for the two measurements. It has got its own proprietary 1-wire protocol, and therefore, the communication between the sensor and a microcontroller is not possible through a direct interface with any of its peripherals. The protocol must be implemented in the firmware of the MCU with precise timing calculations required by the sensor. I have described more about this sensor and its communication protocol in <a title=\"Permanent Link to Measurement of temperature and relative humidity using DHT11 sensor and PIC microcontroller\" href=\"http:\/\/embedded-lab.com\/blog\/?p=4333\" rel=\"bookmark\"> Measurement of temperature and relative humidity using DHT11 sensor and PIC microcontroller.<\/a> The PIC12F683 uses the GP5 I\/O pin to communicate with the DHT11.<\/p>\n<p>To make prototyping easier, I am using my <a href=\"http:\/\/embedded-lab.com\/blog\/?p=5469\" target=\"_blank\">PIC12F Project board<\/a> and my <a href=\"http:\/\/embedded-lab.com\/blog\/?p=5310\" target=\"_blank\">Serial 8-digit seven segment LED display<\/a> in this project.<\/p>\n<div id=\"attachment_5537\" style=\"width: 575px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/07\/PIC12FTR_Setup.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-5537\" class=\"size-full wp-image-5537\" title=\"PIC12FTR_Setup\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/07\/PIC12FTR_Setup.jpg\" alt=\"\" width=\"565\" height=\"408\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/07\/PIC12FTR_Setup.jpg 565w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/07\/PIC12FTR_Setup-550x397.jpg 550w\" sizes=\"auto, (max-width: 565px) 100vw, 565px\" \/><\/a><p id=\"caption-attachment-5537\" class=\"wp-caption-text\">Ambient light sensor on board<\/p><\/div>\n<div id=\"attachment_5538\" style=\"width: 575px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/07\/PIC12FTR_completeSetup.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-5538\" class=\"size-full wp-image-5538\" title=\"PIC12FTR_completeSetup\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/07\/PIC12FTR_completeSetup.jpg\" alt=\"\" width=\"565\" height=\"442\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/07\/PIC12FTR_completeSetup.jpg 565w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/07\/PIC12FTR_completeSetup-550x430.jpg 550w\" sizes=\"auto, (max-width: 565px) 100vw, 565px\" \/><\/a><p id=\"caption-attachment-5538\" class=\"wp-caption-text\">Complete project setup<\/p><\/div>\n<p><strong>Software<\/strong><\/p>\n<p>The firmware for this project is written in C and compiled with mikroC Pro for PIC compiler version 5.30. The subroutines for initializing the MAX7219 and sending display-digit bytes are written in a simple and understandable way so that they can be easily implemented with any other programming language. If you are not familiar with MAX7219&#8217;s internal control and display registers, I would recommend to read one of my earlier posts, <a href=\"http:\/\/embedded-lab.com\/blog\/?p=4935\" target=\"_blank\">Serial 4-digit seven-segment LED display<\/a>. The MAX7219 allows display brightness to be controlled through software by an internal pulse-width modulator (PWM). The PWM output is controlled by the lower nibble (D3-D0) of the intensity register (address 0x0A) and allows 16 brightness levels. The zero nibble value sets the display intensity to minimum, whereas all nibble bits set to 1 selects the maximum intensity level for the display. For auto-brightness control, the ADC output of the LDR sensor is first scaled down to 0-10 (11 brightness levels) by simply dividing the 10-bit ADC count by 100. Then a look-up table is used to map the scaled brightness levels to appropriate nibble values for the intensity control register of the MAX7219. The temperature is displayed in Fahrenheit (F) scale and the relative humidity in percentage (P).<\/p>\n\n<div class=\"wp_syntax\"><table><tr><td class=\"code\"><pre class=\"c\" style=\"font-family:monospace;\"><span style=\"color: #666666; font-style: italic;\">\/\/ Define Software-SPI connections for MAX7219<\/span>\n<span style=\"color: #339933;\">#define CS_Pin       GP0_bit<\/span>\n<span style=\"color: #339933;\">#define MOSI_Pin     GP1_bit<\/span>\n<span style=\"color: #339933;\">#define CLK_Pin      GP2_bit<\/span>\n&nbsp;\n<span style=\"color: #666666; font-style: italic;\">\/\/ Define Data pin for DHT11<\/span>\n<span style=\"color: #339933;\">#define Data         GP5_bit<\/span>\n<span style=\"color: #339933;\">#define DataDir      TRISIO5_bit<\/span>\n&nbsp;\n<span style=\"color: #993333;\">unsigned<\/span> <span style=\"color: #993333;\">short<\/span> TOUT <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000dd;\">0<\/span><span style=\"color: #339933;\">,<\/span> CheckSum<span style=\"color: #339933;\">,<\/span> i<span style=\"color: #339933;\">;<\/span>\n<span style=\"color: #993333;\">unsigned<\/span> <span style=\"color: #993333;\">short<\/span> T_Byte1<span style=\"color: #339933;\">,<\/span> T_Byte2<span style=\"color: #339933;\">,<\/span> RH_Byte1<span style=\"color: #339933;\">,<\/span> RH_Byte2<span style=\"color: #339933;\">;<\/span>\n<span style=\"color: #993333;\">unsigned<\/span> <span style=\"color: #993333;\">short<\/span> check<span style=\"color: #339933;\">,<\/span> brightness<span style=\"color: #339933;\">=<\/span><span style=\"color: #208080;\">0x0b<\/span><span style=\"color: #339933;\">;<\/span>\n<span style=\"color: #993333;\">unsigned<\/span> <span style=\"color: #993333;\">int<\/span> counter <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000dd;\">0<\/span><span style=\"color: #339933;\">,<\/span> TempF<span style=\"color: #339933;\">,<\/span> RH<span style=\"color: #339933;\">,<\/span> ADC_OP<span style=\"color: #339933;\">;<\/span>\n<span style=\"color: #993333;\">unsigned<\/span> <span style=\"color: #993333;\">short<\/span> Brightness_Table<span style=\"color: #009900;\">&#91;<\/span><span style=\"color: #0000dd;\">11<\/span><span style=\"color: #009900;\">&#93;<\/span> <span style=\"color: #339933;\">=<\/span> <span style=\"color: #009900;\">&#123;<\/span><span style=\"color: #0000dd;\">0<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #0000dd;\">1<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #0000dd;\">2<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #0000dd;\">4<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #0000dd;\">5<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #0000dd;\">7<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #0000dd;\">9<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #0000dd;\">11<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #0000dd;\">12<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #0000dd;\">14<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #0000dd;\">15<\/span><span style=\"color: #009900;\">&#125;<\/span><span style=\"color: #339933;\">;<\/span>\n&nbsp;\n<span style=\"color: #993333;\">void<\/span> SPI_Write_Byte<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #993333;\">unsigned<\/span> <span style=\"color: #993333;\">short<\/span> num<span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #009900;\">&#123;<\/span>\n <span style=\"color: #993333;\">unsigned<\/span> <span style=\"color: #993333;\">short<\/span> t<span style=\"color: #339933;\">,<\/span> Mask<span style=\"color: #339933;\">,<\/span> Flag<span style=\"color: #339933;\">;<\/span>\n CLK_Pin <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000dd;\">0<\/span><span style=\"color: #339933;\">;<\/span>\n Mask <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000dd;\">128<\/span><span style=\"color: #339933;\">;<\/span>\n <span style=\"color: #b1b100;\">for<\/span> <span style=\"color: #009900;\">&#40;<\/span>t<span style=\"color: #339933;\">=<\/span><span style=\"color: #0000dd;\">0<\/span><span style=\"color: #339933;\">;<\/span> t<span style=\"color: #339933;\">&amp;<\/span>lt<span style=\"color: #339933;\">;<\/span><span style=\"color: #0000dd;\">8<\/span><span style=\"color: #339933;\">;<\/span> t<span style=\"color: #339933;\">++<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #009900;\">&#123;<\/span>\n  Flag <span style=\"color: #339933;\">=<\/span> num <span style=\"color: #339933;\">&amp;<\/span>amp<span style=\"color: #339933;\">;<\/span> Mask<span style=\"color: #339933;\">;<\/span>\n  <span style=\"color: #b1b100;\">if<\/span><span style=\"color: #009900;\">&#40;<\/span>Flag <span style=\"color: #339933;\">==<\/span> <span style=\"color: #0000dd;\">0<\/span><span style=\"color: #009900;\">&#41;<\/span> MOSI_Pin <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000dd;\">0<\/span><span style=\"color: #339933;\">;<\/span>\n  <span style=\"color: #b1b100;\">else<\/span> MOSI_Pin <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000dd;\">1<\/span><span style=\"color: #339933;\">;<\/span>\n  CLK_Pin <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000dd;\">1<\/span><span style=\"color: #339933;\">;<\/span>\n  CLK_Pin <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000dd;\">0<\/span><span style=\"color: #339933;\">;<\/span>\n  Mask <span style=\"color: #339933;\">=<\/span> Mask <span style=\"color: #339933;\">&amp;<\/span>gt<span style=\"color: #339933;\">;&amp;<\/span>gt<span style=\"color: #339933;\">;<\/span> <span style=\"color: #0000dd;\">1<\/span><span style=\"color: #339933;\">;<\/span>\n <span style=\"color: #009900;\">&#125;<\/span>\n<span style=\"color: #009900;\">&#125;<\/span>\n&nbsp;\n<span style=\"color: #993333;\">void<\/span> MAX7219_INIT<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #009900;\">&#41;<\/span> <span style=\"color: #009900;\">&#123;<\/span>\n  <span style=\"color: #666666; font-style: italic;\">\/\/ Disable Shutdown mode<\/span>\n  CS_Pin <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000dd;\">0<\/span><span style=\"color: #339933;\">;<\/span>              <span style=\"color: #666666; font-style: italic;\">\/\/ CS pin is pulled LOW<\/span>\n  SPI_Write_Byte<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #208080;\">0x0C<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>    <span style=\"color: #666666; font-style: italic;\">\/\/ Select Shutdown register<\/span>\n  SPI_Write_Byte<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #208080;\">0x01<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>    <span style=\"color: #666666; font-style: italic;\">\/\/ Set D0 bit to return to normal operation<\/span>\n  CS_Pin <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000dd;\">1<\/span><span style=\"color: #339933;\">;<\/span>              <span style=\"color: #666666; font-style: italic;\">\/\/ CS pin is pulled HIGH<\/span>\n&nbsp;\n  <span style=\"color: #666666; font-style: italic;\">\/\/ Set BCD decode mode for digits DIG1-DIG3, and DIG5-DIG7<\/span>\n  CS_Pin <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000dd;\">0<\/span><span style=\"color: #339933;\">;<\/span>              <span style=\"color: #666666; font-style: italic;\">\/\/ CS pin is pulled LOW<\/span>\n  SPI_Write_Byte<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #208080;\">0x09<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>    <span style=\"color: #666666; font-style: italic;\">\/\/ Select Decode Mode register<\/span>\n  SPI_Write_Byte<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #208080;\">0b11101110<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>    <span style=\"color: #666666; font-style: italic;\">\/\/ Disable BCD mode for digits DIG0-DIG4<\/span>\n  CS_Pin <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000dd;\">1<\/span><span style=\"color: #339933;\">;<\/span>              <span style=\"color: #666666; font-style: italic;\">\/\/ CS pin is pulled HIGH<\/span>\n&nbsp;\n  <span style=\"color: #666666; font-style: italic;\">\/\/ Set display brighness<\/span>\n  CS_Pin <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000dd;\">0<\/span><span style=\"color: #339933;\">;<\/span>              <span style=\"color: #666666; font-style: italic;\">\/\/ CS pin is pulled LOW<\/span>\n  SPI_Write_Byte<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #208080;\">0x0A<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>    <span style=\"color: #666666; font-style: italic;\">\/\/ Select Intensity register<\/span>\n  SPI_Write_Byte<span style=\"color: #009900;\">&#40;<\/span>brightness<span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>    <span style=\"color: #666666; font-style: italic;\">\/\/ Set brightness<\/span>\n  CS_Pin <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000dd;\">1<\/span><span style=\"color: #339933;\">;<\/span>              <span style=\"color: #666666; font-style: italic;\">\/\/ CS pin is pulled HIGH<\/span>\n&nbsp;\n   <span style=\"color: #666666; font-style: italic;\">\/\/ Set display refresh<\/span>\n  CS_Pin <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000dd;\">0<\/span><span style=\"color: #339933;\">;<\/span>              <span style=\"color: #666666; font-style: italic;\">\/\/ CS pin is pulled LOW<\/span>\n  SPI_Write_Byte<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #208080;\">0x0B<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>    <span style=\"color: #666666; font-style: italic;\">\/\/ Select Scan-Limit register<\/span>\n  SPI_Write_Byte<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #208080;\">0x07<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>    <span style=\"color: #666666; font-style: italic;\">\/\/ Select digits DIG0-DIG3<\/span>\n  CS_Pin <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000dd;\">1<\/span><span style=\"color: #339933;\">;<\/span>              <span style=\"color: #666666; font-style: italic;\">\/\/ CS pin is pulled HIGH<\/span>\n&nbsp;\n <span style=\"color: #666666; font-style: italic;\">\/\/ Enable Display-Test<\/span>\n  CS_Pin <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000dd;\">0<\/span><span style=\"color: #339933;\">;<\/span>              <span style=\"color: #666666; font-style: italic;\">\/\/ CS pin is pulled LOW<\/span>\n  SPI_Write_Byte<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #208080;\">0x0F<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>    <span style=\"color: #666666; font-style: italic;\">\/\/ Select Display-Test register<\/span>\n  SPI_Write_Byte<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #208080;\">0x01<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>    <span style=\"color: #666666; font-style: italic;\">\/\/ Enable Display-Test<\/span>\n  CS_Pin <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000dd;\">1<\/span><span style=\"color: #339933;\">;<\/span>              <span style=\"color: #666666; font-style: italic;\">\/\/ CS pin is pulled HIGH<\/span>\n&nbsp;\n  Delay_ms<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #0000dd;\">1000<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\n <span style=\"color: #666666; font-style: italic;\">\/\/ Disable Display-Test<\/span>\n  CS_Pin <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000dd;\">0<\/span><span style=\"color: #339933;\">;<\/span>              <span style=\"color: #666666; font-style: italic;\">\/\/ CS pin is pulled LOW<\/span>\n  SPI_Write_Byte<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #208080;\">0x0F<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>    <span style=\"color: #666666; font-style: italic;\">\/\/ Select Display-Test register<\/span>\n  SPI_Write_Byte<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #208080;\">0x00<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>    <span style=\"color: #666666; font-style: italic;\">\/\/ Disable Display-Test<\/span>\n  CS_Pin <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000dd;\">1<\/span><span style=\"color: #339933;\">;<\/span>              <span style=\"color: #666666; font-style: italic;\">\/\/ CS pin is pulled HIGH<\/span>\n&nbsp;\n<span style=\"color: #009900;\">&#125;<\/span>\n&nbsp;\n <span style=\"color: #993333;\">void<\/span> Display_Value<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #993333;\">unsigned<\/span> <span style=\"color: #993333;\">int<\/span> j<span style=\"color: #339933;\">,<\/span> <span style=\"color: #993333;\">unsigned<\/span> <span style=\"color: #993333;\">short<\/span> k<span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #009900;\">&#123;<\/span>\n  CS_Pin <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000dd;\">0<\/span><span style=\"color: #339933;\">;<\/span>                    <span style=\"color: #666666; font-style: italic;\">\/\/ CS pin is pulled LOW<\/span>\n  SPI_Write_Byte<span style=\"color: #009900;\">&#40;<\/span>k<span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>             <span style=\"color: #666666; font-style: italic;\">\/\/ Send thousands digit<\/span>\n  SPI_Write_Byte<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #009900;\">&#40;<\/span>j<span style=\"color: #339933;\">\/<\/span><span style=\"color: #0000dd;\">100<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">%<\/span><span style=\"color:#800080;\">10<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\n  CS_Pin <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000dd;\">1<\/span><span style=\"color: #339933;\">;<\/span>                    <span style=\"color: #666666; font-style: italic;\">\/\/ CS pin is pulled HIGH<\/span>\n&nbsp;\n  CS_Pin <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000dd;\">0<\/span><span style=\"color: #339933;\">;<\/span>                    <span style=\"color: #666666; font-style: italic;\">\/\/ CS pin is pulled LOW<\/span>\n  SPI_Write_Byte<span style=\"color: #009900;\">&#40;<\/span>k<span style=\"color: #339933;\">-<\/span><span style=\"color: #0000dd;\">1<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>             <span style=\"color: #666666; font-style: italic;\">\/\/ Send hundreds digit<\/span>\n  SPI_Write_Byte<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #009900;\">&#40;<\/span>j<span style=\"color: #339933;\">\/<\/span><span style=\"color: #0000dd;\">10<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">%<\/span><span style=\"color:#800080;\">10<\/span><span style=\"color: #009900;\">&#41;<\/span> <span style=\"color: #339933;\">|<\/span> <span style=\"color: #208080;\">0x80<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\n  CS_Pin <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000dd;\">1<\/span><span style=\"color: #339933;\">;<\/span>                    <span style=\"color: #666666; font-style: italic;\">\/\/ CS pin is pulled HIGH<\/span>\n&nbsp;\n  CS_Pin <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000dd;\">0<\/span><span style=\"color: #339933;\">;<\/span>                    <span style=\"color: #666666; font-style: italic;\">\/\/ CS pin is pulled LOW<\/span>\n  SPI_Write_Byte<span style=\"color: #009900;\">&#40;<\/span>k<span style=\"color: #339933;\">-<\/span><span style=\"color: #0000dd;\">2<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>             <span style=\"color: #666666; font-style: italic;\">\/\/ Send tens digit<\/span>\n  SPI_Write_Byte<span style=\"color: #009900;\">&#40;<\/span>j<span style=\"color: #339933;\">%<\/span><span style=\"color:#800080;\">10<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\n  CS_Pin <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000dd;\">1<\/span><span style=\"color: #339933;\">;<\/span>                    <span style=\"color: #666666; font-style: italic;\">\/\/ CS pin is pulled HIGH<\/span>\n&nbsp;\n  CS_Pin <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000dd;\">0<\/span><span style=\"color: #339933;\">;<\/span>                     <span style=\"color: #666666; font-style: italic;\">\/\/ CS pin is pulled LOW<\/span>\n  SPI_Write_Byte<span style=\"color: #009900;\">&#40;<\/span>k<span style=\"color: #339933;\">-<\/span><span style=\"color: #0000dd;\">3<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>            <span style=\"color: #666666; font-style: italic;\">\/\/ Send ones digit<\/span>\n  <span style=\"color: #b1b100;\">if<\/span><span style=\"color: #009900;\">&#40;<\/span>k <span style=\"color: #339933;\">==<\/span> <span style=\"color: #0000dd;\">4<\/span> <span style=\"color: #009900;\">&#41;<\/span> SPI_Write_Byte<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #208080;\">0x47<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>  <span style=\"color: #666666; font-style: italic;\">\/\/ Display 'F'<\/span>\n  <span style=\"color: #b1b100;\">if<\/span><span style=\"color: #009900;\">&#40;<\/span>k <span style=\"color: #339933;\">==<\/span> <span style=\"color: #0000dd;\">8<\/span> <span style=\"color: #009900;\">&#41;<\/span> SPI_Write_Byte<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #208080;\">0x67<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>  <span style=\"color: #666666; font-style: italic;\">\/\/ Display P<\/span>\n  CS_Pin <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000dd;\">1<\/span><span style=\"color: #339933;\">;<\/span>                    <span style=\"color: #666666; font-style: italic;\">\/\/ CS pin is pulled HIGH<\/span>\n <span style=\"color: #009900;\">&#125;<\/span>\n&nbsp;\n<span style=\"color: #993333;\">void<\/span> StartSignal<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #009900;\">&#123;<\/span>\n  DataDir <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000dd;\">0<\/span><span style=\"color: #339933;\">;<\/span>     <span style=\"color: #666666; font-style: italic;\">\/\/ Data port is output<\/span>\n  Data    <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000dd;\">0<\/span><span style=\"color: #339933;\">;<\/span>\n  Delay_ms<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #0000dd;\">25<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\n  Data    <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000dd;\">1<\/span><span style=\"color: #339933;\">;<\/span>\n  Delay_us<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #0000dd;\">30<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\n  DataDir <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000dd;\">1<\/span><span style=\"color: #339933;\">;<\/span>     <span style=\"color: #666666; font-style: italic;\">\/\/ Data port is input<\/span>\n&nbsp;\n<span style=\"color: #009900;\">&#125;<\/span>\n&nbsp;\n<span style=\"color: #993333;\">unsigned<\/span> <span style=\"color: #993333;\">short<\/span> CheckResponse<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #009900;\">&#123;<\/span>\n  TOUT <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000dd;\">0<\/span><span style=\"color: #339933;\">;<\/span>\n  TMR2 <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000dd;\">0<\/span><span style=\"color: #339933;\">;<\/span>\n  T2CON.<span style=\"color: #202020;\">TMR2ON<\/span> <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000dd;\">1<\/span><span style=\"color: #339933;\">;<\/span>      <span style=\"color: #666666; font-style: italic;\">\/\/ start timer<\/span>\n  <span style=\"color: #b1b100;\">while<\/span><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #339933;\">!<\/span>Data <span style=\"color: #339933;\">&amp;<\/span>amp<span style=\"color: #339933;\">;&amp;<\/span>amp<span style=\"color: #339933;\">;<\/span> <span style=\"color: #339933;\">!<\/span>TOUT<span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\n  <span style=\"color: #b1b100;\">if<\/span> <span style=\"color: #009900;\">&#40;<\/span>TOUT<span style=\"color: #009900;\">&#41;<\/span> <span style=\"color: #b1b100;\">return<\/span> <span style=\"color: #0000dd;\">0<\/span><span style=\"color: #339933;\">;<\/span>\n  <span style=\"color: #b1b100;\">else<\/span> <span style=\"color: #009900;\">&#123;<\/span>\n   TMR2 <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000dd;\">0<\/span><span style=\"color: #339933;\">;<\/span>\n   <span style=\"color: #b1b100;\">while<\/span><span style=\"color: #009900;\">&#40;<\/span>Data <span style=\"color: #339933;\">&amp;<\/span>amp<span style=\"color: #339933;\">;&amp;<\/span>amp<span style=\"color: #339933;\">;<\/span> <span style=\"color: #339933;\">!<\/span>TOUT<span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\n   <span style=\"color: #b1b100;\">if<\/span> <span style=\"color: #009900;\">&#40;<\/span>TOUT<span style=\"color: #009900;\">&#41;<\/span> <span style=\"color: #b1b100;\">return<\/span> <span style=\"color: #0000dd;\">0<\/span><span style=\"color: #339933;\">;<\/span>\n   <span style=\"color: #b1b100;\">else<\/span> <span style=\"color: #009900;\">&#123;<\/span>\n    T2CON.<span style=\"color: #202020;\">TMR2ON<\/span> <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000dd;\">0<\/span><span style=\"color: #339933;\">;<\/span>\n    <span style=\"color: #b1b100;\">return<\/span> <span style=\"color: #0000dd;\">1<\/span><span style=\"color: #339933;\">;<\/span>\n   <span style=\"color: #009900;\">&#125;<\/span>\n  <span style=\"color: #009900;\">&#125;<\/span>\n<span style=\"color: #009900;\">&#125;<\/span>\n&nbsp;\n<span style=\"color: #993333;\">unsigned<\/span> <span style=\"color: #993333;\">short<\/span> ReadByte<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #009900;\">&#123;<\/span>\n  <span style=\"color: #993333;\">unsigned<\/span> <span style=\"color: #993333;\">short<\/span> num <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000dd;\">0<\/span><span style=\"color: #339933;\">,<\/span> t<span style=\"color: #339933;\">;<\/span>\n  DataDir <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000dd;\">1<\/span><span style=\"color: #339933;\">;<\/span>\n  <span style=\"color: #b1b100;\">for<\/span> <span style=\"color: #009900;\">&#40;<\/span>i<span style=\"color: #339933;\">=<\/span><span style=\"color: #0000dd;\">0<\/span><span style=\"color: #339933;\">;<\/span> i<span style=\"color: #339933;\">&amp;<\/span>lt<span style=\"color: #339933;\">;<\/span><span style=\"color: #0000dd;\">8<\/span><span style=\"color: #339933;\">;<\/span> i<span style=\"color: #339933;\">++<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #009900;\">&#123;<\/span>\n   <span style=\"color: #b1b100;\">while<\/span><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #339933;\">!<\/span>Data<span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\n   TMR2 <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000dd;\">0<\/span><span style=\"color: #339933;\">;<\/span>\n   T2CON.<span style=\"color: #202020;\">TMR2ON<\/span> <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000dd;\">1<\/span><span style=\"color: #339933;\">;<\/span>\n   <span style=\"color: #b1b100;\">while<\/span><span style=\"color: #009900;\">&#40;<\/span>Data<span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\n   T2CON.<span style=\"color: #202020;\">TMR2ON<\/span> <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000dd;\">0<\/span><span style=\"color: #339933;\">;<\/span>\n   <span style=\"color: #b1b100;\">if<\/span><span style=\"color: #009900;\">&#40;<\/span>TMR2 <span style=\"color: #339933;\">&amp;<\/span>gt<span style=\"color: #339933;\">;<\/span> <span style=\"color: #0000dd;\">40<\/span><span style=\"color: #009900;\">&#41;<\/span> num <span style=\"color: #339933;\">|=<\/span> <span style=\"color: #0000dd;\">1<\/span><span style=\"color: #339933;\">&amp;<\/span>lt<span style=\"color: #339933;\">;&amp;<\/span>lt<span style=\"color: #339933;\">;<\/span><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #0000dd;\">7<\/span><span style=\"color: #339933;\">-<\/span>i<span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>  <span style=\"color: #666666; font-style: italic;\">\/\/ If time &amp;gt; 40us, Data is 1<\/span>\n  <span style=\"color: #009900;\">&#125;<\/span>\n  <span style=\"color: #b1b100;\">return<\/span> num<span style=\"color: #339933;\">;<\/span>\n<span style=\"color: #009900;\">&#125;<\/span>\n&nbsp;\n<span style=\"color: #993333;\">void<\/span> interrupt<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #009900;\">&#123;<\/span>\n  <span style=\"color: #b1b100;\">if<\/span><span style=\"color: #009900;\">&#40;<\/span>PIR1.<span style=\"color: #202020;\">TMR2IF<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #009900;\">&#123;<\/span>\n   TOUT <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000dd;\">1<\/span><span style=\"color: #339933;\">;<\/span>\n   T2CON.<span style=\"color: #202020;\">TMR2ON<\/span> <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000dd;\">0<\/span><span style=\"color: #339933;\">;<\/span> <span style=\"color: #666666; font-style: italic;\">\/\/ stop timer<\/span>\n   PIR1.<span style=\"color: #202020;\">TMR2IF<\/span>  <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000dd;\">0<\/span><span style=\"color: #339933;\">;<\/span> <span style=\"color: #666666; font-style: italic;\">\/\/ Clear TMR0 interrupt flag<\/span>\n  <span style=\"color: #009900;\">&#125;<\/span>\n<span style=\"color: #009900;\">&#125;<\/span>\n&nbsp;\n<span style=\"color: #993333;\">void<\/span> Display_Brightness<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #009900;\">&#123;<\/span>\n  CS_Pin <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000dd;\">0<\/span><span style=\"color: #339933;\">;<\/span>                <span style=\"color: #666666; font-style: italic;\">\/\/ CS pin is pulled LOW<\/span>\n  SPI_Write_Byte<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #208080;\">0x0A<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>      <span style=\"color: #666666; font-style: italic;\">\/\/ Select Intensity register<\/span>\n  SPI_Write_Byte<span style=\"color: #009900;\">&#40;<\/span>brightness<span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>    <span style=\"color: #666666; font-style: italic;\">\/\/ Update brightness<\/span>\n  CS_Pin <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000dd;\">1<\/span><span style=\"color: #339933;\">;<\/span>                <span style=\"color: #666666; font-style: italic;\">\/\/ CS pin is pulled HIGH<\/span>\n<span style=\"color: #009900;\">&#125;<\/span>\n&nbsp;\n<span style=\"color: #993333;\">void<\/span> main<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #009900;\">&#41;<\/span> <span style=\"color: #009900;\">&#123;<\/span>\n  TRISIO<span style=\"color: #339933;\">=<\/span><span style=\"color: #208080;\">0b00011000<\/span><span style=\"color: #339933;\">;<\/span>       <span style=\"color: #666666; font-style: italic;\">\/\/ GP3, GP4 are input<\/span>\n  CMCON0 <span style=\"color: #339933;\">=<\/span> <span style=\"color: #208080;\">0x07<\/span><span style=\"color: #339933;\">;<\/span>\n  ANSEL <span style=\"color: #339933;\">=<\/span> <span style=\"color: #208080;\">0x08<\/span><span style=\"color: #339933;\">;<\/span>            <span style=\"color: #666666; font-style: italic;\">\/\/ AN3 active<\/span>\n  INTCON.<span style=\"color: #202020;\">GIE<\/span> <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000dd;\">1<\/span><span style=\"color: #339933;\">;<\/span>    <span style=\"color: #666666; font-style: italic;\">\/\/Enable global interrupt<\/span>\n  INTCON.<span style=\"color: #202020;\">PEIE<\/span> <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000dd;\">1<\/span><span style=\"color: #339933;\">;<\/span>   <span style=\"color: #666666; font-style: italic;\">\/\/Enable peripheral interrupt<\/span>\n  <span style=\"color: #666666; font-style: italic;\">\/\/ Configure Timer2 module<\/span>\n  PIE1.<span style=\"color: #202020;\">TMR2IE<\/span> <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000dd;\">1<\/span><span style=\"color: #339933;\">;<\/span>  <span style=\"color: #666666; font-style: italic;\">\/\/ Enable Timer2 interrupt<\/span>\n  T2CON <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000dd;\">0<\/span><span style=\"color: #339933;\">;<\/span>        <span style=\"color: #666666; font-style: italic;\">\/\/ Prescaler 1:1, and Timer2 is off initially<\/span>\n  PIR1.<span style=\"color: #202020;\">TMR2IF<\/span> <span style=\"color: #339933;\">=<\/span><span style=\"color: #0000dd;\">0<\/span><span style=\"color: #339933;\">;<\/span>   <span style=\"color: #666666; font-style: italic;\">\/\/ Clear TMR INT Flag bit<\/span>\n  TMR2 <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000dd;\">0<\/span><span style=\"color: #339933;\">;<\/span>\n  MAX7219_INIT<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>           <span style=\"color: #666666; font-style: italic;\">\/\/ initialize  max7219<\/span>\n  <span style=\"color: #b1b100;\">do<\/span><span style=\"color: #009900;\">&#123;<\/span>\n    ADC_OP <span style=\"color: #339933;\">=<\/span> ADC_Read<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #0000dd;\">3<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\n    ADC_OP <span style=\"color: #339933;\">=<\/span> ADC_OP<span style=\"color: #339933;\">\/<\/span><span style=\"color: #0000dd;\">100<\/span><span style=\"color: #339933;\">;<\/span>\n    Brightness <span style=\"color: #339933;\">=<\/span> Brightness_Table<span style=\"color: #009900;\">&#91;<\/span>ADC_OP<span style=\"color: #009900;\">&#93;<\/span><span style=\"color: #339933;\">;<\/span>\n    Display_Brightness<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\n    StartSignal<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\n    check <span style=\"color: #339933;\">=<\/span> CheckResponse<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\n    <span style=\"color: #b1b100;\">if<\/span> <span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #339933;\">!<\/span>check<span style=\"color: #009900;\">&#41;<\/span> <span style=\"color: #009900;\">&#123;<\/span>\n     TempF <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000dd;\">0<\/span><span style=\"color: #339933;\">;<\/span>\n     RH <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000dd;\">0<\/span><span style=\"color: #339933;\">;<\/span>\n     <span style=\"color: #009900;\">&#125;<\/span>\n    <span style=\"color: #b1b100;\">else<\/span><span style=\"color: #009900;\">&#123;<\/span>\n&nbsp;\n     RH_Byte1 <span style=\"color: #339933;\">=<\/span> ReadByte<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\n     RH_Byte2 <span style=\"color: #339933;\">=<\/span> ReadByte<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\n     T_Byte1 <span style=\"color: #339933;\">=<\/span> ReadByte<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\n     T_Byte2 <span style=\"color: #339933;\">=<\/span> ReadByte<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\n     CheckSum <span style=\"color: #339933;\">=<\/span> ReadByte<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\n     <span style=\"color: #666666; font-style: italic;\">\/\/ Check for error in Data reception<\/span>\n     <span style=\"color: #b1b100;\">if<\/span> <span style=\"color: #009900;\">&#40;<\/span>CheckSum <span style=\"color: #339933;\">==<\/span> <span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #009900;\">&#40;<\/span>RH_Byte1 <span style=\"color: #339933;\">+<\/span> RH_Byte2 <span style=\"color: #339933;\">+<\/span> T_Byte1 <span style=\"color: #339933;\">+<\/span> T_Byte2<span style=\"color: #009900;\">&#41;<\/span> <span style=\"color: #339933;\">&amp;<\/span>amp<span style=\"color: #339933;\">;<\/span> <span style=\"color: #208080;\">0xFF<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #009900;\">&#41;<\/span>\n     <span style=\"color: #009900;\">&#123;<\/span>\n      TempF <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000dd;\">10<\/span><span style=\"color: #339933;\">*<\/span>T_Byte1<span style=\"color: #339933;\">;<\/span>\n      TempF <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000dd;\">9<\/span><span style=\"color: #339933;\">*<\/span>TempF<span style=\"color: #339933;\">\/<\/span><span style=\"color: #0000dd;\">5<\/span> <span style=\"color: #339933;\">+<\/span> <span style=\"color: #0000dd;\">320<\/span><span style=\"color: #339933;\">;<\/span>\n      RH <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000dd;\">10<\/span><span style=\"color: #339933;\">*<\/span>RH_Byte1<span style=\"color: #339933;\">;<\/span>\n     <span style=\"color: #009900;\">&#125;<\/span>\n&nbsp;\n      <span style=\"color: #b1b100;\">else<\/span><span style=\"color: #009900;\">&#123;<\/span>\n       TempF <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000dd;\">0<\/span><span style=\"color: #339933;\">;<\/span>\n       RH <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000dd;\">0<\/span><span style=\"color: #339933;\">;<\/span>\n      <span style=\"color: #009900;\">&#125;<\/span>\n    <span style=\"color: #009900;\">&#125;<\/span>\n&nbsp;\n    Display_Value<span style=\"color: #009900;\">&#40;<\/span>TempF<span style=\"color: #339933;\">,<\/span> <span style=\"color: #0000dd;\">4<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\n    Display_Value<span style=\"color: #009900;\">&#40;<\/span>RH<span style=\"color: #339933;\">,<\/span> <span style=\"color: #0000dd;\">8<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\n    Delay_ms<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #0000dd;\">500<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\n&nbsp;\n  <span style=\"color: #009900;\">&#125;<\/span><span style=\"color: #b1b100;\">while<\/span><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #0000dd;\">1<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\n<span style=\"color: #009900;\">&#125;<\/span><\/pre><\/td><\/tr><\/table><\/div>\n\n<p><a href=\"http:\/\/embedded-lab.com\/uploads\/HexFiles\/PIC12F\/TRH_Meter.zip\"><strong>Download source and HEX files<\/strong><\/a><\/p>\n<p>The PIC12F683 microcontroller runs at 4.0 MHz internal clock. The MCLR and Power-On Timer are both enabled. The complete configuration bit settings are shown below.<\/p>\n<div id=\"attachment_5541\" style=\"width: 574px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/07\/PIC12Fconfig.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-5541\" class=\"size-full wp-image-5541\" title=\"PIC12Fconfig\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/07\/PIC12Fconfig.jpg\" alt=\"\" width=\"564\" height=\"362\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/07\/PIC12Fconfig.jpg 564w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/07\/PIC12Fconfig-550x353.jpg 550w\" sizes=\"auto, (max-width: 564px) 100vw, 564px\" \/><\/a><p id=\"caption-attachment-5541\" class=\"wp-caption-text\">Configuration bit settings of PIC12F683<\/p><\/div>\n<p><strong>Output<\/strong><\/p>\n<p>The project was tested under different lighting conditions, varying from very dark to very bright, and all the time display was found readable and eye-soothing. If you want to put something like this in your bedroom, you don&#8217;t have to worry about turning it off during nighttime. The display will automatically dim enough so as not to disturb your sleep. While the given look-up table for brightness intensity provides satisfactory adjustment of brightness level at different lighting condition, you can still tweak these numbers to find the best matches for your eyes.<\/p>\n<div id=\"attachment_5534\" style=\"width: 575px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/07\/HighLightCondition.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-5534\" class=\"size-full wp-image-5534\" title=\"HighLightCondition\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/07\/HighLightCondition.jpg\" alt=\"\" width=\"565\" height=\"470\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/07\/HighLightCondition.jpg 565w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/07\/HighLightCondition-550x457.jpg 550w\" sizes=\"auto, (max-width: 565px) 100vw, 565px\" \/><\/a><p id=\"caption-attachment-5534\" class=\"wp-caption-text\">High ambient light condition<\/p><\/div>\n<div id=\"attachment_5535\" style=\"width: 575px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/07\/LowLightCondition.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-5535\" class=\"size-full wp-image-5535\" title=\"LowLightCondition\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/07\/LowLightCondition.jpg\" alt=\"\" width=\"565\" height=\"470\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/07\/LowLightCondition.jpg 565w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/07\/LowLightCondition-550x457.jpg 550w\" sizes=\"auto, (max-width: 565px) 100vw, 565px\" \/><\/a><p id=\"caption-attachment-5535\" class=\"wp-caption-text\">Low ambient light condition<\/p><\/div>\n","protected":false},"excerpt":{"rendered":"<p>The objective of this project is to illustrate a technique of implementing adaptive brightness control to seven segment LED displays. It consists of a closed loop system that continuously assesses ambient light condition using an inexpensive light-dependent resistor (LDR) and uses that information to adjust the brightness of the display. For the proof of concept, the technique is applied to construct a digital temperature and relative humidity meter that adapts the brightness of the seven segment LED displays to the surrounding lighting conditions. There are 8 seven segment LED displays used in this project and they are all driven by<\/p>\n","protected":false},"author":1,"featured_media":5523,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[465,392,270,6,98],"class_list":["post-5517","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-pic-projects","tag-adaptive-brightness","tag-dht11","tag-microchip","tag-pic12f683","tag-seven-segment-display"],"_links":{"self":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts\/5517","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\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/comments?post=5517"}],"version-history":[{"count":32,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts\/5517\/revisions"}],"predecessor-version":[{"id":5562,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts\/5517\/revisions\/5562"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/media\/5523"}],"wp:attachment":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/media?parent=5517"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/categories?post=5517"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/tags?post=5517"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}