SPI Demo - Used for the coursework and VHDL Lab 07 (Sequential Machines), Task 01-01

Dependencies:   mbed

Committer:
noutram
Date:
Wed Nov 14 14:28:12 2018 +0000
Revision:
0:a4e0322eee67
ELEC240 Practical 07 Task 01-01

Who changed what in which revision?

UserRevisionLine numberNew contents of line
noutram 0:a4e0322eee67 1 #include "mbed.h"
noutram 0:a4e0322eee67 2
noutram 0:a4e0322eee67 3 SPI spi(PA_7, PA_6, PA_5); // Ordered as: mosi, miso, sclk could use forth parameter ssel
noutram 0:a4e0322eee67 4 DigitalOut cs(PC_6); // Chip Select for Basic Outputs to illuminate Onboard FPGA DEO nano LEDs CN7 pin 1
noutram 0:a4e0322eee67 5
noutram 0:a4e0322eee67 6 int32_t read_switches(void); //Read 4 Sliding switches on FPGA (Simulating OPTO-Switches from Motor(s)
noutram 0:a4e0322eee67 7
noutram 0:a4e0322eee67 8 //NBB the following line for F429ZI !!!!
noutram 0:a4e0322eee67 9 DigitalIn DO_NOT_USE(PB_12); // MAKE PB_12 (D19) an INPUT do NOT make an OUTPUT under any circumstances !!!!! ************* !!!!!!!!!!!
noutram 0:a4e0322eee67 10 // This Pin is connected to the 5VDC from the FPGA card and an INPUT is 5V Tolerant
noutram 0:a4e0322eee67 11
noutram 0:a4e0322eee67 12 //Ticker ticktock;
noutram 0:a4e0322eee67 13
noutram 0:a4e0322eee67 14 int main() {
noutram 0:a4e0322eee67 15 cs = 1; // Chip must be deselected, Chip Select is active LOW
noutram 0:a4e0322eee67 16 spi.format(8,0); // Setup the DATA frame SPI for 16 bit wide word, Clock Polarity 0 and Clock Phase 0 (0)
noutram 0:a4e0322eee67 17 spi.frequency(1000000); // 1MHz clock rate
noutram 0:a4e0322eee67 18
noutram 0:a4e0322eee67 19 printf("TEST\n\r");
noutram 0:a4e0322eee67 20 while(true) //Loop forever Knight Rider Display on FPGA
noutram 0:a4e0322eee67 21 {
noutram 0:a4e0322eee67 22 read_switches();
noutram 0:a4e0322eee67 23
noutram 0:a4e0322eee67 24 //LED Chaser display KIT lives on!
noutram 0:a4e0322eee67 25 for (uint32_t i=1;i<=128;i*=2)
noutram 0:a4e0322eee67 26 {
noutram 0:a4e0322eee67 27 cs = 0; //Select the device by seting chip select LOW
noutram 0:a4e0322eee67 28 spi.write(0); //Send the command
noutram 0:a4e0322eee67 29 spi.write(i); //Send the data - ignore the return data
noutram 0:a4e0322eee67 30 cs = 1; //De-Select the device by seting chip select HIGH
noutram 0:a4e0322eee67 31 wait_ms(20);
noutram 0:a4e0322eee67 32 }
noutram 0:a4e0322eee67 33 for (uint32_t i=128;i>=1;i/=2)
noutram 0:a4e0322eee67 34 {
noutram 0:a4e0322eee67 35 cs = 0; //Select the device by seting chip select LOW
noutram 0:a4e0322eee67 36 spi.write(0); //Send the command
noutram 0:a4e0322eee67 37 spi.write(i); //Send the data - ignore the return data
noutram 0:a4e0322eee67 38 cs = 1; //De-Select the device by seting chip select HIGH
noutram 0:a4e0322eee67 39 wait_ms(20);
noutram 0:a4e0322eee67 40 }
noutram 0:a4e0322eee67 41 wait_ms(1000);
noutram 0:a4e0322eee67 42 }
noutram 0:a4e0322eee67 43 }
noutram 0:a4e0322eee67 44
noutram 0:a4e0322eee67 45 //Function to read back the state of the switches
noutram 0:a4e0322eee67 46 int read_switches(void){
noutram 0:a4e0322eee67 47 int sw_val = 0;
noutram 0:a4e0322eee67 48 cs = 0; //Select the device by seting chip select LOW
noutram 0:a4e0322eee67 49 spi.write(0); //Command
noutram 0:a4e0322eee67 50 sw_val = spi.write(0x01) & 0x0F; // Just want to read back lower 4bit nibble
noutram 0:a4e0322eee67 51 cs = 1 ; //De-select the device by seting chip select HIGH
noutram 0:a4e0322eee67 52 if (sw_val&(1<<0)){ printf("Switch 0 :"); }
noutram 0:a4e0322eee67 53 if (sw_val&(1<<1)){ printf("Switch 1 :"); }
noutram 0:a4e0322eee67 54 if (sw_val&(1<<2)){ printf("Switch 2 :"); }
noutram 0:a4e0322eee67 55 if (sw_val&(1<<3)){ printf("Switch 3 :"); }
noutram 0:a4e0322eee67 56 if (sw_val>0) { printf("\r\n"); }
noutram 0:a4e0322eee67 57 return sw_val;
noutram 0:a4e0322eee67 58 }