如有朋友做AD方面的东西,相互交流
完整的源码下载:http://www.51hei.com/f/jiaoliu.rar
以下是部分代码:
#include#include #include #define nop() _nop_() #define N0 65336 #define uint unsigned int #define uchar unsigned char #define ulong unsigned long uchar code xian[]={"Welcome"}; uchar code xian1[]={"Designed by WJJ"}; uchar tab[7]; uchar tab2[7]; uchar tab3[5]; uchar tab4[8]={" "}; sbit EOC=P2^6; sbit OE=P2^5; sbit START=P2^7; sbit lcdrs=P3^5; sbit lcdrw=P3^6; sbit lcden=P3^7; uint vmax=0; // 电压测量运算变量 uint count1; //电压测量控制变量 uint count; //频率测量控制变量 ulong mt=0; // 频率测量运算变量 ulong vs=0; ulong t; bit flag=0; //是否显示标志位 对频率 void DelayNS(uint dly) { uint i; for(; dly>0; dly--) for(i=0; i<100; i++); } bit lcd_busy() { bit result; lcdrw = 1; lcdrs = 0; lcden = 1; nop();nop();nop();nop(); result = (bit)(P1&0x80); lcden = 0; return(result); } void write_com(uchar com) { while(lcd_busy()); //LCD忙等待 lcdrs = 0; lcdrw = 0; P1 = com; DelayNS(5); lcden = 1; DelayNS(5); lcden = 0; } void write_data(uchar date) { while(lcd_busy()); //LCD忙等待 lcdrs = 1; lcdrw = 0; P1 = date; DelayNS(5); lcden = 1; DelayNS(5); lcden = 0; } void lcd_init() { lcden = 0; write_com(0x38); DelayNS(5); write_com(0x0c); DelayNS(5); write_com(0x04); DelayNS(5); write_com(0x01); } void timer_init() { EA = 1; EX0=1; IT0=1; TMOD = 0x11; TH0=0; TL0=0; }[page] void main() { uint i; ulong v=0; ulong av; uint hz; bit flag=0; ulong t1=0,t2=0; lcd_init(); write_com(0x84); for(i=0;i<7;i++) { write_data(xian[i]); DelayNS(2); } write_com(0x80+0x40); for(i=0;i<15;i++) { write_data(xian1[i]); DelayNS(2); } DelayNS(1000); DelayNS(1000); timer_init(); write_com(0x01); while(1) { if(count==40) { count=0; EX0=0; ET0=0; TR0=0; t1=100000000/mt; tab[0]=t1/1000+0x30; tab[1]=t1%1000/100+0x30; tab[2]=0x2e; tab[3]=t1%1000%100/10+0x30; tab[4]=t1%1000%100%10+0x30; //周期显示十位 tab[5]=0x48; tab[6]=0x7a; hz=t1/100; t=1000000/(50*hz)+100; TH1=65536-t/256; TL1=65535-t%256; t2=mt/10; tab2[0]=t2/1000+0x30; tab2[1]=t2%1000/100+0x30; tab2[2]=0x2e; tab2[3]=t2%1000%100/10+0x30; tab2[4]=t2%1000%100%10+0x30; tab2[5]=0x6d; tab2[6]=0x73; ET1=1; TR1=1; } if(count1==80) { ET1 = 0; TR1 = 0; count1=0; v=(vmax+1)*196/100; tab3[4]=0x56; tab3[3]=(v%100)%10+0x30; tab3[2]=(v%100)/10+0x30; tab3[1]=0x2e; tab3[0]=v/100+0x30; vs=vs*0.0195*0.0195; av=(sqrt(vs/50))*100; tab4[4]=0x56; tab4[3]=(av%100)%10+0x30; tab4[2]=(av%100)/10+0x30; tab4[1]=0x2e; tab4[0]=av/100+0x30; if((v/sqrt(2)-av)>25) { tab4[7]=0x32; } else { tab4[7]=0x31; } av=0; flag=1; } if(flag==1) { flag=0; write_com(0x80); for(i=0;i<7;i++) { write_data(tab[i]); DelayNS(2); } write_com(0x80+0x09); for(i=0;i<7;i++) { write_data(tab2[i]); DelayNS(2); } write_com(0x80+0x40); for(i=0;i<5;i++) { write_data(tab3[i]); DelayNS(20); } write_com(0x80+0x47); for(i=0;i<8;i++) { write_data(tab4[i]); DelayNS(20); } //write_com(0x80+0x54); //write_data(tab4[7]); //DelayNS(20); vmax=0; EX0=1; } } } void ex0() interrupt 0 { count++; if(count%2==0) { ET0=1; TR0=1; TH0=0; TL0=0; } else { ET0=0; TR0=0; mt=TH0*256+TL0; } } void timer0() interrupt 1 //测频率时计时 { TH0=0; TL0=0; } void timer1() interrupt 3 //控制AD转换 { uint data1; count1++; OE = 0; START = 0; _nop_(); START = 1; _nop_(); START = 0; while(EOC==0); OE = 1; _nop_(); data1 = P0; _nop_(); OE = 0; if(data1>vmax) { vmax=data1; } if(count1<=52) { vs=vs+data1*data1; } TH1=65536-t/256; TL1=65535-t%256; }
上一篇:自己做的门禁系统3
下一篇:基于单片机设计的超声波倒车防撞系统
推荐阅读最新更新时间:2024-03-16 14:26