conehead NZ / Mbed 2 deprecated Ass3

Dependencies:   mbed

Committer:
conehead
Date:
Sun Oct 01 07:04:57 2017 +0000
Revision:
0:9f02a450001f
V1;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
conehead 0:9f02a450001f 1 // Damon Hill - 14234098 - 159.270
conehead 0:9f02a450001f 2 // Assignment 3 - Counters and LCD
conehead 0:9f02a450001f 3
conehead 0:9f02a450001f 4 #include "mbed.h"
conehead 0:9f02a450001f 5 #define LCD_DATA 1
conehead 0:9f02a450001f 6 #define LCD_INSTRUCTION 0
conehead 0:9f02a450001f 7
conehead 0:9f02a450001f 8 static void lcdSetRS(int mode); // LCD functions
conehead 0:9f02a450001f 9 static void lcdPulseEN(void);
conehead 0:9f02a450001f 10 void lcdCommand(unsigned char command);
conehead 0:9f02a450001f 11 void lcdPutChar(unsigned char c);
conehead 0:9f02a450001f 12 void lcdString(char *s);
conehead 0:9f02a450001f 13 int waitButton(unsigned int n); // wait for button press or timeout function
conehead 0:9f02a450001f 14 void bin(void); // binary counter function
conehead 0:9f02a450001f 15 void dec(void); // decimal counter function
conehead 0:9f02a450001f 16 void hex(void); // hexidecimal counter function
conehead 0:9f02a450001f 17
conehead 0:9f02a450001f 18 DigitalOut lcdD4(A2), lcdD5(A3), lcdD6(A4), lcdD7(A5); // LCD outputs
conehead 0:9f02a450001f 19 DigitalOut lcdEN(A1), lcdRS(A0);
conehead 0:9f02a450001f 20 DigitalIn button (USER_BUTTON); // button input
conehead 0:9f02a450001f 21
conehead 0:9f02a450001f 22 Timer t;
conehead 0:9f02a450001f 23
conehead 0:9f02a450001f 24 int main()
conehead 0:9f02a450001f 25 {
conehead 0:9f02a450001f 26 lcdEN.write(0); //-- GPIO_WriteBit(GPIOC, LCD_EN, Bit_RESET);
conehead 0:9f02a450001f 27 wait_us(15000); //-- delay for >15msec second after power on
conehead 0:9f02a450001f 28 lcdCommand(0x28); // 4bit mode, dual line
conehead 0:9f02a450001f 29 lcdCommand(0x01); //-- display clear
conehead 0:9f02a450001f 30 wait_us(2000); // 2msec delay for screen to react
conehead 0:9f02a450001f 31 lcdCommand(0x06); //-- cursor increments
conehead 0:9f02a450001f 32 lcdCommand(0x0c); //-- display on, cursor off
conehead 0:9f02a450001f 33
conehead 0:9f02a450001f 34 unsigned short int b = 0; // variable for wait function output, 1 if button was pressed within time
conehead 0:9f02a450001f 35
conehead 0:9f02a450001f 36 while(1) { // two second count loop
conehead 0:9f02a450001f 37 lcdString("Binary");
conehead 0:9f02a450001f 38 b = waitButton(2000); // waits for 2 sec
conehead 0:9f02a450001f 39 if (b == 1) { // if button is pressed within time
conehead 0:9f02a450001f 40 bin(); // count in binary
conehead 0:9f02a450001f 41 }
conehead 0:9f02a450001f 42 lcdString("Decimal");
conehead 0:9f02a450001f 43 b = waitButton(2000);
conehead 0:9f02a450001f 44 if (b == 1) {
conehead 0:9f02a450001f 45 dec();
conehead 0:9f02a450001f 46 }
conehead 0:9f02a450001f 47 lcdString("Hexideicmal");
conehead 0:9f02a450001f 48 b = waitButton(2000);
conehead 0:9f02a450001f 49 if (b == 1) {
conehead 0:9f02a450001f 50 hex();
conehead 0:9f02a450001f 51 }
conehead 0:9f02a450001f 52 }
conehead 0:9f02a450001f 53 }
conehead 0:9f02a450001f 54
conehead 0:9f02a450001f 55 void lcdString(char *s) // from serial slides
conehead 0:9f02a450001f 56 {
conehead 0:9f02a450001f 57 lcdCommand(0x01); // clear the screen
conehead 0:9f02a450001f 58 wait_ms(2);
conehead 0:9f02a450001f 59
conehead 0:9f02a450001f 60 register unsigned short int i = 0;
conehead 0:9f02a450001f 61 while (s[i] != '\0') { // go through all char until it reaches the end null char
conehead 0:9f02a450001f 62 lcdPutChar(s[i]); // send individually
conehead 0:9f02a450001f 63 i++; // do next one
conehead 0:9f02a450001f 64 }
conehead 0:9f02a450001f 65 }
conehead 0:9f02a450001f 66
conehead 0:9f02a450001f 67 int waitButton(unsigned int n)
conehead 0:9f02a450001f 68 {
conehead 0:9f02a450001f 69 t.reset(); // reset timer
conehead 0:9f02a450001f 70 if (n > 0) {
conehead 0:9f02a450001f 71 t.start(); // if the time is not 0, start the timer
conehead 0:9f02a450001f 72 }
conehead 0:9f02a450001f 73
conehead 0:9f02a450001f 74 while (button == 0) { // Waits for user to release the button before program continues
conehead 0:9f02a450001f 75 }
conehead 0:9f02a450001f 76
conehead 0:9f02a450001f 77 while (t.read_ms() <= n) {
conehead 0:9f02a450001f 78 if (button == 0) { // if button is pressed
conehead 0:9f02a450001f 79 t.stop(); // stop timer
conehead 0:9f02a450001f 80 return(1); // return 1, indicating button pressed within the time elapsed
conehead 0:9f02a450001f 81 }
conehead 0:9f02a450001f 82 wait_ms(10); // wait a little bit
conehead 0:9f02a450001f 83 }
conehead 0:9f02a450001f 84 t.stop(); // stop timer
conehead 0:9f02a450001f 85 return(0); // return 0, indicating NO button pressed within the time elapsed
conehead 0:9f02a450001f 86 }
conehead 0:9f02a450001f 87
conehead 0:9f02a450001f 88 void bin()
conehead 0:9f02a450001f 89 {
conehead 0:9f02a450001f 90 unsigned int i,n,c,f; // initilize all local var
conehead 0:9f02a450001f 91
conehead 0:9f02a450001f 92 for (i = 0; i < 16; i++) { // creates a 0-15 count loop
conehead 0:9f02a450001f 93 lcdCommand(0x01); // clear the screen
conehead 0:9f02a450001f 94 wait_ms(2);
conehead 0:9f02a450001f 95 c = 8; // MSB decimal value
conehead 0:9f02a450001f 96 n = i; // temp variable to edit the count number
conehead 0:9f02a450001f 97 f = 0; // restart bit counter
conehead 0:9f02a450001f 98 while (f < 4) { // for the 4 bits
conehead 0:9f02a450001f 99 if (n >= c) { // if the count is equal or more than the current bit (power of two)
conehead 0:9f02a450001f 100 lcdPutChar('1'); // print 1
conehead 0:9f02a450001f 101 n = n-c; // minus the power from the count
conehead 0:9f02a450001f 102 }
conehead 0:9f02a450001f 103 else {
conehead 0:9f02a450001f 104 lcdPutChar('0'); // else print a 0
conehead 0:9f02a450001f 105 }
conehead 0:9f02a450001f 106 c = c/2; // get next power of 2
conehead 0:9f02a450001f 107 f++; // do next bit
conehead 0:9f02a450001f 108 }
conehead 0:9f02a450001f 109 waitButton(0); // wait forever till a button is pressed
conehead 0:9f02a450001f 110 }
conehead 0:9f02a450001f 111 }
conehead 0:9f02a450001f 112
conehead 0:9f02a450001f 113 void dec()
conehead 0:9f02a450001f 114 {
conehead 0:9f02a450001f 115 unsigned short int i;
conehead 0:9f02a450001f 116
conehead 0:9f02a450001f 117 for (i = 0; i < 16; i++) {
conehead 0:9f02a450001f 118 lcdCommand(0x01); // clear the screen
conehead 0:9f02a450001f 119 wait_ms(2);
conehead 0:9f02a450001f 120 if (i > 9) { // for count greater than 9
conehead 0:9f02a450001f 121 lcdPutChar('1'); // print 1 (for the 10s)
conehead 0:9f02a450001f 122 lcdPutChar(i-10+ '0'); // print 1s value.
conehead 0:9f02a450001f 123 }
conehead 0:9f02a450001f 124 else {
conehead 0:9f02a450001f 125 lcdPutChar(i + '0'); // ASCII '0' + an integer, will give the ASCII value of an integer from 0-9
conehead 0:9f02a450001f 126 }
conehead 0:9f02a450001f 127 waitButton(0); // wait forever till a button is pressed
conehead 0:9f02a450001f 128 }
conehead 0:9f02a450001f 129 }
conehead 0:9f02a450001f 130
conehead 0:9f02a450001f 131 void hex()
conehead 0:9f02a450001f 132 {
conehead 0:9f02a450001f 133 unsigned short int i;
conehead 0:9f02a450001f 134
conehead 0:9f02a450001f 135 for (i = 0; i < 16; i++) {
conehead 0:9f02a450001f 136 lcdCommand(0x01); // clear the screen
conehead 0:9f02a450001f 137 wait_ms(2);
conehead 0:9f02a450001f 138 if (i > 9) { // for count greater than 9
conehead 0:9f02a450001f 139 lcdPutChar(i+7+ '0'); // get the ASCII value for A-F
conehead 0:9f02a450001f 140 }
conehead 0:9f02a450001f 141 else {
conehead 0:9f02a450001f 142 lcdPutChar(i + '0');
conehead 0:9f02a450001f 143 }
conehead 0:9f02a450001f 144 waitButton(0); // wait forever till a button is pressed
conehead 0:9f02a450001f 145 }
conehead 0:9f02a450001f 146 }
conehead 0:9f02a450001f 147
conehead 0:9f02a450001f 148
conehead 0:9f02a450001f 149 //====[Almost unchanged sample code from lecture slides]======
conehead 0:9f02a450001f 150 // || || ||
conehead 0:9f02a450001f 151 // \/ \/ \/
conehead 0:9f02a450001f 152 static void lcdSetRS(int mode)
conehead 0:9f02a450001f 153 {
conehead 0:9f02a450001f 154 lcdRS.write(mode);
conehead 0:9f02a450001f 155 }
conehead 0:9f02a450001f 156 static void lcdPulseEN(void)
conehead 0:9f02a450001f 157 {
conehead 0:9f02a450001f 158 lcdEN.write(1);
conehead 0:9f02a450001f 159 wait_us(1); //-- enable pulse must be >450ns
conehead 0:9f02a450001f 160 lcdEN.write(0);
conehead 0:9f02a450001f 161 wait_us(1);
conehead 0:9f02a450001f 162 }
conehead 0:9f02a450001f 163
conehead 0:9f02a450001f 164 void lcdCommand(unsigned char command)
conehead 0:9f02a450001f 165 {
conehead 0:9f02a450001f 166 lcdSetRS(LCD_INSTRUCTION);
conehead 0:9f02a450001f 167 lcdD4.write((command>>4) & 0x01);
conehead 0:9f02a450001f 168 lcdD5.write((command>>5) & 0x01);
conehead 0:9f02a450001f 169 lcdD6.write((command>>6) & 0x01);
conehead 0:9f02a450001f 170 lcdD7.write((command>>7) & 0x01);
conehead 0:9f02a450001f 171 lcdPulseEN(); //-- this can't be too slow or it will time out
conehead 0:9f02a450001f 172 lcdD4.write(command & 0x01);
conehead 0:9f02a450001f 173 lcdD5.write((command>>1) & 0x01);
conehead 0:9f02a450001f 174 lcdD6.write((command>>2) & 0x01);
conehead 0:9f02a450001f 175 lcdD7.write((command>>3) & 0x01);
conehead 0:9f02a450001f 176 lcdPulseEN();
conehead 0:9f02a450001f 177 wait_us(37); //-- let it work on the data
conehead 0:9f02a450001f 178 }
conehead 0:9f02a450001f 179
conehead 0:9f02a450001f 180 void lcdPutChar(unsigned char c)
conehead 0:9f02a450001f 181 {
conehead 0:9f02a450001f 182 lcdSetRS(LCD_DATA);
conehead 0:9f02a450001f 183 lcdD4.write((c>>4) & 0x01);
conehead 0:9f02a450001f 184 lcdD5.write((c>>5) & 0x01);
conehead 0:9f02a450001f 185 lcdD6.write((c>>6) & 0x01);
conehead 0:9f02a450001f 186 lcdD7.write((c>>7) & 0x01);
conehead 0:9f02a450001f 187 lcdPulseEN(); //-- this can't be too slow or it will time out
conehead 0:9f02a450001f 188 lcdD4.write(c & 0x01);
conehead 0:9f02a450001f 189 lcdD5.write((c>>1) & 0x01);
conehead 0:9f02a450001f 190 lcdD6.write((c>>2) & 0x01);
conehead 0:9f02a450001f 191 lcdD7.write((c>>3) & 0x01);
conehead 0:9f02a450001f 192 lcdPulseEN();
conehead 0:9f02a450001f 193 wait_us(37); //-- let it work on the data
conehead 0:9f02a450001f 194 }
conehead 0:9f02a450001f 195
conehead 0:9f02a450001f 196
conehead 0:9f02a450001f 197