#include "ads1120.h" #define uchar unsigned char #define uint unsigned int #define TEMGET 1 #define PREGET 2 unsigned char Init_Config[4]; unsigned char i; //extern unsigned int Result; //unsigned int Result; #define MAXVALUE 32768 #define TEM_K_FACTOR (1.5/8) #define PRE_K_FACTOR (2.048/16) /*************************************************************************** * Function Name: ADS1120_Init() * Purpose: ADS120 initialize * Params : * *************************************************************************/ void ADS1120_GPIO_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; //´ò¿ª¿ØÖƶ˿ÚʱÖÓ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOE, ENABLE); //¿ØÖÆÒý½Å£¨Êä³ö£©³õʼ»¯ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; //A6 Miso GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOC,&GPIO_InitStructure); //GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //02-019 //GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //B11 DATA ready //GPIO_Init(GPIOE,&GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //B11 DATA ready GPIO_Init(GPIOE,&GPIO_InitStructure); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; //B10 ADC cs GPIO_Init(GPIOC,&GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; GPIO_Init(GPIOB,&GPIO_InitStructure); //GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; //GPIO_Init(GPIOE,&GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_Init(GPIOE,&GPIO_InitStructure); TEM_CS_H; // PRE_CS_H; //SCLK_L; ADReset(TEMGET); //ADReset(PREGET); } /*************************************************************************** * Function Name: WriteOneByte(unsigned char command) * Purpose: write one byte to ADS1120 * Params : * *************************************************************************/ void WriteOneByte(unsigned char command) { unsigned char i; for(i = 0; i < 8;i++) { if(command&0x80) DIN_H; else DIN_L; command <<= 1; SCLK_H; SCLK_L; } } /*************************************************************************** * Function Name: ReadOneByte() * Purpose: read one byte from ADS1120 * Params : result * *************************************************************************/ unsigned char ReadOneByte(void) { unsigned char result,i; SCLK_L; for(i = 0;i<8; i++) { SCLK_H; result <<= 0x01; if(DOUT) result |= 0X01; SCLK_L; } return result; } /*************************************************************************** * Function Name: ReadData() * Purpose: read data from ADS1120 * Params : result 0~65535 * *************************************************************************/ int16_t ReadData(u8 ch_type) { int16_t result; if(ch_type == TEMGET) { TEM_CS_L; }else if(ch_type == PREGET) { //PRE_CS_L; } WriteOneByte(RDATA); result = ReadOneByte(); result = (result<<8) | ReadOneByte(); if(ch_type == TEMGET) { TEM_CS_H; }else if(ch_type == PREGET) { //PRE_CS_H; } return result; } /*************************************************************************** * Function Name: ADReset() * Purpose: reset ADS1120 * Params : * *************************************************************************/ void ADReset(u8 ch_type) { if(ch_type == TEMGET) { TEM_CS_L; }else if(ch_type == PREGET) { //PRE_CS_L; } WriteOneByte(RESET); if(ch_type == TEMGET) { TEM_CS_H; }else if(ch_type == PREGET) { //PRE_CS_H; } } /*************************************************************************** * Function Name: ADPowerDown() * Purpose: * Params : * *************************************************************************/ void ADPowerDown(u8 ch_type) { if(ch_type == TEMGET) { TEM_CS_L; }else if(ch_type == PREGET) { //PRE_CS_L; } WriteOneByte(POWERDOWN); if(ch_type == TEMGET) { TEM_CS_H; }else if(ch_type == PREGET) { //PRE_CS_H; } } /*************************************************************************** * Function Name: ADStartConversion() * Purpose: start conversion * Params : * *************************************************************************/ void ADStartConversion(u8 ch_type) { if(ch_type == TEMGET) { TEM_CS_L; }else if(ch_type == PREGET) { //PRE_CS_L; } WriteOneByte(START); if(ch_type == TEMGET) { TEM_CS_H; }else if(ch_type == PREGET) { //PRE_CS_H; } } /*************************************************************************** * Function Name: ReadRegister() * Purpose: read data from register * Params : data * *************************************************************************/ void ReadRegister(u8 ch_type) { unsigned char i; unsigned long Data; if(ch_type == TEMGET) { TEM_CS_L; }else if(ch_type == PREGET) { //PRE_CS_L; } WriteOneByte(RREG|0x03); // get the register content for (i=0; i< 4; i++) { Data += ReadOneByte(); } if(ch_type == TEMGET) { TEM_CS_H; }else if(ch_type == PREGET) { //PRE_CS_H; } return; } /*************************************************************************** * Function Name: WriteRegister(unsigned char StartAddress, unsigned char NumRegs, unsigned char * pData) * Purpose: write data to register * steps: 1.write address 2.write data * Params NumRegs-----the numbers of register *************************************************************************/ void WriteRegister(unsigned char StartAddress, unsigned char NumRegs, unsigned char * pData,u8 ch_type) { unsigned char i; if(ch_type == TEMGET) { TEM_CS_L; }else if(ch_type == PREGET) { //PRE_CS_L; } // send the command byte WriteOneByte(WREG | (((StartAddress<<2) & 0x0c) |((NumRegs-1)&0x03))); // send the data bytes for (i=0; i< NumRegs; i++) { WriteOneByte(*pData); pData++; } if(ch_type == TEMGET) { TEM_CS_H; }else if(ch_type == PREGET) { //PRE_CS_H; } return; } /*************************************************************************** * Function Name: CofigTemAD(unsigned char channel) * Purpose: set the sampling channel * Params : * *************************************************************************/ void CofigTemAD(void) { /*ÈýÏßÖÆÅäÖüĴæÆ÷*/ Init_Config[0] = 0x66;// Init_Config[1] = 0X04;// Init_Config[2] = 0X55;// Init_Config[3] = 0X70; WriteRegister(0x00,0x04,Init_Config,TEMGET); ReadRegister(TEMGET); } void CofigPreAD() { /*ËÄÏßÖÆÅäÖüĴæÆ÷*/ Init_Config[0] = 0x38;// Init_Config[1] = 0X04;// Init_Config[2] = 0X18;// Init_Config[3] = 0X00; WriteRegister(0x00,0x04,Init_Config,PREGET); ReadRegister(PREGET); } /*************************************************************************** * Function Name: GetAD(unsigned char channel) * Purpose: get the result * Params : Result 0~65535 * *************************************************************************/ int16_t TemGetAD() { int16_t Result; CofigTemAD(); ADStartConversion(TEMGET); while(TEM_DRDY_IN); Result = ReadData(TEMGET); ADPowerDown(TEMGET); return Result; } int16_t PreGetAD() { int16_t Result; CofigPreAD(); ADStartConversion(PREGET); // while(PRE_DRDY_IN); Result = ReadData(PREGET); ADPowerDown(PREGET); return Result; } float pressure_ad_value(void) { unsigned int ADC_Res; float ad_res; //CofigPreAD(); ADC_Res = PreGetAD(); ad_res = (float)ADC_Res / MAXVALUE; ad_res = ad_res * PRE_K_FACTOR; return ad_res; } float temperature_ad_value(void) { unsigned int ADC_Res; float ad_res; //CofigTemAD(); ADC_Res = TemGetAD(); ad_res = (float)ADC_Res/MAXVALUE; ad_res = ad_res * TEM_K_FACTOR; return ad_res; } void ADS1120_Init(void) { // uint16_t i,j; ADS_for_spi(); //ADPowerDown(PREGET); // ADPowerDown(TEMGET); } void ADS_for_spi(void) { GPIO_InitTypeDef GPIO_InitStructure; //´ò¿ª¿ØÖƶ˿ÚʱÖÓ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOE, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; //A6 Miso GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; //B11 DATA ready GPIO_Init(GPIOC,&GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; //B11 DATA ready GPIO_Init(GPIOC,&GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //B11 DATA ready GPIO_Init(GPIOE,&GPIO_InitStructure); TEM_PRE_POWER_Power_ON; TEM_CS_H; //PRE_CS_H; }