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@8:d8bc78bda829, 2018-02-22 (annotated)
- Committer:
- phn10
- Date:
- Thu Feb 22 03:10:01 2018 +0000
- Revision:
- 8:d8bc78bda829
- Parent:
- 7:161fe3793ddb
- Child:
- 9:06c0d5737e5c
write function initial_setup() which set LED display to normal operation, spi frequency, and set all LED display numbers to 0.
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 | |
| phn10 | 8:d8bc78bda829 | 21 | int bcd[4]; |
| phn10 | 8:d8bc78bda829 | 22 | |
| phn10 | 8:d8bc78bda829 | 23 | |
| phn10 | 8:d8bc78bda829 | 24 | /* |
| phn10 | 8:d8bc78bda829 | 25 | * void initial_setup(DigitalOut SS, int data_length, int frequency); |
| phn10 | 8:d8bc78bda829 | 26 | * Description: setup spi data length (in bit), spi frequency, set LED |
| phn10 | 8:d8bc78bda829 | 27 | * display to normal operation, and set all display numbers |
| phn10 | 8:d8bc78bda829 | 28 | * to 0. |
| phn10 | 8:d8bc78bda829 | 29 | * |
| phn10 | 8:d8bc78bda829 | 30 | * Inputs: |
| phn10 | 8:d8bc78bda829 | 31 | * Parameters: |
| phn10 | 8:d8bc78bda829 | 32 | * SPI spi: spi object |
| phn10 | 8:d8bc78bda829 | 33 | * DigitalOut SS: chip select pin |
| phn10 | 8:d8bc78bda829 | 34 | * int data_length: spi data length in bit |
| phn10 | 8:d8bc78bda829 | 35 | * int frequency: frequency of SPI communication |
| phn10 | 8:d8bc78bda829 | 36 | * |
| phn10 | 8:d8bc78bda829 | 37 | * Outputs: |
| phn10 | 8:d8bc78bda829 | 38 | * Returns: void |
| phn10 | 8:d8bc78bda829 | 39 | */ |
| phn10 | 8:d8bc78bda829 | 40 | void initial_setup(DigitalOut SS, int data_length, int frequency) { |
| phn10 | 8:d8bc78bda829 | 41 | SS = 1; |
| phn10 | 8:d8bc78bda829 | 42 | spi.format(data_length, 0); |
| phn10 | 8:d8bc78bda829 | 43 | spi.frequency(frequency); |
| phn10 | 8:d8bc78bda829 | 44 | |
| phn10 | 8:d8bc78bda829 | 45 | SS = 0; |
| phn10 | 8:d8bc78bda829 | 46 | spi.write(0x0C01); //normal operation |
| phn10 | 8:d8bc78bda829 | 47 | SS = 1; |
| phn10 | 8:d8bc78bda829 | 48 | |
| phn10 | 8:d8bc78bda829 | 49 | SS = 0; |
| phn10 | 8:d8bc78bda829 | 50 | spi.write(0x090F); //decode to bits 0:3 |
| phn10 | 8:d8bc78bda829 | 51 | SS = 1; |
| phn10 | 8:d8bc78bda829 | 52 | |
| phn10 | 8:d8bc78bda829 | 53 | SS = 0; |
| phn10 | 8:d8bc78bda829 | 54 | spi.write(0x0F00); //set to normal mode |
| phn10 | 8:d8bc78bda829 | 55 | SS = 1; |
| phn10 | 8:d8bc78bda829 | 56 | |
| phn10 | 8:d8bc78bda829 | 57 | SS = 0; |
| phn10 | 8:d8bc78bda829 | 58 | spi.write(0x0A0F); //intensity set to max |
| phn10 | 8:d8bc78bda829 | 59 | SS = 1; |
| phn10 | 8:d8bc78bda829 | 60 | |
| phn10 | 8:d8bc78bda829 | 61 | SS = 0; |
| phn10 | 8:d8bc78bda829 | 62 | spi.write(0x0B04); //display digits 0:4 |
| phn10 | 8:d8bc78bda829 | 63 | SS = 1; |
| phn10 | 8:d8bc78bda829 | 64 | |
| phn10 | 8:d8bc78bda829 | 65 | SS = 0; |
| phn10 | 8:d8bc78bda829 | 66 | spi.write(0x0100); //set digit 1 to 0 |
| phn10 | 8:d8bc78bda829 | 67 | SS = 1; |
| phn10 | 8:d8bc78bda829 | 68 | |
| phn10 | 8:d8bc78bda829 | 69 | SS = 0; |
| phn10 | 8:d8bc78bda829 | 70 | spi.write(0x0200); //set digit 2 to 0 |
| phn10 | 8:d8bc78bda829 | 71 | SS = 1; |
| phn10 | 8:d8bc78bda829 | 72 | |
| phn10 | 8:d8bc78bda829 | 73 | SS = 0; |
| phn10 | 8:d8bc78bda829 | 74 | spi.write(0x0300); //set digit 3 to 0 |
| phn10 | 8:d8bc78bda829 | 75 | SS = 1; |
| phn10 | 8:d8bc78bda829 | 76 | |
| phn10 | 8:d8bc78bda829 | 77 | SS = 0; |
| phn10 | 8:d8bc78bda829 | 78 | spi.write(0x0400); //set digit 4 to 0 |
| phn10 | 8:d8bc78bda829 | 79 | SS = 1; |
| phn10 | 8:d8bc78bda829 | 80 | |
| phn10 | 8:d8bc78bda829 | 81 | SS = 0; |
| phn10 | 8:d8bc78bda829 | 82 | spi.write(0x0500); //set digit 5 to 0 |
| phn10 | 8:d8bc78bda829 | 83 | SS = 1; |
| phn10 | 8:d8bc78bda829 | 84 | } |
| phn10 | 8:d8bc78bda829 | 85 | |
| phn10 | 8:d8bc78bda829 | 86 | /* |
| phn10 | 8:d8bc78bda829 | 87 | * void mod_bcd(int num); |
| phn10 | 8:d8bc78bda829 | 88 | * Description: |
| phn10 | 8:d8bc78bda829 | 89 | * |
| phn10 | 8:d8bc78bda829 | 90 | * Inputs: |
| phn10 | 8:d8bc78bda829 | 91 | * Parameters: |
| phn10 | 8:d8bc78bda829 | 92 | * int num: |
| phn10 | 8:d8bc78bda829 | 93 | * Globals: |
| phn10 | 8:d8bc78bda829 | 94 | * |
| phn10 | 8:d8bc78bda829 | 95 | * Outputs: |
| phn10 | 8:d8bc78bda829 | 96 | * Parameters: |
| phn10 | 8:d8bc78bda829 | 97 | * Globals: |
| phn10 | 8:d8bc78bda829 | 98 | * Returns: void |
| phn10 | 8:d8bc78bda829 | 99 | */ |
| phn10 | 7:161fe3793ddb | 100 | void mod_bcd(int num) { |
| phn10 | 7:161fe3793ddb | 101 | int size = 4; |
| phn10 | 7:161fe3793ddb | 102 | int dec_arr[size]; |
| phn10 | 7:161fe3793ddb | 103 | int place = 0; |
| phn10 | 7:161fe3793ddb | 104 | while (num != 0) { //converts decimal input to decimal array using %mod |
| phn10 | 7:161fe3793ddb | 105 | int val = num % 10; |
| phn10 | 7:161fe3793ddb | 106 | dec_arr[place] = val; |
| phn10 | 7:161fe3793ddb | 107 | num = num/10; |
| phn10 | 7:161fe3793ddb | 108 | place++; |
| phn10 | 7:161fe3793ddb | 109 | } |
| phn10 | 7:161fe3793ddb | 110 | for (int i = 0; i < size; i++) { //converts decimal array to binary array |
| phn10 | 7:161fe3793ddb | 111 | bcd[i] = convert(dec_arr[i]); |
| phn10 | 7:161fe3793ddb | 112 | } |
| phn10 | 7:161fe3793ddb | 113 | } |
| phn10 | 7:161fe3793ddb | 114 | |
| phn10 | 8:d8bc78bda829 | 115 | /* |
| phn10 | 8:d8bc78bda829 | 116 | * int convert(int dec); |
| phn10 | 8:d8bc78bda829 | 117 | * Description: |
| phn10 | 8:d8bc78bda829 | 118 | * |
| phn10 | 8:d8bc78bda829 | 119 | * Inputs: |
| phn10 | 8:d8bc78bda829 | 120 | * Parameters: |
| phn10 | 8:d8bc78bda829 | 121 | * int dec: |
| phn10 | 8:d8bc78bda829 | 122 | * Globals: |
| phn10 | 8:d8bc78bda829 | 123 | * |
| phn10 | 8:d8bc78bda829 | 124 | * Outputs: |
| phn10 | 8:d8bc78bda829 | 125 | * Parameters: |
| phn10 | 8:d8bc78bda829 | 126 | * Globals: |
| phn10 | 8:d8bc78bda829 | 127 | * Returns: void |
| phn10 | 8:d8bc78bda829 | 128 | */ |
| phn10 | 7:161fe3793ddb | 129 | int convert(int dec) {//convert decimal to binary |
| phn10 | 7:161fe3793ddb | 130 | if (dec == 0) //function complete |
| phn10 | 7:161fe3793ddb | 131 | return 0; |
| phn10 | 7:161fe3793ddb | 132 | else //recursive call until converted |
| phn10 | 7:161fe3793ddb | 133 | return (dec % 2 + 10 * convert(dec / 2)); |
| phn10 | 7:161fe3793ddb | 134 | } |
| phn10 | 7:161fe3793ddb | 135 | |
| phn10 | 8:d8bc78bda829 | 136 | /* |
| phn10 | 8:d8bc78bda829 | 137 | * char to_command(char input, int place); |
| phn10 | 8:d8bc78bda829 | 138 | * Description: |
| phn10 | 8:d8bc78bda829 | 139 | * |
| phn10 | 8:d8bc78bda829 | 140 | * Inputs: |
| phn10 | 8:d8bc78bda829 | 141 | * Parameters: |
| phn10 | 8:d8bc78bda829 | 142 | * char input: |
| phn10 | 8:d8bc78bda829 | 143 | * int place: |
| phn10 | 8:d8bc78bda829 | 144 | * Globals: |
| phn10 | 8:d8bc78bda829 | 145 | * |
| phn10 | 8:d8bc78bda829 | 146 | * Outputs: |
| phn10 | 8:d8bc78bda829 | 147 | * Parameters: |
| phn10 | 8:d8bc78bda829 | 148 | * Globals: |
| phn10 | 8:d8bc78bda829 | 149 | * Returns: char |
| phn10 | 8:d8bc78bda829 | 150 | */ |
| phn10 | 7:161fe3793ddb | 151 | char to_command(char input, int place) { |
| phn10 | 8:d8bc78bda829 | 152 | return 'a'; |
| phn10 | 7:161fe3793ddb | 153 | } |
