Plymouth University UK SoCEM Staff / Mbed 2 deprecated ELEC240-Task-01-01

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 SPI spi(PA_7, PA_6, PA_5);      // Ordered as: mosi, miso, sclk could use forth parameter ssel
00004 DigitalOut cs(PC_6);            // Chip Select for Basic Outputs to illuminate Onboard FPGA DEO nano LEDs CN7 pin 1
00005 
00006 int32_t read_switches(void);    //Read 4 Sliding switches on FPGA (Simulating OPTO-Switches from Motor(s)
00007 
00008 //NBB the following line for F429ZI !!!!
00009 DigitalIn DO_NOT_USE(PB_12);    // MAKE PB_12 (D19) an INPUT do NOT make an OUTPUT under any circumstances !!!!! ************* !!!!!!!!!!!
00010                                 // This Pin is connected to the 5VDC from the FPGA card and an INPUT is 5V Tolerant
00011 
00012 //Ticker ticktock;
00013   
00014 int main() {
00015     cs = 1;                     // Chip must be deselected, Chip Select is active LOW
00016     spi.format(8,0);            // Setup the DATA frame SPI for 16 bit wide word, Clock Polarity 0 and Clock Phase 0 (0)
00017     spi.frequency(1000000);     // 1MHz clock rate
00018 
00019         printf("TEST\n\r");
00020     while(true)                 //Loop forever Knight Rider Display on FPGA
00021     {
00022         read_switches();
00023             
00024         //LED Chaser display KIT lives on!
00025         for (uint32_t i=1;i<=128;i*=2)
00026         {
00027             cs = 0;             //Select the device by seting chip select LOW
00028             spi.write(0);               //Send the command
00029                         spi.write(i);               //Send the data - ignore the return data
00030             cs = 1;             //De-Select the device by seting chip select HIGH
00031             wait_ms(20);
00032         }
00033         for (uint32_t i=128;i>=1;i/=2)
00034         {
00035             cs = 0;             //Select the device by seting chip select LOW
00036             spi.write(0);               //Send the command
00037                         spi.write(i);               //Send the data - ignore the return data
00038             cs = 1;             //De-Select the device by seting chip select HIGH
00039             wait_ms(20);
00040         }
00041                 wait_ms(1000);
00042     }
00043 }
00044 
00045 //Function to read back the state of the switches
00046 int read_switches(void){
00047     int sw_val = 0;
00048     cs = 0;             //Select the device by seting chip select LOW
00049     spi.write(0);   //Command
00050     sw_val = spi.write(0x01) & 0x0F; // Just want to read back lower 4bit nibble
00051     cs = 1 ;            //De-select the device by seting chip select HIGH
00052     if (sw_val&(1<<0)){ printf("Switch 0 :"); }
00053     if (sw_val&(1<<1)){ printf("Switch 1 :"); }
00054     if (sw_val&(1<<2)){ printf("Switch 2 :"); }
00055     if (sw_val&(1<<3)){ printf("Switch 3 :"); }
00056     if (sw_val>0)     { printf("\r\n");       }
00057     return sw_val;    
00058 }