主控采用LM3s6911,采用PCA9534扩展IO驱动流水灯,PCA9534接口如下图
#include "sysinit.h"
#include "pca9534.h"
void I2C_init()
{
//使能I2C所在端口
SysCtlPeripheralEnable( SYSCTL_PERIPH_GPIOA);
//使能处理器I2C功能模块
SysCtlPeripheralEnable( SYSCTL_PERIPH_I2C1);
//配置必须管脚为I2C功能(类型)
GPIOPinTypeI2C(GPIO_PORTA_BASE,GPIO_PIN_6|GPIO_PIN_7);
//配置I2C时钟及传输速率
I2CMasterInitExpClk(I2C1_MASTER_BASE,SysCtlClockGet(),false);
I2CMasterEnable(I2C1_MASTER_BASE);
//使能所需中断
IntMasterEnable();
IntEnable(INT_I2C1);
I2CMasterIntEnable(I2C1_MASTER_BASE);
}
int main()
{
unsigned int a,i;
CLOCK_init();
I2C_init();
Pca9534Write(0x20,0x03,0x00);
while(1)
{
a=0xfe;
for(i=0;i<8;i++)
{
Pca9534Write(0x20,0x01,a);
SysCtlDelay(1000*TheSysClock/3000);
a=a<<1;
//a=a|0x01;
}
}
}
#include "sysinit.h"
#include "pca9534.h"
unsigned int iicintcount;
void Pca9534Write(unsigned char addr,unsigned char command,unsigned char data)
{
I2CMasterControl(I2C1_MASTER_BASE,I2C_MASTER_CMD_BURST_SEND_START);
//设置从机地址,写模式
I2CMasterSlaveAddrSet(I2C1_MASTER_BASE,addr,false);
//放置要发送的控制命令字到I2C master
I2CMasterDataPut(I2C1_MASTER_BASE,command);
I2CMasterControl(I2C1_MASTER_BASE,I2C_MASTER_CMD_BURST_SEND_CONT);
while(I2CMasterBusy(I2C1_MASTER_BASE));
I2CMasterDataPut(I2C1_MASTER_BASE,data);
I2CMasterControl(I2C1_MASTER_BASE,I2C_MASTER_CMD_BURST_SEND_FINISH);
while(I2CMasterBusy(I2C1_MASTER_BASE));
}
unsigned char Pca9534ReadReg(unsigned char addr,unsigned char command)
{
unsigned char getdata;
//设置从机地址,写模式
I2CMasterSlaveAddrSet(I2C1_MASTER_BASE,addr,false);
//放置要发送的控制命令字到I2C master
I2CMasterDataPut(I2C1_MASTER_BASE,command);
//发送控制命令字
I2CMasterControl(I2C1_MASTER_BASE,I2C_MASTER_CMD_BURST_SEND_START);
while(I2CMasterBusy(I2C1_MASTER_BASE));
//从设备地址+读写位写入发送数据寄存器
I2CMasterSlaveAddrSet(I2C1_MASTER_BASE,addr,true);
//再发从设备地址+读写位
I2CMasterControl(I2C1_MASTER_BASE,I2C_MASTER_CMD_BURST_RECEIVE_START);
while(I2CMasterBusy(I2C1_MASTER_BASE));
I2CMasterControl(I2C1_MASTER_BASE,I2C_MASTER_CMD_BURST_RECEIVE_FINISH );
while(I2CMasterBusy(I2C1_MASTER_BASE));
getdata=I2CSlaveDataGet(I2C1_MASTER_BASE);
return getdata;
}
unsigned char Pca9534ReadPort(unsigned char addr)
{
unsigned char getdata;
//设置从机地址,读模式
I2CMasterSlaveAddrSet(I2C1_MASTER_BASE,addr,true);
//发送从地址
I2CMasterControl(I2C1_MASTER_BASE,I2C_MASTER_CMD_BURST_RECEIVE_START);
while(I2CMasterBusy(I2C1_MASTER_BASE));
// getdata=I2CSlaveDataGet(I2C0_MASTER_BASE,I2C_MASTER_CMD_BURST_RECEIVE_START);
//发送停止位
I2CMasterControl(I2C1_MASTER_BASE,I2C_MASTER_CMD_BURST_RECEIVE_FINISH);
while(I2CMasterBusy(I2C1_MASTER_BASE));
getdata=I2CSlaveDataGet(I2C1_MASTER_BASE);
return getdata;
}
void I2C1_ISR(void)
{
I2CMasterIntClear(I2C1_MASTER_BASE);
iicintcount++;
}