关于PIC+TCA6424程序

来源:本站
导读:目前正在解读《关于PIC+TCA6424程序》的相关信息,《关于PIC+TCA6424程序》是由用户自行发布的知识型内容!下面请观看由(电工技术网 - www.9ddd.net)用户发布《关于PIC+TCA6424程序》的详细说明。
简介:最近做了一套PIC+IO扩展芯片TCA6424程序,废话不说,上程序,希望对你的学习有所帮助。

最近做了一套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);

}

提醒:《关于PIC+TCA6424程序》最后刷新时间 2024-03-14 00:55:11,本站为公益型个人网站,仅供个人学习和记录信息,不进行任何商业性质的盈利。如果内容、图片资源失效或内容涉及侵权,请反馈至,我们会及时处理。本站只保证内容的可读性,无法保证真实性,《关于PIC+TCA6424程序》该内容的真实性请自行鉴别。