Вот код
- Код: Выделить всё • Развернуть
/*****************************************************
Chip type : ATmega16
Program type : Application
AVR Core Clock frequency: 16,000000 MHz
Memory model : Small
External RAM size : 0
Data Stack size : 256
*****************************************************/
#include <mega16.h>
#include <delay.h>
#define ACK 0xD0
#define NACK 0xD1
//Функции аппаратного I2C
void start_twi(void)
{
TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN);
while (!(TWCR & (1<<TWINT)));
}
void stop_twi(void)
{
TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWSTO);
}
void write_twi(unsigned char data_byte)
{
TWDR = data_byte;
TWCR = (1<<TWINT) | (1<<TWEN);
while(!(TWCR & (1<<TWINT)));
}
unsigned char i2read (unsigned ACK_Status)
{
if(ACK_Status == ACK)
TWCR = (1<<TWINT)| (1<<TWEA) | (1<<TWEN);
else
TWCR = (1<<TWINT) | (1<<TWEN);
while(!(TWCR & (1<<TWINT)));
return TWDR;
}
void main(void)
{
DDRD=0xFF;
TWBR=0xC;
start_twi();
write_twi(0x90);
write_twi(0xAC);
write_twi(0x00);
stop_twi();
start_twi();
write_twi(0x90);
write_twi(0xEE);
stop_twi();
while(1)
{
delay_ms(1000);
start_twi();
write_twi(0x90);
write_twi(0xAA);
start_twi();
write_twi(0x91);
i2read (ACK);
i2read (NACK);
stop_twi();
}
}