Daniel Bromand
/
Counter_with_LCD_Display
Counter with LCD Display
main.cpp@0:1abd7e24ae31, 2011-06-28 (annotated)
- Committer:
- bromand
- Date:
- Tue Jun 28 15:10:44 2011 +0000
- Revision:
- 0:1abd7e24ae31
- Child:
- 1:f580beec166a
1.0.0.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bromand | 0:1abd7e24ae31 | 1 | //Include mbed header file which contains the definition of DigitalOut class. |
bromand | 0:1abd7e24ae31 | 2 | #include "mbed.h" |
bromand | 0:1abd7e24ae31 | 3 | //Include TextLCD header file which contains the definition of TextLCD class. |
bromand | 0:1abd7e24ae31 | 4 | #include "TextLCD.h" |
bromand | 0:1abd7e24ae31 | 5 | |
bromand | 0:1abd7e24ae31 | 6 | //Initialize TextLCD with the correct pins |
bromand | 0:1abd7e24ae31 | 7 | TextLCD lcd(p24, p26, p27, p28, p29, p30); |
bromand | 0:1abd7e24ae31 | 8 | |
bromand | 0:1abd7e24ae31 | 9 | //This function gets an integer and converts it to a charecter |
bromand | 0:1abd7e24ae31 | 10 | char* ConvertIntToChar(int i) |
bromand | 0:1abd7e24ae31 | 11 | { |
bromand | 0:1abd7e24ae31 | 12 | char* c = ""; |
bromand | 0:1abd7e24ae31 | 13 | switch (i) |
bromand | 0:1abd7e24ae31 | 14 | { |
bromand | 0:1abd7e24ae31 | 15 | case 1: |
bromand | 0:1abd7e24ae31 | 16 | c = "1"; |
bromand | 0:1abd7e24ae31 | 17 | break; |
bromand | 0:1abd7e24ae31 | 18 | case 2: |
bromand | 0:1abd7e24ae31 | 19 | c = "2"; |
bromand | 0:1abd7e24ae31 | 20 | break; |
bromand | 0:1abd7e24ae31 | 21 | case 3: |
bromand | 0:1abd7e24ae31 | 22 | c = "3"; |
bromand | 0:1abd7e24ae31 | 23 | break; |
bromand | 0:1abd7e24ae31 | 24 | case 4: |
bromand | 0:1abd7e24ae31 | 25 | c = "4"; |
bromand | 0:1abd7e24ae31 | 26 | break; |
bromand | 0:1abd7e24ae31 | 27 | case 5: |
bromand | 0:1abd7e24ae31 | 28 | c = "5"; |
bromand | 0:1abd7e24ae31 | 29 | break; |
bromand | 0:1abd7e24ae31 | 30 | case 6: |
bromand | 0:1abd7e24ae31 | 31 | c = "6"; |
bromand | 0:1abd7e24ae31 | 32 | break; |
bromand | 0:1abd7e24ae31 | 33 | case 7: |
bromand | 0:1abd7e24ae31 | 34 | c = "7"; |
bromand | 0:1abd7e24ae31 | 35 | break; |
bromand | 0:1abd7e24ae31 | 36 | case 8: |
bromand | 0:1abd7e24ae31 | 37 | c = "8"; |
bromand | 0:1abd7e24ae31 | 38 | break; |
bromand | 0:1abd7e24ae31 | 39 | case 9: |
bromand | 0:1abd7e24ae31 | 40 | c = "9"; |
bromand | 0:1abd7e24ae31 | 41 | break; |
bromand | 0:1abd7e24ae31 | 42 | case 0: |
bromand | 0:1abd7e24ae31 | 43 | c = "0"; |
bromand | 0:1abd7e24ae31 | 44 | break; |
bromand | 0:1abd7e24ae31 | 45 | } |
bromand | 0:1abd7e24ae31 | 46 | return c; |
bromand | 0:1abd7e24ae31 | 47 | } |
bromand | 0:1abd7e24ae31 | 48 | |
bromand | 0:1abd7e24ae31 | 49 | //Print to LCD function receives an integer as an input and print the number to the LCD display |
bromand | 0:1abd7e24ae31 | 50 | //This function accepts values between 0 and 99999 |
bromand | 0:1abd7e24ae31 | 51 | void PrintToLCD(int iCounter) |
bromand | 0:1abd7e24ae31 | 52 | { |
bromand | 0:1abd7e24ae31 | 53 | //Check for out of range numbers |
bromand | 0:1abd7e24ae31 | 54 | if (iCounter < 0 || iCounter > 99999) |
bromand | 0:1abd7e24ae31 | 55 | return; |
bromand | 0:1abd7e24ae31 | 56 | |
bromand | 0:1abd7e24ae31 | 57 | char* c = ""; |
bromand | 0:1abd7e24ae31 | 58 | //case when the number is less than 10 |
bromand | 0:1abd7e24ae31 | 59 | if(iCounter < 10) |
bromand | 0:1abd7e24ae31 | 60 | { |
bromand | 0:1abd7e24ae31 | 61 | c = ConvertIntToChar(iCounter); |
bromand | 0:1abd7e24ae31 | 62 | lcd.locate(0, 1); |
bromand | 0:1abd7e24ae31 | 63 | lcd.printf(c); |
bromand | 0:1abd7e24ae31 | 64 | } |
bromand | 0:1abd7e24ae31 | 65 | //case when the number is less than 100 |
bromand | 0:1abd7e24ae31 | 66 | else if (iCounter<100) |
bromand | 0:1abd7e24ae31 | 67 | { |
bromand | 0:1abd7e24ae31 | 68 | //devide the number by 10 to get the 10s |
bromand | 0:1abd7e24ae31 | 69 | int i10s = iCounter/10; |
bromand | 0:1abd7e24ae31 | 70 | c = ConvertIntToChar(i10s); |
bromand | 0:1abd7e24ae31 | 71 | lcd.locate(0, 1); |
bromand | 0:1abd7e24ae31 | 72 | lcd.printf(c); |
bromand | 0:1abd7e24ae31 | 73 | //Get the leftover to print the 1s |
bromand | 0:1abd7e24ae31 | 74 | int i1s = iCounter%10; |
bromand | 0:1abd7e24ae31 | 75 | c = ConvertIntToChar(i1s); |
bromand | 0:1abd7e24ae31 | 76 | lcd.locate(1, 1); |
bromand | 0:1abd7e24ae31 | 77 | lcd.printf(c); |
bromand | 0:1abd7e24ae31 | 78 | } |
bromand | 0:1abd7e24ae31 | 79 | else if (iCounter<1000) |
bromand | 0:1abd7e24ae31 | 80 | { |
bromand | 0:1abd7e24ae31 | 81 | //devide the number by 100 to get the 100s |
bromand | 0:1abd7e24ae31 | 82 | int i100s = iCounter/100; |
bromand | 0:1abd7e24ae31 | 83 | c = ConvertIntToChar(i100s); |
bromand | 0:1abd7e24ae31 | 84 | lcd.locate(0, 1); |
bromand | 0:1abd7e24ae31 | 85 | lcd.printf(c); |
bromand | 0:1abd7e24ae31 | 86 | //Get the leftover to compute the 10s and 1s |
bromand | 0:1abd7e24ae31 | 87 | int iTemp = iCounter%100; |
bromand | 0:1abd7e24ae31 | 88 | int i10s = iTemp/10; |
bromand | 0:1abd7e24ae31 | 89 | c = ConvertIntToChar(i10s); |
bromand | 0:1abd7e24ae31 | 90 | lcd.locate(1, 1); |
bromand | 0:1abd7e24ae31 | 91 | lcd.printf(c); |
bromand | 0:1abd7e24ae31 | 92 | int i1s = iTemp%10; |
bromand | 0:1abd7e24ae31 | 93 | c = ConvertIntToChar(i1s); |
bromand | 0:1abd7e24ae31 | 94 | lcd.locate(2, 1); |
bromand | 0:1abd7e24ae31 | 95 | lcd.printf(c); |
bromand | 0:1abd7e24ae31 | 96 | } |
bromand | 0:1abd7e24ae31 | 97 | else if (iCounter<10000) |
bromand | 0:1abd7e24ae31 | 98 | { |
bromand | 0:1abd7e24ae31 | 99 | //devide the number by 1000 to get the 1000s |
bromand | 0:1abd7e24ae31 | 100 | int i1000s = iCounter/1000; |
bromand | 0:1abd7e24ae31 | 101 | c = ConvertIntToChar(i1000s); |
bromand | 0:1abd7e24ae31 | 102 | lcd.locate(0, 1); |
bromand | 0:1abd7e24ae31 | 103 | lcd.printf(c); |
bromand | 0:1abd7e24ae31 | 104 | //Get the leftover to compute the 100s, 10s and 1s |
bromand | 0:1abd7e24ae31 | 105 | int iTemp100s = iCounter%1000; |
bromand | 0:1abd7e24ae31 | 106 | int i100s = iTemp100s/100; |
bromand | 0:1abd7e24ae31 | 107 | c = ConvertIntToChar(i100s); |
bromand | 0:1abd7e24ae31 | 108 | lcd.locate(1, 1); |
bromand | 0:1abd7e24ae31 | 109 | lcd.printf(c); |
bromand | 0:1abd7e24ae31 | 110 | //Get the leftover to compute the 10s and 1s |
bromand | 0:1abd7e24ae31 | 111 | int iTemp10s = iTemp100s%100; |
bromand | 0:1abd7e24ae31 | 112 | int i10s = iTemp10s/10; |
bromand | 0:1abd7e24ae31 | 113 | c = ConvertIntToChar(i10s); |
bromand | 0:1abd7e24ae31 | 114 | lcd.locate(2, 1); |
bromand | 0:1abd7e24ae31 | 115 | lcd.printf(c); |
bromand | 0:1abd7e24ae31 | 116 | int i1s = iTemp10s%10; |
bromand | 0:1abd7e24ae31 | 117 | c = ConvertIntToChar(i1s); |
bromand | 0:1abd7e24ae31 | 118 | lcd.locate(3, 1); |
bromand | 0:1abd7e24ae31 | 119 | lcd.printf(c); |
bromand | 0:1abd7e24ae31 | 120 | } |
bromand | 0:1abd7e24ae31 | 121 | } |
bromand | 0:1abd7e24ae31 | 122 | |
bromand | 0:1abd7e24ae31 | 123 | //Main function |
bromand | 0:1abd7e24ae31 | 124 | int main() |
bromand | 0:1abd7e24ae31 | 125 | { |
bromand | 0:1abd7e24ae31 | 126 | //Clear the LCD display |
bromand | 0:1abd7e24ae31 | 127 | lcd.cls(); |
bromand | 0:1abd7e24ae31 | 128 | //Locate Row 0 Column 0 in the LCD display |
bromand | 0:1abd7e24ae31 | 129 | lcd.locate(0, 0); |
bromand | 0:1abd7e24ae31 | 130 | //Print "The End" |
bromand | 0:1abd7e24ae31 | 131 | lcd.printf("Start Counting:"); |
bromand | 0:1abd7e24ae31 | 132 | //Iterate between 0 and 1000. |
bromand | 0:1abd7e24ae31 | 133 | for(int i=0 ; i<=1000 ; i++) |
bromand | 0:1abd7e24ae31 | 134 | { |
bromand | 0:1abd7e24ae31 | 135 | //Call PrintToLCD function |
bromand | 0:1abd7e24ae31 | 136 | PrintToLCD(i); |
bromand | 0:1abd7e24ae31 | 137 | //wait for 250ms |
bromand | 0:1abd7e24ae31 | 138 | wait(0.25); |
bromand | 0:1abd7e24ae31 | 139 | } |
bromand | 0:1abd7e24ae31 | 140 | //Clear the LCD display |
bromand | 0:1abd7e24ae31 | 141 | lcd.cls(); |
bromand | 0:1abd7e24ae31 | 142 | //Locate Row 0 Column 0 in the LCD display |
bromand | 0:1abd7e24ae31 | 143 | lcd.locate(0, 0); |
bromand | 0:1abd7e24ae31 | 144 | //Print "The End" |
bromand | 0:1abd7e24ae31 | 145 | lcd.printf("The End!!!"); |
bromand | 0:1abd7e24ae31 | 146 | } |