ls7366 STM32例程
时间: 2024-01-18 16:47:10 浏览: 179
STM32单片机常用例程源码包括Flash存储USART,RS485,GPIO,CAN通信,AD采集和串口通信等。
5星 · 资源好评率100%
以下是LS7366 STM32的例程:
```c
#include "stm32f10x.h"
#include "LS7366.h"
void SPI_MasterInit(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
/* Enable SPI1 and GPIOA clocks */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1 | RCC_APB2Periph_GPIOA, ENABLE);
/* Configure SPI1 pins: SCK, MISO and MOSI -------------------------------*/
/* Configure SCK and MOSI pins as Alternate Function Push Pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure MISO pin as Input Floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* SPI1 configuration ------------------------------------------------------*/
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_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);
/* Enable SPI1 */
SPI_Cmd(SPI1, ENABLE);
}
void Delay (uint32_t nCount)
{
while(nCount--)
{
}
}
int main(void)
{
int i;
uint32_t nCount;
uint8_t txData[5], rxData[5];
SPI_MasterInit();
LS7366_Init();
nCount = 0;
while(1)
{
nCount = LS7366_GetCount();
LS7366_ClearCount();
Delay(1000000);
txData[0] = 0x60; //Read command
txData[1] = 0x00; //Address
txData[2] = 0x00;
txData[3] = 0x00;
txData[4] = 0x00;
/* Send SPI data */
GPIO_ResetBits(GPIOA, GPIO_Pin_4);
for (i = 0; i < 5; i++)
{
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPI1, txData[i]);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
rxData[i] = SPI_I2S_ReceiveData(SPI1);
}
GPIO_SetBits(GPIOA, GPIO_Pin_4);
}
return 0;
}
```
这个例程中使用了STM32的SPI1模块来与LS7366进行通信。同时也使用了LS7366库文件中的函数来初始化LS7366芯片并读取编码器的计数值。需要注意的是,在使用此例程时,需要根据具体的硬件连接情况来修改GPIO初始化和SPI初始化的参数。
阅读全文