环境:STM32F103RC,主频 72MHz(外部晶振)或64MHz(HSI)
普通IO口模拟spi
note:自动扫描模式下改完量程,必须要增加AUTO_RST_Mode()。
代码实现:
// ADS8684.c
#define ADS8684_GLOBALS
#include "ADS8684.h"
/********************************************************普通IO口模拟spi驱动********************************************/
/******************************************************
函数名称:ReadWriteByte
函数描述:IO口模拟spi读写8位数据,上升沿写数据,下降沿读数据
Calls:
Called by:
输入参数:data:需要写入的数据
返回值:读到的8位数据
******************************************************/
static uint32_t ReadWriteByte(uint32_t data)
{
uint32_t status = 0;
uint32_t result = 0;
for (int number = 0; number < 8; number++)
{
if (data & (1 << (7 - number)))
{
ADS8684_SDI_H;
}
else
{
ADS8684_SDI_L;
}
ADS8684_CLK_H;
ADS8684_CLK_H;
ADS8684_CLK_H;
status = ADS8684_SD0_STATUS();
ADS8684_CLK_L;
if (status)
{
result |= 1;
}
result <<= 1;
}
return (result >> 1);
}
/***************************************************************************************************************/
/********************************************************ADS8684驱动********************************************/
/******************************************************
函数名称:WriteCommandReg
函数描述:写ADS8688命令寄存器
Calls:
Called by:
输入参数:command:命令寄存器
返回值:无
******************************************************/
static void WriteCommandReg(uint32_t command)
{
ADS8684_CS_L;
ReadWriteByte((command & 0xff00) >> 8);
ReadWriteByte(command & 0xff);
ADS8684_CS_H;
}
/******************************************************
函数名称:AUTO_RST_Mode
函数描述:进入自动扫描模式
Calls:
Called by:
输入参数:无
返回值:无
******************************************************/
void AUTO_RST_Mode(void)
{
WriteCommandReg(AUTO_RST);
}
/******************************************************
函数名称:Write_Program_Register
函数描述:Program Register写操作
Calls:
Called by:
输入参数:Addr:Program Register地址
data:写入Program Register的值
返回值:无
******************************************************/
static void Write_Program_Register(uint32_t Addr, uint32_t data)
{
ADS8684_CS_L;
ReadWriteByte((Addr << 1) | WRITE);
ReadWriteByte(data);
ADS8684_CS_H;
}
/******************************************************
函数名称:Read_Program_Register
函数描述:Program Register读操作
Calls:
Called by:
输入参数:Addr:Program Register地址
返回值:Program Register的值
******************************************************/
uint32_t Read_Program_Register(uint32_t Addr)
{
uint32_t data = 0;
ADS8684_CS_L;
ReadWriteByte((Addr) << 1 | READ);
data = ReadWriteByte(0XFF);
data = ReadWriteByte(0XFF);
ADS8684_CS_H;
return data;
}
/******************************************************
函数名称:Set_Auto_Scan_Sequence
函数描述:设置自动扫描序列通道
Calls:
Called by:
输入参数:seq:需要设置Auto-Scan Sequence Enable Register的值
返回值:无
******************************************************/
void Set_Auto_Scan_Sequence(uint32_t seq)
{
Write_Program_Register(AUTO_SEQ_EN, seq);
}
/******************************************************
函数名称:Set_CH_Range_Select
函数描述:设置各个通道的范围
Calls:
Called by:
输入参数:ch:通道输入范围寄存器地址
range:量程范围
返回值:无
******************************************************/
void Set_CH_Range_Select(uint32_t ch, uint32_t range)
{
Write_Program_Register(ch, range);
}
/***************************************************************************************************************/
/******************************************************
函数名称:Get_AUTO_RST_Mode_Data
函数描述:读取扫描通道序列的AD转换数据
Calls:
Called by:
输入参数:pADCData:传入gUADCData.ChADCData
chNum:总通道数
返回值:无
******************************************************/
static void Get_AUTO_RST_Mode_Data(uint16_t* pADCData, uint32_t chNum)
{
uint32_t datal = 0, datah = 0;
for (uint32_t i = 0; i < chNum; i++)
{
ADS8684_CS_L;
ReadWriteByte(0X00);
ReadWriteByte(0X00);
datah = ReadWriteByte(0XFF);
datal = ReadWriteByte(0XFF);
ADS8684_CS_H;
*(pADCData + i) = (datah << 8) | datal; //高位在前,低位在后
}
}
/******************************************************
函数名称:Init_ADS8684
函数描述:ADS8684的初始化代码
Calls:
Called by: main
输入参数:无
返回值:TRUE:初始化成功;FALSE:初始化失败
******************************************************/
uint32_t Init_ADS8684(void)
{
Set_Auto_Scan_Sequence(0x0F);// 通道1~通道4
if (Read_Program_Register(AUTO_SEQ_EN) != 0x0F)
{
return FALSE;
}
AUTO_RST_Mode(); // 默认通道1~通道4的量程为-10~10V
return TRUE;
}
// ADS8684.h
#ifndef __ADS8684_H
#define __ADS8684_H
#ifdef ADS8684_GLOBALS
#define ADS8684_EXT
#else
#define ADS8684_EXT extern
#endif
#include "global.h"
/**************************************ADS8684寄存器********************************************/
//Command Register
#define NO_OP 0X0000
#define STDBY 0X8200
#define PWR_DN 0X8300
#define RST 0X8500
#define AUTO_RST 0XA000
#define MAN_Ch_0 0XC000
#define MAN_CH_1 0XC400
#define MAN_Ch_2 0XC800
#define MAN_Ch_3 0XCC00
#define MAN_Ch_4 0XD000
#define MAN_Ch_5 0XD400
#define MAN_Ch_6 0XD800
#define MAN_Ch_7 0XDC00
#define MAN_AUX 0XE000
//Program Register
#define AUTO_SEQ_EN 0X01
#define Channel Power Down 0X02
#define Feature Select 0X03
#define Channel_0_Input_Range 0X05
#define Channel_1_Input_Range 0X06
#define Channel_2_Input_Range 0X07
#define Channel_3_Input_Range 0X08
#define Channel_4_Input_Range 0X09
#define Channel_5_Input_Range 0X0A
#define Channel_6_Input_Range 0X0B
#define Channel_7_Input_Range 0X0C
#define Ch_0_Hysteresis 0X15
#define Ch_0_High_Threshold_MSB 0X16
#define Ch_0_High_Threshold_LSB 0X17
#define Ch_0_Low_Threshold_MSB 0X18
#define Ch_0_Low_Threshold_LSB 0X19
#define Ch_7_Hysteresis 0X38
#define Ch_7_High_Threshold_MSB 0X39
#define Ch_7_High_Threshold_LSB 0X3A
#define Ch_7_Low_Threshold_MSB 0X3B
#define Ch_7_Low_Threshold_LSB 0X3C
#define Command_Read_Back 0X3F
#define WRITE 1
#define READ 0
#define CH7_EN 0X80
#define CH6_EN 0X40
#define CH5_EN 0X20
#define CH4_EN 0X10
#define CH3_EN 0X08
#define CH2_EN 0X04
#define CH1_EN 0X02
#define CH0_EN 0X01
#define CH7_PD 0X80
#define CH6_PD 0X40
#define CH5_PD 0X20
#define CH4_PD 0X10
#define CH3_PD 0X08
#define CH2_PD 0X04
#define CH1_PD 0X02
#define CH0_PD 0X01
#define VREF_25_25 0X00
#define VREF_125_125 0X01
#define VREF_0625_0625 0X02
#define VREF_0_25 0X05
#define VREF_0_125 0X06
/***********************************************************************************************/