Display_nums

Dependencies:   mbed

Fork of Microprocessors_Template by EECS397

main.cpp

Committer:
michaelccodega
Date:
2018-02-22
Revision:
3:48be985187c6
Parent:
2:8412c8623314

File content as of revision 3:48be985187c6:

/*******************************************************************************
*EECS397
*
*Assignment Name: Lab 4 Part 1; Display_nums
*
*Author(s): Ashley Roberson, Michael Codega
*
*Purpose:
*
*Last Modified: February 15, 2018
*
*******************************************************************************/
#include "mbed.h"
#define DSP_TST_ON 0x0f01
#define DSP_TST_OFF 0x0f00

//ASCII character values of decimal numbers
#define ZERO 48
#define ONE 49
#define TWO 50
#define THREE 51
#define FOUR 52
#define FIVE 53
#define SIX 54
#define SEVEN 55
#define EIGHT 56
#define NINE 57

#define SPI_SCLOCK      PA_5
#define SPI_MOSI        PA_7
#define SPI_MISO        PA_6
#define NOT_CHIP_SEL    PC_7

SPI display_ctr(SPI_MOSI, SPI_MISO, SPI_SCLOCK);
DigitalOut dsp_ncs(NOT_CHIP_SEL);

Serial pc(USBTX, USBRX);
char input;
int current_digit;

void promptUser();
void write(int digit, int number);

int main()
{
    current_digit = 0;

    // Setting the formal of display_ctr
    dsp_ncs = 1;
    display_ctr.format(16, 0);
    
    // Setting to Normal Mode
    dsp_ncs = 0;
    display_ctr.write(DSP_TST_OFF); //normal mode
    dsp_ncs = 1;
    wait(.5);

    
    // Setting it to display 5 digits
    dsp_ncs = 0;
    display_ctr.write(0x0b04); //display 5 digits
    dsp_ncs = 1;
    wait(.5);

    /* Setting decode mode */
    // This means it only looks at the lower nibble of data, ignoring the 
    // first portion of data
    dsp_ncs = 0;
    display_ctr.write(0x090F); //decode mode
    dsp_ncs = 1;
    wait(.5);
    

    // Setting it to shutdown mode
    dsp_ncs = 0;
    display_ctr.write(0x0c01); //shutdown mode
    dsp_ncs = 1;
    wait(.5);
    
    // Sets the display to full brightness
    dsp_ncs = 0;
    display_ctr.write(0x0A0F);
    dsp_ncs = 1;
    wait(.5);
    dsp_ncs = 0;

    // on start, reset to zero
    write(1, 0);
    write(2, 0);
    write(3, 0);
    write(4, 0);
    write(5, 0);


    while (1)
    {
        promptUser();

    }
}// pcw

//Prompt User for Numbers
void promptUser()
{
    pc.printf("Please enter a number 0-9:\n");
    input = pc.getc();
    if (input < 48 || input > 57)
    {
        pc.printf("Invalid Number.\n");
        promptUser();
    }
    else
    {
        switch (input)
        {
        case ZERO:
            pc.printf("You typed 0\n");
            write(current_digit, 0);
            //display_ctr.write(0x0100);
            break;
        case ONE:
            pc.printf("You typed 1\n");
            write(current_digit, 1);
            break;
        case TWO:
            pc.printf("You typed 2\n");
            write(current_digit, 2);
            break;
        case THREE:
            pc.printf("You typed 3\n");
            write(current_digit, 3);
            break;
        case FOUR:
            pc.printf("You typed 4\n");
            write(current_digit, 4);
            break;
        case FIVE:
            pc.printf("You typed 5\n");
            write(current_digit, 5);
            break;
        case SIX:
            pc.printf("You typed 6\n");
            write(current_digit, 6);
            break;
        case SEVEN:
            pc.printf("You typed 7\n");
            write(current_digit, 7);
            break;
        case EIGHT:
            pc.printf("You typed 8\n");
            write(current_digit, 8);
            break;
        case NINE:
            //display_ctr.write(0x0109);
            write(current_digit, 9);
            break;
        }
        dsp_ncs = 1;
        wait(0.5);
        dsp_ncs = 0;
    }
    current_digit++;
    if(current_digit == 6){
        current_digit = 1;
    }
}

void write(int digit, int number){
    int message;
    message = digit * 256;    
    message = message + number;

    dsp_ncs = 0;
    display_ctr.write(message);
    dsp_ncs = 1;
    wait(.5);
    dsp_ncs = 0;
}