📄 acquisition.c
字号:
//---------------------------------------------------------------------------
// Project Title : Armature (Resistance) tester for stator.
// Project No. : ???
// Project Ver. : 1.00
// File Name : Acquisition.c
// Revision : 1.00
// Initial Date : 22-Oct,2002
// Modify Date : 14-Oct,2003
// Description : Addition of two more resistance 2.2 & 22 ohm to
// enlarge the measuring range
//---------------------------------------------------------------------------
#include <atmel\at89s53.h>
#include <absacc.h>
#include <math.h>
#include "Acquisition.h"
#include "ArmAcqui.h"
//---------------------------------------------------------------------------
#pragma ot (5,SPEED)
//---------------------------------------------------------------------------
#define MSBSamTime -2500/256
#define LSBSamTime -2500%256
//---------------------------------------------------------
#define RES_TEST_FX 22000
#define TEMP_TEST_FX 6250
#define TEMP_TEST_OFFS 6250
//---------------------------------------------------------
#define uchar unsigned char
#define True 1
#define False 0
#define ch0VoltMeas 0x00
#define ch1CurrentMeas 0x01
acquisition_st acqui;
//extern variables
extern uchar pgaGainCh0Vr;
//Public varaiables
uchar adcChannel, adcRepeatTime, adcStep;
unsigned int adcVal;
bit ADC_Conv_completed;
//---------------------------------------------------------------------------
// Functions prototype
//---------------------------------------------------------------------------
void spiadcSampling_Enable(uchar RepeatTimes);
unsigned int rangeRes(void); //get the range of resistance to be measured
uchar pgaGAIN_Select(uchar channel, uchar gainorder);
void _resistance_measure (void);
void _temperature_measure (void);
//********************************************************
void _resistance_measure (void)
{ //newly modified func.: allow 3 biased resistance value
// 0.22, 2.2 & 22 ohm
// The pga gain was modified to be changeable depends on
// measured volage if too small: < 400
unsigned long uns32b;
unsigned int currVd[2], seriesRes;
unsigned long Vd;
uchar i;
acqui.offs[ADC_CURR] = 2048;
acqui.offs[ADC_RESI] = 2048;
acqui.offs[ADC_NULL] = 2048;
acqui.offs[ADC_TEMP] = 2048;
seriesRes = rangeRes();
//measure the bias current
ADC_Conv_completed = False;
adcChannel = ch1CurrentMeas;
spiadcSampling_Enable(100); //sampling for 100 times
while ( !ADC_Conv_completed);
currVd[0] = adcVal;
do {
while ( !ADC_Conv_completed);
ADC_Conv_completed = False;
currVd[1] = adcVal;
if ( currVd[1] / (abs(currVd[1] - currVd[0]) ) > 20 ) {
//changes less than 1/20 or 5%
adcRepeatTime = 0;
break;
} // end if
currVd[0] = currVd[1];
} while (1);
//sampling 5 times to get the average
currVd[0] = 0;
spiadcSampling_Enable(5);
for (i=0; i<5; i++) {
while ( !ADC_Conv_completed);
ADC_Conv_completed = False;
currVd[0] += adcVal;
} // end for
currVd[0] /= 5; //average voltage across the series res.
//obtain a suitable voltage gain of the PGA amplifier
spiadcSampling_Enable(1);
while ( !ADC_Conv_completed);
if ( adcVal < 400 ) //choose suitable pagGain
pgaGainCh0Vr = 1; // x10
else
pgaGainCh0Vr = 0; // x1
pgaGAIN_Select(ch0VoltMeas, ++pgaGainCh0Vr);
//measure the voltage across the resistance of the armature
Vd = 0;
spiadcSampling_Enable(10);
for (i=0; i<10; i++) {
while ( !ADC_Conv_completed);
ADC_Conv_completed = False;
Vd += (long) adcVal;
} // end for
//Calculate the resistance of the armature,
// R = Vd / Ib = Vd / ( CurrVd[0] / seriesRes )
// = Vd * seriesRes / CurrVd[0] / 1000 / pagGain
if ( pgaGainCh0Vr) // pgaGain: x10
uns32b = Vd * seriesRes / 10000; // 10 * 100
else // pgaGain: x1
uns32b = Vd * seriesRes / 1000; // 10 * 100
acqui.resistance = (unsigned int) (uns32b / (long) currVd[0]);
} // end func.
//---------------------------------------------------------------------------
void _temperature_measure (void)
{
spiadcSampling_Enable(1);
while ( !ADC_Conv_completed);
acqui.temperature = adcVal;
} // end func.
//*****************************************************************
void spi_interrupt_service (void)
{
switch (adcStep) {
case 0: // send byte for select channel-1
SPDR = adcChannel; // voltage accros the 0.22/2.2/22 ohm res.
adcStep++;
break;
case 1: // get MSB of channel-1
((unsigned char*)(&adcVal))[1] = SPDR & 0x0f;
SPDR = 0x00; //dummy, trigger next communication
adcStep++;
break;
case 2: // get LSB of channel-1 and start trigger for channel-2
ADCcs = SBIT_HIGH; // chipselect disable.
((unsigned char*)(&adcVal))[0] = SPDR;
ADC_Conv_completed = True;
if ( adcRepeatTime ) {
ADCcs = SBIT_LOW; // Enable SPI again
adcStep = 0;
SPDR = 0x07;
adcRepeatTime--;
} else
SPCR &= ~SPE_; // SPI disable.
break;
default:ADC_Conv_completed = False;
SPCR &= ~SPE_; // SPI disable.
break;
} // end switch
} // end isr
//******************************************************************
unsigned int rangeRes(void)
{ //get the range of resistance to be measured
//assume P3.4, 5 connecting to the 2pole, 3 thro switch
unsigned int code res[3] = {2200, 220, 22}; //multipy by 100
uchar option;
option = (P3 >>4) & 0x03;
return (res[option]);
} // end if
//*************************************************************
uchar pgaGAIN_Select(uchar channel, uchar gainorder)
{ //gainorder--- 00 : x1
// 01 : x10
// 10 : x100
// 11 : x1000
uchar mask, newval;
if ( channel > 3 || gainorder > 3)
return (0x01);
mask = 0x03 << channel;
newval = (P2 & ~mask );
P2 = newval || (gainorder << channel);
return (0);
} // end func.
//************************************************************
void spiadcSampling_Enable(uchar RepeatTimes)
{ // operates under mode 0,0 --- CLK idle in low state
ADCcs = SBIT_LOW;
// SPI Clock Phase, falling edge to clock data out, rising edge to din
// SPI Clock Polarity, idel at high state
// SPE_ enable SPI
SPCR = (SPCR & ~0x03 ) | 0x01; //clk freq = osc clk / 16
SPCR |= (SPE_ | CPHA_ | CPOL_) ;
SPDR = 0x07; // Start bit, Single channel
ADC_Conv_completed = False;
adcStep = 0;
adcRepeatTime = RepeatTimes - 1;
} // end func.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -