|
| 1 | +// |
| 2 | +// Nokia7110_430b.pde |
| 3 | +// Sketch |
| 4 | +// ---------------------------------- |
| 5 | +// Developed with embedXcode |
| 6 | +// |
| 7 | +// Project Nokia7110_430b |
| 8 | +// Created by Rei VILO on 26/05/12 |
| 9 | +// Copyright (c) 2012 http://embeddedcomputing.weebly.com |
| 10 | +// |
| 11 | + |
| 12 | +// |
| 13 | +// See ReadMe.txt for references |
| 14 | +// |
| 15 | + |
| 16 | +// Core library |
| 17 | +#if defined(__MSP430G2452__) || defined(__MSP430G2553__) || defined(__MSP430G2231__) // LaunchPad specific |
| 18 | +#include "Energia.h" |
| 19 | +#else // error |
| 20 | +#error Platform not supported |
| 21 | +#endif |
| 22 | + |
| 23 | +// Include application, user and local libraries |
| 24 | +#include "nokia7110_library.h" |
| 25 | + |
| 26 | +// from AnalogInput_InternalThermometer_430 |
| 27 | +#define NUMBER 8 // take number / 2 |
| 28 | +uint32_t average = 0; |
| 29 | +uint32_t values[NUMBER]; |
| 30 | +uint8_t j = 0; |
| 31 | +uint8_t i = 0; |
| 32 | +boolean flag = false; |
| 33 | + |
| 34 | +boolean backlight = true; |
| 35 | + |
| 36 | + |
| 37 | +// P1.0 Reset |
| 38 | +// P1.3 Temp Sensor (Not used) |
| 39 | +// P2.0 Serial Data |
| 40 | +// P2.1 Backlight |
| 41 | +// P2.2 Chip Select |
| 42 | +// P2.3 Data/Command |
| 43 | +// P2.4 Serial Clock |
| 44 | +// P2.5 Pushbutton |
| 45 | + |
| 46 | +// P1 |
| 47 | +static const unsigned LCD_RESET = BIT0; |
| 48 | +static const unsigned RXD = BIT2; |
| 49 | +static const unsigned SWITCH = BIT3; |
| 50 | + |
| 51 | +// P2 |
| 52 | +static const unsigned LCD_DATA = BIT0; |
| 53 | +static const unsigned LCD_BACKLIGHT = BIT1; |
| 54 | +static const unsigned LCD_CE = BIT2; |
| 55 | +static const unsigned LCD_DC = BIT3; |
| 56 | +static const unsigned LCD_CLK = BIT4; |
| 57 | +static const unsigned LCD_BTN = BIT5; |
| 58 | + |
| 59 | +// !!! template or class? |
| 60 | +Nokia7110<P2OUT, LCD_CLK, LCD_DATA, P2OUT, LCD_DC, P2OUT, LCD_CE, P1OUT, LCD_RESET, 10000> lcd; |
| 61 | + |
| 62 | +// Print integer from -999 to 9999 using 12 x 16 font |
| 63 | +void print_int(int i, const unsigned y) { |
| 64 | + if (i < -999 || i > 9999) return; |
| 65 | + const unsigned neg = i < 0; |
| 66 | + if (neg) i = -i; |
| 67 | + div_t d; |
| 68 | + d.quot = i; |
| 69 | + unsigned x = 48; |
| 70 | + do { |
| 71 | + d = div(d.quot, 10); |
| 72 | + lcd.pd12(d.rem, x -= 12, y); |
| 73 | + } |
| 74 | + while (d.quot); |
| 75 | + if (neg) lcd.pd12(14, x -= 12, y); |
| 76 | + while (x) lcd.pd12(10, x -= 12, y); |
| 77 | +} |
| 78 | + |
| 79 | + |
| 80 | +// Print integer from -999 to 9999 using 6 x 8 font |
| 81 | +void print_int(int i, unsigned x, const unsigned y) { |
| 82 | + if (i < -999 || i > 9999) return; |
| 83 | + const unsigned e = x; |
| 84 | + x += 24; |
| 85 | + const unsigned neg = i < 0; |
| 86 | + if (neg) i = -i; |
| 87 | + div_t d; |
| 88 | + d.quot = i; |
| 89 | + do { |
| 90 | + d = div(d.quot, 10); |
| 91 | + lcd.print(x -= 6, y, '0' + d.rem); |
| 92 | + } |
| 93 | + while (d.quot); |
| 94 | + if (neg) lcd.print(x -= 6, y, '-'); |
| 95 | + while (x > e) lcd.print(x -= 6, y, ' '); |
| 96 | +} |
| 97 | + |
| 98 | + |
| 99 | +// Print number with 1 decimal place passed as integer *10 |
| 100 | +// from -9999=-999.9 to 99999=9999.9 using 12 x 16 font |
| 101 | +void print_dec1(int ui, const unsigned y) { |
| 102 | + if (ui < -9999 || ui > 99999) return; |
| 103 | + boolean neg = (ui < 0); |
| 104 | + if (neg) ui = -ui; |
| 105 | + unsigned x = 60; |
| 106 | + |
| 107 | + lcd.pd12(ui%10, x -= 12, y); |
| 108 | + lcd.pd12(12, x -= 12, y); // . |
| 109 | + ui /= 10; |
| 110 | + |
| 111 | + do { |
| 112 | + lcd.pd12(ui%10, x -= 12, y); |
| 113 | + ui /= 10; |
| 114 | + } |
| 115 | + while (ui>0); |
| 116 | + |
| 117 | + if (neg) lcd.pd12(14, x -= 12, y); |
| 118 | + while (x) lcd.pd12(10, x -= 12, y); |
| 119 | +} |
| 120 | + |
| 121 | + |
| 122 | +void draw_bargraph(int f) { |
| 123 | + int x, y, bg; |
| 124 | + char c; |
| 125 | + unsigned char bgc[9] = { |
| 126 | + 0x80, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC, 0xFE, 0xFF, 0 }; |
| 127 | + char *bgl[8] = { |
| 128 | + "90", "80", "70", "60", "50", "40", "30", "20" }; |
| 129 | + |
| 130 | + x = 20; // |
| 131 | + bg = (f - (x - 5)) * 4 / 5; // |
| 132 | + for (y = 7; y >= 0; --y) { // |
| 133 | + lcd.pos(83, y); // |
| 134 | + if (bg < 0) { |
| 135 | + c = bgc[8]; |
| 136 | + } |
| 137 | + else if (bg > 7) { |
| 138 | + c = bgc[7]; |
| 139 | + } |
| 140 | + else { |
| 141 | + c = bgc[bg]; |
| 142 | + } |
| 143 | + lcd.write((unsigned char *)&c, 1, lcd_data); |
| 144 | + lcd.print(bgl[y], c); |
| 145 | + bg -= 8; |
| 146 | + } // |
| 147 | +} |
| 148 | + |
| 149 | + |
| 150 | +int x, y; |
| 151 | +char c; |
| 152 | + |
| 153 | + |
| 154 | +// Add setup code |
| 155 | +void setup() { |
| 156 | + pinMode(P1_0, OUTPUT); |
| 157 | + pinMode(P1_1, OUTPUT); |
| 158 | + pinMode(P1_2, OUTPUT); |
| 159 | + |
| 160 | + pinMode(P2_0, OUTPUT); |
| 161 | + pinMode(P2_1, OUTPUT); |
| 162 | + pinMode(P2_2, OUTPUT); |
| 163 | + pinMode(P2_3, OUTPUT); |
| 164 | + pinMode(P2_4, OUTPUT); |
| 165 | + |
| 166 | + pinMode(P2_5, INPUT_PULLUP); |
| 167 | + |
| 168 | + |
| 169 | + // from AnalogInput_InternalThermometer_430 |
| 170 | + analogReference(INTERNAL1V5); |
| 171 | + analogRead(TEMPSENSOR); // first reading usually wrong |
| 172 | + |
| 173 | + for (j=0; j<NUMBER; j++) values[j]=0; |
| 174 | + average = 0; |
| 175 | + j=0; |
| 176 | + |
| 177 | + lcd.reset(); |
| 178 | + lcd.init(); |
| 179 | + |
| 180 | + if (false) { |
| 181 | + lcd.clear(); |
| 182 | + lcd.print(30, 6, "MSP430"); |
| 183 | + lcd.print(18, 7, "Nokia 7110"); |
| 184 | + |
| 185 | + delay(3000); |
| 186 | + } |
| 187 | + |
| 188 | + if (false) { |
| 189 | + lcd.clear(); |
| 190 | + lcd.print(9, 7, "Character Set"); |
| 191 | + x = 0; |
| 192 | + y = 0; |
| 193 | + lcd.pos(x, y); |
| 194 | + for (c = 32; c < 127; ++c) { |
| 195 | + lcd.print(c); |
| 196 | + if (++x >= 16) x = 0, lcd.pos(x, ++y); |
| 197 | + } |
| 198 | + delay(3000); |
| 199 | + } |
| 200 | + |
| 201 | + if (false) { |
| 202 | + lcd.clear(); |
| 203 | + lcd.pd12(15, 48, 5); |
| 204 | + lcd.pd12(16, 59, 5); |
| 205 | + lcd.print(24, 7, "\x7F""C"); |
| 206 | + lcd.print(66, 7, "\x7F""K"); |
| 207 | + print_int(8888, 5); |
| 208 | + print_int(8888, 0, 7); |
| 209 | + print_int(8888, 42, 7); |
| 210 | + for(x = 15; x < 96; ++x) { |
| 211 | + draw_bargraph(x); |
| 212 | + __delay_cycles(200000); |
| 213 | + } |
| 214 | + delay(3000); |
| 215 | + } |
| 216 | + |
| 217 | + if (true) { |
| 218 | + lcd.clear(); // |
| 219 | + lcd.print(12, 0, "Nokia7110_430"); |
| 220 | + lcd.print(12, 1, "Thermometer"); |
| 221 | + lcd.pd12(15, 60, 3); // o Degrees |
| 222 | + lcd.pd12(16, 68, 3); // C |
| 223 | + } |
| 224 | +} |
| 225 | + |
| 226 | + |
| 227 | +// Add loop code |
| 228 | +void loop() { |
| 229 | + |
| 230 | + // from AnalogInput_InternalThermometer_430 |
| 231 | + if (i == 10) { |
| 232 | + i = 0; |
| 233 | + average -= values[j]; |
| 234 | + values[j] = ((uint32_t)analogRead(TEMPSENSOR)*27069 - 18169625) * 10 >> 16; |
| 235 | + average += values[j]; |
| 236 | + j++; |
| 237 | + if (j==NUMBER) flag=true; |
| 238 | + j %= NUMBER; |
| 239 | + |
| 240 | + if (flag) { |
| 241 | + print_dec1((average/NUMBER), 3); |
| 242 | + lcd.pos(0, 7); |
| 243 | + lcd.print(backlight ? "Light on " : "Light off"); |
| 244 | + } else { |
| 245 | + lcd.pos(0, 7); |
| 246 | + lcd.print("Wait "); |
| 247 | + lcd.pos(30, 7); |
| 248 | + lcd.print('0'+NUMBER-j); |
| 249 | + } |
| 250 | + } |
| 251 | + |
| 252 | + if (digitalRead(P2_5)==LOW) { |
| 253 | + while (digitalRead(P2_5)==LOW); // debounce |
| 254 | + backlight = ~backlight; |
| 255 | + lcd.pos(0, 7); |
| 256 | + lcd.print(backlight ? "Light on " : "Light off"); |
| 257 | + |
| 258 | + digitalWrite(P2_1, (backlight ? HIGH : LOW)); |
| 259 | + } |
| 260 | + delay(100); |
| 261 | + i++; |
| 262 | +} |
| 263 | + |
0 commit comments