spi四种工作模式时序图 spi通信时序图详解( 二 )


图4
图5
??所以说 , SPI有4中工作模式:
图6
更正:工作模式3的CPOL应为1 。
注意要让主机和从机需要在相同的工作模式下 , 这样才可以实现正常通讯 。
下面介绍用STM32库函数实SPI通讯代码 。
#ifndef __SPI_H#define __SPI_H
#include \\”stm32f10x.h\\”
【spi四种工作模式时序图 spi通信时序图详解】/* ?¨ò?è???±?á? */void SPI2_Config(void);void SPI2_SetSpeed(uint8_t Speed);uint8_t SPI2_WriteReadData(uint8_t dat);
void SPI1_Config(void);void SPI1_SetSpeed(uint8_t speed);uint8_t SPI1_WriteReadData(uint8_t dat);
/***************************************************************************** Function Name: SPI1_Config* Description: 3?ê??ˉSPI2* Input: None* Output: None* Return: None****************************************************************************/
void SPI1_Config(void){GPIO_InitTypeDef GPIO_InitStructure;SPI_InitTypeDefSPI_InitStructure;
/* SPIμ?IO?úoíSPIíaéè′ò?aê±?ó */RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
/* SPIμ?IO?úéè?? */GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);GPIO_SetBits(GPIOA,GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7); //PA5.6.7é?à-/***************************************************************************//************************* éè??SPIμ?2?êy ***********************************//***************************************************************************/
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;//????è???1¤SPI?£ê?SPI_InitStructure.SPI_Mode = SPI_Mode_Master;//?÷?ú?£ê?SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; //8??SPISPI_InitStructure.SPI_CPOL = SPI_CPOL_High;//ê±?óDü????μ???SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;//?úμú?t??ê±?ó2é?ˉêy?YSPI_InitStructure.SPI_NSS = SPI_NSS_Soft;//Nssê1ó?èí?t????/* ????2¨ì??ê?¤·??μ?a256 */SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;//′ó×??????aê?′?ê?SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Cmd(SPI1, ENABLE);SPI_Init(SPI1, &SPI_InitStructure);}
/***************************************************************************** Function Name: SPI2_Config* Description: 3?ê??ˉSPI2* Input: None* Output: None* Return: None****************************************************************************/
void SPI2_Config(void){GPIO_InitTypeDef GPIO_InitStructure;SPI_InitTypeDefSPI_InitStructure;
/* SPIμ?IO?úoíSPIíaéè′ò?aê±?ó */RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
/* SPIμ?IO?úéè?? */GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/***************************************************************************//************************* éè??SPIμ?2?êy ***********************************//***************************************************************************/
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;//????è???1¤SPI?£ê?SPI_InitStructure.SPI_Mode = SPI_Mode_Master;//?÷?ú?£ê?SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; //8??SPISPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;//ê±?óDü????μ???SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;//?úμú?t??ê±?ó2é?ˉêy?YSPI_InitStructure.SPI_NSS = SPI_NSS_Soft;//Nssê1ó?èí?t????/* ????2¨ì??ê?¤·??μ?a256 */SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;//′ó×??????aê?′?ê?SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Cmd(SPI2, ENABLE);SPI_Init(SPI2, &SPI_InitStructure);}
/***************************************************************************** Function Name: SPI1_SetSpeed* Description: éè??SPI1μ?′?ê??ù?è?£* Input: ?ù?è2¨ì??ê·??μ* Output: None* Return: None****************************************************************************/
void SPI1_SetSpeed(uint8_t speed){SPI1->CR1 &= 0xFFC7;SPI1->CR1 |= speed;SPI_Cmd(SPI1, ENABLE);
}
/***************************************************************************** Function Name: SPI2_SetSpeed* Description: éè??SPI2μ?·??μêy£?ò???±?SPI2μ??ù?è.* Input: Speed£o·??μêy* Output: None* Return: None****************************************************************************/
void SPI2_SetSpeed(uint8_t Speed){
SPI2->CR1 &= 0xFFC7;SPI2->CR1 |= Speed;SPI_Cmd(SPI2,ENABLE);
}
/***************************************************************************** Function Name: SPI1_WriteReadData* Description: ê1ó?SPI1D′è?ò???×??úêy?Yí?ê±?áè?ò???×??úêy?Y?£* Input: dat£oòaD′μ?8??êy?Y* Output: None* Return: ?áè?μ?μ?8??êy?Y****************************************************************************/