STM32_HAL_SPI_ADS1256调试记录
1. 硬件电路
##1. 官方设计
2. 软件设计
关于ADC值的计算 因为是24位的,然后1/2^24 大概计算得到 0.000000596,在代入计算ADC测量的值也大概准确
ADC 函数,总共写了三个函数,一个读寄存器 ADS1256_Read:读寄存器 ADS1256_Write:写寄存器 ADS1256_Read_ADC_data:读取ADC的数据
2. 1 ads1256.C
#include <stdio.h>
#include "main.h"
#include "ADS1256.h"
extern SPI_HandleTypeDef hspi3;
void ADS1256_Read(uint8_t address, uint8_t size,uint8_t *recv_buf )
{
address = 0x10 + address;
uint8_t send_data[2] = {address,size-1};
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_3, GPIO_PIN_RESET);
if (HAL_OK == HAL_SPI_Transmit(&hspi3, send_data, 2, 100))
{
if(HAL_OK == HAL_SPI_Receive(&hspi3, recv_buf, size, 100))
{}
}
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_3, GPIO_PIN_SET);
}
void ADS1256_Write(uint8_t address,uint8_t number)
{
uint8_t send_data[4] = {0x50+address,0x00,number};
while(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_2)==0);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_3, GPIO_PIN_RESET);
if (HAL_OK == HAL_SPI_Transmit(&hspi3, send_data, 3, 100))
{ }
while(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_2)==0);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_3, GPIO_PIN_SET);
}
void ADS1256_Read_ADC_data(uint8_t COMMAND ,uint8_t *recv_buf)
{
uint8_t send_data[1] = {0X01};
while(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_2)==0);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_3, GPIO_PIN_RESET);
if (HAL_OK == HAL_SPI_Transmit(&hspi3, send_data, 1, 100))
{
if(HAL_OK == HAL_SPI_Receive(&hspi3, recv_buf, 0x03, 100))
{
}
}
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_3, GPIO_PIN_SET);
}
2.2 ads1256.h
#ifndef __ADS1256_H_
#define __ADS1256_H_
#include "main.h"
void ADS1256_Init(void);
void ADS1256_Read(uint8_t address, uint8_t size,uint8_t *recv_buf );
void ADS1256_Write(uint8_t address,uint8_t number);
void ADS1256_Read_ADC_data(uint8_t COMMAND,uint8_t *recv_buf);
#endif
main.c
大概主函数使用的代码就是如下,可以参考其中的逻辑
#include <stdio.h>
#ifdef __GNUC__
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif
PUTCHAR_PROTOTYPE
{
HAL_UART_Transmit(&huart1,(uint8_t *)&ch,1,0xFFFF);
return ch;
}
void UART0_ShowHex(uint8_t *buf,uint8_t len)
{
uint8_t i;
printf("hex = ");
for( i = 0; i < len; i++){
printf(" %02X",buf[i]);
}
printf( "\r\n");
}
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_SPI3_Init();
MX_USART1_UART_Init();
ADS1256_Init();
while (1)
{
unsigned int sum=0;
unsigned int sum1=0;
unsigned int sum2=0;
float Volts;
uint8_t SPI_Buffer_Read[13] = {0};
uint8_t ADC_Read[5] = {0};
printf("start*****************************************\r\n");
ADS1256_Write(0x01,0x08);
HAL_Delay(2);
ADS1256_Read_ADC_data( 0x01,ADC_Read);
sum |= (ADC_Read[0]<<16);
sum |= (ADC_Read[1]<<8);
sum |= (ADC_Read[2]);
Volts = sum*0.000000596;
printf("AIN0 = %.4fV \r\n",Volts);
ADS1256_Write(0x01,0x18);
HAL_Delay(2);
ADS1256_Read_ADC_data( 0x01,ADC_Read);
sum1 |= (ADC_Read[0]<<16);
sum1 |= (ADC_Read[1]<<8);
sum1 |= (ADC_Read[2]);
Volts = sum1*0.000000596;
printf("AIN1 = %.4fV \r\n",Volts);
ADS1256_Write(0x01,0x28);
HAL_Delay(2) ;
ADS1256_Read_ADC_data( 0x01,ADC_Read);
sum2 |= (ADC_Read[0]<<16);
sum2 |= (ADC_Read[1]<<8);
sum2 |= (ADC_Read[2]);
Volts = sum2*0.000000596;
printf("AIN2 = %.4fV \r\n",Volts);
HAL_Delay(2000);
}
}
|