📄 7705.c
字号:
/************************************************************************/
/************************************************************************/
/* */
/* ad7705 */
/* */
/* Author: zhiqiang huo */
/* */
/* */
/************************************************************************/
//************************************************************************/
// set sig_coun=16, consult_con==1, gain==?,
// self_just
//
//
#include "math.h"
#include "reg52.h"
#include "typedef.h"
#include "function.h"
#include "define.h"
#include "varible.h"
#ifdef DC_SINGAL
#define MID_VIN_AD 0xbfff
#endif
sbit AD_CLK = P1^3;
sbit AD_IN = P1^4;
sbit AD_OUT = P1^5;
sbit CONTROL = P3^4; //lichi current control port
//**************function declare***************
void Adc_init(void);
void Setup_adc_device( unsigned char adjust_mode,unsigned char gain,unsigned char polarity,unsigned char update_rate );
void Poisitive(int value);
void Add_consult( int value);
void Dec_consult( int value);
void Self_adjust(void);
//void Ad_start(void);
unsigned int Read_con_data(void); //
unsigned char Check_drdy(void); //
void Write_reg( unsigned char reg_data);
unsigned int Read_word(void); //
unsigned char Read_reg_byte(void);
void Precess_con_data(void);
void Delay(unsigned int timer);
void Return_to_init_status(void);
void Delay_1us(void);
unsigned char Two_square(void);
extern bit system_init_F;
/*********************************/
//****************int0**************************/
void Adc_init(void)
{
#ifndef DC_SINGAL
adc_gain = Read1B(0x25,0); //get system gain
#else
adc_gain = ADC_GAIN_1 ;
#endif
Delay(DELAY3S);
AD_CLK = 1; //7705-CLK high level
AD_IN = 1;
EA=0;
Return_to_init_status();
Setup_adc_device(ADC_SELF,adc_gain,ADC_BIPOLAR,ADC_200);
EA=1;
Delay(DELAY3S);
// ad_step = 0 ;
start_F = 0 ;
consult_coun = 1 ;
consult = 0 ;
consult_1 = 0 ;
consult_2 = 0 ;
consult_3 = 0 ;
return;
}
/***********************************/
/***********************************/
/*
void Adc_init_1(void)
{
adc_gain = Read1B(0x25,0); //get system gain
AD_CLK = 1; //7705-CLK high level
AD_IN = 1;
Return_to_init_status();
Setup_adc_device(ADC_SELF,adc_gain,ADC_BIPOLAR,ADC_200);
ad_step = 0 ;
start_F = 0 ;
consult_coun = 1 ;
consult_1 = 0 ;
consult_2 = 0 ;
consult_3 = 0 ;
return;
}
*/
/*****************************/
/*****************************/
void Setup_adc_device( unsigned char adjust_mode,unsigned char gain,unsigned char polarity,unsigned char update_rate )
{
// unsigned char byte;
Write_reg(CLK_REG|WRITE|CHANEL_0); //0X20+0X00+0X00=0X20 CLOCK REGISTER
Write_reg(update_rate);
// Write_reg(CLK_REG|READ|CHANEL_0); //0X20+0X00+0X00=0X20 CLOCK REGISTER
// byte = Read_reg_byte();
Write_reg(SET_REG|WRITE|CHANEL_0); //0X10
Write_reg(adjust_mode|gain|polarity);
// Write_reg(SET_REG|READ|CHANEL_0); //0X10
// byte = Read_reg_byte();
return;
}
/************************************************/
/* interrupt 1/10ms,
/*******************************/
/********************************/
/********************************/
void Signal(void) interrupt 0
//void Signal(void) interrupt 3
{
// unsigned int value;
EA=1; //mask interrupt
EX0 = 0 ;
/* debug
ET1 = 0 ;
TH1 = TM10MS_H ;
TL1 = TM10MS_L ;
/*
if(start_F != 1)
{
// if(ad_step == 0)
// Ad_start();
ad_step++;
if(ad_step ==16)
{
start_F = 1 ;
ad_step = 0 ;
}
}
else
{ */
switch(ad_step)
{
case 0: //0x00
CONTROL = 1;
ad_step++;
break;
case 1:
if(system_init_F == 1)
{
Write_reg(SET_REG|WRITE|CHANEL_0); //0X10
Write_reg(adc_gain|ADC_BIPOLAR|START_AD);
}
ad_step++;
break;
case 7:
if(system_init_F == 1)
{
Precess_con_data();
Write_reg(SET_REG|WRITE|CHANEL_0); //0X10
Write_reg(adc_gain|ADC_BIPOLAR|DIS_AD);
}
ad_step++;
break;
// case POLAR_REVERS: //0x08
case 8:
CONTROL = 0 ;
// CONTROL = 1; //test
ad_step++;
break;
case 9:
// Precess_con_data();
// Write_reg(0x04);
if(system_init_F == 1)
{
Write_reg(SET_REG|WRITE|CHANEL_0); //0X10
Write_reg(adc_gain|ADC_BIPOLAR|START_AD);
}
ad_step++;
break;
case 15:
ad_step=0;
if(system_init_F == 1)
{
Precess_con_data();
Write_reg(SET_REG|WRITE|CHANEL_0); //0X10
Write_reg(adc_gain|ADC_BIPOLAR|DIS_AD);
if(consult_coun == 1)
{
consult_coun = 2;
consult_1=consult_1/2.0;
}
else if(consult_coun == 2)
{
consult_coun = 3;
consult_2 = consult_2/2.0;
}
else
{
consult_coun = 1;
consult_3=consult_3/2.0;
consult=(consult_1+consult_2+consult_3)/3.0;
consult_1=0;
consult_2=0;
consult_3=0;
}
}
break;
default:
ad_step++;
break;
}
EX0 = 1;
// ET1 = 1 ;
}
/*************************/
/*************************/
/*
void Poisitive(void)
//{
#ifdef POLARITY
if (consult >= 0x7fff)
{
consult = consult - 0x7fff;
if(CONTROL == 1)
// Add_consult(value);
liuxiang_F = POSITIVE ;
else
// Dec_consult(value);
liuxiang_F = REVERSE ;
}
else
{
consult = 0x7fff - consult;
if(CONTROL == 1)
// Dec_consult(value);
liuxiang_F = REVERSE ;
else
// Add_consult(value);
liuxiang_F = POSITIVE;
}
#else
if (consult >= 0x7fff)
{
consult = consult - 0x7fff;
if(CONTROL == 1)
// Dec_consult(value);
liuxiang_F = REVERSE ;
else
// Add_consult(value);
liuxiang_F = POSITIVE;
}
else
{
consult = 0x7fff - consult;
if(CONTROL == 1)
// Add_consult(value);
liuxiang_F = POSITIVE;
else
// Dec_consult(value);
liuxiang_F = REVERSE ;
}
#endif
da_data = consult ; //remain for da5615 convert used
return;
} */
/***********************************/
void Poisitive(int value)
{
/*
if (value < 0)
{
liuxiang_F = REVERSE ;
value = abs (value); //get abs value
Add_consult(value);
}
else if(value >= 0)
{
liuxiang_F = POSITIVE;
value = abs (value);
Add_consult(value);
}
return;
}
*/
#ifdef POLARITY
if (value < 0)
{
value = 32768+value; //get abs value
// Add_consult(value);
#ifndef DC_SINGAL
if(CONTROL == 1)
{
Add_consult(value);
}
else
{
Dec_consult(value);
}
#else
Add_consult(value);
#endif
}
else if(value >= 0)
{
value = 32768 - value;
// Dec_consult(value);
#ifndef DC_SINGAL
if(CONTROL == 1)
{
Dec_consult(value);
}
else
{
Add_consult(value);
}
#else
Dec_consult(value);
#endif
}
#else
if (value < 0)
{
value = 32768+value;
#ifndef DC_SINGAL
if(CONTROL == 1)
{
Dec_consult(value);
// liuxiang_F = REVERSE ;
}
else
{
Add_consult(value);
// liuxiang_F = POSITIVE;
}
#else
Dec_consult(value);
#endif
}
else if(value >= 0)
{
value = 32768 - value;
#ifndef DC_SINGAL
if(CONTROL == 1)
{
Add_consult(value);
// liuxiang_F = POSITIVE;
}
else
{
Dec_consult(value);
// liuxiang_F = REVERSE ;
}
#else
Add_consult(value);
#endif
}
#endif
return;
}
/************************************/
/************************************/
void Add_consult( int value)
{
if(consult_coun == 1)
consult_1 += value;
else if(consult_coun == 2)
consult_2 += value;
else if(consult_coun == 3)
consult_3 += value;
return;
}
/*********************************/
/*********************************/
void Dec_consult( int value)
{
if(consult_coun == 1)
consult_1 -= value;
else if(consult_coun == 2)
consult_2 -= value;
else if(consult_coun == 3)
consult_3 -= value;
return;
}
//************** AD self autojust**************
void Self_adjust(void)
{
Write_reg(SET_REG|WRITE|CHANEL_0); //0X10
Write_reg(ADC_SELF|adc_gain|ADC_BIPOLAR);
self_adj_F = 1 ;
// Delay_ms(DELAY200MS);
return;
}
//****************start ad turn****************
/*
void Ad_start(void)
{
// Return_to_init_status();
Write_reg(0x10); //active chanel 1, next operation is write setup register
Write_reg(ADC_NORMAL|adc_gain|ADC_BIPOLAR);
// Write_reg(0x20);
return;
} */
//*****************read ad result***************
unsigned int Read_con_data(void)
{
unsigned int word;
while(Check_drdy());
// if(Check_drdy()) //wait DRDY changed low
// return(last_ad_value);
// Write_reg(0x38); //set the next operation for 16 bit read from the data register
Write_reg(0x38);
word = Read_word();
while(!Check_drdy());
// last_ad_value = word ;
return(word);
}
//*******************read DRDY**************
unsigned char Check_drdy(void)
{
unsigned char status;
Write_reg(0x08); //next read opration register is com
status = Read_reg_byte();
return(status&0x80) ;
}
//************************************
// write a byte to register
//************************************
void Write_reg(unsigned char reg_data)
{
unsigned char i;
unsigned char tr_data;
tr_data = reg_data;
for(i=1;i<=8;++i)
{
//7705-CLK low level
AD_IN = tr_data & 0x80;
tr_data <<= 1;
AD_CLK = 0;
AD_CLK = 1 ;
}
AD_IN = 1 ;
AD_CLK = 1;
return ;
}
//************************************
// read a word from data register
//************************************
unsigned int Read_word(void)
{
bit flag;
unsigned int ad_byte=0x0000;
unsigned char i ;
AD_CLK = 1;
AD_OUT = 1;
for(i=1;i<=16;++i)
{
AD_CLK = 1 ;
AD_CLK=0;
AD_OUT = 1;
flag= AD_OUT ;
if(!flag)
ad_byte |= 0x01;
if(i<16)
ad_byte <<= 1 ;
}
AD_IN = 1 ;
AD_CLK = 1;
return(ad_byte); //because tonguo fanxiang
}
//***************************************************
// read a byte from register
//***************************************************
unsigned char Read_reg_byte(void)
{
bit flag;
unsigned char i;
unsigned char com_data ;
com_data = 0x00;
AD_CLK=1;
AD_OUT = 1 ;
for(i=1;i<=8;++i)
{
AD_CLK=1;
AD_CLK=0;
AD_OUT=1;
flag = AD_OUT;
if(!flag)
com_data |= 0x01;
if(i<8)
com_data <<= 1;
}
AD_IN = 1;
AD_CLK = 1 ;
return(com_data);
}
/***********************************/
/***********************************/
void Precess_con_data(void)
{
unsigned int value ;
if(self_adj_F != 1)
{
value = Read_con_data();
Poisitive(value);
}
return;
}
/**********************************/
/**********************************/
void Delay(unsigned int timer)
{
unsigned char i,j;
while(timer)
{
for(i=0;i<14;i++) ///delay 10ms
for(j=0;j<217;j++)
;
timer--;
}
return;
}
/*************************************/
/*************************************/
void Return_to_init_status(void)
{
unsigned char i;
AD_CLK = 1 ;
for(i=1;i<=36;++i)
{
AD_CLK=0; //7705-CLK low level
AD_CLK=1; //7705-CLK high level
}
AD_IN = 1;
AD_CLK = 1 ;
return ;
}
/*******************************/
/*******************************/
void Delay_1us(void)
{
unsigned int i ;
for(i=0;i<300;i++)
;
return;
}
/**********************************/
/**********************************/
unsigned char Two_square(void)
{
switch(filter_data)
{
case 0:
return(1);
case 1:
return(2);
case 2:
return(4);
case 3:
return(8);
case 4:
return(16);
case 5:
return(32);
case 6:
return(64);
default:
return(1);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -