欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

ADS8695 STM32 硬件SPI驱动

程序员文章站 2024-03-19 14:17:58
...

SPI初始化配置

void my_spi1_init(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	RCC_AHBPeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);

	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	GPIO_SetBits(GPIOA, GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7);
	spi1_cs_init();
	SPI1_Configuration();
	//SPI1_DMA_Configuration(DMA1_Channel2, (u32)&SPI1->DR, (u32)spi1_rx_buf, 0x00);
	//SPI1_DMA_Configuration(DMA1_Channel3, (u32)&SPI1->DR, (u32)spi1_tx_buf, 0x10);
}
void spi1_cs_init(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	RCC_AHBPeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC, ENABLE);

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);

	// these two pin is for ads869x reset pin control
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOB, &GPIO_InitStructure);

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOC, &GPIO_InitStructure);

	GPIO_SetBits(GPIOA, GPIO_Pin_4);
	GPIO_SetBits(GPIOB, GPIO_Pin_0 | GPIO_Pin_1);
	GPIO_SetBits(GPIOC, GPIO_Pin_5);
	RST_AD1 = 0;
	RST_AD2 = 0;
	delay_ms(10);
	RST_AD1 = 1;
	RST_AD2 = 1;
}
void SPI1_Configuration(void)
{
	SPI_InitTypeDef SPI_InitStructure;
	SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
	SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
	SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
	SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
	SPI_InitStructure.SPI_NSS = SPI_NSS_Hard;
	SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_32;
	SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
	SPI_InitStructure.SPI_CRCPolynomial = 7;

	SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
	SPI_Init(SPI1, &SPI_InitStructure);
	SPI_SSOutputCmd(SPI1, ENABLE);
	SPI_Cmd(SPI1, ENABLE);
}
void SPI1_DMA_Configuration(DMA_Channel_TypeDef *DMA_CHx, u32 src_addr, u32 des_buf, u8 direction)
{
	//1channel 2src_addr 3des_buf 4direction 5DMA_CMD
	DMA_InitTypeDef DMA_InitStructure;
	RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
	DMA_DeInit(DMA_CHx); //1

	DMA_InitStructure.DMA_PeripheralBaseAddr = src_addr;		    //2
	DMA_InitStructure.DMA_MemoryBaseAddr = des_buf;					//3
	DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC | direction;  //4
	DMA_InitStructure.DMA_BufferSize = 2;
	DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
	DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
	DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
	DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; //8位
	DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
	DMA_InitStructure.DMA_Priority = DMA_Priority_High;
	DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;

	DMA_Init(DMA_CHx, &DMA_InitStructure);
	SPI_I2S_DMACmd(SPI1, SPI_I2S_DMAReq_Rx + (direction >> 4), ENABLE); //5
}

ADS8695初始化(仍然使用软件IO模拟方式发送初始化命令)

static u16 spi_16(u16 data)
{
	u8 i = 0;
	u16 result = 0;

	for (i = 0; i < 16; i++)
	{
		result = result << 1;
		if ((data & 0x8000) == 0x8000)
		{
			ads8695_MOSI = 1;
		}
		else
		{
			ads8695_MOSI = 0;
		}
		delay_us(50);
		ads8695_SCLK = 1;
		data = data << 1;
		if (ads8695_MISO != 0)
		{
			result = result | 0x01;
		}
		ads8695_SCLK = 0;
		delay_us(50);
	}
	return result;
}

u32 ads8695_reg_config(uint8_t command, uint16_t address, uint16_t data)
{
	u32 result = 0;
	u16 temp = 0;
	ads8695_CS = 0;
	delay_us(10);
	temp = (((command << 1) | ((address >> 8) & 1)) << 8) | (address & 0x00FF);
	result = spi_16(temp);
	result = (result << 16) | spi_16(data);
	delay_us(10);
	ads8695_CS = 1;
	return result;
}

Main函数 ADS8695值的硬件SPI方式读取

   void main()
{	
    my_spi1_init();
	ads8695_reg_config(ads8695_WRITE_FULL, ads8695_RANGE_SEL_REG, 0x08);
     while(1)
     {
    	CS_AD2 = 0;
    	delay_us(50);

    	test1 = spi1_read_write_byte(0);
    	test2 = spi1_read_write_byte(0);
    	test = test2 + (test1 << 8);
    	delay_us(50);
	    CS_AD2 = 1;
     }
}