基于51单片机设计的数字电压表,可采集4路电压
单片机源程序如下:
- #include "reg52.h"
- #include "intrins.h"
- #include "stdio.h"
- #define uchar unsigned char
- #define RS_CLR RS=0
- #define RS_SET RS=1
- #define RW_CLR RW=0
- #define RW_SET RW=1
- #define EN_CLR EN=0
- #define EN_SET EN=1
- sbit ST=P3^4;
- sbit EOC=P3^5;
- sbit OE=P3^6;
- sbit ALE=P3^7;
- sbit ADDC=P2^1;
- sbit ADDB=P2^2;
- sbit ADDA=P2^3;
- sbit RS = P2^4;
- sbit RW = P2^5;
- sbit EN = P2^6;
- sbit KEY1=P2^0;
- sbit KEY2=P2^7;
- sbit KEY4=P3^2;
- uchar num=0;
- uchar temp[20];
- float Voltage;
- void DelayUs2x(unsigned char t)
- {
- while(--t);
- }
- void DelayMs(unsigned char t)
- {
-
- while(t--)
- {
- DelayUs2x(245);
- DelayUs2x(245);
- }
- }
- void delay(uchar i)
- {
- uchar j;
- while(i--)
- {
- for(j=125;j>0;j--);
- }
- }
- bit LCD_Check_Busy(void)
- {
- P0=0xFF;
- RS_CLR;
- RW_SET;
- EN_CLR;
- _nop_();
- EN_SET;
- return (bit)(P0 & 0x80);
- }
- void LCD_Write_Com(unsigned char com)
- {
- DelayMs(5);
- RS_CLR;
- RW_CLR;
- EN_SET;
- P0=com;
- _nop_();
- EN_CLR;
- }
- void LCD_Write_Data(unsigned char Data)
- {
- //while(LCD_Check_Busy()); //忙则等待
- DelayMs(5);
- RS_SET;
- RW_CLR;
- EN_SET;
- P0=Data;
- _nop_();
- EN_CLR;
- }
- void LCD_Clear(void)
- {
- LCD_Write_Com(0x01);
- DelayMs(5);
- }
- void LCD_Write_String(unsigned char x,unsigned char y,unsigned char *s)
- {
- if (y == 0)
- {
- LCD_Write_Com(0x80 + x); //表示第一行
- }
- else
- {
- LCD_Write_Com(0xC0 + x); //表示第二行
- }
- while (*s)
- {
- LCD_Write_Data( *s);
- s ++;
- }
- }
-
- void LCD_Write_Char(unsigned char x,unsigned char y,unsigned char Data)
- {
- if (y == 0)
- {
- LCD_Write_Com(0x80 + x);
- }
- else
- {
- LCD_Write_Com(0xC0 + x);
- }
- LCD_Write_Data( Data);
- }
- void LCD_Init(void)
- {
- LCD_Write_Com(0x38); /*显示模式设置*/
- DelayMs(5);
- LCD_Write_Com(0x38);
- DelayMs(5);
- LCD_Write_Com(0x38);
- DelayMs(5);
- LCD_Write_Com(0x38);
- LCD_Write_Com(0x0C); /*显示开及光标设置*/
- LCD_Write_Com(0x06); /*显示光标移动设置*/
- LCD_Write_Com(0x01); /*显示清屏*/
- }
- unsigned char Read_AD()
- {
- uchar temp=0;
- while(EOC==0);
- OE=1;
- temp=P1;
- DelayMs(1);
- OE=0;
- return temp;
- }
- void UartInit(void) //2400bps@12MHz
- {
- PCON &= 0x7f;
- SCON = 0x50;
- AUXR &= 0xbf;
- AUXR &= 0xfe;
- TMOD &= 0x0f;
- TMOD |= 0x20;
- TL1 = 0xF3;
- TH1 = 0xF3;
- ET1 = 0;
- TR1 = 1;
- }
- void SendByte(unsigned char dat)
- {
- SBUF = dat;
- while(!TI);
- TI = 0;
- }
- void SendStr(unsigned char *s)
- {
- while(*s!='\0')// \0 表示字符串结束标志,
- //通过检测是否字符串末尾
- {
- SendByte(*s);
- DelayUs2x(8);
- s++;
- }
- }
- //unsigned char keyscan()
- //{
- //
- // if(KEY1==0)
- // {
- // ALE=1;
- // ADDA=0;
- // ADDB=0;
- // ADDC=0;
- // ALE=0;
- // num=Read_AD();
- // Voltage=(float)num*5.0/256;
- // sprintf(temp,"V1:%3.2f",Voltage);
- // LCD_Write_String(4,0,temp);
- // }
- // else if(KEY2==0)
- // {
- // ALE=1;
- // ADDA=1;
- // ADDB=0;
- // ADDC=0;
- // ALE=0;
- // num=Read_AD();
- // Voltage=(float)num*5.0/256;
- // sprintf(temp,"V2:%3.2f",Voltage);
- // LCD_Write_String(4,0,temp);
- // }
- // else if(KEY3==0)
- // {
- // ALE=1;
- // ADDA=0;
- // ADDB=1;
- // ADDC=0;
- // ALE=0;
- // num=Read_AD();
- // Voltage=(float)num*5.0/256;
- // sprintf(temp,"V3:%3.2f",Voltage);
- // LCD_Write_String(4,0,temp);
- // }
- //}
- unsigned char keyscan()
- {
- if(KEY1==0)
- {
- while(1)
- {
- ST=1;
- ST=0;
- ALE=1;
- ADDA=0;
- ADDB=0;
- ADDC=0;
- ALE=0;
-
- num=Read_AD();
- Voltage=(float)num*10.0/256;
- sprintf(temp,"V1:%4.2f\n",Voltage);
- LCD_Write_String(0,0,temp);
- ST=1;
- ST=0;
- ALE=1;
- ADDA=0;
- ADDB=0;
- ADDC=0;
- ALE=0;
-
- num=Read_AD();
- Voltage=(float)num*10.0/256;
- sprintf(temp,"V1:%4.2f\n",Voltage);
- LCD_Write_String(0,0,temp);
- SendStr(temp);
- DelayMs(2000);
- DelayMs(2000);
- DelayMs(2000);
- DelayMs(2000);
- ST=1;
- ST=0;
- ALE=1;
- ADDA=1;
- ADDB=0;
- ADDC=0;
- ALE=0;
- num=Read_AD();
- Voltage=(float)num*10.0/256;
- sprintf(temp,"V2:%4.2f\n",Voltage);
- LCD_Write_String(9,0,temp);
- ST=1;
- ST=0;
- ALE=1;
- ADDA=1;
- ADDB=0;
- ADDC=0;
- ALE=0;
- num=Read_AD();
- Voltage=(float)num*10.0/256;
- sprintf(temp,"V2:%4.2f\n",Voltage);
- LCD_Write_String(9,0,temp);
- SendStr(temp);
- DelayMs(2000);
- DelayMs(2000);
- DelayMs(2000);
- DelayMs(2000);
-
- ST=1;
- ST=0;
- ALE=1;
- ADDA=0;
- ADDB=1;
- ADDC=0;
- ALE=0;
- num=Read_AD();
- Voltage=(float)num*10.0/256;
- sprintf(temp,"V3:%4.2f\n",Voltage);
- LCD_Write_String(0,1,temp);
- ST=1;
- ST=0;
- ALE=1;
- ADDA=0;
- ADDB=1;
- ADDC=0;
- ALE=0;
- num=Read_AD();
- Voltage=(float)num*10.0/256;
- sprintf(temp,"V3:%4.2f\n",Voltage);
- LCD_Write_String(0,1,temp);
- SendStr(temp);
- DelayMs(2000);
- DelayMs(2000);
- DelayMs(2000);
- DelayMs(2000);
-
- ST=1;
- ST=0;
- ALE=1;
- ADDA=1;
- ADDB=1;
- ADDC=0;
- ALE=0;
- num=Read_AD();
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
数字电压表.rar
(31.49 KB, 下载次数: 17)
|