9 Spi
9 Spi
com
BIHE university
SPI
• Synchronous
• Full-duplex
• Serial
• Fast communication
• For short distances Master Slave
SDO (MOSI) SDI (MOSI)
• Pins
SDI (MISO) SDO (MISO)
– SDO (Data Out)
SCLK SCLK
– SDI (Data In)
– SCLK (shift clock) CE
Wave CE
Generator
GND GND
Master
SDO
SDI
SCK
SCK
SDO
SDI
SCK
SDO
SDI
CE
CE
Slave1
Slave2
Electronics Department, HCMUT 5
Polarity and Phase
www. Micro Digital Ed. com
BIHE university
Idle = 0
Idle = 1
SPI2
SPI1
BIDIMODE 15 Bidirectional data mode enable (0: 2-line unidirectional, 1: 1-line bidirectional)
BIDIOE 14 When BIDIMODE=1 (1-line bidirectional), the BIDIOE bit selects the direction of transfer (0:
receive, 1: transmit)
CRCEN 13 Hardware CRC calculation enable (0: disabled, 1: enabled)
CRCNEXT 12 CRC transfer next (0: data transfer, 1: next transfer is CRC)
DFF 11 Data Frame format (0: 8-bit data frame, 1: 16-bit data frame)
RXONLY 10 Receive only (0: Both transmit & receive (Full-duplex), 1: Receive only)
SSM 9 Software Slave Management (0: NSS pin, 1: SSI bit)
If the bit is set, the SSI bit manages the communication instead of the NSS pin.
SSI 8 Internal Slave Select
LSBFIRST 7 LSB First Enable
1 = Data is transferred least significant bit first.
0 = Data is transferred most significant bit first.
SPE 6 SPI System Enable bit
1 = Enables SPI port and configures pins as serial port pins
BR 0 0 = Disables 1 SPI port and2 configures 3these pins as4I/O ports 5 6 7
BR Speed 5-3PCLK/2
Baud rate control
PCLK/4 PCLK/8 PCLK/16 PCLK/32 PCLK/64 PCLK/128 PCLK/256
MSTR 2 SPI Master/Slave mode Select bit. This bit selects master or slave mode.
1 = SPI in master mode
0 = SPI in slave mode
CPOL 1 SPI Clock Polarity bit
1 = Active-LOW clocks selected. In idle state SCK is high.
0 = Active-HIGH clocks selected. In idle state SCK is low.
CPHA 0 SPI Clock Phase bit
1 = Sampling of data occurs at even edges of the SCK clock.
0 = Sampling of data occurs at odd edges of the SCK clock.
Electronics Department, HCMUT 8
Example
www. Micro Digital Ed. com
BIHE university
#include <stm32f10x.h>
void spi1_init(void);
uint8_t spi1_transfer(uint8_t d);
int main( ) {
RCC->APB2ENR |= 0xFC; /* enable clocks for GPIO */
/*--- make the NSS pin of the slave low if needed ---*/
while(1)
{ }
}
Electronics Department, HCMUT 14
MAX7219/MAX7221
www. Micro Digital Ed. com
BIHE university
#include <stm32f10x.h>
void spi1_init(void);
uint8_t spi1_transfer(uint8_t d);
void max7219_send(uint8_t cmd, uint8_t data);
int main( ) {
RCC->APB2ENR |= 0xFC; /* enable clocks for GPIO */
while(1) { }
}
Electronics Department, HCMUT 20
Example
www. Micro Digital Ed. com
BIHE university
#include <stm32f10x.h>
void spi1_init(void);
uint8_t spi1_transfer(uint8_t d);
void max7219_send(uint8_t cmd, uint8_t data);
int main( ) {
RCC->APB2ENR |= 0xFC; /* enable clocks for GPIO */
spi1_init(); /* initialize the SPI module */
max7219_send(0x09, 0x02); /* enable decoding for digit2 and disable for digit1 */
max7219_send(0x0B, 1); /* 2 (1+1) digits */
max7219_send(0x0C, 0x01); /* turn on */
while(1) {
}
}
Electronics Department, HCMUT 22