Counter with LCD Display

Dependencies:   TextLCD mbed

Revision:
0:1abd7e24ae31
Child:
1:f580beec166a
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Tue Jun 28 15:10:44 2011 +0000
@@ -0,0 +1,146 @@
+//Include mbed header file which contains the definition of DigitalOut class. 
+#include "mbed.h"
+//Include TextLCD header file which contains the definition of TextLCD class. 
+#include "TextLCD.h"
+
+//Initialize TextLCD with the correct pins
+TextLCD lcd(p24, p26, p27, p28, p29, p30);
+
+//This function gets an integer and converts it to a charecter
+char* ConvertIntToChar(int i)
+{
+    char* c = "";
+    switch (i)
+    {
+    case 1:
+        c = "1";
+        break;
+    case 2:
+        c = "2";
+        break;
+    case 3:
+        c = "3";
+        break;
+    case 4:
+        c = "4";
+        break;
+    case 5:
+        c = "5";
+        break;
+    case 6:
+        c = "6";
+        break;
+    case 7:
+        c = "7";
+        break;
+    case 8:
+        c = "8";
+        break;
+    case 9:
+        c = "9";
+        break;
+    case 0:
+        c = "0";
+        break;
+    }
+    return c;
+}
+
+//Print to LCD function receives an integer as an input and print the number to the LCD display 
+//This function accepts values between 0 and 99999
+void PrintToLCD(int iCounter)
+{
+    //Check for out of range numbers
+    if (iCounter < 0 || iCounter > 99999)
+        return;
+        
+    char* c = "";
+    //case when the number is less than 10     
+    if(iCounter < 10)
+    {
+        c = ConvertIntToChar(iCounter);
+        lcd.locate(0, 1);
+        lcd.printf(c);
+    }
+    //case when the number is less than 100
+    else if (iCounter<100)
+    {
+        //devide the number by 10 to get the 10s
+        int i10s = iCounter/10;
+        c = ConvertIntToChar(i10s);
+        lcd.locate(0, 1);
+        lcd.printf(c);
+        //Get the leftover to print the 1s 
+        int i1s = iCounter%10; 
+        c = ConvertIntToChar(i1s);
+        lcd.locate(1, 1);
+        lcd.printf(c); 
+    }
+    else if (iCounter<1000)
+    {
+        //devide the number by 100 to get the 100s
+        int i100s = iCounter/100;
+        c = ConvertIntToChar(i100s);
+        lcd.locate(0, 1);
+        lcd.printf(c); 
+        //Get the leftover to compute the 10s and 1s
+        int iTemp = iCounter%100;
+        int i10s = iTemp/10;
+        c = ConvertIntToChar(i10s);
+        lcd.locate(1, 1);
+        lcd.printf(c);
+        int i1s = iTemp%10; 
+        c = ConvertIntToChar(i1s);
+        lcd.locate(2, 1);
+        lcd.printf(c); 
+    }
+    else if (iCounter<10000)
+    {
+        //devide the number by 1000 to get the 1000s
+        int i1000s = iCounter/1000;
+        c = ConvertIntToChar(i1000s);
+        lcd.locate(0, 1);
+        lcd.printf(c); 
+        //Get the leftover to compute the 100s, 10s and 1s
+        int iTemp100s = iCounter%1000;
+        int i100s = iTemp100s/100;
+        c = ConvertIntToChar(i100s);
+        lcd.locate(1, 1);
+        lcd.printf(c); 
+        //Get the leftover to compute the 10s and 1s
+        int iTemp10s = iTemp100s%100;
+        int i10s = iTemp10s/10;
+        c = ConvertIntToChar(i10s);
+        lcd.locate(2, 1);
+        lcd.printf(c);
+        int i1s = iTemp10s%10; 
+        c = ConvertIntToChar(i1s);
+        lcd.locate(3, 1);
+        lcd.printf(c); 
+    }          
+}
+
+//Main function
+int main() 
+{ 
+    //Clear the LCD display
+    lcd.cls();
+    //Locate Row 0 Column 0 in the LCD display
+    lcd.locate(0, 0);
+    //Print "The End"
+    lcd.printf("Start Counting:");    
+    //Iterate between 0 and 1000. 
+    for(int i=0 ; i<=1000 ; i++)
+    {
+        //Call PrintToLCD function
+        PrintToLCD(i);
+        //wait for 250ms
+        wait(0.25);
+    }
+    //Clear the LCD display
+    lcd.cls();
+    //Locate Row 0 Column 0 in the LCD display
+    lcd.locate(0, 0);
+    //Print "The End"
+    lcd.printf("The End!!!");    
+}