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