Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- SPI_1 and SPI_2 port example code
- Description:
- This sketch sends one byte with value 0x55 over the SPI_1 or SPI_2 port.
- The received byte (the answer from the SPI slave device) is stored to the <data> variable.
- The sketch as it is, works with SPI_1 port. For using the SPI_2 port, just
- un-comment all the nessesary code lines marked with <SPI_2> word.
- Created on 10 Jun 2015 by Vassilis Serasidis
- email: avrsite@yahoo.gr
- Using the first SPI port (SPI_1)
- SS <--> PA4 <--> BOARD_SPI1_NSS_PIN
- SCK <--> PA5 <--> BOARD_SPI1_SCK_PIN
- MISO <--> PA6 <--> BOARD_SPI1_MISO_PIN
- MOSI <--> PA7 <--> BOARD_SPI1_MOSI_PIN
- Using the second SPI port (SPI_2)
- SS <--> PB12 <--> BOARD_SPI2_NSS_PIN
- SCK <--> PB13 <--> BOARD_SPI2_SCK_PIN
- MISO <--> PB14 <--> BOARD_SPI2_MISO_PIN
- MOSI <--> PB15 <--> BOARD_SPI2_MOSI_PIN
- */
- #include <SPI.h>
- #define SPI1_NSS_PIN PA4 //SPI_1 Chip Select pin is PA4. You can change it to the STM32 pin you want.
- const byte WRITE_CMD = 0b01110000;
- const byte WRITE_DTA = 0b01110010;
- const byte READ_DATA = 0b01110011;
- const byte ZEROES = 0x00;
- const byte ADDRESS = 0xB0; //0b10110000
- unsigned int receivedVal = 0;
- byte data;
- void setup() {
- Serial.begin(115200);
- Serial.println("--------------");
- // Setup SPI 1
- SPI.begin(); //Initialize the SPI_1 port.
- SPI.setBitOrder(MSBFIRST); // Set the SPI_1 bit order
- SPI.setDataMode(SPI_MODE3); //Set the SPI_2 data mode 0
- SPI.setClockDivider(SPI_CLOCK_DIV128); // Slow speed (72 / 16 = 4.5 MHz SPI_1 speed)
- pinMode(SPI1_NSS_PIN, OUTPUT);
- }
- void loop() {
- ssd_send_control();
- ssd_read();
- delayMicroseconds(5);
- }
- void ssd_send_control()
- {
- digitalWrite(SPI1_NSS_PIN, LOW);
- const uint8 buf[3] = {WRITE_CMD,0x00,ADDRESS};
- uint32 bufSize = 3;
- SPI.write(buf, bufSize);
- digitalWrite(SPI1_NSS_PIN, HIGH);
- }
- void ssd_read()
- {
- digitalWrite(SPI1_NSS_PIN, LOW);
- SPI.transfer(READ_DATA);
- uint8 ReadBuf[3];
- uint32 bufSize = 3;
- SPI.read(ReadBuf, bufSize);
- digitalWrite(SPI1_NSS_PIN, HIGH);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement