📄 sensor_routines.c
字号:
/******************************************************************************/
/* This file is part of the kp development */
/* Copyright kp */
/******************************************************************************/
/* */
/* wind routines */
/* */
/******************************************************************************/
#include <LPC213x.H> // LPC21xx definitions
#include <bsp.h> // include board support package
#include <math.h>
#include <string.h>
float wind_dir_ave;
char wind_dir_str[3];
// read wind direction bits
int read_wind_dir_bits() {
int x;
x = IOPIN0;
x = x >> 20;
x = x & 0x00000007;
return(x);
}
// Read wind direction
float wind_dir (void) {
float azmith[8] = {225.0,270.0,315.0,0.0,45.0,90.0,135.0,180.0};
float tmp;
tmp = azmith[read_wind_dir_bits()];
// calculate wind direction in degrees 0-> North, 90-> E, etc.
// read io_address and strip out the three bits.
// Average reading and calulate angle
// wind_dir_ave = ((wind_dir_ave*4) + tmp)/5;
wind_dir_ave = tmp;
return(wind_dir_ave);
}
void wind_dir_char(void) {
unsigned char tmp;
tmp = read_wind_dir_bits();
switch (tmp) {
case 0: strcpy(wind_dir_str," E"); break;
case 1: strcpy(wind_dir_str,"SE"); break;
case 2: strcpy(wind_dir_str," S"); break;
case 3: strcpy(wind_dir_str,"SW"); break;
case 4: strcpy(wind_dir_str," W"); break;
case 5: strcpy(wind_dir_str,"NW"); break;
case 6: strcpy(wind_dir_str," N"); break;
case 7: strcpy(wind_dir_str,"NE"); break;
}
}
double wind_speed (unsigned int filterred_data) {
double speed, x, X;
// Get period from interrupt routine and calculate speed.
// Interrupt routine averages the data.
// Calculate wind_speed in MPH
// Anomometer calibration equation is
// f(X) = function of freqency // curve fit equation
// (24.342758-0.008535973*X+7.410868e-7*X^2), Where X=x^-1.0
double const A = -1.5562761;
double const B = 7.78138e7;
double const C = 1.0;
double const D = 2118805.0;
x=(double)filterred_data; // need to capture current value
// before interrupt routine modifies
X=pow(x,-1.0);
speed=(A+B*X)/(C+D*X);
if (speed < 0.0) speed = 0.0;
if (filterred_data > 50000000) speed = 0.0;
return(speed);
}
float air_temp (unsigned char ad0) {
float temp, v0;
// This routine will read the A/D for temperature.
// The readings are avaeraged.
// The retuned value if in degrees F
// general equation for AN22100
// Vout=(V/V5)*[1.375+(22.5mv/C)*Ta]
// rearanged assuming V/V5 = 1 (V is at 5 volts)
// Ta= (Vout-1.375)/(0.0225)
v0=((float)ad0/1023.0)*3.3; // convert to volts 3.3 reference
temp = (v0-1.375)/(0.0225);
return (temp);
}
float air_temp_F (float air_c) {
float air_f;
air_f = ((9.0/5.0)*air_c) +32.0;
return (air_f);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -