attiny13+ds3231+температура-простые часы
// MAX7219
#define DIN PB0
#define CLK PB1
#define CS PB2
#define SDA PB3
#define SCL PB4
#include <avr/io.h>
#include <util/delay.h>
int dp;
int main(void) {
DDRB |=(1<<DIN)|(1<<CLK)|(1<<CS);
PORTB |=(1<<DIN)|(1<<CLK)|(1<<CS);
WriteBit16(0x0F, 0);// тест выкл.
WriteBit16(0x0C, 1);// вкл. индик.
WriteBit16(0x0A, 2);// яркость
WriteBit16(0x09, 0xFF);// дешифраторы вкл.
WriteBit16(0x0B, 3);// кол-во разрядов
// set_time(24,3,8,15,9,55,0);// год 00-99, ДН 1-7 (1=ВС), месяц 1-12, дата 1-31, час 0-23, минуты 0-59, секунды 0-59
while(1) {
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);
int temper = (ds_read(0x11)*100 + ((ds_read(0x12) & 0b11000000) >> 6)*25)/10 ;
if(sec<50){
WriteBit16(1, hour/10%10);
if(dp==0){ WriteBit16(2, hour%10+0xf0);}else{WriteBit16(2, hour%10);}
WriteBit16(3, min/10%10);
WriteBit16(4, min%10);
}
else{
WriteBit16(1, 15);
WriteBit16(2, temper/100%10);
WriteBit16(3, temper/10%10+0xf0);
WriteBit16(4, temper%10);
}
_delay_ms(500);
dp++;
if(dp>1){dp=0;}
}
}
void WriteBit16(byte reg, byte data){
PORTB &=~(1<<CLK);PORTB &=~(1<<CS);
for(int i = 7; i >= 0; i--){
if(((reg >> i) & 1) == 1){PORTB |=(1<<DIN);}
else{PORTB &=~(1<<DIN);}
PORTB |=(1<<CLK);PORTB &=~(1<<CLK);
}
for(int i = 7; i >= 0; i--){
if(((data >> i) & 1) == 1){PORTB |=(1<<DIN);}
else{PORTB &=~(1<<DIN);}
PORTB |=(1<<CLK);PORTB &=~(1<<CLK);
}
PORTB |=(1<<CS);PORTB &=~(1<<CLK);PORTB &=~(1<<DIN);
}
bool i2c_read_bit() {
bool i2c_bit = 1;
DDRB &= ~(1 << SDA);
_delay_us(10);
DDRB &= ~(1 << SCL);
if((PINB >> SDA) & 1) i2c_bit=0;
_delay_us(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){
_delay_us(5);
if(b){DDRB |= (1 << SDA);}else{DDRB &= ~(1 << SDA);}
_delay_us(5);
DDRB &= ~(1 << SCL);
_delay_us(10);
DDRB |= (1 << SCL);
}
void i2c_start(){
_delay_us(10);
DDRB &= ~(1 << SDA); DDRB &= ~(1 << SCL);
_delay_us(10);
DDRB |= (1 << SDA); PORTB &= ~(1 << SDA);
_delay_us(10);
DDRB |= (1 << SCL); PORTB &= ~(1 << SCL);
_delay_us(10);
}
void i2c_stop() {
DDRB |= (1 << SDA);
_delay_us(10);
DDRB &= ~(1 << SCL);
_delay_us(10);
DDRB &= ~(1 << SDA);
}
byte ds_read(byte reg){
byte data = 0;
i2c_start();
i2c_write_byte(0b11010000);
i2c_write_byte(reg);
i2c_start();
i2c_write_byte(0b11010001);
data = i2c_read_byte(0);
i2c_stop();
return data;
}
void ds_write(byte reg, byte data){
i2c_start();
i2c_write_byte(0b11010000);
i2c_write_byte(reg);
i2c_write_byte(data);
i2c_stop();
}
void set_time(byte years, byte days, byte monts, byte datas, byte hours ,byte minute, byte second){
if(second < 255){ds_write(0x00,(second/10<<4)+second%10);}
if(minute < 255){ds_write(0x01,(minute/10<<4)+minute%10);}
if(hours < 255){ds_write(0x02,(hours/10<<4)+hours%10);}
if(days < 255){ds_write(0x03,days);}
if(datas < 255){ds_write(0x04,(datas/10<<4)+datas%10);}
if(monts < 255){ds_write(0x05,(monts/10<<4)+monts%10);}
if(years < 255)ds_write(0x06,(years/10<<4)+years%10);
}