|
| 1 | +--- |
| 2 | +title: SPISettings |
| 3 | +categories: [ "Functions" ] |
| 4 | +subCategories: [ "Communication" ] |
| 5 | +--- |
| 6 | + |
| 7 | +== SPISettings |
| 8 | + |
| 9 | +[float] |
| 10 | +==== Description |
| 11 | + |
| 12 | +The SPISettings object is used to configure the SPI port for your SPI device. All 3 parameters are combined to a single SPISettings object, which is given to SPI.beginTransaction(). |
| 13 | + |
| 14 | +When all of your settings are constants, SPISettings should be used directly in SPI.beginTransaction(). See the syntax section below. For constants, this syntax results in smaller and faster code. |
| 15 | + |
| 16 | +If any of your settings are variables, you may create a SPISettings object to hold the 3 settings. Then you can give the object name to SPI.beginTransaction(). Creating a named SPISettings object may be more efficient when your settings are not constants, especially if the maximum speed is a variable computed or configured, rather than a number you type directly into your sketch. |
| 17 | + |
| 18 | +==== Syntax |
| 19 | + |
| 20 | +---- |
| 21 | +SPI.beginTransaction(SPISettings(14000000, MSBFIRST, SPI_MODE0)) |
| 22 | +---- |
| 23 | + |
| 24 | +NOTE: Best if all 3 settings are constants |
| 25 | + |
| 26 | +---- |
| 27 | +SPISettings mySettting(speedMaximum, dataOrder, dataMode) |
| 28 | +---- |
| 29 | + |
| 30 | +NOTE: Best when any setting is a variable |
| 31 | + |
| 32 | +==== Parameters |
| 33 | + |
| 34 | +speedMaximum: The maximum speed of communication. For a SPI chip rated up to 20 MHz, use 20000000. |
| 35 | + |
| 36 | +dataOrder: MSBFIRST or LSBFIRST |
| 37 | + |
| 38 | +dataMode : SPI_MODE0, SPI_MODE1, SPI_MODE2, or SPI_MODE3 |
| 39 | + |
| 40 | +==== Returns |
| 41 | + |
| 42 | +None. |
| 43 | + |
| 44 | +[float] |
| 45 | +=== `begin()` |
| 46 | + |
| 47 | +==== Description |
| 48 | + |
| 49 | +Initializes the SPI bus by setting SCK, MOSI, and SS to outputs, pulling SCK and MOSI low, and SS high. |
| 50 | + |
| 51 | +==== Syntax |
| 52 | + |
| 53 | +---- |
| 54 | +SPI.begin() |
| 55 | +---- |
| 56 | + |
| 57 | +==== Parameters |
| 58 | + |
| 59 | +None |
| 60 | + |
| 61 | +==== Returns |
| 62 | + |
| 63 | +None |
| 64 | + |
| 65 | +[float] |
| 66 | +=== `end()` |
| 67 | + |
| 68 | +==== Description |
| 69 | + |
| 70 | +Disables the SPI bus (leaving pin modes unchanged). |
| 71 | + |
| 72 | +==== Syntax |
| 73 | + |
| 74 | +---- |
| 75 | +SPI.end() |
| 76 | +---- |
| 77 | + |
| 78 | +==== Parameters |
| 79 | + |
| 80 | +None |
| 81 | + |
| 82 | +==== Returns |
| 83 | + |
| 84 | +None |
| 85 | + |
| 86 | +[float] |
| 87 | +=== `beginTransaction()` |
| 88 | + |
| 89 | +==== Description |
| 90 | + |
| 91 | +Initializes the SPI bus using the defined SPISettings. |
| 92 | + |
| 93 | +==== Syntax |
| 94 | + |
| 95 | +---- |
| 96 | +SPI.beginTransaction(mySettings); |
| 97 | +---- |
| 98 | + |
| 99 | +==== Parameters |
| 100 | + |
| 101 | +mySettings: the chosen settings according to SPISettings. |
| 102 | + |
| 103 | +==== Returns |
| 104 | + |
| 105 | +None. |
| 106 | + |
| 107 | +[float] |
| 108 | +=== `endTransaction()` |
| 109 | + |
| 110 | +==== Description |
| 111 | + |
| 112 | +Stop using the SPI bus. Normally this is called after de-asserting the chip select, to allow other libraries to use the SPI bus. |
| 113 | + |
| 114 | +==== Syntax |
| 115 | + |
| 116 | +---- |
| 117 | +SPI.endTransaction() |
| 118 | +---- |
| 119 | + |
| 120 | +==== Parameters |
| 121 | + |
| 122 | +None. |
| 123 | + |
| 124 | +==== Returns |
| 125 | + |
| 126 | +None. |
| 127 | + |
| 128 | +[float] |
| 129 | +=== `setBitOrder()` |
| 130 | + |
| 131 | +==== Description |
| 132 | + |
| 133 | +This function should not be used in new projects. Use SPISettings with SPI.beginTransaction() to configure SPI parameters. |
| 134 | + |
| 135 | +Sets the order of the bits shifted out of and into the SPI bus, either LSBFIRST (least-significant bit first) or MSBFIRST (most-significant bit first). |
| 136 | + |
| 137 | +==== Syntax |
| 138 | + |
| 139 | +---- |
| 140 | +SPI.setBitOrder(order) |
| 141 | +---- |
| 142 | + |
| 143 | +==== Parameters |
| 144 | + |
| 145 | +order: either LSBFIRST or MSBFIRST |
| 146 | + |
| 147 | +==== Returns |
| 148 | + |
| 149 | +None |
| 150 | + |
| 151 | +[float] |
| 152 | +=== `setClockDivider()` |
| 153 | + |
| 154 | +==== Description |
| 155 | + |
| 156 | +This function should not be used in new projects. Use SPISettings with SPI.beginTransaction() to configure SPI parameters. |
| 157 | + |
| 158 | +Sets the SPI clock divider relative to the system clock. On AVR based boards, the dividers available are 2, 4, 8, 16, 32, 64 or 128. The default setting is SPI_CLOCK_DIV4, which sets the SPI clock to one-quarter the frequency of the system clock (4 Mhz for the boards at 16 MHz). |
| 159 | + |
| 160 | +===== Arduino Due |
| 161 | +On the Due, the system clock can be divided by values from 1 to 255. The default value is 21, which sets the clock to 4 MHz like other Arduino boards. |
| 162 | + |
| 163 | +==== Syntax |
| 164 | + |
| 165 | +---- |
| 166 | +SPI.setClockDivider(divider) |
| 167 | +---- |
| 168 | + |
| 169 | +==== Parameters |
| 170 | + |
| 171 | +divider (On AVR boards): |
| 172 | + |
| 173 | +* SPI_CLOCK_DIV2 |
| 174 | +* SPI_CLOCK_DIV4 |
| 175 | +* SPI_CLOCK_DIV8 |
| 176 | +* SPI_CLOCK_DIV16 |
| 177 | +* SPI_CLOCK_DIV32 |
| 178 | +* SPI_CLOCK_DIV64 |
| 179 | +* SPI_CLOCK_DIV128 |
| 180 | + |
| 181 | +slaveSelectPin: |
| 182 | + |
| 183 | +* slave device SS pin (Arduino Due only) |
| 184 | + |
| 185 | +divider (Arduino Due only): |
| 186 | + |
| 187 | +* a number from 1 to 255 (Arduino Due only) |
| 188 | + |
| 189 | +==== Returns |
| 190 | + |
| 191 | +None |
| 192 | + |
| 193 | +[float] |
| 194 | +=== `setDataMode()` |
| 195 | + |
| 196 | +==== Description |
| 197 | + |
| 198 | +This function should not be used in new projects. Use SPISettings with SPI.beginTransaction() to configure SPI parameters. |
| 199 | + |
| 200 | +Sets the SPI data mode: that is, clock polarity and phase. See the Wikipedia article on SPI for details. |
| 201 | + |
| 202 | +==== Syntax |
| 203 | + |
| 204 | +---- |
| 205 | +SPI.setDataMode(mode) |
| 206 | +---- |
| 207 | + |
| 208 | +==== Parameters |
| 209 | + |
| 210 | +mode: |
| 211 | + |
| 212 | +* SPI_MODE0 |
| 213 | +* SPI_MODE1 |
| 214 | +* SPI_MODE2 |
| 215 | +* SPI_MODE3 |
| 216 | + |
| 217 | +slaveSelectPin |
| 218 | + |
| 219 | +* slave device SS pin (Arduino Due only) |
| 220 | + |
| 221 | +==== Returns |
| 222 | + |
| 223 | +None |
| 224 | + |
| 225 | +[float] |
| 226 | +=== `transfer()` |
| 227 | + |
| 228 | +==== Description |
| 229 | + |
| 230 | +SPI transfer is based on a simultaneous send and receive: the received data is returned in receivedVal (or receivedVal16). In case of buffer transfers the received data is stored in the buffer in-place (the old data is replaced with the data received). |
| 231 | + |
| 232 | +==== Syntax |
| 233 | + |
| 234 | +---- |
| 235 | +receivedVal = SPI.transfer(val) |
| 236 | +receivedVal16 = SPI.transfer16(val16) |
| 237 | +SPI.transfer(buffer, size) |
| 238 | +---- |
| 239 | + |
| 240 | +==== Parameters |
| 241 | + |
| 242 | +* val: the byte to send out over the bus |
| 243 | +* val16: the two bytes variable to send out over the bus |
| 244 | +* buffer: the array of data to be transferred |
| 245 | + |
| 246 | +==== Returns |
| 247 | + |
| 248 | +the received data |
| 249 | + |
| 250 | +[float] |
| 251 | +=== `usingInterrupt()` |
| 252 | + |
| 253 | +==== Description |
| 254 | + |
| 255 | +If your program will perform SPI transactions within an interrupt, call this function to register the interrupt number or name with the SPI library. This allows SPI.beginTransaction() to prevent usage conflicts. Note that the interrupt specified in the call to usingInterrupt() will be disabled on a call to beginTransaction() and re-enabled in endTransaction(). |
| 256 | + |
| 257 | +==== Syntax |
| 258 | + |
| 259 | +---- |
| 260 | +SPI.usingInterrupt(interruptNumber) |
| 261 | +---- |
| 262 | + |
| 263 | +==== Parameters |
| 264 | + |
| 265 | +interruptNumber: the associated interrupt number. |
| 266 | + |
| 267 | +==== Returns |
| 268 | + |
| 269 | +None. |
0 commit comments