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@15:100321eb2aac, 2018-02-27 (annotated)
- Committer:
- phn10
- Date:
- Tue Feb 27 17:12:15 2018 +0000
- Revision:
- 15:100321eb2aac
- Parent:
- 14:be27f6e21a8a
- Child:
- 16:dfa9eb1a808d
add function comment description
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 | 15:100321eb2aac | 11 | * Last Modified: 02/27/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: |
| phn10 | 8:d8bc78bda829 | 100 | * Returns: void |
| phn10 | 8:d8bc78bda829 | 101 | */ |
| spm71 | 10:ae0a262ba48d | 102 | void bin2bcd_array(int num, char bcd[]) { |
| spm71 | 13:8936b2f64aa2 | 103 | int place = 0; |
| phn10 | 7:161fe3793ddb | 104 | while (num != 0) { //converts decimal input to decimal array using %mod |
| phn10 | 7:161fe3793ddb | 105 | int val = num % 10; |
| spm71 | 12:a947a6609a23 | 106 | bcd[place] = val; |
| phn10 | 7:161fe3793ddb | 107 | num = num/10; |
| spm71 | 13:8936b2f64aa2 | 108 | place++; |
| phn10 | 7:161fe3793ddb | 109 | } |
| phn10 | 7:161fe3793ddb | 110 | } |
| spm71 | 10:ae0a262ba48d | 111 | #endif |
| phn10 | 7:161fe3793ddb | 112 | |
| spm71 | 10:ae0a262ba48d | 113 | #ifdef VERSION2 |
| phn10 | 8:d8bc78bda829 | 114 | /* |
| phn10 | 9:06c0d5737e5c | 115 | * void bin2bcd_array(int num); |
| phn10 | 9:06c0d5737e5c | 116 | * Description: converts a number from binary format to binary coded |
| phn10 | 9:06c0d5737e5c | 117 | * decimal array using sprintf() method |
| phn10 | 9:06c0d5737e5c | 118 | * |
| phn10 | 9:06c0d5737e5c | 119 | * Inputs: |
| phn10 | 9:06c0d5737e5c | 120 | * Parameters: |
| phn10 | 9:06c0d5737e5c | 121 | * int num: number in binary format |
| phn10 | 15:100321eb2aac | 122 | * char bcd[]: bcd (binary coded decimal) array |
| phn10 | 9:06c0d5737e5c | 123 | * |
| phn10 | 9:06c0d5737e5c | 124 | * Outputs: |
| phn10 | 9:06c0d5737e5c | 125 | * Returns: void |
| phn10 | 9:06c0d5737e5c | 126 | */ |
| spm71 | 10:ae0a262ba48d | 127 | void bin2bcd_array(int num, char bcd[]) { |
| phn10 | 9:06c0d5737e5c | 128 | char tmp_array[4]; |
| phn10 | 11:6751b9406142 | 129 | char reverse_bcd[4]; |
| phn10 | 11:6751b9406142 | 130 | int last_index = 0; |
| phn10 | 9:06c0d5737e5c | 131 | int i = 0; |
| phn10 | 11:6751b9406142 | 132 | |
| phn10 | 11:6751b9406142 | 133 | sprintf(tmp_array, "%d", num); |
| phn10 | 11:6751b9406142 | 134 | for (i = 0; i < 4; i++) { |
| phn10 | 11:6751b9406142 | 135 | bcd[i] = 0; |
| phn10 | 11:6751b9406142 | 136 | reverse_bcd[i] = 0; |
| phn10 | 11:6751b9406142 | 137 | } |
| phn10 | 11:6751b9406142 | 138 | |
| phn10 | 11:6751b9406142 | 139 | while (last_index < 4 and tmp_array[last_index] != '\0') { |
| phn10 | 11:6751b9406142 | 140 | reverse_bcd[last_index] = tmp_array[last_index] - '0'; |
| phn10 | 11:6751b9406142 | 141 | last_index++; |
| phn10 | 11:6751b9406142 | 142 | } |
| phn10 | 11:6751b9406142 | 143 | |
| phn10 | 11:6751b9406142 | 144 | // reverse the order to fit binary coded decimal |
| phn10 | 11:6751b9406142 | 145 | for (i = 0; i < last_index; i++) { |
| phn10 | 11:6751b9406142 | 146 | bcd[i] = reverse_bcd[last_index - 1 - i]; |
| phn10 | 9:06c0d5737e5c | 147 | } |
| phn10 | 9:06c0d5737e5c | 148 | } |
| spm71 | 10:ae0a262ba48d | 149 | #endif |
| phn10 | 9:06c0d5737e5c | 150 | |
| phn10 | 15:100321eb2aac | 151 | /* |
| phn10 | 15:100321eb2aac | 152 | * void send_command_to_display(char bcd[]); |
| phn10 | 15:100321eb2aac | 153 | * Description: send bcd[] array to the display driver to display number |
| phn10 | 15:100321eb2aac | 154 | * in the display |
| phn10 | 15:100321eb2aac | 155 | * |
| phn10 | 15:100321eb2aac | 156 | * Inputs: |
| phn10 | 15:100321eb2aac | 157 | * Parameters: |
| phn10 | 15:100321eb2aac | 158 | * char bcd[]: bcd (binary coded decimal) array |
| phn10 | 15:100321eb2aac | 159 | * |
| phn10 | 15:100321eb2aac | 160 | * Outputs: |
| phn10 | 15:100321eb2aac | 161 | * Returns: void |
| phn10 | 15:100321eb2aac | 162 | */ |
| phn10 | 11:6751b9406142 | 163 | void send_command_to_display(char bcd[]) { |
| phn10 | 11:6751b9406142 | 164 | int command; |
| phn10 | 11:6751b9406142 | 165 | |
| phn10 | 11:6751b9406142 | 166 | |
| phn10 | 11:6751b9406142 | 167 | for (int i = 1; i <= 4; i++) { |
| phn10 | 11:6751b9406142 | 168 | command = 0; |
| phn10 | 11:6751b9406142 | 169 | // command has following form: 0x0[place]0[value] |
| phn10 | 11:6751b9406142 | 170 | // the value converted to int is 16 * 16 * place + value |
| phn10 | 11:6751b9406142 | 171 | command = 16 * 16 * i + bcd[i - 1]; |
| phn10 | 11:6751b9406142 | 172 | |
| phn10 | 11:6751b9406142 | 173 | SS = 0; |
| phn10 | 11:6751b9406142 | 174 | spi.write(command); |
| phn10 | 11:6751b9406142 | 175 | SS = 1; |
| phn10 | 11:6751b9406142 | 176 | } |
| phn10 | 7:161fe3793ddb | 177 | } |
