登录站点

用户名

密码

FSL单片机普通IO引脚实现IIC通讯协议

已有 837 次阅读  2009-09-27 13:41   标签FSL  IIC  单片机  通讯  协议 

#include <hidef.h>
#include "derivative.h"

#define IIC_SDA_CTL PTCDD_PTCDD1
#define IIC_SDA_DAT PTCD_PTCD1
#define IIC_SCL_CTL PTCDD_PTCDD0
#define IIC_SCL_DAT PTCD_PTCD0
#define IIC_MST_HI 0
#define IIC_MST_LO 1


void IIC_Start(void);
void IIC_Restart(void);
void IIC_Stop(void);
byte IIC_SendByte(byte);
byte IIC_Read(byte *, byte);
byte IIC_Write(byte *, byte);
void IIC_Delay(void);

void IIC_Delay(void)
{
byte i;
for (i=0;i<8;i++) {}
}

//==============================================================
// Master generates a START condition on IIC bus
//==============================================================
void IIC_Start(void)
{
IIC_SDA_CTL = IIC_MST_HI;
IIC_SCL_CTL = IIC_MST_HI;

IIC_Delay();
IIC_SDA_CTL = IIC_MST_LO;
IIC_SDA_DAT = 0;
IIC_Delay();
IIC_SCL_CTL = IIC_MST_LO;
IIC_SCL_DAT = 0;
IIC_Delay();
}

//==============================================================
// Master generates a RESTART condition on IIC bus
//==============================================================
void IIC_Restart(void)
{
IIC_SDA_CTL = IIC_MST_HI; //SDA back to high while SCL remain in low
IIC_Delay();
IIC_SCL_CTL = IIC_MST_HI; //SCL back to high, bus idle now
IIC_Delay();
IIC_SDA_CTL = IIC_MST_LO; //RESTART condition occur
IIC_Delay();
IIC_SCL_CTL = IIC_MST_LO; //SCL to low for standby
IIC_Delay();
}

//==============================================================
// Master generates a STOP condition on IIC bus
//==============================================================
void IIC_Stop(void)
{
IIC_SDA_CTL = IIC_MST_LO; //make sure SDA is low
IIC_Delay();
IIC_SCL_CTL = IIC_MST_HI; //I2C_SCL_CTL go to high first
IIC_Delay();
IIC_SDA_CTL = IIC_MST_HI; //I2C_SDA_CTL have low-high transition while SCL is high
IIC_Delay();
}

//==============================================================
// Master send out a byte of data and return with ACK/NACK
// return with 0x00 if ACK received
// return with 0xff if NACK received
//==============================================================
byte IIC_SendByte(byte outDat)
{
byte bit;

//send out 8-bit data
for (bit=0;bit<8;bit++) {
if (outDat&0x80)
IIC_SDA_CTL = IIC_MST_HI;
else
IIC_SDA_CTL = IIC_MST_LO;
IIC_Delay();
IIC_SCL_CTL = IIC_MST_HI;
IIC_Delay();
IIC_SCL_CTL = IIC_MST_LO;
outDat <<= 1;
}

//check for the ACK/NACK
IIC_SDA_CTL = IIC_MST_HI; //master release SDA
IIC_Delay();
IIC_SCL_CTL = IIC_MST_HI; //master send a clock
IIC_Delay();
if (IIC_SDA_DAT) bit = 0xff; //NACK
else bit = 0; //ACK
IIC_SCL_CTL = IIC_MST_LO;
IIC_Delay();

return(bit);
}

//==============================================================
// Master write a string of bytes through IIC us
// Return with 0x00 if successful
// Return with 0xff if failed
//==============================================================
byte IIC_Write(byte *buff, byte total)
{
while (total) {
if (IIC_SendByte(*buff++)) //get NACK after data byte out
return(0xff); //abort
total--;
//__RESET_WATCHDOG(); //needed only for EEPROM byte-program
}
return(0);
}

//==============================================================
// Master read a byte of data and set ACK/NACK
// return with data read
//==============================================================
byte IIC_ReadByte(byte ackFlag)
{
byte bit, dat;

IIC_SDA_CTL = IIC_MST_HI; //make sure master release SDA

//read 8 bits sof data
for (bit=0;bit<8;bit++) {
dat <<= 1;
IIC_SCL_CTL = IIC_MST_HI;
IIC_Delay();
if (IIC_SDA_DAT) //read back data
dat |= 0x01;
IIC_SCL_CTL = IIC_MST_LO;
IIC_Delay();
}

//echo with ACK/NACK
if (ackFlag==0)
IIC_SDA_CTL = IIC_MST_LO; //echo back ACK
else
IIC_SDA_CTL = IIC_MST_HI; //echo back NACK

IIC_Delay();
IIC_SCL_CTL = IIC_MST_HI;
IIC_Delay();
IIC_SCL_CTL = IIC_MST_LO;
IIC_Delay();

return(dat);
}

//==============================================================
// Master read a string of data bytes through IIC us
//==============================================================
byte IIC_Read(byte *buff, byte total)
{
byte count;

if (total==0) return(0);

if (total==1) { //read one byte only
buff[0] = IIC_ReadByte(1); //NACK after read
return(1);
}

else { //read more than one bytes
count = 0;
while(total>1) {
IIC_Delay();
buff[count++] = IIC_ReadByte(0); //ACK after read
total--;
__RESET_WATCHDOG();
}
IIC_Delay();
buff[count++] = IIC_ReadByte(1);
return(count);
}
}

上一篇: 用VHDL/VerilogHD语言开发PLD/FPGA的完整流程 下一篇: 实现多路任意延时的一种方法

分享 举报