最近做了一套PIC+IO扩展芯片TCA6424程序,废话不说,上程序
void main(void) {
System_Init();
wr_IIC(0x8c,0xff,0xff,0xff);
while(1)
{
p0 = rd_IIC(0x80);
p1 = rd_IIC(0x81);
p2 = rd_IIC(0x82);
p0 = 0;
p1 = 0;
p2 = 0;
}
}
void IIC_Init(void)
{
SCL_DIR = 0;
SDA_DIR = 0;
SCL_PIN = 1;
SDA_PIN = 1;
}
void IICstart(void)
{
SCL_DIR = 0;
SDA_DIR = 0;
SDA_PIN = 1;
asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");
SCL_PIN = 1;
asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");
SDA_PIN = 0;
asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");
SCL_PIN = 0;
asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");
}
void IICstop(void)
{
SCL_DIR = 0;
SDA_DIR = 0;
SDA_PIN = 0;
asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");
SCL_PIN = 1;
asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");
SDA_PIN = 1;
asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");
SCL_PIN = 0;
asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");
}
void IICsend(unsigned char data)
{
unsigned char i=8,n;
SCL_DIR = 0;
SDA_DIR = 0;
for(n=0; n<8; n++)
{
SDA_PIN = 0;
if(data&0x80)
{
SDA_PIN = 1;
}
data = data << 1;
SCL_PIN = 1;
asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");
SCL_PIN = 0;
asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");
}
}
unsigned char IICreceive(void)
{
unsigned char i=8;
unsigned char data=0;
SCL_DIR = 0;
SDA_DIR = 1;
while(i)
{
SCL_PIN = 0;
asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");
SCL_PIN = 1;
asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");
data<<=1;
data|=SDA_PIN ;
asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");
i--;
}
SCL_PIN = 0;
return(data);
}
void IICreadack(void)
{
SDA_PIN = 0;
SDA_DIR = 1;
if(SDA_PIN == 1)
{
for(;;) ;//NACK
}
SCL_PIN = 1;
asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");
SCL_PIN = 0;
asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");
SDA_DIR = 0;
SDA_PIN = 0;
}
void IICsendack(void)
{
SCL_DIR = 0;
SDA_DIR = 0;
SCL_PIN = 0;
asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");
SDA_PIN = 0;
asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");
SCL_PIN = 1;
asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");
SCL_PIN = 0;
asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");
}
void IICsendNack(void)
{
SCL_DIR = 0;
SDA_DIR = 0;
SCL_PIN = 0;
asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");
SDA_PIN = 1;
asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");
SCL_PIN = 1;
asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");
SCL_PIN = 0;
asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");
}
//************以下两个函数基于文件夹“TCA6424 SCAN5 51(pudn)”***********//
void wr_IIC(char a,char b,char c,char d)
{
unsigned char command;
IICstart();
command=0x44;
IICsend(command);
IICreadack();
IICsend(a);
IICreadack();
IICsend(b);
IICreadack();
IICsend(c);
IICreadack();
IICsend(d);
IICreadack();
IICstop();
}
unsigned char rd_IIC(char a)
{
unsigned char i,command,com_data;
IICstart();
command=0x44;
IICsend(command);
IICreadack();
IICsend(a);
IICreadack();
IICstart();
command=0x45;
IICsend(command);
IICreadack();
com_data = IICreceive();
IICsendNack();
IICstop();
return(com_data);
}