MSP430普通I/0口模拟IIC总线
#define SLAVE_ADDRESS 0x04
#define SLAVE_WRITE_ADDRESS (SLAVE_ADDRESS<1)|0x00
#define SLAVE_READ_ADDRESS (SLAVE_ADDRESS<1)|0x01
#define I2C_SDABIT3
#define I2C_SCLBIT4
#define I2C_PORT_INP6IN
#define I2C_PORT_OUTP6OUT
#define I2C_PORT_DIRP6DIR
#define SDA_IN(I2C_PORT_IN & I2C_SDA)
#define SCL_IN(I2C_PORT_IN & I2C_SCL)
#define S_SDAI2C_PORT_DIR &= ~I2C_SDA
#define C_SDAI2C_PORT_DIR |= I2C_SDA;I2C_PORT_OUT &= ~I2C_SDA
#define S_SCLI2C_PORT_DIR &= ~I2C_SCL
#define C_SCLI2C_PORT_DIR |= I2C_SCL;I2C_PORT_OUT &= ~I2C_SCL
void I2C_init(void)
{
I2C_PORT_DIR &= ~(I2C_SDA|I2C_SCL);
I2C_PORT_OUT |= (I2C_SDA|I2C_SCL);
}
void I2C_DELAY(void)
{
asm("nop");asm("nop");
asm("nop");asm("nop");
asm("nop");asm("nop");
asm("nop");asm("nop");
}
void I2C_Start(void)
{
S_SDA;
S_SCL;
I2C_DELAY();
C_SDA;
I2C_DELAY();
C_SCL;
}
void I2C_Stop(void)
{
C_SDA;
I2C_DELAY();
S_SCL;
I2C_DELAY();
S_SDA;
I2C_DELAY();
}
void I2C_Send_Ack(void)
{
C_SDA;
I2C_DELAY();
S_SCL;
I2C_DELAY();
C_SCL;
I2C_DELAY();
}
void I2C_Send_NoAck(void)
{
S_SDA;
I2C_DELAY();
S_SCL;
I2C_DELAY();
C_SCL;
I2C_DELAY();
}
void I2C_Read_Ack(void)
{
unsigned char i = 0;
S_SCL;
S_SDA;
I2C_DELAY();
for(i=0; i<100; i++)
{
if(SDA_IN == 0)
{
I2C_DELAY(); break;
}
I2C_DELAY();
}
C_SCL;
I2C_DELAY();
}
void I2C_WriteByte(unsigned char one_byte)
{
unsigned char i = 0;
S_SDA;
for (i=0; i<8; i++)
{
if (one_byte&0x80)
{
S_SDA;
}
else
{
C_SDA;
}
one_byte<=1;
I2C_DELAY();
S_SCL;
I2C_DELAY();
C_SCL;
I2C_DELAY();
}
}
unsigned char I2C_ReadByte(void)
{
unsigned char read_byte=0;
unsigned char i = 0;
S_SDA;
//C_SCL;
I2C_DELAY();
for (i=0; i<8; i++)
{
S_SCL;
I2C_DELAY();
read_byte<=1;
if (SDA_IN != 0)
{
read_byte |= 0x01;
}
//I2C_DELAY;
C_SCL;
I2C_DELAY();
}
return read_byte;
}
void I2C_write_Nbyte(unsigned char *pbuf, unsigned char cnt)
{
unsigned char i = 0;
I2C_Start();
I2C_WriteByte(SLAVE_WRITE_ADDRESS);
I2C_Read_Ack();
for (i=0; i
I2C_DELAY();I2C_DELAY();
I2C_WriteByte(*pbuf);
pbuf++;
I2C_Read_Ack();
}
I2C_Stop();
}
void I2C_read_Nbyte(unsigned char *pbuf, unsigned char cnt)
{
unsigned char i = 0;
I2C_Start();
I2C_WriteByte(SLAVE_READ_ADDRESS);
I2C_Read_Ack();
for (i=1; i
I2C_DELAY();I2C_DELAY();
*pbuf = I2C_ReadByte();
pbuf++;
I2C_Send_Ack();
}
*pbuf = I2C_ReadByte();
I2C_Stop();
}
MSP430普通I0口模拟IIC总 相关文章:
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)
- openwebmailV1.60安装教学(11-12)
- Linux嵌入式系统开发平台选型探讨(11-09)
- Windows CE 进程、线程和内存管理(二)(11-09)