废话不多说放程序吧
PCF的C程序
#include"PCF8974.h"
#include"intrins.h"
#define Write 0x40
#define Read 0x41
unsigned char dat[]={0};
void IIC_Start(void)
{
CLK = 1;
SDA = 1;
CLK = 0;
SDA = 0;
}
void IIC_Stop(void)
{
CLK = 0;
SDA = 0;
CLK = 1;
SDA = 1;
}
void IIC_Ack(void)
{
CLK = 0;
while(SDA);
CLK = 1;
}
void IIC_Write(unsigned char addr)
{
unsigned char i;
for(i=0;i<8;i++)
{
CLK = 0;
SDA = addr&0x01;
addr<<=1;
CLK = 1;
}
}
void IIC_Read(unsigned char addr)
{
unsigned char i;
for(i=0;i<8;i++)
{
CLK = 0;
_nop_();
_nop_();
SDA = addr&0x01;
addr<<= 1;
CLK = 1;
}
}
void PCF_Write(unsigned char cmd)
{
unsigned char i;
for(i=0;i<8;i++)
{
CLK = 0;
_nop_();
_nop_();
SDA = cmd&0x01;
cmd <<= 1;
CLK = 1;
}
}
void PCF_Read(unsigned char time)
{
unsigned char i,t;
for(t=0;t<time;t++)
{
for(i=0;i<8;i++)
{
CLK = 1;
_nop_();
_nop_();
dat[t]= SDA&0x80;
dat[t]>>=1;
CLK = 0;
}
}
}
void PCF_Set(unsigned char dat,unsigned char time,unsigned char Mode)
{
if(Mode)
{
IIC_Start();
IIC_Write(Write);
IIC_Ack();
PCF_Write(dat);
IIC_Stop();
}
else
{
IIC_Start();
IIC_Read(Read);
IIC_Ack();
PCF_Read(time);
IIC_Stop();
}
}
void write(unsigned char dat)
{
unsigned char i;
IIC_Start();
IIC_Write(0x40);
IIC_Ack();
for(i=0;i<8;i++)
{
CLK = 0;
_nop_();
_nop_();
SDA = dat&0x01;
dat <<= 1;
CLK = 1;
}
IIC_Stop();
}
PCF的h文件
#ifndef __PCF8974_H__
#define __PCF8974_H__
#include"STC15W.h"
sbit CLK = P5^4;
sbit SDA = P5^5;
void PCF_Set(unsigned char dat,unsigned char time,unsigned char Mode);
void write(unsigned char dat);
#endif
主函数
#include"STC15W.h"
#include"PCF8974.h"
#define WriteMode 1
#define ReadMode 0
unsigned char POUT[8]=
{
0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80
};
void delay(unsigned int i)
{
while(i--);
}
void main()
{
unsigned char x;
while(1)
{
write(0x0f);
}
}
注意:国产的PCF8574数据是反的,程序我就是用国产的(本来写命令是0x40,国产的写成0x02);