📄 i2c.c
字号:
#include "i2c.h"
byte i2c_local_buff[5];
void init_i2c(void)
{
int i;
bool FirstCheck = TRUE;
while(!((PIND&0x01)&&(PIND&02)))
{
if(!FirstCheck)
{
DDRD |= 0x20;//将PD5置为输出,RST_MOST;
PORTD &= ~_BV(PD5);//复位OS81050
for(i=0;i<10000;i++);
PORTD |= _BV(PD5);//启动OS81050
DDRD &= 0xdf;
}
else
{
for(i=0;i<10000;i++);
}
FirstCheck = FALSE;
}
/////关于i2c的TWI的 设置。。。。。
TWCR =0x00;
TWBR = 0x0A;
TWSR = 0x00;
TWCR = (1<<TWEN)| // Enable TWI-interface and define TWI pins.
(0<<TWIE)|(0<<TWINT)| // Disable Interupt.
(0<<TWEA)|(0<<TWSTA)|(0<<TWSTO)| // No Signal requests.
(0<<TWWC);
/////下面这两条是什么语句,去掉??
DDRD &= 0xfc;
PORTD |= 0x03;
}
byte i2c_read_buff(byte DevAddr, byte *Buf, byte Length)
{
byte rAddr = DevAddr|0x01;
Length --;
Start();
Wait();
if (TestAck()!=START) return 0;
Write8Bit(rAddr);
Wait();
if(TestAck()!=MR_SLA_ACK) return 0;
while( Length --)
{
TwiAck();
Wait();
if(TestAck()!=MR_DATA_ACK) return 0;
*(Buf ++) = TWDR;
}
Twi();
Wait();
if(TestAck()!=MR_DATA_NOACK) return 0;
*Buf = TWDR;
Stop();
return 1;
}
byte i2c_write_buff(byte DevAddr, byte *Buf, byte Length)
{
int i;
Start();
Wait();
if(TestAck()!=START) return 1;
Write8Bit(DevAddr);
Wait();
if(TestAck()!=MT_SLA_ACK) return 1;
while(Length -- )//写字节;
{
Write8Bit(*(Buf++));
Wait();
if(TestAck()!=MT_DATA_ACK) return 1;
}
Stop();
for(i=0;i<10000;i++);//加延迟,以使芯片将缓冲区数据保存到需要的存储器位置;
return 0;
}
byte i2c_read_byte(byte DevAddr)
{
byte result;
i2c_read_buff(DevAddr, i2c_local_buff, 1);
result = i2c_local_buff[0];
return (result);
}
byte i2c_write_byte(byte DevAddr,byte data)
{
byte result;
i2c_local_buff[0] = data;
result = i2c_write_buff(DevAddr, i2c_local_buff, 1);
return (result);
}
word i2c_read_word(byte DevAddr)
{
word result;
i2c_read_buff(DevAddr, i2c_local_buff, 2);
result = ((word ) i2c_local_buff[0]) << 8;
result |= (word ) i2c_local_buff[1];
return (result);
}
word pm_read (byte *buf)
{
byte addr;
byte data;
word length;
word pm_length;
addr = INIC_ADDR | 0x01; // sending slave address + READ
Start();
Wait();
if (TestAck()!=START) return 0;
Write8Bit(addr);
Wait();
if(TestAck()!=MR_SLA_ACK) return 0;
TwiAck();
Wait();
if(TestAck()!=MR_DATA_ACK) return 0;
data = TWDR;
*(buf++) = data;
length = (word ) data <<8;
TwiAck();
Wait();
if(TestAck()!=MR_DATA_ACK) return 0;
data = TWDR;
*(buf++) = data; // read data from register, put in buffer
length |= data; // or in the low byte of length
pm_length = length + 2; // add 2 for 2 bytes of pm length itself, save for return
if((0<length)&&(length<64))
{ while (length--)
{
if (length != 0) // then this is not last byte
{
TwiAck();
Wait();
if(TestAck()!=MR_DATA_ACK) return 0;
data = TWDR;
*(buf++) = data; // read data from register, put in buffer
}
else // last byte, don't ack
{
Twi();
Wait();
if(TestAck()!=MR_DATA_NOACK) return 0;
data = TWDR;
*(buf++) = data; // read data from register, put in buffer
}
}
}
Stop();
if (length == 0xFFFF) // everything went OK? (this compiler ends loop above with length at -1)
{
return(pm_length); // returns # of bytes in Port Message
}
else
{
return 0; // got a NACK during transmission & broke out of loop
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -