Advertisement
Guest User

Untitled

a guest
Jan 17th, 2018
377
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.14 KB | None | 0 0
  1. /**
  2.     SPI_1 and SPI_2 port example code
  3.  
  4.     Description:
  5.     This sketch sends one byte with value 0x55 over the SPI_1 or SPI_2 port.
  6.     The received byte (the answer from the SPI slave device) is stored to the <data> variable.
  7.  
  8.     The sketch as it is, works with SPI_1 port. For using the SPI_2 port, just
  9.     un-comment all the nessesary code lines marked with <SPI_2> word.
  10.  
  11.     Created on 10 Jun 2015 by Vassilis Serasidis
  12.     email:  avrsite@yahoo.gr
  13.  
  14.     Using the first SPI port (SPI_1)
  15.     SS    <-->  PA4 <-->  BOARD_SPI1_NSS_PIN
  16.     SCK   <-->  PA5 <-->  BOARD_SPI1_SCK_PIN
  17.     MISO  <-->  PA6 <-->  BOARD_SPI1_MISO_PIN
  18.     MOSI  <-->  PA7 <-->  BOARD_SPI1_MOSI_PIN
  19.  
  20.     Using the second SPI port (SPI_2)
  21.     SS    <-->  PB12 <-->  BOARD_SPI2_NSS_PIN
  22.     SCK   <-->  PB13 <-->  BOARD_SPI2_SCK_PIN
  23.     MISO  <-->  PB14 <-->  BOARD_SPI2_MISO_PIN
  24.     MOSI  <-->  PB15 <-->  BOARD_SPI2_MOSI_PIN
  25. */
  26.  
  27.  
  28. #include <SPI.h>
  29.  
  30. #define SPI1_NSS_PIN PA4    //SPI_1 Chip Select pin is PA4. You can change it to the STM32 pin you want.
  31.  
  32. const byte WRITE_CMD = 0b01110000;
  33. const byte WRITE_DTA = 0b01110010;
  34. const byte READ_DATA = 0b01110011;
  35.  
  36. const byte ZEROES  = 0x00;
  37. const byte ADDRESS = 0xB0; //0b10110000
  38.  
  39. unsigned int receivedVal = 0;
  40.  
  41. byte data;
  42.  
  43. void setup() {
  44.  
  45.   Serial.begin(115200);
  46.   Serial.println("--------------");
  47.   // Setup SPI 1
  48.   SPI.begin(); //Initialize the SPI_1 port.
  49.   SPI.setBitOrder(MSBFIRST); // Set the SPI_1 bit order
  50.   SPI.setDataMode(SPI_MODE3); //Set the  SPI_2 data mode 0
  51.   SPI.setClockDivider(SPI_CLOCK_DIV128);      // Slow speed (72 / 16 = 4.5 MHz SPI_1 speed)
  52.   pinMode(SPI1_NSS_PIN, OUTPUT);
  53.  
  54.  
  55. }
  56.  
  57. void loop() {
  58.  
  59.   ssd_send_control();
  60.   ssd_read();
  61.  
  62.   delayMicroseconds(5);    
  63. }
  64.  
  65. void ssd_send_control()
  66. {
  67.   digitalWrite(SPI1_NSS_PIN, LOW);
  68.   const uint8 buf[3] = {WRITE_CMD,0x00,ADDRESS};
  69.   uint32 bufSize = 3;
  70.   SPI.write(buf, bufSize);
  71.   digitalWrite(SPI1_NSS_PIN, HIGH);
  72. }
  73.  
  74. void ssd_read()
  75. {
  76.   digitalWrite(SPI1_NSS_PIN, LOW);
  77.   SPI.transfer(READ_DATA);
  78.   uint8 ReadBuf[3];
  79.   uint32 bufSize = 3;
  80.   SPI.read(ReadBuf, bufSize);
  81.   digitalWrite(SPI1_NSS_PIN, HIGH);
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement