|
这次调试SPI, 在网上查了不少资料, 特别是论坛上的, 现在硬件3线SPI已调通, 跟大家分享下. 由于外设是3线spi,MCU不得不也跟着使用3线SPI. MCU: STM8L051F3 外设: A7139 操作寄存器方式: 官方库函数 SPI: 3线 主要步骤: 1 初始化
/** * @brief Initializes the SPI and CS pins. * @param None * @retval None */ void SPI_LowLevel_Init(void) { /* Enable SPI clock */ CLK_PeripheralClockConfig(SPI_CLK, ENABLE); /* Set the MOSI,MISO and SCK at high level */ GPIO_ExternalPullUpConfig(SPI_SCK_GPIO_PORT, SPI_MISO_PIN | SPI_MOSI_PIN | \ SPI_SCK_PIN, ENABLE); /* SPI Config */ SPI_Init(SPI, SPI_FirstBit_MSB, SPI_BaudRatePrescaler_128, SPI_Mode_Master, SPI_CPOL_Low, SPI_CPHA_1Edge, SPI_Direction_1Line_Tx /*SPI_Direction_2Lines_FullDuplex*/, SPI_NSS_Soft, 0x07); /* SPI enable */ SPI_Cmd(SPI, ENABLE); /* Set MSD ChipSelect pin in Output push-pull high level */ GPIO_Init(CS_GPIO_PORT, CS_PIN, GPIO_Mode_Out_PP_High_Slow); } 2 读写函数
/** * @brief Write a byte . * @param Data: byte to send. * @retval None */ uint8_t SPIx_WriteByte(uint8_t Data) { //SPI_BiDirectionalLineConfig(SPI, SPI_Direction_Tx); //设置单线为发送 /*!< Wait until SPI is not busy */ while (SPI_GetFlagStatus(SPI, SPI_FLAG_BSY) != RESET) {} /*!< Send the byte */ SPI_SendData(SPI, Data); /*!< Wait until SPI is not busy */ while (SPI_GetFlagStatus(SPI, SPI_FLAG_BSY) != RESET) {} //SPI_BiDirectionalLineConfig(SPI, SPI_Direction_Rx); //设置单线为接收 return 1; } /** * @brief Read a byte . * @param None * @retval The received byte. */ uint8_t SPIx_ReadByte(void) { uint8_t Data = 0; //SPI_BiDirectionalLineConfig(SPI, SPI_Direction_Rx); //设置单线为接收 //SPI_Cmd(SPI, ENABLE); /* Wait untils SPI is working */ while (SPI_GetFlagStatus(SPI, SPI_FLAG_BSY) == SET); /*!< Wait until a data is received */ while (SPI_GetFlagStatus(SPI, SPI_FLAG_RXNE) == RESET) {} /*!< Get the received data */ Data = SPI_ReceiveData(SPI); //SPI_Cmd(SPI, DISABLE); //SPI_BiDirectionalLineConfig(SPI, SPI_Direction_Tx); /*!< Return the shifted data */ return Data; } void A7139_SetCID(Uint32 id) { SPI_CS_LOW(); //SCS=0; SPIx_WriteByte(CMD_CID_W); SPIx_WriteByte((Uint8)(id>>24)); SPIx_WriteByte((Uint8)(id>>16)); SPIx_WriteByte((Uint8)(id>>8)); SPIx_WriteByte((Uint8)id); SPI_CS_HIGH(); //SCS=1; } Uint32 A7139_ReadCID(void) { Uint32 data = 0; Uint8 i = 0; Uint32 byte[4] = {0}; SPI_CS_LOW(); SPIx_WriteByte(CMD_CID_R); SPI_BiDirectionalLineConfig(SPI, SPI_Direction_Rx); //设置单线为接收 // for (i = 0; i < 4; i++) // { // data = data << 8; // data |= SPIx_ReadByte(); // } byte[0] = SPIx_ReadByte(); byte[1] = SPIx_ReadByte(); byte[2] = SPIx_ReadByte(); byte[3] = SPIx_ReadByte(); data = (byte[0] << 24) | (byte[1] << 16) | (byte[2] << 8) | byte[3]; SPI_BiDirectionalLineConfig(SPI, SPI_Direction_Tx); SPI_CS_HIGH(); return data; } |
STM8 的MCU有四个供电单元
【STM8-SO8】08-STM8L001J3的点灯
STM8单片机如何实现Bootloader
基于STM8的DALI (数字可寻址调光协议)
开源基于STM32的STM8脱机编程器
【ST MCU实战经验】之STM8中UART奇偶校验的使用方法
【思修电子STM8集合贴】龙顺宇STM8理论/实战视频/书籍/软件/
初次尝试STM8S001J3
分享STM8 风驰光盘的资料,是完整的(包括原理图+例程+PDF注释)
基于STM8的实验代码汇总分享
微信公众号
手机版
https://www.stmcu.org.cn/module/forum/thread-612887-1-1.html