GroupA / Mbed 2 deprecated Lab_6_WaG

Dependencies:   mbed

Fork of WaG by GroupA

Committer:
spm71
Date:
Fri Mar 02 22:18:47 2018 +0000
Revision:
18:0e281922212c
Parent:
16:dfa9eb1a808d
Child:
20:d23bcd97f2c5
More implementation on SPI and started stepper files.

Who changed what in which revision?

UserRevisionLine numberNew 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 */
phn10 8:d8bc78bda829 44 void initial_setup(DigitalOut SS, int data_length, int frequency) {
phn10 8:d8bc78bda829 45 SS = 1;
spm71 18:0e281922212c 46 wag_spi.format(data_length, 0);
spm71 18:0e281922212c 47 wag_spi.frequency(frequency);
phn10 8:d8bc78bda829 48
spm71 18:0e281922212c 49 spi_send(as1107, 0x0C01);
spm71 18:0e281922212c 50 spi_send(as1107, 0x090F);
spm71 18:0e281922212c 51 spi_send(as1107, 0x0F00);
spm71 18:0e281922212c 52 spi_send(as1107, 0x0A0F);
spm71 18:0e281922212c 53 spi_send(as1107, 0x0B04);
spm71 18:0e281922212c 54 spi_send(as1107, 0x0100);
spm71 18:0e281922212c 55 spi_send(as1107, 0x0200);
spm71 18:0e281922212c 56 spi_send(as1107, 0x0300);
spm71 18:0e281922212c 57 spi_send(as1107, 0x0400);
spm71 18:0e281922212c 58 spi_send(as1107, 0x0500);
phn10 8:d8bc78bda829 59 }
phn10 8:d8bc78bda829 60
spm71 10:ae0a262ba48d 61
spm71 10:ae0a262ba48d 62 #ifdef VERSION1
phn10 8:d8bc78bda829 63 /*
spm71 10:ae0a262ba48d 64 * void bin2bcd_array(int num);
spm71 13:8936b2f64aa2 65 * Description: Converts to BCD array using modulo method.
phn10 8:d8bc78bda829 66 *
phn10 8:d8bc78bda829 67 * Inputs:
phn10 8:d8bc78bda829 68 * Parameters:
spm71 13:8936b2f64aa2 69 * int num: number to push to display
spm71 13:8936b2f64aa2 70 char bcd[]: BCD array that will be written to
phn10 8:d8bc78bda829 71 * Globals:
phn10 8:d8bc78bda829 72 *
phn10 8:d8bc78bda829 73 * Outputs:
phn10 8:d8bc78bda829 74 * Returns: void
phn10 8:d8bc78bda829 75 */
spm71 10:ae0a262ba48d 76 void bin2bcd_array(int num, char bcd[]) {
spm71 13:8936b2f64aa2 77 int place = 0;
phn10 7:161fe3793ddb 78 while (num != 0) { //converts decimal input to decimal array using %mod
phn10 7:161fe3793ddb 79 int val = num % 10;
spm71 12:a947a6609a23 80 bcd[place] = val;
phn10 7:161fe3793ddb 81 num = num/10;
spm71 13:8936b2f64aa2 82 place++;
phn10 7:161fe3793ddb 83 }
phn10 7:161fe3793ddb 84 }
spm71 10:ae0a262ba48d 85 #endif
phn10 7:161fe3793ddb 86
spm71 10:ae0a262ba48d 87 #ifdef VERSION2
phn10 8:d8bc78bda829 88 /*
phn10 9:06c0d5737e5c 89 * void bin2bcd_array(int num);
phn10 9:06c0d5737e5c 90 * Description: converts a number from binary format to binary coded
phn10 9:06c0d5737e5c 91 * decimal array using sprintf() method
phn10 9:06c0d5737e5c 92 *
phn10 9:06c0d5737e5c 93 * Inputs:
phn10 9:06c0d5737e5c 94 * Parameters:
phn10 9:06c0d5737e5c 95 * int num: number in binary format
phn10 15:100321eb2aac 96 * char bcd[]: bcd (binary coded decimal) array
phn10 9:06c0d5737e5c 97 *
phn10 9:06c0d5737e5c 98 * Outputs:
phn10 9:06c0d5737e5c 99 * Returns: void
phn10 9:06c0d5737e5c 100 */
spm71 10:ae0a262ba48d 101 void bin2bcd_array(int num, char bcd[]) {
phn10 9:06c0d5737e5c 102 char tmp_array[4];
phn10 11:6751b9406142 103 char reverse_bcd[4];
phn10 11:6751b9406142 104 int last_index = 0;
phn10 9:06c0d5737e5c 105 int i = 0;
phn10 11:6751b9406142 106
phn10 11:6751b9406142 107 sprintf(tmp_array, "%d", num);
phn10 11:6751b9406142 108 for (i = 0; i < 4; i++) {
phn10 11:6751b9406142 109 bcd[i] = 0;
phn10 11:6751b9406142 110 reverse_bcd[i] = 0;
phn10 11:6751b9406142 111 }
phn10 11:6751b9406142 112
phn10 11:6751b9406142 113 while (last_index < 4 and tmp_array[last_index] != '\0') {
phn10 11:6751b9406142 114 reverse_bcd[last_index] = tmp_array[last_index] - '0';
phn10 11:6751b9406142 115 last_index++;
phn10 11:6751b9406142 116 }
phn10 11:6751b9406142 117
phn10 11:6751b9406142 118 // reverse the order to fit binary coded decimal
phn10 11:6751b9406142 119 for (i = 0; i < last_index; i++) {
phn10 11:6751b9406142 120 bcd[i] = reverse_bcd[last_index - 1 - i];
phn10 9:06c0d5737e5c 121 }
phn10 9:06c0d5737e5c 122 }
spm71 10:ae0a262ba48d 123 #endif
phn10 9:06c0d5737e5c 124
phn10 15:100321eb2aac 125 /*
phn10 15:100321eb2aac 126 * void send_command_to_display(char bcd[]);
phn10 15:100321eb2aac 127 * Description: send bcd[] array to the display driver to display number
phn10 15:100321eb2aac 128 * in the display
phn10 15:100321eb2aac 129 *
phn10 15:100321eb2aac 130 * Inputs:
phn10 15:100321eb2aac 131 * Parameters:
phn10 15:100321eb2aac 132 * char bcd[]: bcd (binary coded decimal) array
phn10 15:100321eb2aac 133 *
phn10 15:100321eb2aac 134 * Outputs:
phn10 15:100321eb2aac 135 * Returns: void
phn10 15:100321eb2aac 136 */
phn10 11:6751b9406142 137 void send_command_to_display(char bcd[]) {
phn10 11:6751b9406142 138 int command;
phn10 11:6751b9406142 139
phn10 11:6751b9406142 140
phn10 11:6751b9406142 141 for (int i = 1; i <= 4; i++) {
phn10 11:6751b9406142 142 command = 0;
phn10 11:6751b9406142 143 // command has following form: 0x0[place]0[value]
phn10 11:6751b9406142 144 // the value converted to int is 16 * 16 * place + value
phn10 11:6751b9406142 145 command = 16 * 16 * i + bcd[i - 1];
spm71 18:0e281922212c 146 spi_send(as1107, command);
phn10 11:6751b9406142 147 }
phn10 7:161fe3793ddb 148 }