/*本风速传感器采用小型直流有刷电机与三杯式旋转风杯组装
而成,其工作原理为,当环境有水平流动风时,旋转风杯能够产生旋转,并带动小型电机产生电压,其电压与旋转速度基本成正比。利用此信号电压,可以对环境风速进行测量。这款风速传感器采用海洋军用的高精度测量风速传感器,与传统产品先别,具有更高精度;是建筑机械(起重机、履带吊、门吊、塔吊)、铁路、港口、码头、风力发电、光伏发电、电厂、气象、索道、环境、温室、养殖等领域不可缺少的风速检测装置。用它可以实现设备风速指示数字化及自动化,是相关设备不可缺少的风速检测设备。*/
//使用该程序和电路再搭配我们的风速风向传感器就可以实时显示风速风向数据
#冠拓电子
#include <stc89c52.h>
#include <lcd.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define MAIN_Fosc 11059200L //定义主时钟, 模拟串口和和延时会自动适应。5~35MHZ 22118400L
// 7 6 5 4 3 2 1 0 Reset Value
//sfr ADC_CONTR = 0xBC; ADC_POWER SPEED1 SPEED0 ADC_FLAG ADC_START CHS2 CHS1 CHS0 0000,0000 //AD 转换控制寄存器
#define ADC_OFF() ADC_CONTR = 0
#define ADC_ON (1 << 7)
#define ADC_90T (3 << 5)
#define ADC_180T (2 << 5)
#define ADC_360T (1 << 5)
#define ADC_540T 0
#define ADC_FLAG (1 << 4) //软件清0
#define ADC_START (1 << 3) //自动清0
#define ADC_CH0 0
#define ADC_CH1 1
#define ADC_CH2 2
#define ADC_CH3 3
#define ADC_CH4 4
#define ADC_CH5 5
#define ADC_CH6 6
#define ADC_CH7 7
//sbit k1 = P2^0;
//sbit buzzer = P3^7;
/************* 本地变量声明 **************/
int wind_set = 50;
float wind_speed = 0.0;
float wind_ppm = 0.0;
void wind_Value_Conversion();
float FX_Value=0.0;//风向传感器输出的模拟电压值
//int dir_set = 1;
//int dir_set1; /*1=N\2=S\3=W\4=E\5=EN\6=ES\7=WN\8=WS*/
unsigned int FX_NUM=0;//AD采集后的数字量
unsigned char disbuff[10]={0}; //显示字符数组
void FX_Value_Conversion(); //风向处理显示函数
void Delay1(unsigned int time); //延时子函数
void DelayUs(unsigned int time); //延时主程序
uint adc10_start(uchar channel); //channel = 0~7 AD转换
/*void press_k1(void)
{
if(k1 == 0)
{
Delay1(100);
if(k1 == 0)
{
dir_set += 1;
if(dir_set>8)
{
dir_set = 1;
}
}
}
}
/*void press_k2()
{
if(k2 == 0)
{
Delay1(100);
if(k2 == 0)
{
wind_set -= 1;
if(wind_set<0)
{
wind_set = 999;
}
}
}
}
*/
// void display_set() //按键设置显示
//
//{
// if(dir_set == 1) /*1=N\2=S\3=W\4=E\5=EN\6=ES\7=WN\8=WS*/
// {
// DisplayString(0x09,0,"set:N ");
// }
// else if(dir_set == 2)
// {
// DisplayString(0x09,0,"set:S ");
// }
// else if(dir_set == 3)
// {
// DisplayString(0x09,0,"set:W ");
// }
// else if(dir_set == 4)
// {
// DisplayString(0x09,0,"set:E ");
// }
// else if(dir_set == 5)
// {
// DisplayString(0x09,0,"set:EN");
// }
// else if(dir_set == 6)
// {
// DisplayString(0x09,0,"set:ES");
// }
// else if(dir_set == 7)
// {
// DisplayString(0x09,0,"set:WN");
// }
// else if(dir_set == 8)
// {
// DisplayString(0x09,0,"set:WS");
// }
// }
/*void display_set() //按键设置显示
{
uchar num1, num2, num3;
num1 = wind_set/100+'0';
num2 = wind_set%100/10+'0';
num3 = wind_set%10+'0';
DisplaySingleChar(0x04,1,num1);
DisplaySingleChar(0x05,1,num2);
DisplaySingleChar(0x06,1,num3);
DisplayString(0x7,1,"km/h");
} */
//void buzzer_on()
//{
// if(dir_set==dir_set1) /*从1****8取值*/
// {
// buzzer = 0;
// }
// else
// {
// buzzer = 1;
// }
//}
void main()
{
P1ASF = 0x07; //12C5A60AD/S2系列模拟输入(AD)选择
ADC_CONTR = ADC_360T | ADC_ON;
LCDInit(); //显示屏初始化
DisplayString(0x0,0," Welcome... ");
Delay1(500);
//DisplayString(0x0,0,"Wind Speed");
//DisplayString(0x0,1,"dir:");
//Delay1(500);
WriteCmd(LCD_CLS);
//DisplayString(0x0,0,"Speed:");
//DisplayString(0x0,1,"dir:");
DisplayString(0x0,0," mv");
//DisplayString(0x0b,0,"set:");
//DisplayString(0x0,1,"dir:");
//DisplayString(0x0,1,"Set:");
while(1)
{
//press_k1();
// press_k2();
wind_speed = adc10_start(0);
wind_Value_Conversion();
FX_NUM = adc10_start(1);//P1.1口采集模拟电压
FX_Value_Conversion();
//display_set();
//buzzer_on();
}
}
void wind_Value_Conversion() //风速测量显示函数
{
uchar num1, num2, num3;
wind_speed=(wind_speed*5)/1024;
wind_ppm = 0.027*1000*3.6*wind_speed ; //公式:风速F=0.027*V(电压)*3.6 km/h
num1 = (int)(wind_ppm)/100; //取整数位并显示
num2 = (int)(wind_ppm)%100/10;
num3 = (int)(wind_ppm)%10;
DisplaySingleChar(0x09,1,num1+0x30);
DisplaySingleChar(0x0a,1,num2+0x30);
DisplaySingleChar(0x0b,1,num3+0x30);
DisplayString(0x0c,1,"km/h");
}
void FX_Value_Conversion() //风向处理显示函数
{
FX_Value=(FX_NUM*5.0)/1024;//数字量转换为电压值显示,10为ADC,所以除以1024
//转换为电压值字符,单位毫伏mv
disbuff[0]=(int)(FX_Value*1000)/1000+'0';
disbuff[1]=(int)(FX_Value*1000)%1000/100+'0';
disbuff[2]=(int)(FX_Value*1000)%100/10+'0';
disbuff[3]=(int)(FX_Value*1000)%10+'0';
//显示电压值
DisplaySingleChar(0x00,0,disbuff[0]);
DisplaySingleChar(0x01,0,disbuff[1]);
DisplaySingleChar(0x02,0,disbuff[2]);
DisplaySingleChar(0x03,0,disbuff[3]);
if(FX_Value<=0.35) //北风 /*1=N\2=S\3=W\4=E\5=EN\6=ES\7=WN\8=WS*/
{
DisplayString(0x00,1,"dir:N "); //北风
//dir_set1 = 1;
}
else if(FX_Value>0.35&&FX_Value<=1.05)
{
DisplayString(0x00,1,"dir:EN");//东北风
//dir_set1 = 5;
}
else if(FX_Value>1.05&&FX_Value<=1.75)
{
DisplayString(0x00,1,"dir:E "); //东风
//dir_set1 = 4;
}
else if(FX_Value>1.75&&FX_Value<=2.45)
{
DisplayString(0x00,1,"dir:ES"); //东南风
//dir_set1 = 6;
}
else if(FX_Value>2.45&&FX_Value<=3.2)
{
DisplayString(0x00,1,"dir:S "); //南风
//dir_set1 = 2;
}
else if(FX_Value>3.2&&FX_Value<=3.85)
{
DisplayString(0x00,1,"dir:WS"); //西南风
//dir_set1 = 8;
}
else if(FX_Value>3.85&&FX_Value<=4.6)
{
DisplayString(0x00,1,"dir:W "); //西风
//dir_set1 = 3;
}
else if(FX_Value>4.6)
{
DisplayString(0x00,1,"dir:WN"); //西北风
//dir_set1 = 7;
}
}
uint adc10_start(uchar channel) //channel = 0~7 AD转换
{
uint adc;
uchar i;
ADC_RES = 0;
ADC_RESL = 0;
ADC_CONTR = (ADC_CONTR & 0xe0) | ADC_START | channel;
i = 250;
do{
if(ADC_CONTR & ADC_FLAG)
{
ADC_CONTR &= ~ADC_FLAG;
adc = (uint)ADC_RES;
adc = (adc << 2) | (ADC_RESL & 3);
return adc;
}
}while(--i);
return 1024;
}
//延时函数//
void Delay1(unsigned int time)
{
unsigned int timeCounter = 0;
for (timeCounter = time;timeCounter > 0 ;timeCounter --)
DelayUs(255);
}
void DelayUs(unsigned int time)
{
unsigned int timeCounter = 0;
for (timeCounter = 0;timeCounter < time;timeCounter ++)
_nop_();
}
|