STM32的SPI接口
SPI接口:
本文引用地址:http://butianyuan.cn/article/201611/321483.htm MISO——主設(shè)備輸入/從設(shè)備輸出
MOSI——主設(shè)備輸出/從設(shè)備輸入
SCK——串口時(shí)鐘
NSS——從設(shè)備選擇
初始化例程如下:
void SPI_Init(void)
SPI_InitTypeDef
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7; //PA5.6.7復(fù)用
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_SetBits(GPIOA,GPIO_Pin_3);
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Hard;//SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI1, &SPI_InitStructure);
SPI_Cmd(SPI1, ENABLE); //使能SPI外設(shè)
SPI_ReadWriteByte(0xff);//啟動(dòng)傳輸
u8 SPIx_ReadWriteByte(u8 TxData)
不依靠軟件庫的初始化:
SPI1->CR1|=0<<10;//全雙工模式
SPI1->CR1|=1<<9; //軟件nss管理
SPI1->CR1|=1<<8;
SPI1->CR1|=1<<2; //SPI主機(jī)
SPI1->CR1|=0<<11;//8bit數(shù)據(jù)格式
SPI1->CR1|=1<<1; //空閑模式下SCK為1 CPOL=1
SPI1->CR1|=1<<0; //數(shù)據(jù)采樣從第二個(gè)時(shí)間邊沿開始,CPHA=1
SPI1->CR1|=7<<3; //Fsck=Fcpu/256
SPI1->CR1|=0<<7; //MSBfirst
SPI1->CR1|=1<<6; //SPI設(shè)備使能
評(píng)論