AD conversion module PCF8591 based on IIC bus

Publisher:星辰小鹿Latest update time:2015-04-27 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
/*This program is debugged using the Puzhong development board*/

#include
#include
#define uint unsigned int
#define uchar unsigned char

#define addw 0x90
#define addr 0x91

sbit scl=P2^0;
sbit sda=P2^1;

sbit rs=P2^6;
sbit rw=P2^5;
sbit en=P2^7;

float Ch0value,Ch1value;

void delayms(int i) 
{ int x,y;
for (x=i;x>0;x--)
 for(y=110;y>0;y--); 
}

void usdelay() 
{
_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_(); 
}


void start() //Start signal
{//When the clock line is at a high level, the data line changes from high to low, which is the start signal
sda=1;
usdelay();
scl=1;
usdelay();
sda=0; //The data line changes from high to low, which is the start signal
usdelay();
scl=0;
}

void stop() //End signal
{//When the clock line is at a high level, the data line changes from low to high, which is the end signal
sda=0;
usdelay();
scl=1;
usdelay();
sda=1;
usdelay();
sda=0; //This sentence is optional
scl=0; //This sentence is optional
}

void ack()//应答信号  
{ uchar i;
scl=1;
usdelay(); 
    while((sda==1)&&(i<200))i++;
scl=0;
    usdelay();
}

void Noack() //No response
{
sda=1; //When the clock line is high, the data line is high, which means no response
usdelay();
    scl=1;
usdelay();
scl=0;
    usdelay(); 
}


void send_byte(uchar dat)
{
uchar i;
for(i=0;i<8;i++)
  {   
   scl=0;
   usdelay();
   sda=(bit)(dat&0x80); //Data transmission is from high to low
   //(bit) is the forced conversion command, which changes the data into a one-bit database of either 0 or 1
   usdelay();
   scl=1;
   usdelay();
   dat<<=1;
   }
  scl=0;
  usdelay();
  sda=1;//Release the bus
  usdelay();
}

fly read_byte()
{

uchar i,k;
scl=0; 
usdelay();
sda=1; //Release bus
usdelay();
for(i=0;i<8;i++)
  {
   scl=1;
   usdelay();
   k=(k<<1)|sda;//Judge 8 times, shift 7 times
   scl=0;
   usdelay();   
  }
return k;
}
[page]

uchar read_adc(uchar channel)
{
  uchar temp;
  start();
  send_byte(addw);
  ack();
  send_byte(0x40|channel);
  ack();
  start();
  send_byte(addr);
  ack();
  temp=read_byte();
  Noack();
  stop();
  return temp;

}

void init()
{
  scl=1;
  sda=1;
}
 

/*********1602Program************************/

  void write_com(suchar com)
 {
  rs=0;
  rw=0;
  en=0;
  P0=com;
  delayms(2);
  en=1;
  delayms(2);
  en=0;
  }


void write_dat(uchar dat)
 {
  rs=1;
  rw=0;
  en=0;
  P0=dat;
  delayms(2);
  en=1;
  delayms(2);
  en=0;
  }

void zfc(uchar *p)
{
 while(*p!=0)
 write_dat(*p++);
}

void init_1602()
{
write_com(0x38);
  delayms(2);
write_com(0x0c);
 delayms(2);
write_com(0x06);
 delayms(2);
write_com(0x01);
write_com(0x80);
zfc("Channel 0 :");
write_com(0x80+0x40);
zfc("Channel 1 :");
}

void display()
{
uchar a0,a1,a2, b0,b1,b2;
a0=Ch0value*196/10000;
a1=Ch0value*196%10000/1000;
a2=Ch0value*196%1000/100;

b0=Ch1value*196/10000;
b1=Ch1value*196%10000/1000;
b2=Ch1value*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(0x80+0x40+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)
{
 Ch0value=read_adc(0);
delayms(50);
Ch1value=read_adc(1);
delayms(50);
 display(); 
}
}

Reference address:AD conversion module PCF8591 based on IIC bus

Previous article:DS1302 uses 1602 display and keypad to adjust time
Next article:Clock written with 32768Hz crystal oscillator of MSP430

Popular Resources
Popular amplifiers
Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号