//风速程序:
#include<reg51.h>
#include<intrins.h>
#include <absacc.h>
#include <math.h>
#define uchar unsigned char
#define uint unsigned int
#define BUSY 0x80
#define DATAPORT P1
sbit ADCS =P3^5; //ADC0832 chip seclect
sbit ADDI =P3^7; //ADC0832 k in
sbit ADDO =P3^7; //ADC0832 k out
sbit ADCLK =P3^6; //ADC0832 clock signal
sbit LCM_RS=P2^0; //1602使能
sbit LCM_RW=P2^1; //1602使能
sbit LCM_EN=P2^2; //1602使能
uchar ad_data;
uchar v_data;
unsigned char v_bai;
unsigned char v_shi;
unsigned char v_ge;
uchar code str0[]={"Fs: m/s"};
void delay(uint);
void lcd_wait(void);
void delay_LCM(uint);
void initLCM( void);
void lcd_wait(void);
void WriteCommandLCM(uchar WCLCM,uchar BusyC);
void WriteDataLCM(uchar WDLCM);
void DisplayOneChar(uchar X,uchar Y,uchar DData);
void DisplayListChar(uchar X,uchar Y,uchar code *DData);
void display(void);
uchar Adc0832(unsigned char channel);
void data_pro(void);
/**********main funcation************/
void main(void)
{
delay(500);
ad_data=0;
initLCM( );
WriteCommandLCM(0x01,1);
DisplayListChar(0,0,str0);
while(1)
{
ad_data =Adc0832(0);
data_pro();
display();
}
}
/*********延时K*1ms,12.000mhz**********/
void delay(uint k)
{
uint i,j;
for(i=0;i<k;i++)
{
for(j=0;j<60;j++)
{;}
}
}
/**********写指令到LCM子函数************/
void WriteCommandLCM(uchar WCLCM,uchar BusyC)
{
if(BusyC)lcd_wait();
DATAPORT=WCLCM;
LCM_RS=0;
LCM_RW=0;
LCM_EN=1;
_nop_();
_nop_();
_nop_();
LCM_EN=0;
}
/**********写数据到LCM子函数************/
void WriteDataLCM(uchar WDLCM)
{
lcd_wait( );
DATAPORT=WDLCM;
LCM_RS=1;
LCM_RW=0;
LCM_EN=1;
_nop_();
_nop_();
_nop_();
LCM_EN=0;
}
/***********LCM内部等待函数*************/
void lcd_wait(void)
{
DATAPORT=0xff;
LCM_EN=1;
LCM_RS=0;
LCM_RW=1;
_nop_();
_nop_();
_nop_();
while(DATAPORT&BUSY)
{ LCM_EN=0;
_nop_();
_nop_();
LCM_EN=1;
_nop_();
_nop_();
}
LCM_EN=0;
}
/**********LCM初始化子函数***********/
void initLCM( )
{
DATAPORT=0;
delay(15);
WriteCommandLCM(0x38,0);
delay(5);
WriteCommandLCM(0x38,0);
delay(5);
WriteCommandLCM(0x38,0);
delay(5);
WriteCommandLCM(0x38,1);
WriteCommandLCM(0x08,1);
WriteCommandLCM(0x01,1);
WriteCommandLCM(0x06,1);
WriteCommandLCM(0x0c,1);
}
/****显示指定坐标的一个字符子函数****/
void DisplayOneChar(uchar X,uchar Y,uchar DData)
{
Y&=1;
X&=15;
if(Y)X|=0x40;
X|=0x80;
WriteCommandLCM(X,0);
WriteDataLCM(DData);
}
/*******显示指定坐标的一串字符子函数*****/
void DisplayListChar(uchar X,uchar Y,uchar code *DData)
{
uchar ListLength=0;
Y&=0x01;
X&=0x0f;
while(X<16)
{
DisplayOneChar(X,Y,DData[ListLength]);
ListLength++;
X++;
}
}
/*****************系统显示子函数*****************/
void display(void)
{
WriteCommandLCM(0x0c,1);
DisplayListChar(0,0,str0);
DisplayOneChar(7,0,v_bai+0x30);
DisplayOneChar(8,0,v_shi+0x30);
DisplayOneChar(9,0,v_ge +0x30);
delay(1000);
}
/************
读ADC0832函数
************/
//采集并返回
uchar Adc0832(unsigned char channel)
{
uchar i=0;
uchar j;
uint dat=0;
uchar ndat=0;
if(channel==0)channel=2;
if(channel==1)channel=3;
ADDI=1;
_nop_();
_nop_();
ADCS=0;
_nop_();
_nop_();
ADCLK=1;
_nop_();
_nop_();
ADCLK=0;
_nop_();
_nop_();
ADCLK=1;
ADDI=channel&0x1;
_nop_();
_nop_();
ADCLK=0;
_nop_();
_nop_();
ADCLK=1;
ADDI=(channel>>1)&0x1;
_nop_();
_nop_();
ADCLK=0;
ADDI=1;
_nop_();
_nop_();
dat=0;
for(i=0;i<8;i++)
{
dat|=ADDO;
ADCLK=1;
_nop_();
_nop_();
ADCLK=0;
_nop_();
_nop_();
dat<<=1;
if(i==7)dat|=ADDO;
}
for(i=0;i<8;i++)
{
j=0;
j=j|ADDO;
ADCLK=1;
_nop_();
_nop_();
ADCLK=0;
_nop_();
_nop_();
j=j<<7;
ndat=ndat|j;
if(i<7)ndat>>=1;
}
ADCS=1;
ADCLK=0;
ADDO=1;
dat<<=8;
dat|=ndat;
return(dat);
}
void data_pro(void) //风速计算函数
{
unsigned int temp;
float v;
if(14<ad_data<243)
{
int vary=ad_data;
v=vary*20;
v=(v-250)/7; v=(v-0.4)/1.6*32.4;
// v=sqrt(v*1600)*0.2-9;
temp=(int)(v);
v_bai=temp/100;
v_shi=(temp%100)/10;
v_ge=temp%10;
}
}