本文包含原理图、PCB、源代码、封装库、中英文PDF等资源
您需要 登录 才可以下载或查看,没有账号?注册会员
×
#include<reg52.h>
#include<intrins.h>
#include<iic.h>
sbit pules=P1^1;
sbit test=P1^0;
vout=0;
unsigned char x,vo;
void delay(unsigned char x)
{
unsigned char i,j;
for(i=0;i<x;i++)
{
for(j=0;j<255;j++)
{
};
};
}
void pcf8591_sendvout(unsigned char x)
{
iic_start();
iic_sendbyte(0x90);
iic_testack();
test=iic_sda;
iic_sendbyte(0x40);
iic_testack();
test=iic_sda;
// iic_sendseven(x);
iic_sendbyte(x);
iic_testack();
test=iic_sda;
// iic_sendbyte(0xff);
iic_testack();
iic_stop();
}
unsigned char pcf8591_recievevdata()
{
unsigned char x;
iic_start();
iic_sendbyte(0x90);
iic_testack();
iic_sendbyte(0x00);
iic_testack();
iic_start();
iic_sendbyte(0x91);
iic_testack();
x=iic_recievebyte();
iic_noack();
iic_stop();
return x;
}
void UartInit(void) //9600bps@11.0592MHz
{
PCON &= 0x7f; //波特率不倍速
SCON = 0x50; //8位数据,可变波特率
TMOD=0x20;
TL1 = 0xfd; //设定定时初值
TH1 = 0xfd; //设定定时器重装值
ET1 = 0; //禁止定时器1中断
TR1 = 1; //启动定时器1
}
void ints() interrupt 4
{
if(RI)
{
RI=0;
if(SBUF==0x30) x=1;
if(SBUF==0x60) x=0;
}
if(TI)
{
TI=0;
}
}
void send()
{
SBUF=Cnt;
while(!TI);
TI=0;
SBUF=vout;
while(!TI);
TI=0;
}
main()
{
UartInit();
ES=1;
EA=1;
while(1)
{
while(x==0)
{
delay(255);
pules=~pules;
pcf8591_sendvout(vo++);
//vout=pcf8591_recievevdata();
delay(255);
delay(255);
delay(255);
delay(255);
delay(255);
delay(255);
send();
};
}
}
#ifndef __iic_h__
#define __iic_h__
sbit iic_scl=P1^5; //棕色线
sbit iic_sda=P1^7; //黄色的线
#define NOP; {_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();}
unsigned char Cnt=0;
void iic_start() //启动总线
{
iic_sda=1;
NOP;
iic_scl=1;
NOP;
iic_sda=0;
NOP;
iic_scl=0;
NOP;
}
void iic_stop()
{
iic_sda=0;
NOP;
iic_scl=1;
NOP;
iic_sda=1;
NOP;
}
void iic_sendonebit(bit x)
{
iic_scl=0;
NOP;
iic_sda=x;
NOP;
iic_scl=1;
NOP;
iic_scl=0;
}
bit iic_recieveonebit()
{
bit x;
iic_sda=1;
NOP;
iic_scl=0;
NOP;
x=!iic_sda;
NOP;
iic_scl=1;
NOP;
iic_scl=0;
return x;
}
bit iic_testack()
{
bit x;
x=iic_recieveonebit();
if(x)
{
Cnt++;
}
return x;
}
void iic_ack()
{
iic_sendonebit(0);
}
void iic_noack()
{
iic_sendonebit(1);
}
void iic_sendbyte(unsigned char x)
{
unsigned char i,j;
j=x;
for(i=0;i<8;i++)
{
iic_sendonebit((bit)(j&0x80));
j<<=1;
}
}
void iic_sendseven(unsigned char x)
{
unsigned char i,j;
j=x;
for(i=0;i<8;i++)
{
iic_sendonebit((bit)(j&0x80));
j<<=1;
}
iic_sda=1;
}
unsigned char iic_recievebyte()
{
unsigned char i,temp=0;
for(i=0;i<8;i++)
{
temp|=iic_recieveonebit();
temp<<=1;
}
return temp;
}
#endif |