msg_pt100/board/ads1220.c

152 lines
4.1 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "ads1220.h"
#include "key.h"
#include "events_init.h"
long ads1220_data = 0; //adc数据单位mv
long maxv = 0; //最大电压
long maxv_loca = 0; //最大电压对应位置
long max_point_cnt = 0;
long minv = 0; //最小电压
long minv_loca = 0; //最小电压对应位置
long min_point_cnt = 0;
int test_flag = 0; //测试标志
Point_Data point_data; //采样点数据
/*******************************************************************************
函数名称 ads1220_init
功 能 ads1220初始化
参 数
返 回 值
*******************************************************************************/
void ads1220_init(void)
{
uint8_t config_0 = 0x3A; //写入配置寄存器0,使用AINP = AIN1AINN = AIN2增益 = 32PGA 启用
uint8_t config_1 = 0xC4; //写入配置寄存器1,使用正常工作模式,连续转换模式,DR = 1000SPS
uint8_t config_2 = 0x98; //写入配置寄存器2,使用外部基准REFP1、REFN1同时抑制 50Hz 和 60HzPSW = 1
uint8_t config_3 = 0x00; //写入配置寄存器3,使用设置独立的DRY引脚
ADS1220_CS_LOW();
ads1220_write(ADS1220_RESET);//复位命令
delay_ms(10);
ADS1220_CS_HIGH();
ads1220_writeregister(0x00, 1, &config_0);
ads1220_writeregister(0x01, 1, &config_1);
ads1220_writeregister(0x02, 1, &config_2);
ads1220_writeregister(0x03, 1, &config_3);
ADS1220_CS_LOW();
delay_ms(10);
ads1220_write(ADS1220_START);//开启转换
}
/*******************************************************************************
函数名称 ads1220_write
功 能 写入一个字节
参 数 dat--写入的数据
返 回 值
*******************************************************************************/
void ads1220_write(uint8_t dat)
{
HAL_SPI_Transmit(&hspi1, &dat, 1, 10);
}
/*******************************************************************************
函数名称 ads1220_receivebyte
功 能 接收一个字节
参 数
返 回 值 接收到的数据
*******************************************************************************/
unsigned char ads1220_receivebyte(void)
{
uint8_t SData = 0xff, Result = 0;
HAL_SPI_TransmitReceive(&hspi1, &SData, &Result, 1, 10);
return Result;
}
/*******************************************************************************
函数名称 ads1220_writeregister
功 能 数据写入到寄存器
参 数 StartAddress--寄存器写入起始地址NumRegs--寄存器数量pData---写入的数据
返 回 值
*******************************************************************************/
void ads1220_writeregister(int StartAddress, int NumRegs, uint8_t * pData)
{
int i;
ADS1220_CS_LOW();
/* send the command byte */
ads1220_write( ADS1220_WREG | (((StartAddress << 2) & 0x0c) | ((NumRegs - 1) & 0x03)));
/* send the data bytes */
for(i = 0; i < NumRegs; i++)
{
ads1220_write(*pData++);
}
ADS1220_CS_HIGH();
}
long ads_data;
/*******************************************************************************
函数名称 ads1220_readdata
功 能 读取ads1220数据
参 数
返 回 值 ads1220转换数据
*******************************************************************************/
long ads1220_readdata(void)
{
ADS1220_CS_LOW();
ads1220_write(ADS1220_RDATA);
ads_data = ads1220_receivebyte();
ads_data = (ads_data << 8) | ads1220_receivebyte();
ads_data = (ads_data << 8) | ads1220_receivebyte();
/* sign extend data */
if (ads_data & 0x800000)
{
//ads_data -= 0x800000;
ads_data = ads_data - 0xFFFFFF;
}
ADS1220_CS_HIGH();
return ads_data;
}
/*******************************************************************************
函数名称 get_data
功 能 获取四路输入电流并存储
参 数
返 回 值
*******************************************************************************/
void get_data(void)
{
ads1220_data = ads1220_readdata() * 2500.0 / 8388608.0;
//测试过程中确定采样电压的极值和对应位置
if(test_status == 2)
{
if(maxv < ads1220_data)
{
max_point_cnt = point_data.cnt;
maxv = ads1220_data;
maxv_loca = ((CaptureNumber - 30000) / 720.0) * 254.0;
}
if(minv > ads1220_data)
{
min_point_cnt = point_data.cnt;
minv = ads1220_data;
minv_loca = ((CaptureNumber - 30000) / 720.0) * 254.0;
}
}
}