|
/*本風(fēng)速傳感器采用小型直流有刷電機(jī)與三杯式旋轉(zhuǎn)風(fēng)杯組裝
而成,其工作原理為,當(dāng)環(huán)境有水平流動(dòng)風(fēng)時(shí),旋轉(zhuǎn)風(fēng)杯能夠產(chǎn)生旋轉(zhuǎn),并帶動(dòng)小型電機(jī)產(chǎn)生電壓,其電壓與旋轉(zhuǎn)速度基本成正比。利用此信號(hào)電壓,可以對(duì)環(huán)境風(fēng)速進(jìn)行測(cè)量。這款風(fēng)速傳感器采用海洋軍用的高精度測(cè)量風(fēng)速傳感器,與傳統(tǒng)產(chǎn)品先別,具有更高精度;是建筑機(jī)械(起重機(jī)、履帶吊、門吊、塔吊)、鐵路、港口、碼頭、風(fēng)力發(fā)電、光伏發(fā)電、電廠、氣象、索道、環(huán)境、溫室、養(yǎng)殖等領(lǐng)域不可缺少的風(fēng)速檢測(cè)裝置。用它可以實(shí)現(xiàn)設(shè)備風(fēng)速指示數(shù)字化及自動(dòng)化,是相關(guān)設(shè)備不可缺少的風(fēng)速檢測(cè)設(shè)備。*/
//使用該程序和電路再搭配我們的風(fēng)速風(fēng)向傳感器就可以實(shí)時(shí)顯示風(fēng)速風(fēng)向數(shù)據(jù)
#冠拓電子
#include <stc89c52.h>
#include <lcd.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define MAIN_Fosc 11059200L //定義主時(shí)鐘, 模擬串口和和延時(shí)會(huì)自動(dòng)適應(yīng)。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 轉(zhuǎn)換控制寄存器
#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) //自動(dòng)清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;//風(fēng)向傳感器輸出的模擬電壓值
//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采集后的數(shù)字量
unsigned char disbuff[10]={0}; //顯示字符數(shù)組
void FX_Value_Conversion(); //風(fēng)向處理顯示函數(shù)
void Delay1(unsigned int time); //延時(shí)子函數(shù)
void DelayUs(unsigned int time); //延時(shí)主程序
uint adc10_start(uchar channel); //channel = 0~7 AD轉(zhuǎn)換
/*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() //按鍵設(shè)置顯示
//
//{
// 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() //按鍵設(shè)置顯示
{
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() //風(fēng)速測(cè)量顯示函數(shù)
{
uchar num1, num2, num3;
wind_speed=(wind_speed*5)/1024;
wind_ppm = 0.027*1000*3.6*wind_speed ; //公式:風(fēng)速F=0.027*V(電壓)*3.6 km/h
num1 = (int)(wind_ppm)/100; //取整數(shù)位并顯示
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() //風(fēng)向處理顯示函數(shù)
{
FX_Value=(FX_NUM*5.0)/1024;//數(shù)字量轉(zhuǎn)換為電壓值顯示,10為ADC,所以除以1024
//轉(zhuǎn)換為電壓值字符,單位毫伏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) //北風(fēng) /*1=N\2=S\3=W\4=E\5=EN\6=ES\7=WN\8=WS*/
{
DisplayString(0x00,1,"dir:N "); //北風(fēng)
//dir_set1 = 1;
}
else if(FX_Value>0.35&&FX_Value<=1.05)
{
DisplayString(0x00,1,"dir:EN");//東北風(fēng)
//dir_set1 = 5;
}
else if(FX_Value>1.05&&FX_Value<=1.75)
{
DisplayString(0x00,1,"dir:E "); //東風(fēng)
//dir_set1 = 4;
}
else if(FX_Value>1.75&&FX_Value<=2.45)
{
DisplayString(0x00,1,"dir:ES"); //東南風(fēng)
//dir_set1 = 6;
}
else if(FX_Value>2.45&&FX_Value<=3.2)
{
DisplayString(0x00,1,"dir:S "); //南風(fēng)
//dir_set1 = 2;
}
else if(FX_Value>3.2&&FX_Value<=3.85)
{
DisplayString(0x00,1,"dir:WS"); //西南風(fēng)
//dir_set1 = 8;
}
else if(FX_Value>3.85&&FX_Value<=4.6)
{
DisplayString(0x00,1,"dir:W "); //西風(fēng)
//dir_set1 = 3;
}
else if(FX_Value>4.6)
{
DisplayString(0x00,1,"dir:WN"); //西北風(fēng)
//dir_set1 = 7;
}
}
uint adc10_start(uchar channel) //channel = 0~7 AD轉(zhuǎn)換
{
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;
}
//延時(shí)函數(shù)//
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_();
}
|
評(píng)分
-
查看全部評(píng)分
|