我用 stm32f407 驱动SPI CC1101片子一直不成功,代码如下: 希望好心人给我发一份示例:
lzzy.ha@qq.com
[mw_shl_code=c,true]
#define SPI1_NSS GPIO_Pin_4
#define SPI1_SCK GPIO_Pin_5
#define SPI1_MISO GPIO_Pin_6
#define SPI1_MOSI GPIO_Pin_7
#define GPIO_SPI1 GPIOA
#define RCC_GPIO_SPI1 RCC_AHB1Periph_GPIOA
void SPI1_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
SPI_InitTypeDef SPI_InitStruct;
// NVIC_InitTypeDef NVIC_InitStruct;
{// GPIO SPI
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
GPIO_InitStruct.GPIO_Pin = SPI1_SCK | SPI1_MISO | SPI1_MOSI;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_Speed = GPIO_High_Speed;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIO_SPI1, &GPIO_InitStruct);
// connect SPI1 pins to SPI alternate function
GPIO_PinAFConfig(GPIO_SPI1, GPIO_PinSource5, GPIO_AF_SPI1);
GPIO_PinAFConfig(GPIO_SPI1, GPIO_PinSource6, GPIO_AF_SPI1);
GPIO_PinAFConfig(GPIO_SPI1, GPIO_PinSource7, GPIO_AF_SPI1);
// CS
GPIO_InitStruct.GPIO_Pin = SPI1_NSS;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_Speed = GPIO_High_Speed;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIO_SPI1, &GPIO_InitStruct);
SPI1_Unselect();
}
{// SPI
RCC_AHB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
SPI_InitStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex; // set to full duplex mode, seperate MOSI and MISO lines
SPI_InitStruct.SPI_Mode = SPI_Mode_Master; // transmit in master mode, NSS pin has to be always high
SPI_InitStruct.SPI_DataSize = SPI_DataSize_8b; // one packet of data is 8 bits wide
SPI_InitStruct.SPI_CPOL = SPI_CPOL_Low; // clock is low when idle
SPI_InitStruct.SPI_CPHA = SPI_CPHA_1Edge; // data sampled at first edge
SPI_InitStruct.SPI_NSS = SPI_NSS_Soft; // set the NSS management to internal and pull internal NSS high
SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_128; // SPI frequency is APB2 frequency / 4
SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;// data is transmitted MSB first
SPI_InitStruct.SPI_CRCPolynomial = 7;
SPI_Init(SPI1, &SPI_InitStruct);
SPI_Cmd(SPI1, ENABLE); // enable SPI1
}
}
void SPI1_Select()
{
GPIO_ResetBits(GPIO_SPI1, SPI1_NSS);
}
void SPI1_Unselect()
{
GPIO_SetBits(GPIO_SPI1, SPI1_NSS);
}
uint8_t spi1_send_byte(uint8_t byte)
{
while(!SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE));
SPI_I2S_SendData(SPI1, byte);
while(!SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE));
return SPI_I2S_ReceiveData(SPI1);
}
uint8_t drv_spi_read_write_byte(uint8_t ch)
{
return spi1_send_byte(ch);
}[/mw_shl_code]
[mw_shl_code=c,true]
int main(void)
{
SystemInit();
LED_GPIO_Config();
//init_USART1(115200);
//printf("System starting...1
");
// CC1101_Init();
SPI1_Init();
uint8_t value;
SPI1_Select();
spi1_send_byte( CC1101_VERSION| READ_SINGLE );
value = spi1_send_byte(0xff);
SPI1_Unselect();
}[/mw_shl_code]
一周热门 更多>