GroupA / Mbed 2 deprecated Lab_6_WaG

Dependencies:   mbed

Fork of WaG by GroupA

display.cpp

Committer:
phn10
Date:
2018-02-23
Revision:
11:6751b9406142
Parent:
10:ae0a262ba48d
Child:
12:a947a6609a23

File content as of revision 11:6751b9406142:

/******************************************************************************
* EECS 397
*
* Assignment Name: Lab 4: display_test2
* 
* Authors: Sam Morrison and Phong Nguyen 
* File name: display.cpp
* Purpose: Contain function definitions
* 
* Created: 02/21/2018
* Last Modified: 02/21/2018
*
******************************************************************************/
#include "mbed.h"
#include "io_pins.h"
#include "display.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

//#define VERSION1
#define VERSION2


/*
 * void initial_setup(DigitalOut SS, int data_length, int frequency);
 * Description: setup spi data length (in bit), spi frequency, set LED 
 *              display to normal operation, and set all display numbers
 *              to 0.
 * 
 * Inputs: 
 *      Parameters:
 *          SPI spi: spi object 
 *          DigitalOut SS: chip select pin
 *          int data_length: spi data length in bit
 *          int frequency: frequency of SPI communication
 *      
 * Outputs:
 *      Returns: void
*/
void initial_setup(DigitalOut SS, int data_length, int frequency) {
    SS = 1; 
    spi.format(data_length, 0);
    spi.frequency(frequency);
    
    SS = 0;
    spi.write(0x0C01); //normal operation
    SS = 1;
    
    SS = 0;
    spi.write(0x090F); //decode to bits 0:3
    SS = 1;
    
    SS = 0;
    spi.write(0x0F00); //set to normal mode
    SS = 1;
    
    SS = 0;
    spi.write(0x0A0F); //intensity set to max
    SS = 1;
    
    SS = 0;
    spi.write(0x0B04); //display digits 0:4
    SS = 1;
    
    SS = 0;
    spi.write(0x0100); //set digit 1 to 0
    SS = 1;
    
    SS = 0;
    spi.write(0x0200); //set digit 2 to 0
    SS = 1;
    
    SS = 0;
    spi.write(0x0300); //set digit 3 to 0
    SS = 1;
    
    SS = 0;
    spi.write(0x0400); //set digit 4 to 0
    SS = 1;
    
    SS = 0;
    spi.write(0x0500); //set digit 5 to 0
    SS = 1;
}


#ifdef VERSION1
/*
 * void bin2bcd_array(int num);
 * Description: 
 *
 * Inputs: 
 *      Parameters:
 *          int num: 
 *      Globals:
 *      
 * Outputs:
 *      Parameters:
 *      Globals:
 *      Returns: void
*/
void bin2bcd_array(int num, char bcd[]) {
    int size = 4;
    int dec_arr[size];
    int place = 3;
    while (num != 0) { //converts decimal input to decimal array using %mod
        int val = num % 10;
        dec_arr[place] = val;
        num = num/10;
        place--;
    }
    for (int i = size - 1; i >= 0; i--) { //converts decimal array to binary array
        bcd[i] = convert(dec_arr[i]);
    }
}
#endif

/*
 * int convert(int dec);
 * Description: 
 *
 * Inputs: 
 *      Parameters:
 *          int dec: 
 *      Globals:
 *      
 * Outputs:
 *      Parameters:
 *      Globals:
 *      Returns: void
*/
int convert(int dec) {//convert decimal to binary
    if (dec == 0) //function complete
        return 0; 
    else //recursive call until converted
        return (dec % 2 + 10 * convert(dec / 2) + ' '); 
}

#ifdef VERSION2
/*
 * void bin2bcd_array(int num);
 * Description: converts a number from binary format to binary coded
 *              decimal array using sprintf() method
 *
 * Inputs: 
 *      Parameters:
 *          int num: number in binary format
 *          int &bcd: pointer to bcd (binary coded decimal) array
 *      
 * Outputs:
 *      Returns: void
*/
void bin2bcd_array(int num, char bcd[]) {
    char tmp_array[4];
    char reverse_bcd[4];
    int last_index = 0;
    int i = 0;
    
    sprintf(tmp_array, "%d", num);
    for (i = 0; i < 4; i++) {
        bcd[i] = 0;
        reverse_bcd[i] = 0;
    }
    
    while (last_index < 4 and tmp_array[last_index] != '\0') {
        reverse_bcd[last_index] = tmp_array[last_index] - '0';   
        last_index++;    
    }
    
    // reverse the order to fit binary coded decimal
    for (i = 0; i < last_index; i++) {
        bcd[i] = reverse_bcd[last_index - 1 - i];
    }
}
#endif

void send_command_to_display(char bcd[]) {
    int command;
    
    
    for (int i = 1; i <= 4; i++) {
        command = 0;
        // command has following form: 0x0[place]0[value]
        // the value converted to int is 16 * 16 * place + value
        command = 16 * 16 * i + bcd[i - 1]; 
        
        SS = 0;
        spi.write(command);
        SS = 1;
    }
}