交流数字电压表设计(共7页).doc
精选优质文档-倾情为你奉上交流数字电压表设计实例专业(年级、班) 设 计 人 指 导 教 师 辅 导 教 师 2009 年 01 月 01 日(设计结束日)交流数字电压表 学号: 姓名: 班级: 一、 实验目的1、 了解硬件平台的各个模块及其作用;2、 能运用硬件平台上的各个模块完成各项设计;3、 能熟练运用keil软件进行编程设计。二、 实验内容在所给硬件平台的基础上,并利用keil软件完成一个交流数字电压表。主要由检测、显示、档位和A/D、单片机灯组成要求:用4位数码管实时显示被测量交流电压的峰值,单位为V 测量范围:01999V测量误差不超过15用4各按键选择档位三、 硬件设计电路图为:ACDC转换电路AD转换数码管显示检测电路按键电路:四、软件设计#include <reg52.h>#include <intrins.h>#include <string.h>#define uchar unsigned char #define uint unsigned int #define Busy 0x80 Busy #define LCM_Data P0 #define LCM_RS P2_7#define LCM_RW P2_6#define LCM_E P2_5#define ALE P2_4#define START P3_2#define OE P2_3#define EOC P3_3#define A P2_0#define B P2_1#define C P2_2sbit P3_6=P36;sbit P3_5=P35;sbit P3_4=P34;sbit P3_2=P32;sbit P3_3=P33;sbit P2_5=P25;sbit P2_6=P26;sbit P2_4=P24;sbit P2_0=P20;sbit P2_1=P21;sbit P2_2=P22;sbit P2_3=P23;sbit P2_7=P27;uchar data ad;char ch=0;float volt; uchar data NDIG; uchar X,Y;bit key1=0,key2=0; uchar code W = " good luck "uchar code hhuc="this is micheal"uchar code tongdao="tongdao : "uchar code danwei=" v "void delayms(uint n);void WriteDataLCM(uchar WDLCM);void WriteCommandLCM(uchar WCLCM,uBuysC);void CheckBusy_LCM(void);void LCMInit(void);void DisplayOneChar(uchar X, uchar Y, uchar DData);void DisplayListChar(uchar X,uchar Y, uchar *DData);void disp(uchar xx, uchar yy,float canshu);void nextp(void);void WriteDataLCM(uchar WDLCM) CheckBusy_LCM(); LCM_RS = 1; LCM_RW = 0; LCM_Data = WDLCM; LCM_E = 1; _nop_(); _nop_(); _nop_(); LCM_E = 0;void WriteCommandLCM(uchar WCLCM,bit BuysC) if (BuysC) CheckBusy_LCM(); LCM_RS = 0; LCM_RW = 0; LCM_Data = WCLCM; LCM_E = 1; LCM_E = 1; _nop_(); _nop_(); _nop_(); LCM_E = 0; void CheckBusy_LCM(void) LCM_Data = 0x0FF; LCM_E = 1; LCM_RS = 0; LCM_RW = 1; _nop_(); _nop_(); _nop_(); while (LCM_Data & Busy); LCM_E = 0;void LCMInit(void) WriteCommandLCM(0x38,0); delayms(5); WriteCommandLCM(0x38,0); delayms(5); WriteCommandLCM(0x38,0); delayms(5); WriteCommandLCM(0x38,1); delayms(5); WriteCommandLCM(0x08,1); delayms(5); WriteCommandLCM(0x01,1); delayms(5); WriteCommandLCM(0x06,1); delayms(5); WriteCommandLCM(0x0C,1); delayms(5); void DisplayOneChar(uchar X, uchar Y, uchar DData) Y &= 0x01; X &= 0x0F; if (Y) X |= 0x40; X |= 0x80; WriteCommandLCM(X, 1); WriteDataLCM(DData);void DisplayListChar(uchar X, uchar Y, uchar *DData) uchar ListLength; ListLength = 0; Y &= 0x01; X &= 0x0F; while (X<NDIG) DisplayOneChar(X, Y, DDataListLength); ListLength+; X+; void delayms(uint n) for(n=125*n;n;n-);void read_ad(uchar port) switch(port) case 0:A=0;B=0;C=0;break; case 1:A=1;B=0;C=0;break; case 2:A=0;B=1;C=0;break; case 3:A=1;B=1;C=0;break; case 4:A=0;B=0;C=1;break; case 5:A=1;B=0;C=1;break; case 6:A=0;B=1;C=1;break; case 7:A=1;B=1;C=1;break; default:break; OE=0; ALE=0; START=0; ALE=1; START=1; ALE=0; START=0; while(!EOC); OE=1; P0=0x0ff; ad=0xff&P1; OE=0; volt=(ad/255.0)*5;void nextp(void) X+=1;if(X=16) X=0; Y+=1; if(Y=2) Y=0;void disp(uchar xx, uchar yy,float canshu) uint b1,b2; float d; uint bb; bit flag=0; X=xx;Y=yy; b2=(uint)canshu; b1=b2; bb=b1+'0' DisplayOneChar(X,Y,bb);nextp(); DisplayOneChar(X,Y,'.');nextp(); d=canshu-b2; bb=(uint)(d*10)+'0' DisplayOneChar(X,Y,bb);nextp(); bb=(uint)(d*100)+'0' DisplayOneChar(X,Y,bb);nextp();void key_scan(void) uint i;if(P3_5=0)for(i=5000;i>0;i-);if(P3_5=0)key1=1;while(!P3_5);if(P3_6=0)for(i=5000;i>0;i-);if(P3_6=0)key2=1;while(!P3_6);void key_Process(void)if(key1)ch+;key1=0;if(key2)ch-;key2=0;if(ch>7)ch=0;if(ch<0)ch=7;DisplayOneChar(7,0,'0'+ch);void main(void) uchar j; LCMInit(); NDIG=strlen(W); DisplayListChar(0, 0, W); NDIG=strlen(hhuc); DisplayListChar(0, 1, hhuc); for(j=30;j>0;j-) delayms(20); WriteCommandLCM(0x01,1); NDIG=strlen(tongdao); DisplayListChar(0, 0, tongdao); NDIG=strlen(danwei); DisplayListChar(0, 1, danwei); while(1) key_scan(); key_Process(); read_ad(ch); disp(10, 1,volt); 专心-专注-专业