Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Fork of WaG by
display.cpp@14:be27f6e21a8a, 2018-02-27 (annotated)
- Committer:
- spm71
- Date:
- Tue Feb 27 17:06:37 2018 +0000
- Revision:
- 14:be27f6e21a8a
- Parent:
- 13:8936b2f64aa2
- Child:
- 15:100321eb2aac
Updated code for tracking input.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
phn10 | 7:161fe3793ddb | 1 | /****************************************************************************** |
phn10 | 7:161fe3793ddb | 2 | * EECS 397 |
phn10 | 7:161fe3793ddb | 3 | * |
phn10 | 7:161fe3793ddb | 4 | * Assignment Name: Lab 4: display_test2 |
phn10 | 7:161fe3793ddb | 5 | * |
phn10 | 7:161fe3793ddb | 6 | * Authors: Sam Morrison and Phong Nguyen |
phn10 | 7:161fe3793ddb | 7 | * File name: display.cpp |
phn10 | 7:161fe3793ddb | 8 | * Purpose: Contain function definitions |
phn10 | 7:161fe3793ddb | 9 | * |
phn10 | 7:161fe3793ddb | 10 | * Created: 02/21/2018 |
phn10 | 7:161fe3793ddb | 11 | * Last Modified: 02/21/2018 |
phn10 | 7:161fe3793ddb | 12 | * |
phn10 | 7:161fe3793ddb | 13 | ******************************************************************************/ |
phn10 | 8:d8bc78bda829 | 14 | #include "mbed.h" |
phn10 | 8:d8bc78bda829 | 15 | #include "io_pins.h" |
phn10 | 8:d8bc78bda829 | 16 | #include "display.h" |
phn10 | 8:d8bc78bda829 | 17 | #include <stdlib.h> |
phn10 | 8:d8bc78bda829 | 18 | #include <stdio.h> |
phn10 | 8:d8bc78bda829 | 19 | #include <string.h> |
phn10 | 8:d8bc78bda829 | 20 | |
spm71 | 14:be27f6e21a8a | 21 | //#define VERSION1 |
spm71 | 14:be27f6e21a8a | 22 | #define VERSION2 |
spm71 | 10:ae0a262ba48d | 23 | |
phn10 | 8:d8bc78bda829 | 24 | |
phn10 | 8:d8bc78bda829 | 25 | /* |
phn10 | 8:d8bc78bda829 | 26 | * void initial_setup(DigitalOut SS, int data_length, int frequency); |
phn10 | 8:d8bc78bda829 | 27 | * Description: setup spi data length (in bit), spi frequency, set LED |
phn10 | 8:d8bc78bda829 | 28 | * display to normal operation, and set all display numbers |
phn10 | 8:d8bc78bda829 | 29 | * to 0. |
phn10 | 8:d8bc78bda829 | 30 | * |
phn10 | 8:d8bc78bda829 | 31 | * Inputs: |
phn10 | 8:d8bc78bda829 | 32 | * Parameters: |
phn10 | 8:d8bc78bda829 | 33 | * SPI spi: spi object |
phn10 | 8:d8bc78bda829 | 34 | * DigitalOut SS: chip select pin |
phn10 | 8:d8bc78bda829 | 35 | * int data_length: spi data length in bit |
phn10 | 8:d8bc78bda829 | 36 | * int frequency: frequency of SPI communication |
phn10 | 8:d8bc78bda829 | 37 | * |
phn10 | 8:d8bc78bda829 | 38 | * Outputs: |
phn10 | 8:d8bc78bda829 | 39 | * Returns: void |
phn10 | 8:d8bc78bda829 | 40 | */ |
phn10 | 8:d8bc78bda829 | 41 | void initial_setup(DigitalOut SS, int data_length, int frequency) { |
phn10 | 8:d8bc78bda829 | 42 | SS = 1; |
phn10 | 8:d8bc78bda829 | 43 | spi.format(data_length, 0); |
phn10 | 8:d8bc78bda829 | 44 | spi.frequency(frequency); |
phn10 | 8:d8bc78bda829 | 45 | |
phn10 | 8:d8bc78bda829 | 46 | SS = 0; |
phn10 | 8:d8bc78bda829 | 47 | spi.write(0x0C01); //normal operation |
phn10 | 8:d8bc78bda829 | 48 | SS = 1; |
phn10 | 8:d8bc78bda829 | 49 | |
phn10 | 8:d8bc78bda829 | 50 | SS = 0; |
phn10 | 8:d8bc78bda829 | 51 | spi.write(0x090F); //decode to bits 0:3 |
phn10 | 8:d8bc78bda829 | 52 | SS = 1; |
phn10 | 8:d8bc78bda829 | 53 | |
phn10 | 8:d8bc78bda829 | 54 | SS = 0; |
phn10 | 8:d8bc78bda829 | 55 | spi.write(0x0F00); //set to normal mode |
phn10 | 8:d8bc78bda829 | 56 | SS = 1; |
phn10 | 8:d8bc78bda829 | 57 | |
phn10 | 8:d8bc78bda829 | 58 | SS = 0; |
phn10 | 8:d8bc78bda829 | 59 | spi.write(0x0A0F); //intensity set to max |
phn10 | 8:d8bc78bda829 | 60 | SS = 1; |
phn10 | 8:d8bc78bda829 | 61 | |
phn10 | 8:d8bc78bda829 | 62 | SS = 0; |
phn10 | 8:d8bc78bda829 | 63 | spi.write(0x0B04); //display digits 0:4 |
phn10 | 8:d8bc78bda829 | 64 | SS = 1; |
phn10 | 8:d8bc78bda829 | 65 | |
phn10 | 8:d8bc78bda829 | 66 | SS = 0; |
phn10 | 8:d8bc78bda829 | 67 | spi.write(0x0100); //set digit 1 to 0 |
phn10 | 8:d8bc78bda829 | 68 | SS = 1; |
phn10 | 8:d8bc78bda829 | 69 | |
phn10 | 8:d8bc78bda829 | 70 | SS = 0; |
phn10 | 8:d8bc78bda829 | 71 | spi.write(0x0200); //set digit 2 to 0 |
phn10 | 8:d8bc78bda829 | 72 | SS = 1; |
phn10 | 8:d8bc78bda829 | 73 | |
phn10 | 8:d8bc78bda829 | 74 | SS = 0; |
phn10 | 8:d8bc78bda829 | 75 | spi.write(0x0300); //set digit 3 to 0 |
phn10 | 8:d8bc78bda829 | 76 | SS = 1; |
phn10 | 8:d8bc78bda829 | 77 | |
phn10 | 8:d8bc78bda829 | 78 | SS = 0; |
phn10 | 8:d8bc78bda829 | 79 | spi.write(0x0400); //set digit 4 to 0 |
phn10 | 8:d8bc78bda829 | 80 | SS = 1; |
phn10 | 8:d8bc78bda829 | 81 | |
phn10 | 8:d8bc78bda829 | 82 | SS = 0; |
phn10 | 8:d8bc78bda829 | 83 | spi.write(0x0500); //set digit 5 to 0 |
phn10 | 8:d8bc78bda829 | 84 | SS = 1; |
phn10 | 8:d8bc78bda829 | 85 | } |
phn10 | 8:d8bc78bda829 | 86 | |
spm71 | 10:ae0a262ba48d | 87 | |
spm71 | 10:ae0a262ba48d | 88 | #ifdef VERSION1 |
phn10 | 8:d8bc78bda829 | 89 | /* |
spm71 | 10:ae0a262ba48d | 90 | * void bin2bcd_array(int num); |
spm71 | 13:8936b2f64aa2 | 91 | * Description: Converts to BCD array using modulo method. |
phn10 | 8:d8bc78bda829 | 92 | * |
phn10 | 8:d8bc78bda829 | 93 | * Inputs: |
phn10 | 8:d8bc78bda829 | 94 | * Parameters: |
spm71 | 13:8936b2f64aa2 | 95 | * int num: number to push to display |
spm71 | 13:8936b2f64aa2 | 96 | char bcd[]: BCD array that will be written to |
phn10 | 8:d8bc78bda829 | 97 | * Globals: |
phn10 | 8:d8bc78bda829 | 98 | * |
phn10 | 8:d8bc78bda829 | 99 | * Outputs: |
spm71 | 13:8936b2f64aa2 | 100 | * Parameters: |
phn10 | 8:d8bc78bda829 | 101 | * Globals: |
phn10 | 8:d8bc78bda829 | 102 | * Returns: void |
phn10 | 8:d8bc78bda829 | 103 | */ |
spm71 | 10:ae0a262ba48d | 104 | void bin2bcd_array(int num, char bcd[]) { |
spm71 | 13:8936b2f64aa2 | 105 | int place = 0; |
phn10 | 7:161fe3793ddb | 106 | while (num != 0) { //converts decimal input to decimal array using %mod |
phn10 | 7:161fe3793ddb | 107 | int val = num % 10; |
spm71 | 12:a947a6609a23 | 108 | bcd[place] = val; |
phn10 | 7:161fe3793ddb | 109 | num = num/10; |
spm71 | 13:8936b2f64aa2 | 110 | place++; |
phn10 | 7:161fe3793ddb | 111 | } |
phn10 | 7:161fe3793ddb | 112 | } |
spm71 | 10:ae0a262ba48d | 113 | #endif |
phn10 | 7:161fe3793ddb | 114 | |
phn10 | 8:d8bc78bda829 | 115 | /* |
phn10 | 8:d8bc78bda829 | 116 | * int convert(int dec); |
phn10 | 8:d8bc78bda829 | 117 | * Description: |
phn10 | 8:d8bc78bda829 | 118 | * |
phn10 | 8:d8bc78bda829 | 119 | * Inputs: |
phn10 | 8:d8bc78bda829 | 120 | * Parameters: |
phn10 | 8:d8bc78bda829 | 121 | * int dec: |
phn10 | 8:d8bc78bda829 | 122 | * Globals: |
phn10 | 8:d8bc78bda829 | 123 | * |
phn10 | 8:d8bc78bda829 | 124 | * Outputs: |
phn10 | 8:d8bc78bda829 | 125 | * Parameters: |
phn10 | 8:d8bc78bda829 | 126 | * Globals: |
phn10 | 8:d8bc78bda829 | 127 | * Returns: void |
phn10 | 8:d8bc78bda829 | 128 | */ |
phn10 | 7:161fe3793ddb | 129 | int convert(int dec) {//convert decimal to binary |
phn10 | 7:161fe3793ddb | 130 | if (dec == 0) //function complete |
phn10 | 7:161fe3793ddb | 131 | return 0; |
phn10 | 7:161fe3793ddb | 132 | else //recursive call until converted |
spm71 | 10:ae0a262ba48d | 133 | return (dec % 2 + 10 * convert(dec / 2) + ' '); |
phn10 | 7:161fe3793ddb | 134 | } |
phn10 | 7:161fe3793ddb | 135 | |
spm71 | 10:ae0a262ba48d | 136 | #ifdef VERSION2 |
phn10 | 8:d8bc78bda829 | 137 | /* |
phn10 | 9:06c0d5737e5c | 138 | * void bin2bcd_array(int num); |
phn10 | 9:06c0d5737e5c | 139 | * Description: converts a number from binary format to binary coded |
phn10 | 9:06c0d5737e5c | 140 | * decimal array using sprintf() method |
phn10 | 9:06c0d5737e5c | 141 | * |
phn10 | 9:06c0d5737e5c | 142 | * Inputs: |
phn10 | 9:06c0d5737e5c | 143 | * Parameters: |
phn10 | 9:06c0d5737e5c | 144 | * int num: number in binary format |
phn10 | 9:06c0d5737e5c | 145 | * int &bcd: pointer to bcd (binary coded decimal) array |
phn10 | 9:06c0d5737e5c | 146 | * |
phn10 | 9:06c0d5737e5c | 147 | * Outputs: |
phn10 | 9:06c0d5737e5c | 148 | * Returns: void |
phn10 | 9:06c0d5737e5c | 149 | */ |
spm71 | 10:ae0a262ba48d | 150 | void bin2bcd_array(int num, char bcd[]) { |
phn10 | 9:06c0d5737e5c | 151 | char tmp_array[4]; |
phn10 | 11:6751b9406142 | 152 | char reverse_bcd[4]; |
phn10 | 11:6751b9406142 | 153 | int last_index = 0; |
phn10 | 9:06c0d5737e5c | 154 | int i = 0; |
phn10 | 11:6751b9406142 | 155 | |
phn10 | 11:6751b9406142 | 156 | sprintf(tmp_array, "%d", num); |
phn10 | 11:6751b9406142 | 157 | for (i = 0; i < 4; i++) { |
phn10 | 11:6751b9406142 | 158 | bcd[i] = 0; |
phn10 | 11:6751b9406142 | 159 | reverse_bcd[i] = 0; |
phn10 | 11:6751b9406142 | 160 | } |
phn10 | 11:6751b9406142 | 161 | |
phn10 | 11:6751b9406142 | 162 | while (last_index < 4 and tmp_array[last_index] != '\0') { |
phn10 | 11:6751b9406142 | 163 | reverse_bcd[last_index] = tmp_array[last_index] - '0'; |
phn10 | 11:6751b9406142 | 164 | last_index++; |
phn10 | 11:6751b9406142 | 165 | } |
phn10 | 11:6751b9406142 | 166 | |
phn10 | 11:6751b9406142 | 167 | // reverse the order to fit binary coded decimal |
phn10 | 11:6751b9406142 | 168 | for (i = 0; i < last_index; i++) { |
phn10 | 11:6751b9406142 | 169 | bcd[i] = reverse_bcd[last_index - 1 - i]; |
phn10 | 9:06c0d5737e5c | 170 | } |
phn10 | 9:06c0d5737e5c | 171 | } |
spm71 | 10:ae0a262ba48d | 172 | #endif |
phn10 | 9:06c0d5737e5c | 173 | |
phn10 | 11:6751b9406142 | 174 | void send_command_to_display(char bcd[]) { |
phn10 | 11:6751b9406142 | 175 | int command; |
phn10 | 11:6751b9406142 | 176 | |
phn10 | 11:6751b9406142 | 177 | |
phn10 | 11:6751b9406142 | 178 | for (int i = 1; i <= 4; i++) { |
phn10 | 11:6751b9406142 | 179 | command = 0; |
phn10 | 11:6751b9406142 | 180 | // command has following form: 0x0[place]0[value] |
phn10 | 11:6751b9406142 | 181 | // the value converted to int is 16 * 16 * place + value |
phn10 | 11:6751b9406142 | 182 | command = 16 * 16 * i + bcd[i - 1]; |
phn10 | 11:6751b9406142 | 183 | |
phn10 | 11:6751b9406142 | 184 | SS = 0; |
phn10 | 11:6751b9406142 | 185 | spi.write(command); |
phn10 | 11:6751b9406142 | 186 | SS = 1; |
phn10 | 11:6751b9406142 | 187 | } |
phn10 | 7:161fe3793ddb | 188 | } |