微波EDA网,见证研发工程师的成长!
首页 > 研发问答 > 嵌入式设计讨论 > MCU和单片机设计讨论 > PCF8591 AD转换问题,求助大神

PCF8591 AD转换问题,求助大神

时间:10-02 整理:3721RD 点击:
程序如下
#include<reg52.h>
#include<intrins.h>
#define uint unsigned int
#define uchar unsigned char
sbit scl=P2^0;
sbit sda=P2^1;
sbit rs=P0^0;
sbit rw=P0^1;
sbit en=P0^2;
sbit bf=P1^7;
sbit dula=P2^6;
sbit wela=P2^7;
uchar Ch2value,Ch3value;
void delay(uchar z)
{
uchar x,y;
for(x=z;x>0;x--)
  for(y=110;y>0;y++);
}
void usdelay()
{
_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();
}
void start()
{
sda=1;
usdelay();
scl=1;
usdelay();
sda=0;
usdelay();
// scl=0;
}
void stop()
{
sda=0;
usdelay();
scl=1;
usdelay();
sda=1;
usdelay();
}
void ack()
{
/* uchar i;
scl=1;
usdelay();
while((sda==1)&&(i<200))
  i++;
scl=0;
usdelay(); */
sda=0;
_nop_();
scl=0;
_nop_();
scl=1;               //应答信号
_nop_();
scl=0;
_nop_();
}
void noack()
{
sda=1;
usdelay();
scl=0;
_nop_();
scl=1;               //应答信号
_nop_();
scl=0;
_nop_();
}
void send_byte(uchar dat)
{
uchar i,temp;
temp=dat;
scl = 0;
usdelay();
sda = 1;
usdelay();
for(i=0;i<8;i++)
{
/* scl=0;
  usdelay();
  sda=(bit)(dat&0x80);
  usdelay();
  scl=1;
  usdelay();
  dat<<=1; */
  temp = temp << 1;
  scl = 0;
  usdelay();
  sda = CY;
  usdelay();
  scl = 1;
  usdelay();
}
scl=0;
usdelay();
sda=1;
usdelay();
}
uchar read_byte()
{
uchar i,k;
scl=0;
usdelay();
sda=1;
usdelay();
for(i=0;i<8;i++)
{
  scl=1;
  usdelay();
  k=(k<<1)|sda;
  scl=0;
  usdelay();
}
sda=1;
scl=1;
return k;
}
  
uchar read_adc(uchar channel)
{
uchar temp;
start();
send_byte(0x90);
ack();
send_byte(0x00+channel);
ack();
start();
send_byte(0x91);
ack();
temp=read_byte();
noack();
stop();
return temp;
}
void init()
{
sda=1;
_nop_();
scl=1;
_nop_();
}
/******************************************/
void wait()
{
P1=0xff;
do
{
  rs=0;
  rw=1;
  en=0;
  en=1;
}while(bf==1);
en=0;
}
void write_dat(uchar dat)
{
wait();
en=0;
P1=dat;
rs=1;
rw=0;
en=1;
en=0;
}
void write_com(uchar com)
{
wait();
en=0;
P1=com;
rs=0;
rw=0;
en=1;
en=0;
}
void zfc(uchar *p)
{
while(*p!=0)
  write_dat(*p++);
}
void init_1602()
{
wela=0;
dula=0;
write_com(0x38);
delay(2);
write_com(0x0c);
delay(2);
write_com(0x06);
delay(2);
write_com(0x01);
write_com(0x80);
zfc("Channel 2 :");
write_com(0xc0);
zfc("Channel 3 :");
}
  
void display()
{
uchar a0,a1,a2,b0,b1,b2;
a0=Ch2value*196/10000;
a1=Ch2value*196%10000/1000;
a2=Ch2value*196%1000/100;
b0=Ch3value*196/10000;
b1=Ch3value*196%10000/1000;
b2=Ch3value*196%1000/100;
write_com(0x80+11);
write_dat(0x30+a0);
write_dat('.');
write_dat(0x30+a1);
write_dat(0x30+a2);
write_dat('V');
write_com(0xc0+11);
write_dat(0x30+b0);
write_dat('.');
write_dat(0x30+b1);
write_dat(0x30+b2);
write_dat('V');
}
void main()
{
init_1602();
init();
while(1)
{
  Ch2value=read_adc(2);
  delay(5);
  Ch3value=read_adc(3);
  delay(5);
  display();
}
}

这个程序在我学习板上只显示4.99V,求助大神是怎么回事。万分感谢大神的帮助!

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top