1

Тема: Аппаратный I2C

Пример: чтение секунд в DS3231

http://forum.rcl-radio.ru/uploads/images/2021/05/8a7e35b4d5fd02d1c1676e7c89a1b3e7.png

#define ADDR    0b1101000
#define CPU_F   16000000
#define SCL_F   100000

void setup() {
  Serial.begin(9600);
   TWBR = (((CPU_F)/(SCL_F)-16 )/2) ;
   TWSR = 0;

}

void loop() {
  // write
  TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN);  // START
  while (!(TWCR & (1<<TWINT)));

  TWDR = ADDR << 1;
  TWCR = (1<<TWINT) | (1<<TWEN);
  while (!(TWCR & (1<<TWINT)));

  TWDR = 0;
  TWCR = (1<<TWINT) | (1<<TWEN);
  while (!(TWCR & (1<<TWINT)));

  // read
  TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN);  // START
  while (!(TWCR & (1<<TWINT)));
   
  TWDR = (ADDR << 1) | 1;
  TWCR = (1<<TWINT) | (1<<TWEN);
  while (!(TWCR & (1<<TWINT)));
   
  TWCR = (1<<TWINT) | (1<<TWEN);
  while (!(TWCR & (1<<TWINT)));
  byte data = TWDR;
   
  TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWSTO); // СТОП
   
   byte sec =  (data & 0x0F) + (((data & 0x70) >> 4) * 10);
Serial.println(sec);

delay(100);
}

2

Re: Аппаратный I2C

http://forum.rcl-radio.ru/uploads/images/2021/05/e7dcedfa28044bee2007ac6c012f574b.png

#define ADDR    0b1101000
#define CPU_F   16000000
#define SCL_F   100000

void setup() {
  Serial.begin(9600);
   TWBR = (((CPU_F)/(SCL_F)-16 )/2) ;
   TWSR = 0;
  set_time(21,5,4,29,9,57,0);// год 00-99, ДН 1-7 (1=ВС), месяц 1-12, дата 1-31, час 0-23, минуты 0-59, секунды 0-59
}

void loop() {
  byte sec =  (i2c_read(ADDR,0) & 0x0F) + (((i2c_read(ADDR,0) & 0x70) >> 4) * 10);
  byte min =  (i2c_read(ADDR,1) & 0x0F) + (((i2c_read(ADDR,1) & 0x70) >> 4) * 10);
  byte hour = ((i2c_read(ADDR,2) & 0x0F) + ((i2c_read(ADDR,2) & 0x70) >> 4) * 10);
  byte day =  (i2c_read(ADDR,3) & 0x0F);
  byte datat = ((i2c_read(ADDR,4) & 0x0F) + ((i2c_read(ADDR,4) & 0x70) >> 4) * 10);
  byte mont = ((i2c_read(ADDR,5) & 0x0F) + ((i2c_read(ADDR,5) & 0x70) >> 4) * 10);
  byte year = ((i2c_read(ADDR,6) & 0x0F) + ((i2c_read(ADDR,6) & 0x70) >> 4) * 10);
  int temper = (i2c_read(ADDR,0x11)*100 + ((i2c_read(ADDR,0x12) & 0b11000000) >> 6)*25) ;
Serial.print(hour);
Serial.print(":");
Serial.print(min);
Serial.print(":");
Serial.print(sec);
Serial.print(" ");
Serial.print(datat);
Serial.print("-");
Serial.print(mont);
Serial.print("-");
Serial.print(year);
Serial.print(" ");
Serial.print(day);
Serial.print(" ");
Serial.print(temper/100);
Serial.print(".");
Serial.print(temper%10);
Serial.println(" C");

delay(100);
}

byte i2c_read(byte i2c_addr, byte i2c_reg){
   TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN);  // START
  while (!(TWCR & (1<<TWINT)));
   TWDR = i2c_addr << 1;
   TWCR = (1<<TWINT) | (1<<TWEN);
  while (!(TWCR & (1<<TWINT)));
   TWDR = i2c_reg;
   TWCR = (1<<TWINT) | (1<<TWEN);
  while (!(TWCR & (1<<TWINT)));
   TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN);  // START
  while (!(TWCR & (1<<TWINT))); 
   TWDR = (i2c_addr << 1) | 1;
   TWCR = (1<<TWINT) | (1<<TWEN);
  while (!(TWCR & (1<<TWINT)));
   TWCR = (1<<TWINT) | (1<<TWEN);
  while (!(TWCR & (1<<TWINT)));
   byte i2c_data = TWDR;  
   TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWSTO); // СТОП
   return i2c_data;
  }

void i2c_write(byte i2c_addr, byte i2c_reg, byte i2c_dat){
   TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN);  // START
  while (!(TWCR & (1<<TWINT)));
   TWDR = i2c_addr << 1;
   TWCR = (1<<TWINT) | (1<<TWEN);
  while (!(TWCR & (1<<TWINT)));
   TWDR = i2c_reg;
   TWCR = (1<<TWINT) | (1<<TWEN);
  while (!(TWCR & (1<<TWINT)));
   TWDR = i2c_dat;
   TWCR = (1<<TWINT) | (1<<TWEN);
  while (!(TWCR & (1<<TWINT)));
   TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWSTO); // СТОП
  }  

void set_time(byte years, byte days, byte monts, byte datas, byte hours ,byte minute, byte second){
    if(second < 255){i2c_write(ADDR,0x00,(second/10<<4)+second%10);}
    if(minute < 255){i2c_write(ADDR,0x01,(minute/10<<4)+minute%10);} 
    if(hours < 255){i2c_write(ADDR,0x02,(hours/10<<4)+hours%10);}
    if(days < 255){i2c_write(ADDR,0x03,days);}
    if(datas < 255){i2c_write(ADDR,0x04,(datas/10<<4)+datas%10);}
    if(monts < 255){i2c_write(ADDR,0x05,(monts/10<<4)+monts%10);}
    if(years < 255){i2c_write(ADDR,0x06,(years/10<<4)+years%10);}
  }