
#include <FONT COLOR=BLUE SIZE=+1>&lt;</FONT>REG52.H<FONT COLOR=BLUE SIZE=+1>&gt;</FONT>

<FONT COLOR=GREEN><I>// EXAMPLE Port &amp; Pin definitions to match the 8052.com Tutorial
</I></FONT>sbit LCD_EN <FONT COLOR=BLUE SIZE=+1>=</FONT> P3<FONT COLOR=BLUE SIZE=+1>^</FONT><FONT COLOR=BROWN>7</FONT><FONT COLOR=BLUE SIZE=+1><B>;</B></FONT>
sbit LCD_RS <FONT COLOR=BLUE SIZE=+1>=</FONT> P3<FONT COLOR=BLUE SIZE=+1>^</FONT><FONT COLOR=BROWN>6</FONT><FONT COLOR=BLUE SIZE=+1><B>;</B></FONT>

sbit LCD_RW <FONT COLOR=BLUE SIZE=+1>=</FONT> P3<FONT COLOR=BLUE SIZE=+1>^</FONT><FONT COLOR=BROWN>5</FONT><FONT COLOR=BLUE SIZE=+1><B>;</B></FONT>
#define LCD_PORT P0

<FONT COLOR=GREEN><I>// Funtion prototype
</I></FONT><FONT COLOR=RED><B>void</B></FONT> lcd_data<FONT COLOR=BLUE SIZE=+1><B>(</B></FONT>unsigned <FONT COLOR=RED><B>char</B></FONT> dat<FONT COLOR=BLUE SIZE=+1><B>)</B></FONT><FONT COLOR=BLUE SIZE=+1><B>;</B></FONT>


<FONT COLOR=GREEN><I>/*
 * this routine displays the supplied 8-bit unsigned number
 * in hexadecimal notation.
 *
 * The hexadecimal display is achieved by sending two ASCII-coded
 * characters to the LCD; eg the number 0xA4 is displayed by sending 
 * a '4' character followed by an 'A' character.
 */</I></FONT> 
<FONT COLOR=RED><B>void</B></FONT> hex2lcd<FONT COLOR=BLUE SIZE=+1><B>(</B></FONT> unsigned <FONT COLOR=RED><B>char</B></FONT> number <FONT COLOR=BLUE SIZE=+1><B>)</B></FONT> 
<FONT COLOR=BLUE SIZE=+1><B>{</B></FONT> 
  unsigned <FONT COLOR=RED><B>char</B></FONT> MSD<FONT COLOR=BLUE SIZE=+1><B>;</B></FONT> <FONT COLOR=GREEN><I>// used to extract the Most-Significant  (MS) Hex Digit of the number

</I></FONT>  unsigned <FONT COLOR=RED><B>char</B></FONT> LSD<FONT COLOR=BLUE SIZE=+1><B>;</B></FONT> <FONT COLOR=GREEN><I>// used to extract the Least-Significant (LS) Hex Digit of the number
</I></FONT>
  MSD <FONT COLOR=BLUE SIZE=+1>=</FONT> <FONT COLOR=BLUE SIZE=+1><B>(</B></FONT>number<FONT COLOR=BLUE SIZE=+1>&amp;</FONT>0xf0<FONT COLOR=BLUE SIZE=+1><B>)</B></FONT> <FONT COLOR=BLUE SIZE=+1>&gt;</FONT><FONT COLOR=BLUE SIZE=+1>&gt;</FONT><FONT COLOR=BROWN>4</FONT><FONT COLOR=BLUE SIZE=+1><B>;</B></FONT> <FONT COLOR=GREEN><I>// extract the MS Hex Digit; ie the top 4 bits

</I></FONT>  LSD <FONT COLOR=BLUE SIZE=+1>=</FONT> <FONT COLOR=BLUE SIZE=+1><B>(</B></FONT>number<FONT COLOR=BLUE SIZE=+1>&amp;</FONT>0x0f<FONT COLOR=BLUE SIZE=+1><B>)</B></FONT><FONT COLOR=BLUE SIZE=+1><B>;</B></FONT>     <FONT COLOR=GREEN><I>// extract the LS Hex Digit; ie the bottom 4 bits
</I></FONT>
  <FONT COLOR=GREEN><I>// First, obtain the character to represent the MS Hex digit
</I></FONT>  <FONT COLOR=GREEN><I>// Note that the conversion differs slightly depending on whether the value is 0x0-0x9 or 0xA-0xF
</I></FONT>  <FONT COLOR=RED><B>if</B></FONT><FONT COLOR=BLUE SIZE=+1><B>(</B></FONT> MSD <FONT COLOR=BLUE SIZE=+1>&lt;</FONT> 0xA <FONT COLOR=BLUE SIZE=+1><B>)</B></FONT>

  <FONT COLOR=BLUE SIZE=+1><B>{</B></FONT> 
	<FONT COLOR=GREEN><I>// The MS Digit is less than 0xA; ie it is in the range 0x0-0x9
</I></FONT>	<FONT COLOR=GREEN><I>// Convert it to the corresponding ASCII character code '0' to '9'.
</I></FONT>	<FONT COLOR=GREEN><I>// This conversion is easy, as we just have to add the value of the digit, 0x0-0x9,
</I></FONT>	<FONT COLOR=GREEN><I>// to the value of the ASCII code for a '0' character
</I></FONT>    MSD <FONT COLOR=BLUE SIZE=+1>+</FONT><FONT COLOR=BLUE SIZE=+1>=</FONT> '<FONT COLOR=BROWN>0</FONT>'<FONT COLOR=BLUE SIZE=+1><B>;</B></FONT> 
  <FONT COLOR=BLUE SIZE=+1><B>}</B></FONT>

  <FONT COLOR=RED><B>else</B></FONT> 
  <FONT COLOR=BLUE SIZE=+1><B>{</B></FONT>
	<FONT COLOR=GREEN><I>// The MS Digit is not less than 0xA; ie it is in the range 0xA-0xF
</I></FONT>	<FONT COLOR=GREEN><I>// Convert it to the corresponding ASCII character code 'A' to 'F'
</I></FONT>	<FONT COLOR=GREEN><I>// This conversion is slightly more complicated, as the values we are converting
</I></FONT>	<FONT COLOR=GREEN><I>// are in the range 0xA-0xF; so we cannot simply add the value to be converted
</I></FONT>	<FONT COLOR=GREEN><I>// to the ASCII code for an 'A' character - hence the subtraction of 0xA
</I></FONT>	<FONT COLOR=GREEN><I>// (note that a decent compiler will do the subtraction at compile-time, and 

</I></FONT>	<FONT COLOR=GREEN><I>//  will not generate code to work it out every time this line is executed!)
</I></FONT>    MSD <FONT COLOR=BLUE SIZE=+1>+</FONT><FONT COLOR=BLUE SIZE=+1>=</FONT> <FONT COLOR=BLUE SIZE=+1><B>(</B></FONT>'A' <FONT COLOR=BLUE SIZE=+1>-</FONT> 0xA<FONT COLOR=BLUE SIZE=+1><B>)</B></FONT><FONT COLOR=BLUE SIZE=+1><B>;</B></FONT> 
  <FONT COLOR=BLUE SIZE=+1><B>}</B></FONT>

  <FONT COLOR=GREEN><I>// Now obtain the character to represent the LS Hex digit
</I></FONT>  <FONT COLOR=GREEN><I>// The process is exactly the same as for the MS digit
</I></FONT>  <FONT COLOR=GREEN><I>// Note that, since we have the MS and LS Digits in separate variables, it doesn't
</I></FONT>  <FONT COLOR=GREEN><I>// matter in which order we convert the digits.
</I></FONT>  <FONT COLOR=RED><B>if</B></FONT><FONT COLOR=BLUE SIZE=+1><B>(</B></FONT> LSD <FONT COLOR=BLUE SIZE=+1>&lt;</FONT> 0xA <FONT COLOR=BLUE SIZE=+1><B>)</B></FONT>

  <FONT COLOR=BLUE SIZE=+1><B>{</B></FONT> 
    LSD <FONT COLOR=BLUE SIZE=+1>+</FONT><FONT COLOR=BLUE SIZE=+1>=</FONT> '<FONT COLOR=BROWN>0</FONT>'<FONT COLOR=BLUE SIZE=+1><B>;</B></FONT> 
  <FONT COLOR=BLUE SIZE=+1><B>}</B></FONT>
  <FONT COLOR=RED><B>else</B></FONT> 
  <FONT COLOR=BLUE SIZE=+1><B>{</B></FONT>

    LSD <FONT COLOR=BLUE SIZE=+1>+</FONT><FONT COLOR=BLUE SIZE=+1>=</FONT> <FONT COLOR=BLUE SIZE=+1><B>(</B></FONT>'A' <FONT COLOR=BLUE SIZE=+1>-</FONT> 0xA<FONT COLOR=BLUE SIZE=+1><B>)</B></FONT><FONT COLOR=BLUE SIZE=+1><B>;</B></FONT> 
  <FONT COLOR=BLUE SIZE=+1><B>}</B></FONT>

  <FONT COLOR=GREEN><I>// Now send the two characters forming the hex representation to the display

</I></FONT>  <FONT COLOR=GREEN><I>// Note that it is now important that we send them in the correct order!
</I></FONT>  lcd_data<FONT COLOR=BLUE SIZE=+1><B>(</B></FONT> MSD <FONT COLOR=BLUE SIZE=+1><B>)</B></FONT><FONT COLOR=BLUE SIZE=+1><B>;</B></FONT> 
  lcd_data<FONT COLOR=BLUE SIZE=+1><B>(</B></FONT> LSD <FONT COLOR=BLUE SIZE=+1><B>)</B></FONT><FONT COLOR=BLUE SIZE=+1><B>;</B></FONT> 

<FONT COLOR=BLUE SIZE=+1><B>}</B></FONT> 

<FONT COLOR=GREEN><I>/*
 * this routine displays the supplied 8-bit unsigned number
 * in decimal notation, with leading zeros; ie "000" to "255"
 *
 * The decimal display is achieved by sending three ASCII-coded
 * characters to the LCD; eg the number 0x78 (123 decimal) is displayed by 
 * sending  a '1' character followed by a '2' character followed by a '3' character.
 */</I></FONT> 
<FONT COLOR=RED><B>void</B></FONT> dec2lcd<FONT COLOR=BLUE SIZE=+1><B>(</B></FONT>unsigned <FONT COLOR=RED><B>char</B></FONT> number<FONT COLOR=BLUE SIZE=+1><B>)</B></FONT> 
<FONT COLOR=BLUE SIZE=+1><B>{</B></FONT> 
  unsigned <FONT COLOR=RED><B>char</B></FONT> h<FONT COLOR=BLUE SIZE=+1>=</FONT><FONT COLOR=BROWN>0</FONT><FONT COLOR=BLUE SIZE=+1><B>;</B></FONT> <FONT COLOR=GREEN><I>// used to extract the "hundreds" Digit of the number in decimal notation

</I></FONT>  unsigned <FONT COLOR=RED><B>char</B></FONT> t<FONT COLOR=BLUE SIZE=+1>=</FONT><FONT COLOR=BROWN>0</FONT><FONT COLOR=BLUE SIZE=+1><B>;</B></FONT> <FONT COLOR=GREEN><I>// used to extract the "tens"     Digit of the number in decimal notation
</I></FONT>
  <FONT COLOR=GREEN><I>// First, determine the number of "hundreds" in the number.
</I></FONT>  <FONT COLOR=GREEN><I>// We do this by repeatedly subtracting 100 until the number falls below 100, 
</I></FONT>  <FONT COLOR=GREEN><I>// and counting how many times we subtracted 100

</I></FONT>  <FONT COLOR=RED><B>while</B></FONT><FONT COLOR=BLUE SIZE=+1><B>(</B></FONT> number <FONT COLOR=BLUE SIZE=+1>&gt;</FONT><FONT COLOR=BLUE SIZE=+1>=</FONT> <FONT COLOR=BROWN>100</FONT> <FONT COLOR=BLUE SIZE=+1><B>)</B></FONT> 
  <FONT COLOR=BLUE SIZE=+1><B>{</B></FONT> 
    number <FONT COLOR=BLUE SIZE=+1>-</FONT><FONT COLOR=BLUE SIZE=+1>=</FONT> <FONT COLOR=BROWN>100</FONT><FONT COLOR=BLUE SIZE=+1><B>;</B></FONT> 
    h<FONT COLOR=BLUE SIZE=+1><FONT COLOR=BLUE SIZE=+1>+</FONT><FONT COLOR=BLUE SIZE=+1>+</FONT></FONT><FONT COLOR=BLUE SIZE=+1><B>;</B></FONT> 			<FONT COLOR=GREEN><I>// Count another "hundred"

</I></FONT>  <FONT COLOR=BLUE SIZE=+1><B>}</B></FONT> 

  <FONT COLOR=GREEN><I>// The number now has no "hundreds" - either it had none to start with, or
</I></FONT>  <FONT COLOR=GREEN><I>// they have all just been subtracted. 
</I></FONT>  <FONT COLOR=GREEN><I>// Next, determine the number of "tens" in the number.
</I></FONT>  <FONT COLOR=GREEN><I>// Again, we do this by repeatedly subtracting 10 until the number falls below 10, 
</I></FONT>  <FONT COLOR=GREEN><I>// and counting how many times we subtracted 10
</I></FONT>  <FONT COLOR=RED><B>while</B></FONT><FONT COLOR=BLUE SIZE=+1><B>(</B></FONT>number<FONT COLOR=BLUE SIZE=+1>&gt;</FONT><FONT COLOR=BLUE SIZE=+1>=</FONT><FONT COLOR=BROWN>10</FONT><FONT COLOR=BLUE SIZE=+1><B>)</B></FONT> 
  <FONT COLOR=BLUE SIZE=+1><B>{</B></FONT> 
    number <FONT COLOR=BLUE SIZE=+1>-</FONT><FONT COLOR=BLUE SIZE=+1>=</FONT> <FONT COLOR=BROWN>10</FONT><FONT COLOR=BLUE SIZE=+1><B>;</B></FONT> 
    t<FONT COLOR=BLUE SIZE=+1><FONT COLOR=BLUE SIZE=+1>+</FONT><FONT COLOR=BLUE SIZE=+1>+</FONT></FONT><FONT COLOR=BLUE SIZE=+1><B>;</B></FONT> 
  <FONT COLOR=BLUE SIZE=+1><B>}</B></FONT> 

  <FONT COLOR=GREEN><I>// The number now has no "hundreds" and no "tens" - either it had none to start with, or

</I></FONT>  <FONT COLOR=GREEN><I>// they have all just been subtracted. 
</I></FONT>  <FONT COLOR=GREEN><I>// All that is now left is the units!
</I></FONT>  <FONT COLOR=GREEN><I>// Now send the 3 digits - "hundreds", "tens", and "units" - to the display as ASCII-Coded
</I></FONT>  <FONT COLOR=GREEN><I>// Characters.
</I></FONT>  <FONT COLOR=GREEN><I>// Because each digit must be in the range 0-9, the corresponding ASCII code is found by
</I></FONT>  <FONT COLOR=GREEN><I>// adding the digit value to the value of the ASCII code for a '0' character 
</I></FONT>  lcd_data<FONT COLOR=BLUE SIZE=+1><B>(</B></FONT>      h <FONT COLOR=BLUE SIZE=+1>+</FONT> '<FONT COLOR=BROWN>0</FONT>' <FONT COLOR=BLUE SIZE=+1><B>)</B></FONT><FONT COLOR=BLUE SIZE=+1><B>;</B></FONT> <FONT COLOR=GREEN><I>// hundreds

</I></FONT>  lcd_data<FONT COLOR=BLUE SIZE=+1><B>(</B></FONT>      t <FONT COLOR=BLUE SIZE=+1>+</FONT> '<FONT COLOR=BROWN>0</FONT>' <FONT COLOR=BLUE SIZE=+1><B>)</B></FONT><FONT COLOR=BLUE SIZE=+1><B>;</B></FONT> <FONT COLOR=GREEN><I>// tens
</I></FONT>  lcd_data<FONT COLOR=BLUE SIZE=+1><B>(</B></FONT> number <FONT COLOR=BLUE SIZE=+1>+</FONT> '<FONT COLOR=BROWN>0</FONT>' <FONT COLOR=BLUE SIZE=+1><B>)</B></FONT><FONT COLOR=BLUE SIZE=+1><B>;</B></FONT> <FONT COLOR=GREEN><I>// units (remember: number was left containing just units)

</I></FONT><FONT COLOR=BLUE SIZE=+1><B>}</B></FONT> 

<FONT COLOR=GREEN><I>/* 
 * This Routine sends the supplied byte to the LCD
 *
 * Note that you must supply appropriate definitions for the port pins
 * used by the LCD, and these must be bit-addressable
 *
 * Note also that the LCD must be properly initialised, and the control lines
 * in their proper states,  before calling this function.
 *
 * Note also that a software delay loop is used to set the length of the
 * Enable pulse - YOU WILL ALMOST CERTAINLY NEED TO MODIFY THIS TO MATCH
 * YOUR PARTICULAR CLOCK FREQUENCY, CHIP INSTRUCTION TIMINGS, COMPILER
 * SETTINGS, etc. (in fact, it would be better to write a separate assembler
 * delay routine).
 *
 */</I></FONT>
<FONT COLOR=RED><B>void</B></FONT> lcd_data<FONT COLOR=BLUE SIZE=+1><B>(</B></FONT> unsigned <FONT COLOR=RED><B>char</B></FONT> dat<FONT COLOR=BLUE SIZE=+1><B>)</B></FONT> 
<FONT COLOR=BLUE SIZE=+1><B>{</B></FONT> 
  unsigned <FONT COLOR=RED><B>char</B></FONT> a<FONT COLOR=BLUE SIZE=+1><B>;</B></FONT> <FONT COLOR=GREEN><I>// Loop counter for the delay loop - YOU MAY NEED TO MODIFY THIS!

</I></FONT>
  LCD_RS<FONT COLOR=BLUE SIZE=+1>=</FONT><FONT COLOR=BROWN>1</FONT><FONT COLOR=BLUE SIZE=+1><B>;</B></FONT>           <FONT COLOR=GREEN><I>// Set the Register Select line high indicating data for display
</I></FONT>  LCD_RW<FONT COLOR=BLUE SIZE=+1>=</FONT><FONT COLOR=BROWN>0</FONT><FONT COLOR=BLUE SIZE=+1><B>;</B></FONT> 		  <FONT COLOR=GREEN><I>// Set the Read/Write line low indicating a Write to the display
</I></FONT>  LCD_PORT<FONT COLOR=BLUE SIZE=+1>=</FONT><FONT COLOR=BLUE SIZE=+1><B>(</B></FONT> dat <FONT COLOR=BLUE SIZE=+1><B>)</B></FONT><FONT COLOR=BLUE SIZE=+1><B>;</B></FONT>   <FONT COLOR=GREEN><I>// Write the supplied byte to the LCD port

</I></FONT>
  <FONT COLOR=GREEN><I>// Now pulse the Enable line so that the LCD will see the data
</I></FONT>  LCD_EN<FONT COLOR=BLUE SIZE=+1>=</FONT><FONT COLOR=BROWN>1</FONT><FONT COLOR=BLUE SIZE=+1><B>;</B></FONT> 		      <FONT COLOR=GREEN><I>// Set the EN line high
</I></FONT>  <FONT COLOR=RED><B>for</B></FONT><FONT COLOR=BLUE SIZE=+1><B>(</B></FONT> a<FONT COLOR=BLUE SIZE=+1>=</FONT><FONT COLOR=BROWN>0</FONT><FONT COLOR=BLUE SIZE=+1><B>;</B></FONT> a<FONT COLOR=BLUE SIZE=+1>&lt;</FONT>0xf<FONT COLOR=BLUE SIZE=+1><B>;</B></FONT> a<FONT COLOR=BLUE SIZE=+1><FONT COLOR=BLUE SIZE=+1>+</FONT><FONT COLOR=BLUE SIZE=+1>+</FONT></FONT> <FONT COLOR=BLUE SIZE=+1><B>)</B></FONT><FONT COLOR=BLUE SIZE=+1><B>;</B></FONT> <FONT COLOR=GREEN><I>// Pause a while - YOU WILL NEED TO MODIFY THIS!

</I></FONT>  LCD_EN<FONT COLOR=BLUE SIZE=+1>=</FONT><FONT COLOR=BROWN>0</FONT><FONT COLOR=BLUE SIZE=+1><B>;</B></FONT> 		      <FONT COLOR=GREEN><I>// Set the EN line low
</I></FONT><FONT COLOR=BLUE SIZE=+1><B>}</B></FONT> 