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