最近做了一套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);
}
上一篇:PIC18F4520定时器初值设置
下一篇:PIC单片机双机异步通信程序详解
推荐阅读最新更新时间:2024-11-12 06:11
设计资源 培训 开发板 精华推荐
- 具有 DrMOS、fSW = 500kHz 的 LTC3861IUH-1、4 相 1V/100A 转换器的典型应用电路
- 用于便携式的 12 位、1 通道 DAC
- LTC2946IMS-1 12V、50A 功率、电荷和能量监视器的典型应用
- 使用 ROHM Semiconductor 的 BU4840 的参考设计
- OM13089: LPCXpresso54114电路板
- 具有比率跟踪降压稳压器的 LT8607IMSE 5V 和 3.3V 典型应用电路
- DI-113 - 适用于便携式音频播放器的 5 W 高效充电器
- CH347-"要你命3000"高速四模式编程器+串口/JTAG调试设备
- 使用 ROHM Semiconductor 的 BD45382 的参考设计
- OP162GSZ-REEL高速仪表放大器典型应用电路