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@10:ae0a262ba48d, 2018-02-22 (annotated)
- Committer:
- spm71
- Date:
- Thu Feb 22 16:22:19 2018 +0000
- Revision:
- 10:ae0a262ba48d
- Parent:
- 9:06c0d5737e5c
- Child:
- 11:6751b9406142
Fixed concurrent functions
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 | 10:ae0a262ba48d | 21 | #define VERSION1 |
spm71 | 10:ae0a262ba48d | 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); |
phn10 | 8:d8bc78bda829 | 91 | * Description: |
phn10 | 8:d8bc78bda829 | 92 | * |
phn10 | 8:d8bc78bda829 | 93 | * Inputs: |
phn10 | 8:d8bc78bda829 | 94 | * Parameters: |
phn10 | 8:d8bc78bda829 | 95 | * int num: |
phn10 | 8:d8bc78bda829 | 96 | * Globals: |
phn10 | 8:d8bc78bda829 | 97 | * |
phn10 | 8:d8bc78bda829 | 98 | * Outputs: |
phn10 | 8:d8bc78bda829 | 99 | * Parameters: |
phn10 | 8:d8bc78bda829 | 100 | * Globals: |
phn10 | 8:d8bc78bda829 | 101 | * Returns: void |
phn10 | 8:d8bc78bda829 | 102 | */ |
spm71 | 10:ae0a262ba48d | 103 | void bin2bcd_array(int num, char bcd[]) { |
phn10 | 7:161fe3793ddb | 104 | int size = 4; |
phn10 | 7:161fe3793ddb | 105 | int dec_arr[size]; |
spm71 | 10:ae0a262ba48d | 106 | int place = 3; |
phn10 | 7:161fe3793ddb | 107 | while (num != 0) { //converts decimal input to decimal array using %mod |
phn10 | 7:161fe3793ddb | 108 | int val = num % 10; |
phn10 | 7:161fe3793ddb | 109 | dec_arr[place] = val; |
phn10 | 7:161fe3793ddb | 110 | num = num/10; |
spm71 | 10:ae0a262ba48d | 111 | place--; |
phn10 | 7:161fe3793ddb | 112 | } |
spm71 | 10:ae0a262ba48d | 113 | for (int i = size - 1; i >= 0; i--) { //converts decimal array to binary array |
phn10 | 7:161fe3793ddb | 114 | bcd[i] = convert(dec_arr[i]); |
phn10 | 7:161fe3793ddb | 115 | } |
phn10 | 7:161fe3793ddb | 116 | } |
spm71 | 10:ae0a262ba48d | 117 | #endif |
phn10 | 7:161fe3793ddb | 118 | |
phn10 | 8:d8bc78bda829 | 119 | /* |
phn10 | 8:d8bc78bda829 | 120 | * int convert(int dec); |
phn10 | 8:d8bc78bda829 | 121 | * Description: |
phn10 | 8:d8bc78bda829 | 122 | * |
phn10 | 8:d8bc78bda829 | 123 | * Inputs: |
phn10 | 8:d8bc78bda829 | 124 | * Parameters: |
phn10 | 8:d8bc78bda829 | 125 | * int dec: |
phn10 | 8:d8bc78bda829 | 126 | * Globals: |
phn10 | 8:d8bc78bda829 | 127 | * |
phn10 | 8:d8bc78bda829 | 128 | * Outputs: |
phn10 | 8:d8bc78bda829 | 129 | * Parameters: |
phn10 | 8:d8bc78bda829 | 130 | * Globals: |
phn10 | 8:d8bc78bda829 | 131 | * Returns: void |
phn10 | 8:d8bc78bda829 | 132 | */ |
phn10 | 7:161fe3793ddb | 133 | int convert(int dec) {//convert decimal to binary |
phn10 | 7:161fe3793ddb | 134 | if (dec == 0) //function complete |
phn10 | 7:161fe3793ddb | 135 | return 0; |
phn10 | 7:161fe3793ddb | 136 | else //recursive call until converted |
spm71 | 10:ae0a262ba48d | 137 | return (dec % 2 + 10 * convert(dec / 2) + ' '); |
phn10 | 7:161fe3793ddb | 138 | } |
phn10 | 7:161fe3793ddb | 139 | |
spm71 | 10:ae0a262ba48d | 140 | #ifdef VERSION2 |
phn10 | 8:d8bc78bda829 | 141 | /* |
phn10 | 9:06c0d5737e5c | 142 | * void bin2bcd_array(int num); |
phn10 | 9:06c0d5737e5c | 143 | * Description: converts a number from binary format to binary coded |
phn10 | 9:06c0d5737e5c | 144 | * decimal array using sprintf() method |
phn10 | 9:06c0d5737e5c | 145 | * |
phn10 | 9:06c0d5737e5c | 146 | * Inputs: |
phn10 | 9:06c0d5737e5c | 147 | * Parameters: |
phn10 | 9:06c0d5737e5c | 148 | * int num: number in binary format |
phn10 | 9:06c0d5737e5c | 149 | * int &bcd: pointer to bcd (binary coded decimal) array |
phn10 | 9:06c0d5737e5c | 150 | * |
phn10 | 9:06c0d5737e5c | 151 | * Outputs: |
phn10 | 9:06c0d5737e5c | 152 | * Returns: void |
phn10 | 9:06c0d5737e5c | 153 | */ |
spm71 | 10:ae0a262ba48d | 154 | void bin2bcd_array(int num, char bcd[]) { |
phn10 | 9:06c0d5737e5c | 155 | char tmp_array[4]; |
phn10 | 9:06c0d5737e5c | 156 | sprintf(tmp_array, "%d", num); |
phn10 | 9:06c0d5737e5c | 157 | |
phn10 | 9:06c0d5737e5c | 158 | int i = 0; |
phn10 | 9:06c0d5737e5c | 159 | while (tmp_array[i] != '\0') { |
phn10 | 9:06c0d5737e5c | 160 | bcd[3 - i] = tmp_array[i]; |
phn10 | 9:06c0d5737e5c | 161 | i++; |
phn10 | 9:06c0d5737e5c | 162 | } |
phn10 | 9:06c0d5737e5c | 163 | } |
spm71 | 10:ae0a262ba48d | 164 | #endif |
phn10 | 9:06c0d5737e5c | 165 | |
phn10 | 9:06c0d5737e5c | 166 | |
phn10 | 9:06c0d5737e5c | 167 | /* |
phn10 | 8:d8bc78bda829 | 168 | * char to_command(char input, int place); |
phn10 | 8:d8bc78bda829 | 169 | * Description: |
phn10 | 8:d8bc78bda829 | 170 | * |
phn10 | 8:d8bc78bda829 | 171 | * Inputs: |
phn10 | 8:d8bc78bda829 | 172 | * Parameters: |
phn10 | 8:d8bc78bda829 | 173 | * char input: |
phn10 | 8:d8bc78bda829 | 174 | * int place: |
phn10 | 8:d8bc78bda829 | 175 | * Globals: |
phn10 | 8:d8bc78bda829 | 176 | * |
phn10 | 8:d8bc78bda829 | 177 | * Outputs: |
phn10 | 8:d8bc78bda829 | 178 | * Parameters: |
phn10 | 8:d8bc78bda829 | 179 | * Globals: |
phn10 | 8:d8bc78bda829 | 180 | * Returns: char |
phn10 | 8:d8bc78bda829 | 181 | */ |
phn10 | 7:161fe3793ddb | 182 | char to_command(char input, int place) { |
phn10 | 8:d8bc78bda829 | 183 | return 'a'; |
phn10 | 7:161fe3793ddb | 184 | } |