请问一下这个程序怎么改才能显示被测出来的电压值
时间:10-02
整理:3721RD
点击:
#include <reg52.h>
#include <intrins.h>
#include<math.h>
#define uchar unsigned char //定义符号uchar为数据类型符unsigned char
#define uint unsigned int //定义符号uint为数据类型符unsigned int
#define ulong unsigned long //定义符号ulong为数据类型符unsigned long
sbit icl7135_B1=P1^0;
sbit icl7135_B2=P1^1;
sbit icl7135_B4=P1^2;
sbit icl7135_B8=P1^3;
sbit icl7135_RH=P1^4;
sbit icl7135_STRB=P3^2;
sbit icl7135_POL=P1^5;
sbit cd4051_A=P1^6;
sbit cd4051_B=P1^7;
#define DB_1602 P2
sbit rs = P0^0;
sbit rw = P0^1;
sbit ep = P0^2;
sbit beem_ov=P0^0;
sbit beem_low=P0^1;
uchar msg1[16]="Voltage: ";
uchar msg2[16]=" V";
uchar tem[7],count;
float adshuju;
void Display();
void explode(ulong);
void explode_data(float);
void Delay(uint t);
void lcd_wcmd(uchar);
void lcd_wda(uchar dat);
void lcd_init();
void DisplayMsg(uchar *);
//===============================================//
main()
{
lcd_init(); // 初始化LCD
lcd_wcmd(0x80); // 设置显示位置为第一行的第1个字符
DisplayMsg(msg1);
while(1)
{
count=0;
adshuju=0;
cd4051_B=1;
cd4051_A=0;
icl7135_RH=1;//启动7135转换
while(count!=5);
if(count==5)
{
count=0;
if(adshuju>19999)
beem_ov=0;//输入电压太大指示
else if(adshuju>=2)
{
adshuju=adshuju/0.2/10000;
Display();
}
else
{
adshuju=0;
cd4051_B=0;
cd4051_A=1;
icl7135_RH=1;
while(count!=5);
if(adshuju>2)
{
adshuju=adshuju/2/10000;
Display();
}
else
{
adshuju=0;
cd4051_B=0;
cd4051_A=0;
icl7135_RH=1;
while(count!=5);
if(adshuju>=2)
{
adshuju=adshuju/20/10000;
Display();
}
else
beem_low=0;//输入电压太小测量不出指示
}
}
}
}
}
//==============================================//
void ISR_Int0()interrupt 0
{
uchar jgbcd;
count++;
if(count==1)icl7135_RH=0;
P1=P1&0x0f;
jgbcd=P1&0x0f;
adshuju+=jgbcd*pow(10,5-count);
}
//==============================================//
void Display()
{
uchar i;
explode_data(adshuju);
if(icl7135_POL==1)
tem[0]=0x20;
else
tem[0]=0x2d;
for(i=0;i<6;i++)
msg2[2+i]=tem[i];
//lcd_wcmd(0x80);
//DisplayMsg(msg1);
lcd_wcmd(0xc0);
DisplayMsg(tem[i]);
}
//======================================================//
void explode(ulong wdata)
{
uchar i;
unsigned long xh;
xh=10000;
for(i=0;i<7;i++)
tem[i]=0;
for(i=1;i<6;i++)
{
tem[i]=wdata/xh;
wdata=wdata-xh*tem[i];
xh=xh/10;
}
for(i=1;i<6;i++)
tem[i]=tem[i]+0x30;
}
//=======================================================//
void explode_data(float gdata)
{
uchar i;
idata ulong temp;
if(gdata>=10000)
explode(temp);
else if(gdata>=1000)
{
temp=gdata*10;
explode(temp);
tem[5]='.';
}
else if(gdata>=100)
{
temp=gdata*100;
explode(temp);
for(i=4;i>3;i--)
tem[i+1]=tem[i];
tem[4]='.';
}
else if(gdata>=10)
{
temp=gdata*1000;
explode(temp);
for(i=4;i>2;i--)
tem[i+1]=tem[i];
tem[3]='.';
}
else if(gdata>=0)
{
temp=gdata*10000;
explode(temp);
for(i=4;i>1;i--)
tem[i+1]=tem[i];
tem[2]='.';
}
}
//============================================================//
void Delay(uint t)
{ // 延时子程序
uchar i;
while(t--)
{
for(i = 0; i< 250; i++)
{
_nop_();
_nop_();
_nop_();
_nop_();
}
}
}
//======================================================//
bit lcd_bz()
{ // 测试LCD忙碌状态
bit result;
rs = 0;
rw = 1;
ep = 1;
_nop_();
_nop_();
_nop_();
_nop_();
result = (bit)(P2 & 0x80);
ep = 0;
return result;
}
void lcd_wcmd(uchar cmd)
{ // 写入指令数据到LCD
while(lcd_bz());
rs = 0;
rw = 0;
ep = 0;
_nop_();
_nop_();
P2 = cmd;
_nop_();
_nop_();
_nop_();
_nop_();
ep = 1;
_nop_();
_nop_();
_nop_();
_nop_();
ep = 0;
}
//============================================================//
void lcd_wdat(uchar dat)
{ //写入字符显示数据到LCD
while(lcd_bz());
rs = 1;
rw = 0;
ep = 0;
P2 = dat;
_nop_();
_nop_();
_nop_();
_nop_();
ep = 1;
_nop_();
_nop_();
_nop_();
_nop_();
ep = 0;
}
void lcd_init()
{ //LCD初始化设定
lcd_wcmd(0x38);
Delay(1);
lcd_wcmd(0x0c);
Delay(1);
lcd_wcmd(0x06);
Delay(1);
lcd_wcmd(0x01); //清除LCD的显示内容
Delay(1);
}
void DisplayMsg(uchar *p)
{
uchar i;
for(i=0;i<16;i++)
lcd_wdat(*p++);
}
#include <intrins.h>
#include<math.h>
#define uchar unsigned char //定义符号uchar为数据类型符unsigned char
#define uint unsigned int //定义符号uint为数据类型符unsigned int
#define ulong unsigned long //定义符号ulong为数据类型符unsigned long
sbit icl7135_B1=P1^0;
sbit icl7135_B2=P1^1;
sbit icl7135_B4=P1^2;
sbit icl7135_B8=P1^3;
sbit icl7135_RH=P1^4;
sbit icl7135_STRB=P3^2;
sbit icl7135_POL=P1^5;
sbit cd4051_A=P1^6;
sbit cd4051_B=P1^7;
#define DB_1602 P2
sbit rs = P0^0;
sbit rw = P0^1;
sbit ep = P0^2;
sbit beem_ov=P0^0;
sbit beem_low=P0^1;
uchar msg1[16]="Voltage: ";
uchar msg2[16]=" V";
uchar tem[7],count;
float adshuju;
void Display();
void explode(ulong);
void explode_data(float);
void Delay(uint t);
void lcd_wcmd(uchar);
void lcd_wda(uchar dat);
void lcd_init();
void DisplayMsg(uchar *);
//===============================================//
main()
{
lcd_init(); // 初始化LCD
lcd_wcmd(0x80); // 设置显示位置为第一行的第1个字符
DisplayMsg(msg1);
while(1)
{
count=0;
adshuju=0;
cd4051_B=1;
cd4051_A=0;
icl7135_RH=1;//启动7135转换
while(count!=5);
if(count==5)
{
count=0;
if(adshuju>19999)
beem_ov=0;//输入电压太大指示
else if(adshuju>=2)
{
adshuju=adshuju/0.2/10000;
Display();
}
else
{
adshuju=0;
cd4051_B=0;
cd4051_A=1;
icl7135_RH=1;
while(count!=5);
if(adshuju>2)
{
adshuju=adshuju/2/10000;
Display();
}
else
{
adshuju=0;
cd4051_B=0;
cd4051_A=0;
icl7135_RH=1;
while(count!=5);
if(adshuju>=2)
{
adshuju=adshuju/20/10000;
Display();
}
else
beem_low=0;//输入电压太小测量不出指示
}
}
}
}
}
//==============================================//
void ISR_Int0()interrupt 0
{
uchar jgbcd;
count++;
if(count==1)icl7135_RH=0;
P1=P1&0x0f;
jgbcd=P1&0x0f;
adshuju+=jgbcd*pow(10,5-count);
}
//==============================================//
void Display()
{
uchar i;
explode_data(adshuju);
if(icl7135_POL==1)
tem[0]=0x20;
else
tem[0]=0x2d;
for(i=0;i<6;i++)
msg2[2+i]=tem[i];
//lcd_wcmd(0x80);
//DisplayMsg(msg1);
lcd_wcmd(0xc0);
DisplayMsg(tem[i]);
}
//======================================================//
void explode(ulong wdata)
{
uchar i;
unsigned long xh;
xh=10000;
for(i=0;i<7;i++)
tem[i]=0;
for(i=1;i<6;i++)
{
tem[i]=wdata/xh;
wdata=wdata-xh*tem[i];
xh=xh/10;
}
for(i=1;i<6;i++)
tem[i]=tem[i]+0x30;
}
//=======================================================//
void explode_data(float gdata)
{
uchar i;
idata ulong temp;
if(gdata>=10000)
explode(temp);
else if(gdata>=1000)
{
temp=gdata*10;
explode(temp);
tem[5]='.';
}
else if(gdata>=100)
{
temp=gdata*100;
explode(temp);
for(i=4;i>3;i--)
tem[i+1]=tem[i];
tem[4]='.';
}
else if(gdata>=10)
{
temp=gdata*1000;
explode(temp);
for(i=4;i>2;i--)
tem[i+1]=tem[i];
tem[3]='.';
}
else if(gdata>=0)
{
temp=gdata*10000;
explode(temp);
for(i=4;i>1;i--)
tem[i+1]=tem[i];
tem[2]='.';
}
}
//============================================================//
void Delay(uint t)
{ // 延时子程序
uchar i;
while(t--)
{
for(i = 0; i< 250; i++)
{
_nop_();
_nop_();
_nop_();
_nop_();
}
}
}
//======================================================//
bit lcd_bz()
{ // 测试LCD忙碌状态
bit result;
rs = 0;
rw = 1;
ep = 1;
_nop_();
_nop_();
_nop_();
_nop_();
result = (bit)(P2 & 0x80);
ep = 0;
return result;
}
void lcd_wcmd(uchar cmd)
{ // 写入指令数据到LCD
while(lcd_bz());
rs = 0;
rw = 0;
ep = 0;
_nop_();
_nop_();
P2 = cmd;
_nop_();
_nop_();
_nop_();
_nop_();
ep = 1;
_nop_();
_nop_();
_nop_();
_nop_();
ep = 0;
}
//============================================================//
void lcd_wdat(uchar dat)
{ //写入字符显示数据到LCD
while(lcd_bz());
rs = 1;
rw = 0;
ep = 0;
P2 = dat;
_nop_();
_nop_();
_nop_();
_nop_();
ep = 1;
_nop_();
_nop_();
_nop_();
_nop_();
ep = 0;
}
void lcd_init()
{ //LCD初始化设定
lcd_wcmd(0x38);
Delay(1);
lcd_wcmd(0x0c);
Delay(1);
lcd_wcmd(0x06);
Delay(1);
lcd_wcmd(0x01); //清除LCD的显示内容
Delay(1);
}
void DisplayMsg(uchar *p)
{
uchar i;
for(i=0;i<16;i++)
lcd_wdat(*p++);
}
奖励2积分