SPI程序
void spi_init(void)
{
SPI_InitTypeDef SPI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/*?±???è?? */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOC, ENABLE);//?°??GPIO?è???±?±???????? ?????????è??
/*SCK ?±???????è??*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/*MOSI ?????è?? */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* MISO ?????è??*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/*NSS ?????????è??*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
/*RC522复位管脚*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
/*???? NSS????*/
SET_SPI_CS;
/* */
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;//SPI ?è???????????ò?????¤
SPI_InitStructure.SPI_Mode = SPI_Mode_Master; //?è?????÷SPI
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; //?è????·???????8?????á??
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; //?±???ü????
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; //????????????1???±????
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //????NSS??????SSI??????
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8; //?¨????·???????4 ?¨??????9M
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //??????????????????
SPI_InitStructure.SPI_CRCPolynomial = 7; //CRC???é
SPI_Init(SPI1, &SPI_InitStructure);
/*????SPI1 */
SPI_Cmd(SPI1, ENABLE);
}
u8 SPIWriteByte(u8 Byte)
{
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);//????·???????
SPI_I2S_SendData(SPI1, Byte);//·???????×???
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);//????·????ê??
return SPI_I2S_ReceiveData(SPI1);//????????×???
}
RC522读寄存器程序
u8 ReadRawRC(unsigned char Address)
{
u8 ucAddr;
u8 ucResult=0;
CLR_SPI_CS;
ucAddr = ((Address<<1)&0x7E)|0x80;
SPIWriteByte(ucAddr);
ucResult=SPIWriteByte(0);
SET_SPI_CS;
return ucResult;
}
读寄存器时无法读到
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
---------------------------------
oo 谢谢帮顶
一周热门 更多>