Dependencies:   mbed

Committer:
BenRJG
Date:
Sun Apr 01 15:43:02 2018 +0000
Revision:
1:acc66d3a1a1c
Parent:
0:87903e5535bc
Child:
2:b615682e3e4f
Converted supplied SPI code to separate SPI files

Who changed what in which revision?

UserRevisionLine numberNew contents of line
BenRJG 1:acc66d3a1a1c 1 #include "SPI.h"
BenRJG 0:87903e5535bc 2
BenRJG 0:87903e5535bc 3 int main (void)
BenRJG 0:87903e5535bc 4 {
BenRJG 1:acc66d3a1a1c 5 cs = 1; // Chip must be deselected, Chip Select is active LOW
BenRJG 1:acc66d3a1a1c 6 LCD_cs = 1; // Chip must be deselected, Chip Select is active LOW
BenRJG 1:acc66d3a1a1c 7 ADC_cs = 1; // Chip must be deselected, Chip Select is active LOW
BenRJG 1:acc66d3a1a1c 8 spi.format(16,0); // Setup the DATA frame SPI for 16 bit wide word, Clock Polarity 0 and Clock Phase 0 (0)
BenRJG 1:acc66d3a1a1c 9 spi_cmd.format(8,0); // Setup the COMMAND SPI as 8 Bit wide word, Clock Polarity 0 and Clock Phase 0 (0)
BenRJG 1:acc66d3a1a1c 10 spi.frequency(1000000); // 1MHz clock rate
BenRJG 1:acc66d3a1a1c 11 spi_cmd.frequency(1000000); // 1MHz clock rate
BenRJG 1:acc66d3a1a1c 12
BenRJG 1:acc66d3a1a1c 13 int adval_d = 0; //A to D value read back
BenRJG 1:acc66d3a1a1c 14 float adval_f =0.0f;
BenRJG 1:acc66d3a1a1c 15 int err = 0; //error variable used for debugging, trapping etc.,
BenRJG 1:acc66d3a1a1c 16
BenRJG 1:acc66d3a1a1c 17 char adval[32];
BenRJG 1:acc66d3a1a1c 18
BenRJG 1:acc66d3a1a1c 19 // Preload some arrays
BenRJG 1:acc66d3a1a1c 20 // char hello_world[]="Hello World";
BenRJG 1:acc66d3a1a1c 21 char splash_screen1[]="Martin Simpson";
BenRJG 1:acc66d3a1a1c 22 char splash_screen2[]="Plymouth UNI";
BenRJG 1:acc66d3a1a1c 23 char DVM[]="Voltage=";
BenRJG 1:acc66d3a1a1c 24 // Start up sequences
BenRJG 1:acc66d3a1a1c 25 lcd_cls();
BenRJG 1:acc66d3a1a1c 26 lcd_locate(1,1);
BenRJG 1:acc66d3a1a1c 27 lcd_display(splash_screen1); //Credit line 1
BenRJG 1:acc66d3a1a1c 28 lcd_locate(2,2);
BenRJG 1:acc66d3a1a1c 29 lcd_display(splash_screen2); //Credit line 2
BenRJG 1:acc66d3a1a1c 30 wait(2);
BenRJG 1:acc66d3a1a1c 31 lcd_cls();
BenRJG 1:acc66d3a1a1c 32 pulse_bar_graph(); //Flashy bargraph clear screen
BenRJG 1:acc66d3a1a1c 33 lcd_locate(1,0);
BenRJG 1:acc66d3a1a1c 34 lcd_display(DVM); //Type Voltage display
BenRJG 1:acc66d3a1a1c 35 lcd_locate(1,13);
BenRJG 1:acc66d3a1a1c 36 lcd_display("V"); //Units display
BenRJG 1:acc66d3a1a1c 37
BenRJG 1:acc66d3a1a1c 38 while(true) //Loop forever Knight Rider Display on FPGA
BenRJG 0:87903e5535bc 39 {
BenRJG 1:acc66d3a1a1c 40 adval_d = read_adc();
BenRJG 0:87903e5535bc 41
BenRJG 1:acc66d3a1a1c 42 adval_f = 3.3f*((float)adval_d/4095);//Convert 12 bit to a float and scale
BenRJG 1:acc66d3a1a1c 43 sprintf(adval,"%.3f",adval_f); //Store in an array string
BenRJG 1:acc66d3a1a1c 44 lcd_locate(1,8); //and display on LCD
BenRJG 1:acc66d3a1a1c 45 lcd_display(adval); //
BenRJG 1:acc66d3a1a1c 46
BenRJG 1:acc66d3a1a1c 47 err = bar_graph(adval_d/255); // 16*256 =4096 12 bit ADC!
BenRJG 1:acc66d3a1a1c 48 if (err < 0){printf("Display Overload\r\n");}
BenRJG 1:acc66d3a1a1c 49
BenRJG 1:acc66d3a1a1c 50 read_switches();
BenRJG 1:acc66d3a1a1c 51 //LED Chaser display KIT lives on!
BenRJG 1:acc66d3a1a1c 52 for (uint32_t i=1;i<=128;i*=2)
BenRJG 1:acc66d3a1a1c 53 {
BenRJG 1:acc66d3a1a1c 54 cs = 0; //Select the device by seting chip select LOW
BenRJG 1:acc66d3a1a1c 55 spi_cmd.write(0);
BenRJG 1:acc66d3a1a1c 56 spi.write(i);
BenRJG 1:acc66d3a1a1c 57 cs = 1; //De-Select the device by seting chip select HIGH
BenRJG 1:acc66d3a1a1c 58 wait_ms(20);
BenRJG 1:acc66d3a1a1c 59 }
BenRJG 1:acc66d3a1a1c 60 for (uint32_t i=128;i>=1;i/=2)
BenRJG 1:acc66d3a1a1c 61 {
BenRJG 1:acc66d3a1a1c 62 cs = 0; //Select the device by seting chip select LOW
BenRJG 1:acc66d3a1a1c 63 spi_cmd.write(0);
BenRJG 1:acc66d3a1a1c 64 spi.write(i);
BenRJG 1:acc66d3a1a1c 65 cs = 1; //De-Select the device by seting chip select HIGH
BenRJG 1:acc66d3a1a1c 66 wait_ms(20);
BenRJG 1:acc66d3a1a1c 67 }
BenRJG 0:87903e5535bc 68 }
BenRJG 0:87903e5535bc 69 }