1

Тема: ATtiny13 + LCD1602_I2C + ds18b20

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

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

byte led_b;
int t;

void setup() {
    i2c_write(led_b |= (1<<LED));// включаем подсветку
    lcdInit();
}

void loop() {
   t = read_temp();
   lcdCurs(0,3);
   lcdChar(84);lcdChar(69);lcdChar(77);lcdChar(80);
   lcdCurs(1,3); 
   lcdChar(t/100 +'0');
   lcdChar(t/10%10 +'0');
   lcdChar(46);
   lcdChar(t%10 +'0');
   lcdChar(32);lcdChar(67);
   delay(1000);
}

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(); 
}
 
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(){
     delayMicroseconds(10);  
     DDRB &= ~(1 << SDA); DDRB &= ~(1 << SCL); 
     delayMicroseconds(10); 
     DDRB |= (1 << SDA);  PORTB &= ~(1 << SDA);
     delayMicroseconds(10); 
     DDRB |= (1 << SCL);  PORTB &= ~(1 << SCL);   
     delayMicroseconds(10);
}
 
void i2c_stop()  {
     DDRB |= (1 << SDA);            
     delayMicroseconds(10);
     DDRB &= ~(1 << SCL);               
     delayMicroseconds(10); 
     DDRB &= ~(1 << SDA);             
}
 
void i2c_write(byte lcd){
     i2c_start();
     i2c_write_byte(ADDR<<1);
     i2c_write_byte(lcd);
     i2c_stop();
  }   

//////// ds18b20 /////////////

// reset
uint8_t therm_reset(){
    uint8_t i;
    PORTB &= ~(1 << OUT);
    DDRB |= (1 << OUT);
    delayMicroseconds(480);  
    DDRB &= ~(1 << OUT);
    delayMicroseconds(60);
    i=((PINB >> OUT) & 1);
    delayMicroseconds(420);
    return i;
}
// write bit
void therm_write_bit(uint8_t bit){
    PORTB &= ~(1 << OUT);
    DDRB |= (1 << OUT);
    delayMicroseconds(1);
    if(bit) DDRB &= ~(1 << OUT);
    delayMicroseconds(60);
    DDRB &= ~(1 << OUT);
}
// read bit
uint8_t therm_read_bit(void){
    uint8_t bit=0;
    PORTB &= ~(1 << OUT);
    DDRB |= (1 << OUT);
    delayMicroseconds(1);
    DDRB &= ~(1 << OUT);
    delayMicroseconds(14);
    if(PINB & (1 << OUT)) bit=1;
    delayMicroseconds(45);
    return bit;
}
 
// read byte
uint8_t therm_read_byte(void){
    uint8_t i=8, n=0;
    while(i--){n>>=1;n|=(therm_read_bit()<<7);}
    return n;
}
 
// write byte
void therm_write_byte(uint8_t byte){
    uint8_t i=8;
    while(i--){therm_write_bit(byte&1);byte >>= 1;
    }
}
// read temp
int read_temp(){
    uint8_t temperature[2];
    int temper;
    therm_reset();
    therm_write_byte(0xCC);
    therm_write_byte(0x44);
    while(!therm_read_bit());
    therm_reset();
    therm_write_byte(0xCC);
    therm_write_byte(0xBE);
    temperature[0]=therm_read_byte();
    temperature[1]=therm_read_byte();
    therm_reset();
    temper = (temperature[1] << 8 | temperature[0])*10/16;
    return (int)temper;
}   

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