#include "SPI.h"
uint16_t angle=0;
/**
* @brief SPI1????.
* @param ?
* @retval ?
*/
//void APP_ConfigSPI(void)
//{
// LL_SPI_InitTypeDef SPI_InitStruct = {0};
// LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
// /* ???? */
// LL_APB1_GRP2_EnableClock(LL_APB1_GRP2_PERIPH_SPI1);
// LL_IOP_GRP1_EnableClock(LL_IOP_GRP1_PERIPH_GPIOA);
// /* ?PA4??????? */
//// LL_GPIO_SetPinMode(GPIOA, LL_GPIO_PIN_4, LL_GPIO_MODE_OUTPUT);
// /**SPI1 ????
// PA5 ------> SPI1_SCK
// PA7 ------> SPI1_MOSI
// PA6 ------> SPI1_MISO
// */
// GPIO_InitStruct.Pin = LL_GPIO_PIN_5;
// GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
// GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
// GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
// GPIO_InitStruct.Pull = LL_GPIO_PULL_UP;
// GPIO_InitStruct.Alternate = LL_GPIO_AF_0;
// LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
// GPIO_InitStruct.Pin = LL_GPIO_PIN_6;
// GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
// GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
// GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
// GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
// GPIO_InitStruct.Alternate = LL_GPIO_AF_0;
// LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
// GPIO_InitStruct.Pin = LL_GPIO_PIN_7;
// GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
// GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
// GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
// GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
// GPIO_InitStruct.Alternate = LL_GPIO_AF_0;
// LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
// SPI_InitStruct.TransferDirection = LL_SPI_FULL_DUPLEX;
// SPI_InitStruct.Mode = LL_SPI_MODE_MASTER;
// SPI_InitStruct.DataWidth = LL_SPI_DATAWIDTH_16BIT;
// SPI_InitStruct.ClockPolarity = LL_SPI_POLARITY_LOW;
// SPI_InitStruct.ClockPhase = LL_SPI_PHASE_2EDGE;
// SPI_InitStruct.NSS = LL_SPI_NSS_SOFT;
// SPI_InitStruct.BaudRate = LL_SPI_BAUDRATEPRESCALER_DIV128;
// SPI_InitStruct.BitOrder = LL_SPI_MSB_FIRST;
// LL_SPI_Init(SPI1, &SPI_InitStruct);
//}
void APP_ConfigSPI(void)
{
LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
/* ???? */
LL_APB1_GRP2_EnableClock(LL_APB1_GRP2_PERIPH_SPI1);
LL_IOP_GRP1_EnableClock(LL_IOP_GRP1_PERIPH_GPIOA);
/* ?PA4??????? */
// LL_GPIO_SetPinMode(GPIOA, LL_GPIO_PIN_4, LL_GPIO_MODE_OUTPUT);
/**SPI1 ????
PA5 ------> SPI1_SCK
PA7 ------> SPI1_MOSI
PA6 ------> SPI1_MISO
*/
GPIO_InitStruct.Pin = LL_GPIO_PIN_5;
GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
LL_GPIO_SetOutputPin(GPIOA,LL_GPIO_PIN_5);
GPIO_InitStruct.Pin = LL_GPIO_PIN_6;
GPIO_InitStruct.Mode = LL_GPIO_MODE_INPUT;
GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.Pin = LL_GPIO_PIN_7;
GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
uint16_t read_spi_data(uint16_t tx_data)
{
uint16_t retry=0,data;
LL_GPIO_ResetOutputPin(GPIOA,LL_GPIO_PIN_4);
LL_mDelay(2);
while(LL_SPI_IsActiveFlag_TXE(SPI1)==RESET) // ָ SPI ־λ : ͻ ձ ־λ
{
retry++;
if(retry>200)return 0;
}
LL_SPI_TransmitData16(SPI1,tx_data);
retry=0;
while(LL_SPI_IsActiveFlag_RXNE(SPI1)==RESET) // ָ SPI ־λ : ܻ ǿձ ־λ
{
retry++;
if(retry>200)return 0;
}
data=LL_SPI_ReceiveData16(SPI1);
LL_mDelay(2);
LL_GPIO_SetOutputPin(GPIOA,LL_GPIO_PIN_4);
return(data);
}
uint8_t spi_write_read_action(uint8_t addr,uint8_t command)
{
int i=0;
uint8_t data=0;
LL_GPIO_ResetOutputPin(GPIOA,LL_GPIO_PIN_4); //cs ͡
LL_mDelay(2);
for(i=0;i<16;)
{
LL_mDelay(1);
LL_GPIO_ResetOutputPin(GPIOA,LL_GPIO_PIN_5); //CLK ͡
if(i<8)
{
if(addr&0x80)
{
LL_GPIO_SetOutputPin(GPIOA,LL_GPIO_PIN_7); // mosi 0 д 1
}
else
{
LL_GPIO_ResetOutputPin(GPIOA,LL_GPIO_PIN_7); // mosi 0 д 1
}
}
else if((i>=8)&&(command&0x80))
{
LL_GPIO_SetOutputPin(GPIOA,LL_GPIO_PIN_7); // mosi
command=command<<1;
}
else
{
LL_GPIO_ResetOutputPin(GPIOA,LL_GPIO_PIN_7); // mosi
command=command<<1;
}
LL_mDelay(1);
LL_GPIO_SetOutputPin(GPIOA,LL_GPIO_PIN_5); //CLK ߡ
if(i>=8)
{
data=data<<1;
if(LL_GPIO_IsInputPinSet(GPIOA,LL_GPIO_PIN_6))
{
data=data|0x0001;
}
}
i++;
addr=addr<<1;
}
LL_mDelay(1);
LL_GPIO_ResetOutputPin(GPIOA,LL_GPIO_PIN_5); //CLK ͡
LL_mDelay(1);
LL_GPIO_SetOutputPin(GPIOA,LL_GPIO_PIN_5); //CLK ߡ
LL_mDelay(2);
LL_GPIO_SetOutputPin(GPIOA,LL_GPIO_PIN_4); //CS ߡ
return data;
}
uint16_t spi_read_angle_action(void)
{
int i=0;
uint8_t addr=0x81;
LL_GPIO_ResetOutputPin(GPIOA,LL_GPIO_PIN_4); //cs ͡
LL_mDelay(2);
for(i=0;i<24;)
{
LL_GPIO_ResetOutputPin(GPIOA,LL_GPIO_PIN_5); //CLK ͡
if((i<8)&&(addr&0x80))
{
LL_GPIO_SetOutputPin(GPIOA,LL_GPIO_PIN_7); // mosi
}
else
{
LL_GPIO_ResetOutputPin(GPIOA,LL_GPIO_PIN_7); // mosi
}
LL_mDelay(1);
LL_GPIO_SetOutputPin(GPIOA,LL_GPIO_PIN_5); //CLK ߡ
if(i>=8)
{
angle=angle<<1;
if(LL_GPIO_IsInputPinSet(GPIOA,LL_GPIO_PIN_6))
{
angle=angle|0x0001;
}
}
LL_mDelay(1);
addr=addr<<1;
i++;
}
LL_GPIO_ResetOutputPin(GPIOA,LL_GPIO_PIN_5); //CLK ͡
LL_mDelay(1);
LL_GPIO_SetOutputPin(GPIOA,LL_GPIO_PIN_5); //CLK ߡ
LL_mDelay(2);
LL_GPIO_SetOutputPin(GPIOA,LL_GPIO_PIN_4); //CS ߡ
return angle;
}
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉
全部0条评论
快来发表一下你的评论吧 !