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@20:d23bcd97f2c5, 2018-03-06 (annotated)
- Committer:
- spm71
- Date:
- Tue Mar 06 17:25:48 2018 +0000
- Revision:
- 20:d23bcd97f2c5
- Parent:
- 18:0e281922212c
- Child:
- 22:09dd6977576b
implemented menu and first option in the menu.; implementing the second option
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 | 18:0e281922212c | 11 | * Last Modified: 03/02/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 | /* |
| phn10 | 8:d8bc78bda829 | 29 | * void initial_setup(DigitalOut SS, int data_length, int frequency); |
| 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: |
| phn10 | 8:d8bc78bda829 | 36 | * SPI spi: spi object |
| phn10 | 8:d8bc78bda829 | 37 | * DigitalOut SS: chip select pin |
| phn10 | 8:d8bc78bda829 | 38 | * int data_length: spi data length in bit |
| phn10 | 8:d8bc78bda829 | 39 | * int frequency: frequency of SPI communication |
| phn10 | 8:d8bc78bda829 | 40 | * |
| phn10 | 8:d8bc78bda829 | 41 | * Outputs: |
| phn10 | 8:d8bc78bda829 | 42 | * Returns: void |
| phn10 | 8:d8bc78bda829 | 43 | */ |
| spm71 | 20:d23bcd97f2c5 | 44 | void initial_setup(struct spi_cfg spi_obj) { |
| spm71 | 20:d23bcd97f2c5 | 45 | spi_obj.spi_ncs = 1; |
| phn10 | 8:d8bc78bda829 | 46 | |
| spm71 | 20:d23bcd97f2c5 | 47 | spi_send(spi_obj, 0x0C01); |
| spm71 | 20:d23bcd97f2c5 | 48 | spi_send(spi_obj, 0x090F); |
| spm71 | 20:d23bcd97f2c5 | 49 | spi_send(spi_obj, 0x0F00); |
| spm71 | 20:d23bcd97f2c5 | 50 | spi_send(spi_obj, 0x0A0F); |
| spm71 | 20:d23bcd97f2c5 | 51 | spi_send(spi_obj, 0x0B04); |
| spm71 | 20:d23bcd97f2c5 | 52 | spi_send(spi_obj, 0x0100); |
| spm71 | 20:d23bcd97f2c5 | 53 | spi_send(spi_obj, 0x0200); |
| spm71 | 20:d23bcd97f2c5 | 54 | spi_send(spi_obj, 0x0300); |
| spm71 | 20:d23bcd97f2c5 | 55 | spi_send(spi_obj, 0x0400); |
| spm71 | 20:d23bcd97f2c5 | 56 | spi_send(spi_obj, 0x0500); |
| phn10 | 8:d8bc78bda829 | 57 | } |
| phn10 | 8:d8bc78bda829 | 58 | |
| spm71 | 10:ae0a262ba48d | 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 | } |
| phn10 | 7:161fe3793ddb | 146 | } |
