1

Тема: ATtiny13 + LCD1602_I2C + ds1307

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


#define ADDR    0x27
#define ADDR_T  0b1101000
#define SDA 3 // PB3
#define SCL 4 // PB4
#define RS      0
#define E       2
#define LED     3

byte led_b,init_t;


void setup() {
    lcdInit();
    i2c_write(led_b |= (1<<LED));// включаем подсветку
  //   set_time(21,2,5,11,15,47,0);// год 00-99, ДН 1-7 (1=ВС), месяц 1-12, дата 1-31, час 0-23, минуты 0-59, секунды 0-59
   delay(500);
}

void loop() {
   byte sec =  (ds_read(0) & 0x0F) + (((ds_read(0) & 0x70) >> 4) * 10);
   byte min =  (ds_read(1) & 0x0F) + (((ds_read(1) & 0x70) >> 4) * 10);
   byte hour = ((ds_read(2) & 0x0F) + ((ds_read(2) & 0x70) >> 4) * 10);
   byte datat = ((ds_read(4) & 0x0F) + ((ds_read(4) & 0x70) >> 4) * 10);
   byte mont = ((ds_read(5) & 0x0F) + ((ds_read(5) & 0x70) >> 4) * 10);
   byte year = ((ds_read(6) & 0x0F) + ((ds_read(6) & 0x70) >> 4) * 10);
   lcdCurs(0,4);
   lcdChar(hour/10+'0');lcdChar(hour%10+'0');lcdChar(58);
   lcdChar(min/10+'0');lcdChar(min%10+'0');lcdChar(58);
   lcdChar(sec/10+'0');lcdChar(sec%10+'0');
   lcdCurs(1,4); 
   lcdChar(datat/10+'0');lcdChar(datat%10+'0');lcdChar(45);
   lcdChar(mont/10+'0');lcdChar(mont%10+'0');lcdChar(45);
   lcdChar(year/10+'0');lcdChar(year%10+'0');
   delay(200);
}

void lcdSend(bool rs, byte data) {
    if(rs==0){led_b |= (1<<RS);} else {led_b &= ~(1<<RS);}//RS
    
    delay(1);
    if(((data >> 7) & 1) ==1){i2c_write(led_b |= (1<<7));} else {i2c_write(led_b &= ~(1<<7));}
    if(((data >> 6) & 1) ==1){i2c_write(led_b |= (1<<6));} else {i2c_write(led_b &= ~(1<<6));}
    if(((data >> 5) & 1) ==1){i2c_write(led_b |= (1<<5));} else {i2c_write(led_b &= ~(1<<5));}
    if(((data >> 4) & 1) ==1){i2c_write(led_b |= (1<<4));} else {i2c_write(led_b &= ~(1<<4));}
    e_pin();
    if(((data >> 3) & 1) ==1){i2c_write(led_b |= (1<<7));} else {i2c_write(led_b &= ~(1<<7));}
    if(((data >> 2) & 1) ==1){i2c_write(led_b |= (1<<6));} else {i2c_write(led_b &= ~(1<<6));}
    if(((data >> 1) & 1) ==1){i2c_write(led_b |= (1<<5));} else {i2c_write(led_b &= ~(1<<5));}
    if(((data >> 0) & 1) ==1){i2c_write(led_b |= (1<<4));} else {i2c_write(led_b &= ~(1<<4));}
    e_pin();
}

void lcdInit(){ 
    lcd(0x03);delay(5);
    lcd(0x03);delay(5);
    lcd(0x03);delayMicroseconds(200);
    lcd(0b00000010);delay(5);
    lcd(0b00001100);delay(5);
    lcdClear();
  } 

void lcdClear(){lcd(0b00000001);} 
void lcd(uint8_t sett) {lcdSend(true, sett);}
void lcdChar(const char chr) {lcdSend(false, (uint8_t)chr);}
void e_pin(){i2c_write(led_b |= (1<<E));delay(1);i2c_write(led_b &= ~(1<<E));}


void lcdCurs(byte str, byte mesto){
  if(str==0){lcd(0b10000000+mesto);}
  if(str==1){lcd(0b11000000+mesto);}
  }  
  

///// i2c /////////////

bool i2c_read_bit() {
    bool i2c_bit = 1;        
    DDRB &= ~(1 << SDA);            
  //  delayMicroseconds(10); 
    DDRB &= ~(1 << SCL);                
    if((PINB >> SDA) & 1) i2c_bit=0;                            
  //  delayMicroseconds(10);  
    DDRB |= (1 << SCL);              
    return i2c_bit;  
}
 
byte i2c_write_byte(byte data){
    for (byte i=0; i<8; i++){i2c_write_bit((data&0x80)==0);data<<=1;}    
    return i2c_read_bit(); 
}

byte i2c_read_byte(byte a){
    byte i, data=0;                
    for(i=0; i<8; i++){if (!i2c_read_bit()) data++;if(i!=7) data<<=1;}        
    i2c_write_bit(a);return data;  
}
 
void i2c_write_bit(byte b){
    delayMicroseconds(5);
    if(b){DDRB |= (1 << SDA);}else{DDRB &= ~(1 << SDA);}
    delayMicroseconds(5);
    DDRB &= ~(1 << SCL);       
    delayMicroseconds(10);
    DDRB |= (1 << SCL);
}
 
void i2c_start(){
     DDRB &= ~(1 << SDA); DDRB &= ~(1 << SCL);
     DDRB |= (1 << SDA);  PORTB &= ~(1 << SDA);
     DDRB |= (1 << SCL);  PORTB &= ~(1 << SCL);
}
 
void i2c_stop()  {
     DDRB |= (1 << SDA);           
     DDRB &= ~(1 << SCL);            
     DDRB &= ~(1 << SDA);           
}
 
void i2c_write(byte lcd){
     i2c_start();
     i2c_write_byte(ADDR<<1);
     i2c_write_byte(lcd);
     i2c_stop();
  }   

void ds_write(byte reg, byte data){
     i2c_start();
     i2c_write_byte(ADDR_T<<1);
     i2c_write_byte(reg);
     i2c_write_byte(data);
     i2c_stop();
  }  
  
byte ds_read(byte reg){
     byte dat=0;
     i2c_start();
     i2c_write_byte(ADDR_T<<1);
     i2c_write_byte(reg);
     i2c_start(); 
     i2c_write_byte((ADDR_T<<1)|1); 
     dat = i2c_read_byte(0);
     i2c_stop();
     return dat;
  }

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

Скетч использует 950 байт (92%) памяти устройства. Всего доступно 1024 байт.
Глобальные переменные используют 1 байт (1%) динамической памяти, оставляя 63 байт для локальных переменных. Максимум: 64 байт.