📄 blink.c
字号:
/*********************************************************************
Author : ADI - Apps www.analog.com/MicroConverter
Date : Sept. 2005
File : blink.c
Hardware : Applicable to ADuC702x rev H or I silicon
Currently targetting ADuC7026.
Description : Blinks LED continuously
*********************************************************************/
#include<aduc7026.h>
#include<stdio.h> // standard i/o functions
//#include<intrins.h> // in-line intrinsic code (for chkfloat)
// definitions....
#define Tmin_590 (-25) // minimum spec'd temperature of AD590
#define Tmax_590 (105) // maximum spec'd temperature of AD590
// definitons....
#define TMIN (-200) // = minimum temperature in degC
#define TMAX (1000) // = maximum temperature in degC
#define NSEG 127 // = number of sections in table
#define TSEG 9.44882 // = (TMAX-TMIN)/NSEG = temperature span in degC of each segment
// lookup table....
const float C_emf[] = {-8.82595,-8.57746,-8.30754,-8.01734,-7.70775,-7.37957,-7.03357,-6.67046,-6.29089,-5.89543,-5.48463,-5.05899,-4.619,-4.16519,-3.69805,-3.21809,-2.72578,-2.22157,-1.70585,-1.17894,-0.641114,-0.0928852,0.464227,1.02937,1.60273,2.1843,2.77407,3.37195,3.97784,4.59159,5.21303,5.84197,6.47821,7.1215,7.77162,8.42832,9.09134,9.76044,10.4353,11.1158,11.8016,12.4924,13.1881,13.8883,14.5929,15.3017,16.0144,16.7308,17.4507,18.174,18.9005,19.63,20.3624,21.0974,21.835,22.5751,23.3174,24.0619,24.8085,25.5569,26.3072,27.0592,27.8128,28.5678,29.3243,30.082,30.8408,31.6008,32.3617,33.1235,33.886,34.6492,35.4129,36.1771,36.9416,37.7064,38.4713,39.2363,40.0013,40.7661,41.5307,42.295,43.0589,43.8223,44.5852,45.3474,46.109,46.8698,47.6298,48.389,49.1473,49.9047,50.6611,51.4165,52.1709,52.9243,53.6766,54.4279,55.1781,55.9273,56.6754,57.4224,58.1683,58.9131,59.6569,60.3995,61.1409,61.8812,62.6204,63.3582,64.0948,64.8301,65.5639,66.2963,67.0272,67.7565,68.484,69.2099,69.934,70.6562,71.3765,72.095,72.8116,73.5265,74.2398,74.9517,75.6626,76.3728};
float ADC0norm; // average normalized ADC0 conversion result
float ADC1norm; // average normalized ADC1 conversion result
float scaleVsens; // Vsens scaling coefficient
float scale592; // AD592 scaling coefficient
float Tmj; // measurement-junction temperature [degC]
float Tcj; // cold-junction temperature [degC]
float calADC0; // normalized ADC0 value for calibration
float calADC1; // normalized ADC1 value for calibration
float calTmj; // measurement-junction temperature for cal
float pcADC0; // previous cal ADC0 result (for cal)
float pcTin; // previous cal temperature input value
unsigned char fahrenheit = 0; // 0 for degC, 1 for degF
unsigned char cjc = 0; // 0 for thermocouple, 1 for CJC sensor
unsigned char newADCdata = 0; // 1 means new ADC data available
unsigned char serial_in; // serial input data
float T_emf (float v); // temperature at emf voltage v
float V_emf (float t); // emf voltage at temperature t
float Tmin_emf (); // minimum spec'd measurement temperature
float Tmax_emf (); // maximum spec'd measurement temperature
void delay(int);
void Temperature ();
int main(void)
{
float ADC0temp, ADC1temp; // temporary locations for normalized ADC results
volatile unsigned char ADC0H, ADC0M, ADC0L = 0;
volatile unsigned char ADC1H, ADC1M, ADC1L = 0;
POWKEY1 = 0x01; //41.78MHz
POWCON = 0x00;
POWKEY2 = 0xF4;
/*
PLLKEY1 = 0xAA; // Use external clock on P0.7
PLLCON = 0x03;
PLLKEY2 = 0x55;
*/
// REMAP = 1;
GP4DAT = 0x04000000; // P4.2 configured as an output. LED is turned on
while (1) {
pcTin=-1000; // previous cal value to indicate no prev.cal.
ADC0norm=0; // corresponds to MJ temp = CJ temp
fahrenheit = 0;
scale592 = 2500;
ADC1norm=0.11926; // corresponds to CJC temp of 25degC
ADC0H = 0x55;
ADC0M = 0x22;
ADC0L= 0x11;
ADC1H= 0x33;
ADC1M= 0x30;
ADC1L = 0x29;
// if ADCnorm variables are ready for new data..
// ..then put normalized ADC results into ADCtemp variables
ADC0temp = (ADC0H*0x10000+ADC0M*0x100+ADC0L)/8388608.0-1.0;
ADC1temp = (ADC1H*0x10000+ADC1M*0x100+ADC1L)/16777216.0;
// ..and then perform running average with ADC data stored in ADCnorm variables
ADC0norm = 0.8*ADC0norm + 0.2*ADC0temp;
ADC1norm = 0.8*ADC1norm + 0.2*ADC1temp;
Temperature ();
}
}
void delay (int length)
{
while (length >=0)
length--;
}
void Temperature () //__ram // __ram attribute indicates that the code is to be placed in RAM
{
float Vcj; // cold-junction voltage (computed) [mV]
float Vsens; // sense voltage (from ADC) [mV]
// determine cold-junction temperature and EMF voltage
Tcj=ADC1norm*scale592-273.15; // AD592 temperature [degC]
Vcj=V_emf(Tcj); // cold-junction voltage[mV]
// determine measurement-junction temperature
Vsens=ADC0norm*scaleVsens; // sense voltage [mV]
Tmj=T_emf(Vsens+Vcj); // measurement-junction [degC]
/*
// output results via UART
if (cjc) {
printf ("Cold-Junction Temperature = ");
if (fahrenheit) printf ("%+8.2fF",(1.8*Tcj+32));
else printf ("%+8.2fC",Tcj);
// indicate error if any
if (Tcj<Tmin_590) printf (" *ERROR: Under-Range Cold-Junction");
if (Tcj>Tmax_590) printf (" *ERROR: Over-Range Cold-Junction");
}
else {
printf ("Measurement-Junction Temperature = ");
if (fahrenheit) printf ("%+8.2fF",(1.8*Tmj+32));
else printf ("%+8.2fC",Tmj);
// indicate error(s) if any
if (Tcj<Tmin_590) printf (" *ERROR: Under-Range Cold-Junction");
if (Tcj>Tmax_590) printf (" *ERROR: Over-Range Cold-Junction");
if (Tmj<Tmin_emf()) printf (" *ERROR: Under-Range Measurement-Junction");
if (Tmj>Tmax_emf()) printf (" *ERROR: Over-Range Measurement-Junction");
}
printf ("\r\n");
*/
// complement the LED (P3.4 on eval board)
GP4DAT ^= 0x00040000; // Complement P4.2
}
float T_emf (float v)// __ram // __ram attribute indicates that the code is to be placed in RAM
{
float t;
int i, adder;
// set up initial values
i = NSEG/2; // starting value for 'i' index
adder = (i+1)/2; // adder value used in do loop
// determine if input v is within range
if (v<C_emf[0]) // if input is under-range..
i=0; // ..then use lowest coefficients
else if (v>C_emf[NSEG]) // if input is over-range..
i=NSEG-1; // ..then use highest coefficients
// if input within range, determine which coefficients to use
else do {
if (C_emf[i]>v) i-=adder; // either decrease i by adder..
if (C_emf[i+1]<v) i+=adder; // ..or increase i by adder
if (i<0) i=0; // make sure i is >=0..
if (i>NSEG-1) i=NSEG-1; // ..and <=NSEG-1
adder = (adder+1)/2; // divide adder by two (rounded)
} while ((C_emf[i]>v)||(C_emf[i+1]<v)); // repeat 'til done
// compute final result
t = TMIN+TSEG*i + (v-C_emf[i])*TSEG/(C_emf[i+1]-C_emf[i]);
return (t);
}
// _____________________________________________________________
// EMF Voltage as a function of Temperature V_emf
// (for type E thermocouples)
// input: t = temperature of thermocouple in degC
// output: V_emf() = corresponding EMF voltage in mV
// Calculates EMF voltage of thermocouple as a function of
// temperature via a piecewise linear approximation method.
float V_emf (float t)// __ram // __ram attribute indicates that the code is to be placed in RAM
{
float v;
int i;
i=(t-TMIN)/TSEG; // determine which coefficients to use
if (i<0) // if input is under-range..
i=0; // ..then use lowest coefficients
else if (i>NSEG-1) // if input is over-range..
i=NSEG-1; // ..then use highest coefficients
// compute final result
v = C_emf[i]+(t-(TMIN+TSEG*i))*(C_emf[i+1]-C_emf[i])/TSEG;
return (v);
}
// _____________________________________________________________
// Minimum Temperature Function Tmin_emf
// Returns minimum temperature specified by lookup table.
float Tmin_emf () {
return (TMIN);
}
// _____________________________________________________________
// Maximum Temperature Function Tmax_emf
// Returns maximum temperature specified by lookup table.
float Tmax_emf () {
return (TMAX);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -