/////////////stm8l152 PB4/5/6/7口初始化 如下
void SPI_FLASH_Init(void)
{
/* Enable SPI clock */
CLK_PeripheralClockConfig(CLK_Peripheral_SPI1, ENABLE);
/* Set the MOSI,MISO and SCK at high level */
GPIO_ExternalPullUpConfig(GPIOB, P_SCLK | P_SI | P_SO, ENABLE);
/* Configure SPI_FLASH_CS as Output push-pull, used as Flash Chip select */
GPIO_Init(GPIOB, P_CSn, GPIO_Mode_Out_PP_High_Slow);
/* Deselect the FLASH: Chip Select high */
//SPI_FLASH_CS_HIGH();
GPIOB->ODR |= P_CSn ; // CSN =1
/* SPI configuration */
SPI_Init(SPI1, SPI_FirstBit_MSB, SPI_BaudRatePrescaler_2, SPI_Mode_Master,
SPI_CPOL_High, SPI_CPHA_2Edge, SPI_Direction_2Lines_FullDuplex,
SPI_NSS_Soft, 0x07);
/* Enable SPI */
SPI_Cmd(SPI1, ENABLE);
}
我现在用spi口对cc1100进行数据读写控制,采用如下子函数读写,看有不妥地方在哪?
////////////spi write 单一字节
#define WRITE_BURST 0x40
#define READ_SINGLE 0x80
#define READ_BURST 0xC0
#define WRITE_SINGLE 0x00
unsigned char SPI_write(uint8_t value)
{
GPIOB->ODR &= ~P_CSn ; // CSN =0 enable spi
SPI_FLASH_SendByte(WRITE_SINGLE); //
SPI_FLASH_SendByte(WRITE_BURST);
SPI_FLASH_SendByte(value);
GPIOB->ODR |= P_CSn ; // CSN =1
}
////SPI接口 从CC1100读取单一字节
unsigned char SPI_read()
{
uint8_t value;
GPIOB->ODR &= ~P_CSn ; // CSN =0 enable spi
SPI_FLASH_SendByte(READ_SINGLE);
SPI_FLASH_SendByte(READ_BURST);
value=SPI_ReceiveData(SPI1); //
GPIOB->ODR |= P_CSn ; // CSN =1
return value;
}
若有错误地方,希望指点下。非常感谢!
} |