//spi bus device
static struct spi_dev_device ad5290_spi_dev[3];
extern struct spi_bus_device spi_bus1;
static void ad5290_spi_cs0(unsigned char state)
{
if (state)
GPIO_SetBits(GPIOC, GPIO_Pin_7);
else
GPIO_ResetBits(GPIOC, GPIO_Pin_7);
}
static void ad5290_spi_cs1(unsigned char state)
{
if (state)
GPIO_SetBits(GPIOC, GPIO_Pin_9);
else
GPIO_ResetBits(GPIOC, GPIO_Pin_9);
}
static void ad5290_spi_cs2(unsigned char state)
{
if (state)
GPIO_SetBits(GPIOA, GPIO_Pin_12);
else
GPIO_ResetBits(GPIOA, GPIO_Pin_12);
}
//init
void ad5290_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
//spi cs
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOC,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_SetBits(GPIOC,GPIO_Pin_7);
GPIO_SetBits(GPIOC,GPIO_Pin_9);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_SetBits(GPIOA,GPIO_Pin_12);
/*device init*/
stm32f1xx_spi_init(0,0,&spi_bus1,8); /*spi bus init*/
//device 0
ad5290_spi_dev[0].spi_cs = ad5290_spi_cs0;
ad5290_spi_dev[0].spi_bus = &spi_bus1;
//device 1
ad5290_spi_dev[1].spi_cs = ad5290_spi_cs1;
ad5290_spi_dev[1].spi_bus = &spi_bus1;
//device 2
ad5290_spi_dev[2].spi_cs = ad5290_spi_cs2;
ad5290_spi_dev[2].spi_bus = &spi_bus1;
}
/**
* [url=home.php?mod=space&uid=247401]@brief[/url] set resistance out
* @param \set_value limits from 0x00 to 0xff
* \index sensor num,1 or 2 or 3
* @retval None
*/
void ad5290_set_out(u8 set_value,u8 index)
{
if(index < 1 || index > 3)
return;
spi_send(&ad5290_spi_dev[index-1],&set_value,1);
}
AD5290 代码,接了3片,用的是以下spi封装代码,参考。
https://blog.csdn.net/qq_20553613/article/details/78998617 |