ADC124S051驱动代码(HAL库)—4通道数据读取(有部分注释)
#include "ADC124S051.h"
#include "stm32f1xx_hal.h"
#include "define.h"
#include "delay.h"
uint32_t *ADC_read_multiple_channel(uint8_t chipselcet) //这个参数片选,我的板子上有多个ADC所以要有个片选
static uint32_t value_array[NUMBER_OF_CHANNEL] = { 0 };
uint8_t i = 0,j=0;
uint8_t channel1 = 0B00000000;
uint8_t channel2 = 0B00001000;
uint8_t channel3 = 0B00010000;
uint8_t channel4 = 0B00011000;
uint32_t data1 = 0;
uint32_t data2 = 0;
uint32_t data3 = 0;
uint32_t data4 = 0;
HAL_GPIO_WritePin(GPIOB, SCLK, 0);
HAL_GPIO_WritePin(GPIOA, chipselcet, 0);
delay_us(1);
for (j = 0; j < 4; j++)
{
switch (j)
{
case 0:
for (int i = 0; i < 16; i++)
{
HAL_GPIO_WritePin(GPIOB, DATAOUT, (channel1 & 0x80) >> 7); //写入ADC control register
channel1 = channel1 << 1;
HAL_GPIO_WritePin(GPIOB, SCLK, 1);
delay_us(1);
if ((i >= 4) && (i <= 15)) //从SCLK的第5个sclk开始读数据
{
if (HAL_GPIO_ReadPin(GPIOB, DATAIN)) //如果读到的状态是1
data1 |= (1 << (15 - i));
}
HAL_GPIO_WritePin(GPIOB, SCLK, 0);
delay_us(1);
}
break;
case 1:
for (int i = 0; i < 16; i++)
{
HAL_GPIO_WritePin(GPIOB, DATAOUT, (channel2 & 0x80) >> 7); //写入ADC control register
channel2 = channel2 << 1;
HAL_GPIO_WritePin(GPIOB, SCLK, 1);
delay_us(1);
if ((i >= 4) && (i <= 15)) //从SCLK的第5个sclk开始读数据
{
if (HAL_GPIO_ReadPin(GPIOB, DATAIN)) //如果读到的状态是1
data2 |= (1 << (15 - i));
}
HAL_GPIO_WritePin(GPIOB, SCLK, 0);
delay_us(1);
}
break;
case 2:
for (int i = 0; i < 16; i++)
{
HAL_GPIO_WritePin(GPIOB, DATAOUT, (channel3 & 0x80) >> 7); //写入ADC control register
channel3 = channel3 << 1;
HAL_GPIO_WritePin(GPIOB, SCLK, 1);
delay_us(1);
if ((i >= 4) && (i <= 15)) //从SCLK的第5个sclk开始读数据
{
if (HAL_GPIO_ReadPin(GPIOB, DATAIN)) //如果读到的状态是1
data3 |= (1 << (15 - i));
}
HAL_GPIO_WritePin(GPIOB, SCLK, 0);
delay_us(1);
}
break;
case 3:
for (int i = 0; i < 16; i++)
{
HAL_GPIO_WritePin(GPIOB, DATAOUT, (channel4 & 0x80) >> 7); //写入ADC control register
channel4 = channel4 << 1;
HAL_GPIO_WritePin(GPIOB, SCLK, 1);
delay_us(1);
if ((i >= 4) && (i <= 15)) //从SCLK的第5个sclk开始读数据
{
if (HAL_GPIO_ReadPin(GPIOB, DATAIN)) //如果读到的状态是1
data4 |= (1 << (15 - i));
}
HAL_GPIO_WritePin(GPIOB, SCLK, 0);
delay_us(1);
}
break;
default:
break;
}
}
HAL_GPIO_WritePin(GPIOA, chipselcet, 1);
value_array[0] = data2; //IN1
value_array[1] = data3; //IN2
value_array[2] = data4; //IN3
value_array[3] = data1; //IN4
return value_array;
}