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@32:0dc2b4a3eee6, 2018-03-22 (annotated)
- Committer:
- spm71
- Date:
- Thu Mar 22 16:06:21 2018 +0000
- Revision:
- 32:0dc2b4a3eee6
- Parent:
- 23:3da1d39c1ae9
- Child:
- 39:abf211b17e3c
Fixed LED display code
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 | * | 
| spm71 | 18:0e281922212c | 4 | * Assignment Name: Lab 5: WaG | 
| 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 | * | 
| spm71 | 18:0e281922212c | 10 | * Created: 03/01/2018 | 
| spm71 | 22:09dd6977576b | 11 | * Last Modified: 03/08/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> | 
| spm71 | 18:0e281922212c | 20 | #include "spi.h" | 
| spm71 | 23:3da1d39c1ae9 | 21 | #include "utility.h" | 
| phn10 | 8:d8bc78bda829 | 22 | |
| spm71 | 14:be27f6e21a8a | 23 | //#define VERSION1 | 
| spm71 | 14:be27f6e21a8a | 24 | #define VERSION2 | 
| spm71 | 10:ae0a262ba48d | 25 | |
| spm71 | 18:0e281922212c | 26 | extern SPI wag_spi; | 
| spm71 | 18:0e281922212c | 27 | extern spi_cfg as1107; | 
| phn10 | 8:d8bc78bda829 | 28 | |
| phn10 | 8:d8bc78bda829 | 29 | /* | 
| spm71 | 22:09dd6977576b | 30 | * void initial_setup(struct spi_cfg spi_obj); | 
| phn10 | 8:d8bc78bda829 | 31 | * Description: setup spi data length (in bit), spi frequency, set LED | 
| phn10 | 8:d8bc78bda829 | 32 | * display to normal operation, and set all display numbers | 
| phn10 | 8:d8bc78bda829 | 33 | * to 0. | 
| phn10 | 8:d8bc78bda829 | 34 | * | 
| phn10 | 8:d8bc78bda829 | 35 | * Inputs: | 
| phn10 | 8:d8bc78bda829 | 36 | * Parameters: | 
| spm71 | 22:09dd6977576b | 37 | * struct spi_cfg spi_obj: spi_cfg object | 
| phn10 | 8:d8bc78bda829 | 38 | * | 
| phn10 | 8:d8bc78bda829 | 39 | * Outputs: | 
| phn10 | 8:d8bc78bda829 | 40 | * Returns: void | 
| phn10 | 8:d8bc78bda829 | 41 | */ | 
| spm71 | 20:d23bcd97f2c5 | 42 | void initial_setup(struct spi_cfg spi_obj) { | 
| spm71 | 20:d23bcd97f2c5 | 43 | spi_obj.spi_ncs = 1; | 
| phn10 | 8:d8bc78bda829 | 44 | |
| spm71 | 20:d23bcd97f2c5 | 45 | spi_send(spi_obj, 0x0C01); | 
| spm71 | 20:d23bcd97f2c5 | 46 | spi_send(spi_obj, 0x090F); | 
| spm71 | 20:d23bcd97f2c5 | 47 | spi_send(spi_obj, 0x0F00); | 
| spm71 | 20:d23bcd97f2c5 | 48 | spi_send(spi_obj, 0x0A0F); | 
| spm71 | 20:d23bcd97f2c5 | 49 | spi_send(spi_obj, 0x0B04); | 
| spm71 | 20:d23bcd97f2c5 | 50 | spi_send(spi_obj, 0x0100); | 
| spm71 | 20:d23bcd97f2c5 | 51 | spi_send(spi_obj, 0x0200); | 
| spm71 | 20:d23bcd97f2c5 | 52 | spi_send(spi_obj, 0x0300); | 
| spm71 | 20:d23bcd97f2c5 | 53 | spi_send(spi_obj, 0x0400); | 
| spm71 | 20:d23bcd97f2c5 | 54 | spi_send(spi_obj, 0x0500); | 
| phn10 | 8:d8bc78bda829 | 55 | } | 
| phn10 | 8:d8bc78bda829 | 56 | |
| spm71 | 10:ae0a262ba48d | 57 | |
| spm71 | 23:3da1d39c1ae9 | 58 | |
| spm71 | 23:3da1d39c1ae9 | 59 | |
| spm71 | 10:ae0a262ba48d | 60 | #ifdef VERSION1 | 
| phn10 | 8:d8bc78bda829 | 61 | /* | 
| spm71 | 10:ae0a262ba48d | 62 | * void bin2bcd_array(int num); | 
| spm71 | 13:8936b2f64aa2 | 63 | * Description: Converts to BCD array using modulo method. | 
| phn10 | 8:d8bc78bda829 | 64 | * | 
| phn10 | 8:d8bc78bda829 | 65 | * Inputs: | 
| phn10 | 8:d8bc78bda829 | 66 | * Parameters: | 
| spm71 | 13:8936b2f64aa2 | 67 | * int num: number to push to display | 
| spm71 | 13:8936b2f64aa2 | 68 | char bcd[]: BCD array that will be written to | 
| phn10 | 8:d8bc78bda829 | 69 | * Globals: | 
| phn10 | 8:d8bc78bda829 | 70 | * | 
| phn10 | 8:d8bc78bda829 | 71 | * Outputs: | 
| phn10 | 8:d8bc78bda829 | 72 | * Returns: void | 
| phn10 | 8:d8bc78bda829 | 73 | */ | 
| spm71 | 10:ae0a262ba48d | 74 | void bin2bcd_array(int num, char bcd[]) { | 
| spm71 | 13:8936b2f64aa2 | 75 | int place = 0; | 
| phn10 | 7:161fe3793ddb | 76 | while (num != 0) { //converts decimal input to decimal array using %mod | 
| phn10 | 7:161fe3793ddb | 77 | int val = num % 10; | 
| spm71 | 12:a947a6609a23 | 78 | bcd[place] = val; | 
| phn10 | 7:161fe3793ddb | 79 | num = num/10; | 
| spm71 | 13:8936b2f64aa2 | 80 | place++; | 
| phn10 | 7:161fe3793ddb | 81 | } | 
| phn10 | 7:161fe3793ddb | 82 | } | 
| spm71 | 10:ae0a262ba48d | 83 | #endif | 
| phn10 | 7:161fe3793ddb | 84 | |
| spm71 | 10:ae0a262ba48d | 85 | #ifdef VERSION2 | 
| phn10 | 8:d8bc78bda829 | 86 | /* | 
| phn10 | 9:06c0d5737e5c | 87 | * void bin2bcd_array(int num); | 
| phn10 | 9:06c0d5737e5c | 88 | * Description: converts a number from binary format to binary coded | 
| phn10 | 9:06c0d5737e5c | 89 | * decimal array using sprintf() method | 
| phn10 | 9:06c0d5737e5c | 90 | * | 
| phn10 | 9:06c0d5737e5c | 91 | * Inputs: | 
| phn10 | 9:06c0d5737e5c | 92 | * Parameters: | 
| phn10 | 9:06c0d5737e5c | 93 | * int num: number in binary format | 
| phn10 | 15:100321eb2aac | 94 | * char bcd[]: bcd (binary coded decimal) array | 
| phn10 | 9:06c0d5737e5c | 95 | * | 
| phn10 | 9:06c0d5737e5c | 96 | * Outputs: | 
| phn10 | 9:06c0d5737e5c | 97 | * Returns: void | 
| phn10 | 9:06c0d5737e5c | 98 | */ | 
| spm71 | 10:ae0a262ba48d | 99 | void bin2bcd_array(int num, char bcd[]) { | 
| phn10 | 9:06c0d5737e5c | 100 | char tmp_array[4]; | 
| phn10 | 11:6751b9406142 | 101 | char reverse_bcd[4]; | 
| phn10 | 11:6751b9406142 | 102 | int last_index = 0; | 
| phn10 | 9:06c0d5737e5c | 103 | int i = 0; | 
| phn10 | 11:6751b9406142 | 104 | |
| phn10 | 11:6751b9406142 | 105 | sprintf(tmp_array, "%d", num); | 
| phn10 | 11:6751b9406142 | 106 | for (i = 0; i < 4; i++) { | 
| phn10 | 11:6751b9406142 | 107 | bcd[i] = 0; | 
| phn10 | 11:6751b9406142 | 108 | reverse_bcd[i] = 0; | 
| phn10 | 11:6751b9406142 | 109 | } | 
| phn10 | 11:6751b9406142 | 110 | |
| phn10 | 11:6751b9406142 | 111 | while (last_index < 4 and tmp_array[last_index] != '\0') { | 
| phn10 | 11:6751b9406142 | 112 | reverse_bcd[last_index] = tmp_array[last_index] - '0'; | 
| phn10 | 11:6751b9406142 | 113 | last_index++; | 
| phn10 | 11:6751b9406142 | 114 | } | 
| phn10 | 11:6751b9406142 | 115 | |
| phn10 | 11:6751b9406142 | 116 | // reverse the order to fit binary coded decimal | 
| phn10 | 11:6751b9406142 | 117 | for (i = 0; i < last_index; i++) { | 
| phn10 | 11:6751b9406142 | 118 | bcd[i] = reverse_bcd[last_index - 1 - i]; | 
| phn10 | 9:06c0d5737e5c | 119 | } | 
| phn10 | 9:06c0d5737e5c | 120 | } | 
| spm71 | 10:ae0a262ba48d | 121 | #endif | 
| phn10 | 9:06c0d5737e5c | 122 | |
| phn10 | 15:100321eb2aac | 123 | /* | 
| phn10 | 15:100321eb2aac | 124 | * void send_command_to_display(char bcd[]); | 
| phn10 | 15:100321eb2aac | 125 | * Description: send bcd[] array to the display driver to display number | 
| phn10 | 15:100321eb2aac | 126 | * in the display | 
| phn10 | 15:100321eb2aac | 127 | * | 
| phn10 | 15:100321eb2aac | 128 | * Inputs: | 
| phn10 | 15:100321eb2aac | 129 | * Parameters: | 
| phn10 | 15:100321eb2aac | 130 | * char bcd[]: bcd (binary coded decimal) array | 
| phn10 | 15:100321eb2aac | 131 | * | 
| phn10 | 15:100321eb2aac | 132 | * Outputs: | 
| phn10 | 15:100321eb2aac | 133 | * Returns: void | 
| phn10 | 15:100321eb2aac | 134 | */ | 
| phn10 | 11:6751b9406142 | 135 | void send_command_to_display(char bcd[]) { | 
| phn10 | 11:6751b9406142 | 136 | int command; | 
| phn10 | 11:6751b9406142 | 137 | |
| phn10 | 11:6751b9406142 | 138 | |
| phn10 | 11:6751b9406142 | 139 | for (int i = 1; i <= 4; i++) { | 
| phn10 | 11:6751b9406142 | 140 | command = 0; | 
| phn10 | 11:6751b9406142 | 141 | // command has following form: 0x0[place]0[value] | 
| phn10 | 11:6751b9406142 | 142 | // the value converted to int is 16 * 16 * place + value | 
| phn10 | 11:6751b9406142 | 143 | command = 16 * 16 * i + bcd[i - 1]; | 
| spm71 | 18:0e281922212c | 144 | spi_send(as1107, command); | 
| phn10 | 11:6751b9406142 | 145 | } | 
| spm71 | 23:3da1d39c1ae9 | 146 | } | 
| spm71 | 23:3da1d39c1ae9 | 147 | |
| spm71 | 23:3da1d39c1ae9 | 148 | /* | 
| spm71 | 23:3da1d39c1ae9 | 149 | * void test_target_leds(); | 
| spm71 | 23:3da1d39c1ae9 | 150 | * Description: test each LED on the target board | 
| spm71 | 23:3da1d39c1ae9 | 151 | * | 
| spm71 | 23:3da1d39c1ae9 | 152 | * Inputs: | 
| spm71 | 23:3da1d39c1ae9 | 153 | * None | 
| spm71 | 23:3da1d39c1ae9 | 154 | * | 
| spm71 | 23:3da1d39c1ae9 | 155 | * Outputs: | 
| spm71 | 23:3da1d39c1ae9 | 156 | * Returns: void | 
| spm71 | 23:3da1d39c1ae9 | 157 | */ | 
| spm71 | 23:3da1d39c1ae9 | 158 | void test_target_leds() { | 
| spm71 | 23:3da1d39c1ae9 | 159 | int pressed = uti_chk_ubutton(); | 
| spm71 | 32:0dc2b4a3eee6 | 160 | int led_command = 0; | 
| spm71 | 23:3da1d39c1ae9 | 161 | while (pressed == 0); | 
| spm71 | 23:3da1d39c1ae9 | 162 | while (pressed == 0) { | 
| spm71 | 32:0dc2b4a3eee6 | 163 | for (int i = 0x1; i <= 0x8; i++) { | 
| spm71 | 32:0dc2b4a3eee6 | 164 | led_command = 0x0500 + i; | 
| spm71 | 32:0dc2b4a3eee6 | 165 | spi_send(as1107, led_command); //light up LED[i] | 
| spm71 | 23:3da1d39c1ae9 | 166 | wait(0.1); | 
| spm71 | 32:0dc2b4a3eee6 | 167 | spi_send(as1107, 0x0500); //turn off LED[i] | 
| spm71 | 23:3da1d39c1ae9 | 168 | } | 
| spm71 | 23:3da1d39c1ae9 | 169 | } | 
| phn10 | 7:161fe3793ddb | 170 | } | 
