1

Тема: DS3231 с выводом в монитор порта

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

#define ADDR    0b1101000
#define CPU_F   16000000 // Clock Speed
#define SCL_F   100000 // // I2C Speed
/// uart init /////
#define BAUD 9600 // USART Speed
 

void setup() {
 // Serial.begin(9600);
   TWBR = (((CPU_F)/(SCL_F)-16 )/2) ;
   TWSR = 0;
   USART_Init(CPU_F/16/BAUD-1); 
 // 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) ;

USART_int(hour);USART_String(":");
USART_int(min);USART_String(":");
USART_int(sec);USART_String(" ");
USART_int(datat);USART_String("-");
USART_int(mont);USART_String("-");
USART_int(year);USART_String(" ");
USART_int(day);USART_String(" ");
USART_int(temper/100);USART_String(".");
USART_int(temper%10);USART_String(" C\n");

delay(1000);
}

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);}
  }  

void USART_Init( unsigned int ubrr ){
UBRR0H = (unsigned char)(ubrr>>8);
UBRR0L = (unsigned char)ubrr;
UCSR0B = (1<<RXEN0)|(1<<TXEN0);
UCSR0C = (1<<USBS0)|(3<<UCSZ00);
}  

void USART_String(char *data){
  for(int i=0;i<strlen(data);i++){
  while(!( UCSR0A & (1 << UDRE0 )));
  UDR0 = data[i];}
}

void USART_int(int to_int){
  String stringVar = String(to_int, DEC);
  char charVar[sizeof(stringVar)];
  stringVar.toCharArray(charVar, sizeof(charVar));
  USART_String(charVar);}

Скетч использует 2812 байт (19%) памяти устройства. Всего доступно 14336 байт.
Глобальные переменные используют 31 байт (3%) динамической памяти, оставляя 993 байт для локальных переменных. Максимум: 1024 байт.