CELIS/SYSTEM/ads1015.c
2024-08-09 14:24:48 +08:00

322 lines
5.8 KiB
C
Raw Blame History

#include "ads1015.h"
#include "stm32f10x_i2c.h"
#include "sys.h"
static void ADS1115_delay(u16 D)
{
while(--D);
}
void delay_nms(u16 ms)
{
u16 i;
u32 M = 0;//720W
for(i = 0;i < ms; i++)
for(M=12000;M > 0;M--);
}
void delay_nus(u16 us)
{
u16 i;
u16 M = 0;//720W
for(i = 0;i < us; i++)
for(M=72;M > 0;M--);
}
/////////////////PA8 SDA////PA9 SCL///////////////////////////////////
void ADS1115_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB ,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;//A SCL SDA
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
SDA_A1;
SCL_A1;
}
//I2C????
void I2CStart_A(void)
{
SDA_A1;
ADS1115_delay(5);
SCL_A1;
ADS1115_delay(5);
SDA_A0;
ADS1115_delay(5);//MIN 160ns
SCL_A0;
ADS1115_delay(5);
}
//I2C????
void I2CStop_A(void)
{
SDA_A0;
ADS1115_delay(5);
SCL_A1;
ADS1115_delay(5);
SDA_A1;
ADS1115_delay(5);//MIN 160ns
}
//I2C ????
void I2CWriteByte_A(u8 DATA)
{
u8 i;
SCL_A0;
for(i = 0;i < 8; i++)
{
if(DATA&0x80)
{
SDA_A1;
}
else
{
SDA_A0;
}
SCL_A1;//????????
ADS1115_delay(5);
SCL_A0;
ADS1115_delay(5);
DATA = DATA << 1;
}
SDA_A1;
SCL_A1;
ADS1115_delay(5);
SCL_A0;
}
//I2C ????
u8 I2CReadByte_A(void)
{
u8 TData=0,i;
for(i=0;i<8;i++)
{
SCL_A1;
ADS1115_delay(5);
TData=TData<<1;
if(SDA_AI)
{
TData|=0x01;
}
SCL_A0;
ADS1115_delay(5);
}
SCL_A0;
ADS1115_delay(5);
SDA_A0;
ADS1115_delay(5);
SCL_A1;
ADS1115_delay(5);
SCL_A0;
ADS1115_delay(5);
SDA_A1;
return TData;
}
/*********************************************************************
*????: ADS1115Config
*? ?: ??ADS1115??????,??????
*? ?: HCMD :????8?(??,??,????)
LCMD : ????8?(????? ???? ???? ??????)
*? ?; ?
********************************************************************/
void ADS1115Config_A(u8 LCMD,u8 HCMD)
{
u8 i=0;
u8 Initdata[4];
Initdata[0] = 0x90; // ??0x90 ??ADR?? ????
Initdata[1] = 0x01;// ?????
Initdata[2] = HCMD; // ??????
Initdata[3] = LCMD; // ??????
SCL_A1;
I2CStart_A(); //??
for(i=0;i<4;i++)
{
I2CWriteByte_A(Initdata[i]);
ADS1115_delay(10);
}
I2CStop_A(); //??
}
void SetThresHold_A(u16 L_TH,u16 H_TH) //??????
{
SCL_A1;
I2CStart_A(); // ??
I2CWriteByte_A(0x90);
I2CWriteByte_A(0x02);//???????
I2CWriteByte_A((L_TH>>8));
I2CWriteByte_A(L_TH);
I2CStop_A(); //??
I2CStart_A(); //??
I2CWriteByte_A(0x90);
I2CWriteByte_A(0x03);//???????
I2CWriteByte_A((H_TH>>8));
I2CWriteByte_A(H_TH);
I2CStop_A(); //??
}
/*******************************************************************
*????: ReadAD_A
*? ?: ??AD????
*? ?: ???????????????
*? ?; ?
********************************************************************/
vs16 Data[2]={0,0};
u16 ReadAD_A(void)
{
//???????
SCL_A1;
I2CStart_A();
I2CWriteByte_A(0x90);
I2CWriteByte_A(0x00);
I2CStop_A();
I2CStart_A();
I2CWriteByte_A(0x91);
Data[0] = I2CReadByte_A();
Data[1] = I2CReadByte_A();
I2CStop_A();
Data[0] = Data[0]<<8 | Data[1];
return (Data[0]);//&0x7fff
}
u16 getad(u8 LCMD,u8 HCMD)
{
u16 value=0;
static u16 lastvalue=0;
ADS1115Config_A(LCMD,HCMD); //????
// delay_nms(5);
value=ReadAD_A();
return value;
}
u16 lvbo(u8 LCMD,u8 HCMD) //?30??????
{
static u16 data;
static u32 U=0, temp; //u32 float?double
static u8 index = 0;
static u8 flag = 0;
#if 1
if(flag == 0)
{
U+=getad(LCMD,HCMD);
if( index++>=9 )flag=1;
return data; //
}
else
{
flag = 0;
temp=U;
U=0;
index =0;
data = temp/10;
return data; //
}
#else
for(index=0;index<10;index++)
{
U+=getad(LCMD,HCMD);
}
temp=U;
U=0;
#endif
return ((float)temp/10); //
}
u16 t,result;
float t1;
u16 resultFlag = 0;
float GetSampleVolt(u8 key)
{
static u32 timer = 0;
if(key==1)
{
// result=lvbo(0xeb,0x82); //A0 A1???????? ???+???1111 1011,1000 0010
result=lvbo(0x82,0xeb);
if(result >= 0x8000 && result <= 0xffff)
result = 0xffff - result; //????????,??A0 A1?????
else if(result >= 0xffff)
result = 0;
t1=4.096*2*result/65535; //?????
if(result == 0x7fff || result == 0x8000)
{
//printf("????!\r\n\r\n");
}
else
{
//printf("????!\r\n\r\n");
}
}else if(key==2){
result=lvbo(0xb2,0xe3);
if(result >= 0x8000)
{
resultFlag =1;
result = 0xffff - result; //????????,??A2 A3?????
}
else
{
resultFlag =0;
}
t1=4.096*2*result/65535; //adc <20><>ѹ
// 4.99/1332.99 X s =t1;
//t1 = t1*1332.99/4.99;
t1 = t1*13329.9/4.99;
if(resultFlag == 1)
{
if(TickOut(&timer, 2000)==TRUE)
{
printf("Negative voltage sample fault!\r\n");
TickOut(&timer, 0);
}
t1 = 0-t1;
}
else
{
//printf("????!\r\n\r\n");
}
}
else if(key == 3)
{
result=lvbo(0xb4,0xe3);
if((result >= 0x8000) && (result <= 0xffff))
result = 0xffff - result; //????????,??A0 A1?????
else if(result >= 0xffff)
result = 0;
t1=2.048*2*result/65535; //?????
//printf("???2.048V,A0-A1???? = %f V\r\n",t1);//??
if(result == 0x7fff || result == 0x8000)
{
//printf("????!\r\n\r\n");
}
else
{
//printf("????!\r\n\r\n");
}
}
return t1;
}