基于ACS712 51 ADC0832 电流表程序有点问题,不能正确显示电流求帮助

2019-07-15 16:56发布

#include <reg51.h>
#include <intrins.h>
#define uint unsigned int
#define uchar unsigned char
#define delay4us() {_nop_();_nop_();_nop_();_nop_();}
  
sbit RS = P2^0;
sbit RW = P2^1;
sbit E  = P2^2;
sbit CS  = P1^0;
sbit CLK = P1^1;
sbit DIO = P1^2;
uchar Display_Buffer[] = "0.00A";
uchar code Line1[] = "  Current:DC";
void DelayMS(uint ms)
{
  uchar i;
while(ms--)
{
   for(i=0;i<120;i++);
}
}
bit LCD_Busy_Check()
{
  bit result;
RS = 0;
RW = 1;
E  = 1;
delay4us();
result = (bit)(P0&0x80);
E  = 0;
return result;
}
void LCD_Write_Command(uchar cmd)
{
  while(LCD_Busy_Check());
RS = 0;
RW = 0;
E  = 0;
_nop_();
_nop_();
P0 = cmd;
delay4us();
E = 1;
delay4us();
E = 0;
}
void Set_Disp_Pos(uchar pos)
{
  LCD_Write_Command(pos | 0x80);
}
void LCD_Write_Data(uchar dat)
{
  while(LCD_Busy_Check());
RS = 1;
RW = 0;
E  = 0;
P0 = dat;
delay4us();
E = 1;
delay4us();
E = 0;
}
void LCD_Initialise()
{
LCD_Write_Command(0x38); DelayMS(1);
LCD_Write_Command(0x0c); DelayMS(1);
LCD_Write_Command(0x06); DelayMS(1);
LCD_Write_Command(0x01); DelayMS(1);
}
uchar Get_AD_Result()
{
  uchar i,dat1=0,dat2=0;
CS  = 0;
CLK = 0;
DIO = 1; _nop_(); _nop_();
CLK = 1; _nop_(); _nop_();
CLK = 0;DIO = 1; _nop_(); _nop_();
CLK = 1; _nop_(); _nop_();
CLK = 0;DIO = 0; _nop_(); _nop_();
CLK = 1;DIO = 1; _nop_(); _nop_();
CLK = 0;DIO = 1; _nop_(); _nop_();
for(i=0;i<8;i++)
{
   CLK = 1; _nop_(); _nop_();
  CLK = 0; _nop_(); _nop_();
  dat1 = dat1 << 1 | DIO;
}
for(i=0;i<8;i++)
{
   dat2 = dat2 | ((uchar)(DIO)<<i);
  CLK = 1; _nop_(); _nop_();
  CLK = 0; _nop_(); _nop_();
}
CS = 1;
return (dat1 == dat2) ? dat1:0;
}
void main()
{
  uchar i;
uint d;
  //uint d;
LCD_Initialise();
DelayMS(10);
while(1)
{
  //d= Get_AD_Result()*500.0/255;
  //d=(d-128)*30/128;
  d=Get_AD_Result();
  d=(d-2.5)/0.067;
  d=d*500/255;
  
  Display_Buffer[0]=d/100+0x30;
  Display_Buffer[2]=d/10%10+0x30;
   Display_Buffer[3]=d%10+0x30;
  Set_Disp_Pos(0x01);
  i = 0;
  while(Line1!='')
  {
    LCD_Write_Data(Line1[i++]);
  }
  Set_Disp_Pos(0x46);
  i = 0;
  while(Display_Buffer!='')
  {
    LCD_Write_Data(Display_Buffer[i++]);
  }
}
}

QQ截图20150602081816.png
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。