Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of Muscle_Controlled_Servo by
Diff: SPI.cpp
- Revision:
- 4:bcef9164776e
- Parent:
- 3:d6e142b6ead1
- Child:
- 5:11489c0bd020
--- a/SPI.cpp Sun Apr 01 16:22:36 2018 +0000 +++ b/SPI.cpp Tue Apr 03 20:00:23 2018 +0000 @@ -21,11 +21,37 @@ char adval[32]; //Ticker ticktock; + +void spi_write_data(short destination, unsigned int data) +{ + cs = destination; //Select the device by seting chip select LOW + spi_cmd.write(0); + spi.write(data); + cs = CS_RESET; //De-Select the device by seting chip select HIGH +} + +int spi_read_data(short destination) +{ + cs = destination; //Select the device by seting chip select LOW + unsigned int read = spi.write(0); + cs = CS_RESET; //De-Select the device by seting chip select HIGH + return read; +} + +int spi_read_write_data(short destination, unsigned int data) +{ + cs = destination; //Select the device by seting chip select LOW + spi_cmd.write(0); + unsigned int read = spi.write(data); + cs = CS_RESET; //De-Select the device by seting chip select HIGH + return read; +} + + + void SPI_INIT (void) { - LED_cs = 1; // Chip must be deselected, Chip Select is active LOW - LCD_cs = 1; // Chip must be deselected, Chip Select is active LOW - ADC_cs = 1; // Chip must be deselected, Chip Select is active LOW + cs = CS_RESET; // Chip must be deselected, Chip Select is active LOW spi.format(16,0); // Setup the DATA frame SPI for 16 bit wide word, Clock Polarity 0 and Clock Phase 0 (0) spi_cmd.format(8,0); // Setup the COMMAND SPI as 8 Bit wide word, Clock Polarity 0 and Clock Phase 0 (0) @@ -48,8 +74,7 @@ lcd_locate(2,2); lcd_display(splash_screen2); //Credit line 2 wait(2); - lcd_cls(); - pulse_bar_graph(); //Flashy bargraph clear screen + lcd_cls(); lcd_locate(1,0); lcd_display(DVM); //Type Voltage display lcd_locate(1,13); @@ -72,24 +97,19 @@ //LED Chaser display KIT lives on! for (uint32_t i=1;i<=128;i*=2) { - LED_cs = 0; //Select the device by seting chip select LOW - spi_cmd.write(0); - spi.write(i); - LED_cs = 1; //De-Select the device by seting chip select HIGH + spi_write_data(CS_GENERAL,i); //Send data through spi wait_ms(20); } for (uint32_t i=128;i>=1;i/=2) { - LED_cs = 0; //Select the device by seting chip select LOW - spi_cmd.write(0); - spi.write(i); - LED_cs = 1; //De-Select the device by seting chip select HIGH + spi_write_data(CS_GENERAL,i); //Send data through spi wait_ms(20); } } - +////////////////////////////////////////////////////////// int lcd_cls(void){ - LCD_cs = 0;spi_cmd.write(0);spi.write(0x0001);LCD_cs = 1;wait_us(CD); //Clear Screen + spi_write_data(CS_LCD,0x0001); + wait_us(CD); //Clear Screen return 0; } @@ -103,10 +123,7 @@ } if(column<16){column_addr=column;} else{return -1;} - LCD_cs = 0; - spi_cmd.write(0); - spi.write(line_addr+column_addr); - LCD_cs = 1; + spi_write_data(CS_LCD,(line_addr+column_addr)); wait_us(CD); //DDRAM location Second line is 0x00C0 first line starts at 0x0080 return 0; } @@ -125,10 +142,7 @@ } for (int i=0; i<strlen(str);i++){ - LCD_cs = 0; - spi_cmd.write(0); - spi.write((command_data<<8)+str[i]); - LCD_cs = 1; + spi_write_data(CS_LCD,((command_data<<8)+str[i])); wait_us(wait_time); } return 0; @@ -136,51 +150,32 @@ int bar_graph(uint8_t level){ if (level>16){return -1;} //return code !=0 is error - LCD_cs = 0;spi_cmd.write(0);spi.write(0x00C0);LCD_cs = 1;wait_us(CD); //DDRAM location Second line is 0x00C0 first line starts at 0x0080 + spi_write_data(CS_LCD,0x00C0); + wait_us(CD); //DDRAM location Second line is 0x00C0 first line starts at 0x0080 for (int i=1; i<=level ;i++) { - if(level>0){LCD_cs = 0;spi_cmd.write(0);spi.write(0x01FF);LCD_cs = 1;wait_us(DD);} // BLACK SPACE - else{LCD_cs = 0;spi_cmd.write(0);spi.write(0x0120);LCD_cs = 1;wait_us(DD);} // WHITE SPACE + if(level>0){cs = CS_LCD;spi_cmd.write(0);spi.write(0x01FF);cs = CS_RESET;wait_us(DD);} // BLACK SPACE + else{spi_write_data(CS_LCD,0x0120);wait_us(DD);} // WHITE SPACE } for (int i=level; i<=16 ;i++) { - LCD_cs = 0;spi_cmd.write(0);spi.write(0x0120);LCD_cs = 1;wait_us(DD); // SPACE + spi_write_data(CS_LCD,0x0120);wait_us(DD); // SPACE } return 0; // return code ==0 is OK } - +/////////////////////////////////////////////////////////////////////////////////////////////// int read_adc(void){ int adval_d; float adval_f; - ADC_cs = 0; - adval_d = spi.write(0x00); - ADC_cs =1 ; + adval_d = spi_read_data(CS_ADC); adval_f = 3.3f*((float)adval_d/4095); printf("%d %.3fV\r\n",adval_d,adval_f); return adval_d; } - -void pulse_bar_graph(void){ - for (uint8_t i=0;i<16;i++) - { - printf("%u\r\n",i); - bar_graph(i); - wait_ms(100); - } - for (int8_t i=15;i>=0;i--) - { - printf("%u\r\n",i); - bar_graph(i); - wait_ms(100); - } -} - +//////////////////////////////////////////////////////////////////// int read_switches(void){ int sw_val; - LED_cs = 0; - spi_cmd.write(0); - sw_val = spi.write(0x00)&0x0F; // Just want lower 4bit nibble - LED_cs = 1 ; + sw_val = spi_read_data(CS_GENERAL)&0x0F; // Just want lower 4bit nibble if (sw_val&(1<<0)){printf("Switch 0 :");} if (sw_val&(1<<1)){printf("Switch 1 :");} if (sw_val&(1<<2)){printf("Switch 2 :");}