Skip to content

Commit 7d7f113

Browse files
author
rogerclarkmelbourne
committed
improved vasillis's SPI 2 demo
1 parent 8301021 commit 7d7f113

1 file changed

Lines changed: 34 additions & 22 deletions

File tree

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
/**
22
SPI_1 and SPI_2 port example code
3-
3+
44
Description:
55
This sketch sends one byte with value 0x55 over the SPI_1 or SPI_2 port.
66
The received byte (the answer from the SPI slave device) is stored to the <data> variable.
7-
7+
88
The sketch as it is, works with SPI_1 port. For using the SPI_2 port, just
99
un-comment all the nessesary code lines marked with <SPI_2> word.
10-
10+
1111
Created on 10 Jun 2015 by Vassilis Serasidis
1212
email: avrsite@yahoo.gr
13-
13+
1414
Using the first SPI port (SPI_1)
1515
SS <--> PA4 <--> BOARD_SPI1_NSS_PIN
1616
SCK <--> PA5 <--> BOARD_SPI1_SCK_PIN
1717
MISO <--> PA6 <--> BOARD_SPI1_MISO_PIN
1818
MOSI <--> PA7 <--> BOARD_SPI1_MOSI_PIN
19-
19+
2020
Using the second SPI port (SPI_2)
2121
SS <--> PB12 <--> BOARD_SPI2_NSS_PIN
2222
SCK <--> PB13 <--> BOARD_SPI2_SCK_PIN
@@ -30,35 +30,47 @@
3030
#define SPI1_NSS_PIN PA4 //SPI_1 Chip Select pin is PA4. You can change it to the STM32 pin you want.
3131
#define SPI2_NSS_PIN PB12 //SPI_2 Chip Select pin is PB12. You can change it to the STM32 pin you want.
3232

33-
//SPIClass SPI_2(2); //un-comment this line in case you want to use the SPI_2 port.
33+
SPIClass SPI_2(2); //Create an instance of the SPI Class called SPI_2 that uses the 2nd SPI Port
3434
byte data;
3535

3636
void setup() {
37+
38+
// Setup SPI 1
3739
SPI.begin(); //Initialize the SPI_1 port.
38-
//SPI_2.begin(); //Initialize the SPI_2 port.
39-
4040
SPI.setBitOrder(MSBFIRST); // Set the SPI_1 bit order
41-
//SPI_2.setBitOrder(MSBFIRST); // Set the SPI_2 bit order
42-
4341
SPI.setDataMode(SPI_MODE0); //Set the SPI_2 data mode 0
44-
//SPI_2.setDataMode(SPI_MODE0); //Set the SPI_2 data mode 0
45-
4642
SPI.setClockDivider(SPI_CLOCK_DIV16); // Slow speed (72 / 16 = 4.5 MHz SPI_1 speed)
47-
//SPI_2.setClockDivider(SPI_CLOCK_DIV16); // Slow speed (72 / 16 = 4.5 MHz SPI_2 speed)
43+
pinMode(SPI1_NSS_PIN, OUTPUT);
44+
45+
// Setup SPI 2
46+
SPI_2.begin(); //Initialize the SPI_2 port.
47+
SPI_2.setBitOrder(MSBFIRST); // Set the SPI_2 bit order
48+
SPI_2.setDataMode(SPI_MODE0); //Set the SPI_2 data mode 0
49+
SPI_2.setClockDivider(SPI_CLOCK_DIV16); // Use a different speed to SPI 1
50+
pinMode(SPI2_NSS_PIN, OUTPUT);
51+
4852

49-
pinMode(SPI1_NSS_PIN, OUTPUT); // note: this must be after the SPI.begin() for gpio control of CSN
50-
//pinMode(SPI2_NSS_PIN, OUTPUT); // note: this must be after the SPI_2.begin() for gpio control of CSN
5153
}
5254

5355
void loop() {
56+
57+
sendSPI();
58+
sendSPI2();
59+
60+
delayMicroseconds(10); //Delay 10 micro seconds.
61+
}
62+
63+
void sendSPI()
64+
{
5465
digitalWrite(SPI1_NSS_PIN, LOW); // manually take CSN low for SPI_1 transmission
55-
//digitalWrite(SPI2_NSS_PIN, LOW); // manually take CSN low for SPI_2 transmission
56-
5766
data = SPI.transfer(0x55); //Send the HEX data 0x55 over SPI-1 port and store the received byte to the <data> variable.
58-
//data = SPI_2.transfer(0x55); //Send the HEX data 0x55 over SPI-2 port and store the received byte to the <data> variable.
59-
6067
digitalWrite(SPI1_NSS_PIN, HIGH); // manually take CSN high between spi transmissions
61-
//digitalWrite(SPI2_NSS_PIN, HIGH); // manually take CSN high between spi transmissions
62-
63-
delayMicroseconds(10); //Delay 10 micro seconds.
6468
}
69+
70+
71+
void sendSPI2()
72+
{
73+
digitalWrite(SPI2_NSS_PIN, LOW); // manually take CSN low for SPI_2 transmission
74+
data = SPI_2.transfer(0x55); //Send the HEX data 0x55 over SPI-2 port and store the received byte to the <data> variable.
75+
digitalWrite(SPI2_NSS_PIN, HIGH); // manually take CSN high between spi transmissions
76+
}

0 commit comments

Comments
 (0)