压力传感器程序(共8页).doc
精选优质文档-倾情为你奉上#include<12c5a60s2.h>#include<intrins.h>#define uint unsigned int#define uchar unsigned char#define ADC_POWER 0x80 #define ADC_FLAG 0x10#define ADC_START 0x08#define ADC_SPEEDLL 0x00 /540个时钟周期转换一次#define ADC_SPEEDL 0x20 /360个时钟周期转换一次#define ADC_SPEEDH 0x40 /180个时钟周期转换一次#define ADC_SPEEDHH 0x60 /90个时钟周期转换一次sbit lcden=P34;sbit rs=P35;sbit wr=P36;sbit dula=P26;sbit wela=P27;uchar code table=" Voltage value"uchar code table1=" mV"uchar t;float AD_val;/*延时函数*/void delay(uint z)uint x,y;for(x=z;x>0;x-)for(y=110;y>0;y-);/*写指令*/void write_com(uchar com)rs=0;wr=0;P0=com;lcden=0;delay(5);lcden=1;delay(5);lcden=0;/*写数据*/void write_date(uchar date)rs=1;wr=0;P0=date;lcden=0;delay(5);lcden=1;delay(5);lcden=0;/*初始化液晶*/void lcd_init()uchar i,j;wela=0;dula=0;write_com(0x38);write_com(0x0c);write_com(0x06);write_com(0x01);for(i=0;i<14;i+)write_date(tablei);delay(10);write_com(0x80+0x40);for(j=0;j<16;j+)write_date(table1j);delay(10);/* 分离小数*/void fenli(uchar add,float num) uchar ge,bai,shi,ge1;num=num*1000;ge=(uint)num/1000;bai=(uint)num%1000/100;shi=(uint)num%100/10;ge1=(uint)num%10;write_com(0x80+0x40+add);write_date(0x30+ge);write_date(0x20+14);write_date(0x30+bai);write_date(0x30+shi);write_date(0x30+ge1);/* 初始化AD*/void AD_init()P1ASF=0x04;ADC_RES=0;ADC_RESL=0;ADC_CONTR|= ADC_POWER|ADC_SPEEDHH;delay(2); /* 读AD值*/uint get_AD(uchar ch)uint AD_date;ch&=0x07;ADC_CONTR=ADC_POWER|ADC_START|ADC_SPEEDHH|ch;_nop_();_nop_();_nop_();_nop_();while(!(ADC_CONTR&ADC_FLAG);ADC_CONTR&=0x82;AD_date=ADC_RES*4+(ADC_RESL&0x03);return AD_date;/* AD_val平均值*/float AD_work(uchar ch)uchar i;float AD_val;for(i=0;i<100;i+)AD_val+=get_AD(ch);AD_val/=100;AD_val=(AD_val*5)/1024;return AD_val;/* 初始化T0定时器*/void T0_init()TMOD=0x01;TH0=(65535-50000)/256;TL0=(65535-50000)%256;EA=1;ET0=1;TR0=1;/* 主函数*/void main()AD_init();T0_init();lcd_init();while(1)AD_val=AD_work(2);if(t=10)t=0;fenli(9,AD_val);/* T0定时器中断*/void timer0() interrupt 1TH0=(65535-50000)/256;TL0=(65535-50000)%256;t+;专心-专注-专业