ADS8695 STM32 软件IO模拟

ads8695.h

#ifndef _ADC_H_
#define _ADC_H_	


// Register Locations and Names
#define ads8695_DEVICE_ID_REG   0x00
#define ads8695_RST_PWRCTL_REG  0x04
#define ads8695_SDI_CTL_REG     0x08
#define ads8695_SDO_CTL_REG     0x0C
#define ads8695_DATAOUT_CTL_REG 0x10
#define ads8695_RANGE_SEL_REG   0x14
#define ads8695_ALARM_REG       0x20
#define ads8695_ALARM_H_TH_REG  0x24
#define ads8695_ALARM_L_TH_REG  0x28

// SPI commands
#define ads8695_NOP          0x00   // 0b 000 0000
#define ads8695_CLEAR_HWORD  0x60   // 0b 110 0000
#define ads8695_READ_HWORD   0x64   // 0b 110 0100
#define ads8695_READ         0x24   // 0b 010 0100
#define ads8695_WRITE_FULL   0x68   // 0b 110 1000
#define ads8695_WRITE_MS     0x67   // 0b 110 1001
#define ads8695_WRITE_LS     0x6A   // 0b 110 1010
#define ads8695_SET_HWORD    0x6C   // 0b 110 1100

// ADC Range
#define ADC_RANGE_A          0x03         // 0b 0-1.25 Vref
#define ADC_RANGE_B          0x0B         // 0b ±1.25  Vref



#define ads8695_CS_PORT           GPIOC
#define ads8695_CS_PIN            GPIO_Pin_5
#define ads8695_SCLK_PORT         GPIOA
#define ads8695_SCLK_PIN          GPIO_Pin_5
#define ads8695_MISO_PORT         GPIOA
#define ads8695_MISO_PIN          GPIO_Pin_6
#define ads8695_MOSI_PORT         GPIOA
#define ads8695_MOSI_PIN          GPIO_Pin_7

#define ads8695_CS    PCout(5)
#define ads8695_MOSI  PAout(7)
#define ads8695_MISO  PAin(6)
#define ads8695_SCLK  PAout(5)


void ads8695_init( void );
u32 ads8695_reg_config(uint8_t command, uint16_t address, uint16_t data);
u32 ads8695_read( void );
u8 ads8695_readwrite( u8 dat);
void ads8695_reg_write(unsigned char cmd,unsigned char addr);
#endif

ads8695.c

#include "adc.h"
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;
}

void ads8695_init()
{
	GPIO_InitTypeDef GPIO_InitStructure;

	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOC, ENABLE);

	GPIO_InitStructure.GPIO_Pin = ads8695_CS_PIN;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(ads8695_CS_PORT, &GPIO_InitStructure);

	GPIO_InitStructure.GPIO_Pin = ads8695_MOSI_PIN;
	GPIO_Init(ads8695_MOSI_PORT, &GPIO_InitStructure);

	GPIO_InitStructure.GPIO_Pin = ads8695_SCLK_PIN;
	GPIO_Init(ads8695_SCLK_PORT, &GPIO_InitStructure);

	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
	GPIO_InitStructure.GPIO_Pin = ads8695_MISO_PIN;
	GPIO_Init(ads8695_MISO_PORT, &GPIO_InitStructure);
	ads8695_SCLK = 0;

	//rest two ads869x,then they are read available
	delay_ms(100);
	RST_AD1 = 0;
	RST_AD2 = 0;
	delay_ms(10);
	RST_AD1 = 1;
	RST_AD2 = 1;
}

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;
}

u8 ads8695_readwrite(u8 dat)
{
	u8 i, temp;
	for (i = 0; i < 8; i++)
	{
		ads8695_SCLK = 0; //????
		delay_us(20);
		if (dat & 0x80)
		{
			ads8695_MOSI = 1;
		}
		else
		{
			ads8695_MOSI = 0;
		}
		dat <<= 1;
		ads8695_SCLK = 1;
		delay_us(10);
		temp <<= 1;
		if (ads8695_MISO)
		{
			temp++;
		}
	}
	return temp;
}

void ads8695_reg_write(unsigned char cmd, unsigned char addr)
{
	ads8695_CS = 0;
	delay_us(10);
	ads8695_readwrite(cmd);
	ads8695_readwrite(addr);
	ads8695_readwrite(0x00);
	ads8695_readwrite(0x00);
	delay_us(10);
	ads8695_CS = 0;
}

u32 ads8695_read()
{
	unsigned char i = 0;
	unsigned long sum = 0;
	unsigned long r = 0;

	ads8695_CS = 0;
	delay_us(25);
	for (i = 0; i < 2; i++)
	{
		sum = sum << 8;
		r = ads8695_readwrite(0x00);
		sum |= r;
	}
	ads8695_CS = 1;
	return sum;
}

main.c

ads8695_init();
ads8695_reg_config(ads8695_WRITE_FULL, ads8695_RANGE_SEL_REG, 0x08);
CS_AD2 = 0;
test_value = ads8695_read();
CS_AD2 = 1;

 

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 成长之路 设计师:Amelia_0503 返回首页