Lab 6 code.

Dependencies:   mbed

Fork of WaG by GroupA

main.cpp

Committer:
phn10
Date:
2018-02-22
Revision:
6:749a691e2abf
Parent:
4:0ed77d8b3e42
Child:
7:161fe3793ddb

File content as of revision 6:749a691e2abf:

/******************************************************************************
* EECS 397
*
* Assignment Name: Lab 4: display_test2
* 
* Authors: Sam Morrison and Phong Nguyen 
*
* Purpose: Configures the board for 5-digit display
*
* Last Modified: 02/19/2018
*
******************************************************************************/
#include "mbed.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

DigitalOut SS(CHIP_SELECT);

SPI spi(MOSI, MISO, SCLK);
Serial pc(USBTX, USBRX);

bool num_range(char input[5]);
int convert(int dec);
char to_command(char input, int place);
int bcd[4];

int main(void) {
    SS = 1; 
    
    spi.format(16,0);
    spi.frequency(1000000);
    
    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
    
    float command = 0x0100;
    int input;
    //char command_text[10] = "0x0";
    while(1) {
        pc.printf("Select a digit between 0 and 9999:\n");
        bool check = scanf("%d", &input);
        while (check != 1) {
            pc.printf("Select a digit between 0 and 9999:\n");
            check = scanf("%d", &input);
        }
        
        /*
        while (num_range(input) == false) { //ask for input until 0-9999 is selected
            pc.printf("Select a digit between 0 and 9999:\n");
            scanf("%d", input);
        }
        
        
        command_text[3] = place + '0';
        command_text[4] = '0';
        command_text[5] = input;
        
        command = atof(command_text); //converts string to float
        */
        
        pc.printf("command: %s\n", command_text);
        
        
        SS = 0;
        spi.write(command); // need to cycle 4 times
        SS = 1;
        //pc.printf("command: &f\n", commmand);
    }
    
    /*
    SS = 0;
    spi.write(0x0100); //set digit 0 to 0
    SS = 1;
    */
}

bool num_range(char input[5]) {
     bool result = true;
     for (int i = 0; i < 5; i++) {
        if (i > 3)
            if (input[i] != 0) //checks for number larger than 9999
                result = false; 
        if (input[i] < 48 or input[i] > 57) //checks for digits outside 0-9
            result = false;
     }
     return result;
}

void mod_bcd(int num) {
    int size = 4;
    int dec_arr[size];
    int place = 0;
    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 = 0; i < size; i++) { //converts decimal array to binary array
        bcd[i] = convert(dec_arr[i]);
    }
}


char to_command(char input, int place) {
    
}

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)); 
}