1

Тема: Часы на ИВЛ1-7/5 на Atmega8

Собрал часы на атмега8. При прошивке столкнулся с проблемой, скетч не компилировался выдавало ошибку. После замены переменной min на minute скетч компилируется часы идут. Хотелось бы расширить функционал часов и добавить в эти часы датчик температуры на ds18b20 с отображением градусов, будильник с зажиганием одного сегмента на индикаторе будильник. Часы запустил на индикаторе ИЛЦ7-4/7Л.

2

Re: Часы на ИВЛ1-7/5 на Atmega8

http://forum.rcl-radio.ru/uploads/images/2023/03/db7b3189f53c3619e75d5440c518880a.jpg
вот так они пока что без корпуса выглядят.

3

Re: Часы на ИВЛ1-7/5 на Atmega8

Опубликуйте скетч который используете.

4

Re: Часы на ИВЛ1-7/5 на Atmega8

// ATMEGA8 12MHz
#define G0_D       PD6
#define G1_B       PB1
#define G2_B       PB2
#define G3_B       PB5
#define DP_C       PC1 //PD SETKA 

#define E_B        PB3
#define G_B        PB4
#define B_B        PB0
#define F_D        PD7
#define A_D        PD5
#define D_C        PC2
#define C_C        PC3

#define SET        PD0
#define UP         PD1
#define DOWN       PD2

#include <avr/io.h>
#include <util/delay.h>

#define ADDR_DS1307    0b1101000
#define CPU_F          12000000 // Clock Speed
#define SCL_F          100000 // // I2C Speed

byte a[4],an,segm,i;
int msec,sec,minute,hour,set;
int set_sec,set_minute,set_hour;

int main() {
  TWBR = (((CPU_F)/(SCL_F)-16 )/2) ;
  TWSR = 0;
// set_time(22,7,2,26,13,10,0);// год 00-99, ДН 1-7 (1=ВС), месяц 1-12, дата 1-31, час 0-23, минуты 0-59, секунды 0-59
  cli();
// (12000000/((18749+1)x64))=10 Hz
  OCR1A = 18749;
  TCCR1B |= (1 << WGM12);
// Prescaler 64
  TCCR1B |= (1 << CS11) | (1 << CS10);
  TIMSK |= (1 << OCIE1A);
// (12000000/((155+1)x256))=300.48076923077 Hz
  OCR2 = 155;
  TCCR2 |= (1 << WGM21);
// Prescaler 256
  TCCR2 |= (1 << CS22) | (1 << CS21);
  TIMSK |= (1 << OCIE2);
  sei();
  DDRB|=(1<<G1_B)|(1<<G2_B)|(1<<G3_B)|(1<<E_B)|(1<<G_B)|(1<<B_B);
  DDRC|=(1<<D_C)|(1<<C_C)|(1<<DP_C);
  DDRD|=(1<<G0_D)|(1<<F_D)|(1<<A_D);
  PORTD|=(1<<SET)|(1<<UP)|(1<<DOWN);


while(1) {
   sec =  (i2c_read_1bit(ADDR_DS1307,0) & 0x0F) + (((i2c_read_1bit(ADDR_DS1307,0) & 0x70) >> 4) * 10);
   minute =  (i2c_read_1bit(ADDR_DS1307,1) & 0x0F) + (((i2c_read_1bit(ADDR_DS1307,1) & 0x70) >> 4) * 10);
   hour = ((i2c_read_1bit(ADDR_DS1307,2) & 0x0F) + ((i2c_read_1bit(ADDR_DS1307,2) & 0x70) >> 4) * 10);   

   if(((PIND >> SET) & 1) == 0){set++;if(set>3){set=0;}_delay_ms(300);}

   set_sec=sec;
   set_minute=minute;
   set_hour=hour;

   if(((PIND >> UP) & 1) == 0 && set==1){set_hour++;if(set_hour>23){set_hour=23;}_delay_ms(300);set_time(255,255,255,255,set_hour,255,255);}
   if(((PIND >> DOWN) & 1) == 0 && set==1){set_hour--;if(set_hour<0){set_hour=0;}_delay_ms(300);set_time(255,255,255,255,set_hour,255,255);}

   if(((PIND >> UP) & 1) == 0 && set==2){set_minute++;if(set_minute>59){set_minute=59;}_delay_ms(300);set_time(255,255,255,255,255,set_minute,255);}
   if(((PIND >> DOWN) & 1) == 0 && set==2){set_minute--;if(set_minute<0){set_minute=0;}_delay_ms(300);set_time(255,255,255,255,255,set_minute,255);}

   if(((PIND >> UP) & 1) == 0 && set==3){set_sec=0;_delay_ms(300);set_time(255,255,255,255,255,255,set_sec);}
   if(((PIND >> DOWN) & 1) == 0 && set==3){set_sec=0;_delay_ms(300);set_time(255,255,255,255,255,255,set_sec);}

if(set==0){   
   a[3]=hour/10;
   a[2]=hour%10;
   a[1]=minute/10;
   a[0]=minute%10;
}
   if(set==1&&(msec==0||msec==5)){a[3]=10;}else{a[3]=hour/10;}
   if(set==1&&(msec==0||msec==5)){a[2]=10;}else{a[2]=hour%10;}
   if(set==2&&(msec==0||msec==5)){a[1]=10;}else{a[1]=minute/10;}
   if(set==2&&(msec==0||msec==5)){a[0]=10;}else{a[0]=minute%10;}
if(set==3){
   if(msec==0||msec==5){a[1]=10;a[3]=10;a[2]=10;}else{a[3]=10;a[2]=10;a[1]=sec/10;}
   if(msec==0||msec==5){a[0]=10;a[3]=10;a[2]=10;}else{a[3]=10;a[2]=10;a[0]=sec%10;}
}
   if(msec<5){PORTC |=(1<<DP_C);}
   if(msec>=5){PORTC &=~(1<<DP_C);}

_delay_ms(100);
}}

ISR(TIMER2_COMP_vect){
    switch(i){
    case 0:  segm=a[0];  an=0;  anod();segment(); break;
    case 1:  segm=a[1];  an=1;  anod();segment(); break;
    case 2:  segm=a[2];  an=2;  anod();segment(); break;
    case 3:  segm=a[3];  an=3;  anod();segment(); break;
    }i++;if(i>3){i=0;}}

void segment(){                                                               
   switch(segm){                                                                 
    case 0: PORTD &=~(1<<A_D);PORTB &=~(1<<B_B);PORTC &=~(1<<C_C);PORTC &=~(1<<D_C);PORTB &=~(1<<E_B);PORTD &=~(1<<F_D);PORTB |=(1<<G_B);break;
    case 1: PORTD |=(1<<A_D);PORTB &=~(1<<B_B);PORTC &=~(1<<C_C);PORTC |=(1<<D_C);PORTB |=(1<<E_B);PORTD |=(1<<F_D);PORTB |=(1<<G_B);break;
    case 2: PORTD &=~(1<<A_D);PORTB &=~(1<<B_B);PORTC |=(1<<C_C);PORTC &=~(1<<D_C);PORTB &=~(1<<E_B);PORTD |=(1<<F_D);PORTB &=~(1<<G_B);break;
    case 3: PORTD &=~(1<<A_D);PORTB &=~(1<<B_B);PORTC &=~(1<<C_C);PORTC &=~(1<<D_C);PORTB |=(1<<E_B);PORTD |=(1<<F_D);PORTB &=~(1<<G_B);break;
    case 4: PORTD |=(1<<A_D);PORTB &=~(1<<B_B);PORTC &=~(1<<C_C);PORTC |=(1<<D_C);PORTB |=(1<<E_B);PORTD &=~(1<<F_D);PORTB &=~(1<<G_B);break;   
    case 5: PORTD &=~(1<<A_D);PORTB |=(1<<B_B);PORTC &=~(1<<C_C);PORTC &=~(1<<D_C);PORTB |=(1<<E_B);PORTD &=~(1<<F_D);PORTB &=~(1<<G_B);break;
    case 6: PORTD &=~(1<<A_D);PORTB |=(1<<B_B);PORTC &=~(1<<C_C);PORTC &=~(1<<D_C);PORTB &=~(1<<E_B);PORTD &=~(1<<F_D);PORTB &=~(1<<G_B);break;
    case 7: PORTD &=~(1<<A_D);PORTB &=~(1<<B_B);PORTC &=~(1<<C_C);PORTC |=(1<<D_C);PORTB |=(1<<E_B);PORTD |=(1<<F_D);PORTB |=(1<<G_B);break;
    case 8: PORTD &=~(1<<A_D);PORTB &=~(1<<B_B);PORTC &=~(1<<C_C);PORTC &=~(1<<D_C);PORTB &=~(1<<E_B);PORTD &=~(1<<F_D);PORTB &=~(1<<G_B);break;
    case 9: PORTD &=~(1<<A_D);PORTB &=~(1<<B_B);PORTC &=~(1<<C_C);PORTC &=~(1<<D_C);PORTB |=(1<<E_B);PORTD &=~(1<<F_D);PORTB &=~(1<<G_B);break;
    case 10: PORTD |=(1<<A_D);PORTB |=(1<<B_B);PORTC |=(1<<C_C);PORTC |=(1<<D_C);PORTB |=(1<<E_B);PORTD |=(1<<F_D);PORTB |=(1<<G_B);break;
    }}

void anod(){
switch(an){                                                             
    case 0: PORTB |=(1<<G3_B);PORTD &=~(1<<G0_D);break;
    case 1: PORTD |=(1<<G0_D);PORTB &=~(1<<G1_B);break;
    case 2: PORTB |=(1<<G1_B);PORTB &=~(1<<G2_B);break;
    case 3: PORTB |=(1<<G2_B);PORTB &=~(1<<G3_B);break;                                                     
    }}   

ISR(TIMER1_COMPA_vect){msec++;if(msec>9){msec=0;}}

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

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); // СТОП
  }

int i2c_read_1bit(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)|(1<<TWEA);
  while(~TWCR&(1<<TWINT));
    byte i2c_data = TWDR;
  TWCR = (1<<TWINT) | (1<<TWEN);
  while (!(TWCR & (1<<TWINT)));

   TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWSTO); // СТОП
  return i2c_data;
  }

5

Re: Часы на ИВЛ1-7/5 на Atmega8

Тестирование датчика температуры

к PC0 подключите DS18B20

Мне надо посмотреть что померил датчик

Измерение температуры производится 1 раз в минуту в 0 секунд

Посмотрите что покажет индикатор

Должно быть типа 275 (27,5 гр)

Если все работает, как выводить температуру, сколько раз в минуту, в какое время минуты, как отображать

Варианты

27.5_С
27.5С_
_27.5С
27.5*C

В индикаторе есть точка и колокольчик, у Вас под каждый анод колокольчика и точки отдельный транзисторный ключ? По скетчу видно что колокольчик не должен работать, он в скетче не прописан, но должна работать точка, мигать в такт секунд.

По скетчу видно, что на точке нет транзисторного ключа, на точку просто подано напряжение через резистор, на анод точки постоянно подается напряжение, управление точкой осуществляется через сетку. Этот вопрос надо решать.

У Вас сейчас свободны пины PD3 PD4 , все остальное занято.

Анод точки надо пустить через транзисторный ключ, а сетку запитать на постоянку, и еще добавить ключ для колокольчика, остается всего один свободный пин. Его можно использовать для пищалки будильника.

Но это все позже, надо сначала с датчиком температуры разобраться.


// ATMEGA8 12MHz
#define G0_D       PD6
#define G1_B       PB1
#define G2_B       PB2
#define G3_B       PB5
#define DP_C       PC1 //PD SETKA 

#define E_B        PB3
#define G_B        PB4
#define B_B        PB0
#define F_D        PD7
#define A_D        PD5
#define D_C        PC2
#define C_C        PC3

#define SET        PD0
#define UP         PD1
#define DOWN       PD2

#define DS18B20    PC0

#include <avr/io.h>
#include <util/delay.h>

#define ADDR_DS1307    0b1101000
#define CPU_F          12000000 // Clock Speed
#define SCL_F          100000 // // I2C Speed

byte a[4],an,segm,i;
int msec,sec,minute,hour,set;
int set_sec,set_minute,set_hour;
int temp;

int main() {
  TWBR = (((CPU_F)/(SCL_F)-16 )/2) ;
  TWSR = 0;
// set_time(22,7,2,26,13,10,0);// год 00-99, ДН 1-7 (1=ВС), месяц 1-12, дата 1-31, час 0-23, минуты 0-59, секунды 0-59
  cli();
// (12000000/((18749+1)x64))=10 Hz
  OCR1A = 18749;
  TCCR1B |= (1 << WGM12);
// Prescaler 64
  TCCR1B |= (1 << CS11) | (1 << CS10);
  TIMSK |= (1 << OCIE1A);
// (12000000/((155+1)x256))=300.48076923077 Hz
  OCR2 = 155;
  TCCR2 |= (1 << WGM21);
// Prescaler 256
  TCCR2 |= (1 << CS22) | (1 << CS21);
  TIMSK |= (1 << OCIE2);
  sei();
  DDRB|=(1<<G1_B)|(1<<G2_B)|(1<<G3_B)|(1<<E_B)|(1<<G_B)|(1<<B_B);
  DDRC|=(1<<D_C)|(1<<C_C)|(1<<DP_C);
  DDRD|=(1<<G0_D)|(1<<F_D)|(1<<A_D);
  PORTD|=(1<<SET)|(1<<UP)|(1<<DOWN);


while(1) {
   sec =  (i2c_read_1bit(ADDR_DS1307,0) & 0x0F) + (((i2c_read_1bit(ADDR_DS1307,0) & 0x70) >> 4) * 10);
   minute =  (i2c_read_1bit(ADDR_DS1307,1) & 0x0F) + (((i2c_read_1bit(ADDR_DS1307,1) & 0x70) >> 4) * 10);
   hour = ((i2c_read_1bit(ADDR_DS1307,2) & 0x0F) + ((i2c_read_1bit(ADDR_DS1307,2) & 0x70) >> 4) * 10);   

   if(((PIND >> SET) & 1) == 0){set++;if(set>3){set=0;}_delay_ms(300);}

   set_sec=sec;
   set_minute=minute;
   set_hour=hour;

   if(((PIND >> UP) & 1) == 0 && set==1){set_hour++;if(set_hour>23){set_hour=23;}_delay_ms(300);set_time(255,255,255,255,set_hour,255,255);}
   if(((PIND >> DOWN) & 1) == 0 && set==1){set_hour--;if(set_hour<0){set_hour=0;}_delay_ms(300);set_time(255,255,255,255,set_hour,255,255);}

   if(((PIND >> UP) & 1) == 0 && set==2){set_minute++;if(set_minute>59){set_minute=59;}_delay_ms(300);set_time(255,255,255,255,255,set_minute,255);}
   if(((PIND >> DOWN) & 1) == 0 && set==2){set_minute--;if(set_minute<0){set_minute=0;}_delay_ms(300);set_time(255,255,255,255,255,set_minute,255);}

   if(((PIND >> UP) & 1) == 0 && set==3){set_sec=0;_delay_ms(300);set_time(255,255,255,255,255,255,set_sec);}
   if(((PIND >> DOWN) & 1) == 0 && set==3){set_sec=0;_delay_ms(300);set_time(255,255,255,255,255,255,set_sec);}

   if(sec==0){temp=read_temp();}

if(set==0){   
   a[3]=hour/10;
   a[2]=hour%10;
   a[1]=minute/10;
   a[0]=minute%10;
}

if(set==0){   
   a[3]=temp/1000%10;
   a[2]=temp/100%10;
   a[1]=temp/10%10;
   a[0]=temp%10;
}



   if(set==1&&(msec==0||msec==5)){a[3]=10;}else{a[3]=hour/10;}
   if(set==1&&(msec==0||msec==5)){a[2]=10;}else{a[2]=hour%10;}
   if(set==2&&(msec==0||msec==5)){a[1]=10;}else{a[1]=minute/10;}
   if(set==2&&(msec==0||msec==5)){a[0]=10;}else{a[0]=minute%10;}
if(set==3){
   if(msec==0||msec==5){a[1]=10;a[3]=10;a[2]=10;}else{a[3]=10;a[2]=10;a[1]=sec/10;}
   if(msec==0||msec==5){a[0]=10;a[3]=10;a[2]=10;}else{a[3]=10;a[2]=10;a[0]=sec%10;}
}
   if(msec<5){PORTC |=(1<<DP_C);}
   if(msec>=5){PORTC &=~(1<<DP_C);}

_delay_ms(100);
}}

ISR(TIMER2_COMP_vect){
    switch(i){
    case 0:  segm=a[0];  an=0;  anod();segment(); break;
    case 1:  segm=a[1];  an=1;  anod();segment(); break;
    case 2:  segm=a[2];  an=2;  anod();segment(); break;
    case 3:  segm=a[3];  an=3;  anod();segment(); break;
    }i++;if(i>3){i=0;}}

void segment(){                                                               
   switch(segm){                                                                 
    case 0: PORTD &=~(1<<A_D);PORTB &=~(1<<B_B);PORTC &=~(1<<C_C);PORTC &=~(1<<D_C);PORTB &=~(1<<E_B);PORTD &=~(1<<F_D);PORTB |=(1<<G_B);break;
    case 1: PORTD |=(1<<A_D);PORTB &=~(1<<B_B);PORTC &=~(1<<C_C);PORTC |=(1<<D_C);PORTB |=(1<<E_B);PORTD |=(1<<F_D);PORTB |=(1<<G_B);break;
    case 2: PORTD &=~(1<<A_D);PORTB &=~(1<<B_B);PORTC |=(1<<C_C);PORTC &=~(1<<D_C);PORTB &=~(1<<E_B);PORTD |=(1<<F_D);PORTB &=~(1<<G_B);break;
    case 3: PORTD &=~(1<<A_D);PORTB &=~(1<<B_B);PORTC &=~(1<<C_C);PORTC &=~(1<<D_C);PORTB |=(1<<E_B);PORTD |=(1<<F_D);PORTB &=~(1<<G_B);break;
    case 4: PORTD |=(1<<A_D);PORTB &=~(1<<B_B);PORTC &=~(1<<C_C);PORTC |=(1<<D_C);PORTB |=(1<<E_B);PORTD &=~(1<<F_D);PORTB &=~(1<<G_B);break;   
    case 5: PORTD &=~(1<<A_D);PORTB |=(1<<B_B);PORTC &=~(1<<C_C);PORTC &=~(1<<D_C);PORTB |=(1<<E_B);PORTD &=~(1<<F_D);PORTB &=~(1<<G_B);break;
    case 6: PORTD &=~(1<<A_D);PORTB |=(1<<B_B);PORTC &=~(1<<C_C);PORTC &=~(1<<D_C);PORTB &=~(1<<E_B);PORTD &=~(1<<F_D);PORTB &=~(1<<G_B);break;
    case 7: PORTD &=~(1<<A_D);PORTB &=~(1<<B_B);PORTC &=~(1<<C_C);PORTC |=(1<<D_C);PORTB |=(1<<E_B);PORTD |=(1<<F_D);PORTB |=(1<<G_B);break;
    case 8: PORTD &=~(1<<A_D);PORTB &=~(1<<B_B);PORTC &=~(1<<C_C);PORTC &=~(1<<D_C);PORTB &=~(1<<E_B);PORTD &=~(1<<F_D);PORTB &=~(1<<G_B);break;
    case 9: PORTD &=~(1<<A_D);PORTB &=~(1<<B_B);PORTC &=~(1<<C_C);PORTC &=~(1<<D_C);PORTB |=(1<<E_B);PORTD &=~(1<<F_D);PORTB &=~(1<<G_B);break;
    case 10: PORTD |=(1<<A_D);PORTB |=(1<<B_B);PORTC |=(1<<C_C);PORTC |=(1<<D_C);PORTB |=(1<<E_B);PORTD |=(1<<F_D);PORTB |=(1<<G_B);break;
    }}

void anod(){
switch(an){                                                             
    case 0: PORTB |=(1<<G3_B);PORTD &=~(1<<G0_D);break;
    case 1: PORTD |=(1<<G0_D);PORTB &=~(1<<G1_B);break;
    case 2: PORTB |=(1<<G1_B);PORTB &=~(1<<G2_B);break;
    case 3: PORTB |=(1<<G2_B);PORTB &=~(1<<G3_B);break;                                                     
    }}   

ISR(TIMER1_COMPA_vect){msec++;if(msec>9){msec=0;}}

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

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); // СТОП
  }

int i2c_read_1bit(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)|(1<<TWEA);
  while(~TWCR&(1<<TWINT));
    byte i2c_data = TWDR;
  TWCR = (1<<TWINT) | (1<<TWEN);
  while (!(TWCR & (1<<TWINT)));

   TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWSTO); // СТОП
  return i2c_data;
  }

// reset
uint8_t therm_reset(){
    uint8_t i;
    PORTC &= ~(1 << DS18B20);
    DDRC |= (1 << DS18B20);
    _delay_us(480);  
    DDRC &= ~(1 << DS18B20);
    _delay_us(60);
    i=((PINC >> DS18B20) & 1);
    _delay_us(420);
    return i;
}
// write bit
void therm_write_bit(uint8_t bit){
    PORTC &= ~(1 << DS18B20);
    DDRC |= (1 << DS18B20);
    _delay_us(1);
    if(bit) DDRC &= ~(1 << DS18B20);
    _delay_us(60);
    DDRC &= ~(1 << DS18B20);
}
// read bit
uint8_t therm_read_bit(void){
    uint8_t bit=0;
    PORTC &= ~(1 << DS18B20);
    DDRC |= (1 << DS18B20);
    _delay_us(1);
    DDRC &= ~(1 << DS18B20);
    _delay_us(14);
    if(PINC & (1 << DS18B20)) bit=1;
    _delay_us(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];
    float 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])/1.60;
    return (int)temper;
}  

6

Re: Часы на ИВЛ1-7/5 на Atmega8

почему то температуру не выводит.

7

Re: Часы на ИВЛ1-7/5 на Atmega8

датчик исправный. на +5В через 4к7 резистор подтянул с сигнального провода.

8

Re: Часы на ИВЛ1-7/5 на Atmega8

http://forum.rcl-radio.ru/uploads/images/2023/03/ba4ce75252d5c6cf62a29b452af7b523.jpg

9

Re: Часы на ИВЛ1-7/5 на Atmega8

Время не должно показывать.

// ATMEGA8 12MHz
#define G0_D       PD6
#define G1_B       PB1
#define G2_B       PB2
#define G3_B       PB5
#define DP_C       PC1 //PD SETKA 

#define E_B        PB3
#define G_B        PB4
#define B_B        PB0
#define F_D        PD7
#define A_D        PD5
#define D_C        PC2
#define C_C        PC3

#define SET        PD0
#define UP         PD1
#define DOWN       PD2

#define DS18B20    PC0

#include <avr/io.h>
#include <util/delay.h>

#define ADDR_DS1307    0b1101000
#define CPU_F          12000000 // Clock Speed
#define SCL_F          100000 // // I2C Speed

byte a[4],an,segm,i;
int msec,sec,minute,hour,set;
int set_sec,set_minute,set_hour;
int temp;

int main() {
  TWBR = (((CPU_F)/(SCL_F)-16 )/2) ;
  TWSR = 0;
// set_time(22,7,2,26,13,10,0);// год 00-99, ДН 1-7 (1=ВС), месяц 1-12, дата 1-31, час 0-23, минуты 0-59, секунды 0-59
  cli();
// (12000000/((18749+1)x64))=10 Hz
  OCR1A = 18749;
  TCCR1B |= (1 << WGM12);
// Prescaler 64
  TCCR1B |= (1 << CS11) | (1 << CS10);
  TIMSK |= (1 << OCIE1A);
// (12000000/((155+1)x256))=300.48076923077 Hz
  OCR2 = 155;
  TCCR2 |= (1 << WGM21);
// Prescaler 256
  TCCR2 |= (1 << CS22) | (1 << CS21);
  TIMSK |= (1 << OCIE2);
  sei();
  DDRB|=(1<<G1_B)|(1<<G2_B)|(1<<G3_B)|(1<<E_B)|(1<<G_B)|(1<<B_B);
  DDRC|=(1<<D_C)|(1<<C_C)|(1<<DP_C);
  DDRD|=(1<<G0_D)|(1<<F_D)|(1<<A_D);
  PORTD|=(1<<SET)|(1<<UP)|(1<<DOWN);


while(1) {
   sec =  (i2c_read_1bit(ADDR_DS1307,0) & 0x0F) + (((i2c_read_1bit(ADDR_DS1307,0) & 0x70) >> 4) * 10);
   minute =  (i2c_read_1bit(ADDR_DS1307,1) & 0x0F) + (((i2c_read_1bit(ADDR_DS1307,1) & 0x70) >> 4) * 10);
   hour = ((i2c_read_1bit(ADDR_DS1307,2) & 0x0F) + ((i2c_read_1bit(ADDR_DS1307,2) & 0x70) >> 4) * 10);   

   if(((PIND >> SET) & 1) == 0){set++;if(set>3){set=0;}_delay_ms(300);}

   set_sec=sec;
   set_minute=minute;
   set_hour=hour;

   if(((PIND >> UP) & 1) == 0 && set==1){set_hour++;if(set_hour>23){set_hour=23;}_delay_ms(300);set_time(255,255,255,255,set_hour,255,255);}
   if(((PIND >> DOWN) & 1) == 0 && set==1){set_hour--;if(set_hour<0){set_hour=0;}_delay_ms(300);set_time(255,255,255,255,set_hour,255,255);}

   if(((PIND >> UP) & 1) == 0 && set==2){set_minute++;if(set_minute>59){set_minute=59;}_delay_ms(300);set_time(255,255,255,255,255,set_minute,255);}
   if(((PIND >> DOWN) & 1) == 0 && set==2){set_minute--;if(set_minute<0){set_minute=0;}_delay_ms(300);set_time(255,255,255,255,255,set_minute,255);}

   if(((PIND >> UP) & 1) == 0 && set==3){set_sec=0;_delay_ms(300);set_time(255,255,255,255,255,255,set_sec);}
   if(((PIND >> DOWN) & 1) == 0 && set==3){set_sec=0;_delay_ms(300);set_time(255,255,255,255,255,255,set_sec);}

   temp=read_temp();
 
   a[3]=temp/1000%10;
   a[2]=temp/100%10;
   a[1]=temp/10%10;
   a[0]=temp%10;




   if(set==1&&(msec==0||msec==5)){a[3]=10;}else{a[3]=hour/10;}
   if(set==1&&(msec==0||msec==5)){a[2]=10;}else{a[2]=hour%10;}
   if(set==2&&(msec==0||msec==5)){a[1]=10;}else{a[1]=minute/10;}
   if(set==2&&(msec==0||msec==5)){a[0]=10;}else{a[0]=minute%10;}
if(set==3){
   if(msec==0||msec==5){a[1]=10;a[3]=10;a[2]=10;}else{a[3]=10;a[2]=10;a[1]=sec/10;}
   if(msec==0||msec==5){a[0]=10;a[3]=10;a[2]=10;}else{a[3]=10;a[2]=10;a[0]=sec%10;}
}
   if(msec<5){PORTC |=(1<<DP_C);}
   if(msec>=5){PORTC &=~(1<<DP_C);}

_delay_ms(100);
}}

ISR(TIMER2_COMP_vect){
    switch(i){
    case 0:  segm=a[0];  an=0;  anod();segment(); break;
    case 1:  segm=a[1];  an=1;  anod();segment(); break;
    case 2:  segm=a[2];  an=2;  anod();segment(); break;
    case 3:  segm=a[3];  an=3;  anod();segment(); break;
    }i++;if(i>3){i=0;}}

void segment(){                                                               
   switch(segm){                                                                 
    case 0: PORTD &=~(1<<A_D);PORTB &=~(1<<B_B);PORTC &=~(1<<C_C);PORTC &=~(1<<D_C);PORTB &=~(1<<E_B);PORTD &=~(1<<F_D);PORTB |=(1<<G_B);break;
    case 1: PORTD |=(1<<A_D);PORTB &=~(1<<B_B);PORTC &=~(1<<C_C);PORTC |=(1<<D_C);PORTB |=(1<<E_B);PORTD |=(1<<F_D);PORTB |=(1<<G_B);break;
    case 2: PORTD &=~(1<<A_D);PORTB &=~(1<<B_B);PORTC |=(1<<C_C);PORTC &=~(1<<D_C);PORTB &=~(1<<E_B);PORTD |=(1<<F_D);PORTB &=~(1<<G_B);break;
    case 3: PORTD &=~(1<<A_D);PORTB &=~(1<<B_B);PORTC &=~(1<<C_C);PORTC &=~(1<<D_C);PORTB |=(1<<E_B);PORTD |=(1<<F_D);PORTB &=~(1<<G_B);break;
    case 4: PORTD |=(1<<A_D);PORTB &=~(1<<B_B);PORTC &=~(1<<C_C);PORTC |=(1<<D_C);PORTB |=(1<<E_B);PORTD &=~(1<<F_D);PORTB &=~(1<<G_B);break;   
    case 5: PORTD &=~(1<<A_D);PORTB |=(1<<B_B);PORTC &=~(1<<C_C);PORTC &=~(1<<D_C);PORTB |=(1<<E_B);PORTD &=~(1<<F_D);PORTB &=~(1<<G_B);break;
    case 6: PORTD &=~(1<<A_D);PORTB |=(1<<B_B);PORTC &=~(1<<C_C);PORTC &=~(1<<D_C);PORTB &=~(1<<E_B);PORTD &=~(1<<F_D);PORTB &=~(1<<G_B);break;
    case 7: PORTD &=~(1<<A_D);PORTB &=~(1<<B_B);PORTC &=~(1<<C_C);PORTC |=(1<<D_C);PORTB |=(1<<E_B);PORTD |=(1<<F_D);PORTB |=(1<<G_B);break;
    case 8: PORTD &=~(1<<A_D);PORTB &=~(1<<B_B);PORTC &=~(1<<C_C);PORTC &=~(1<<D_C);PORTB &=~(1<<E_B);PORTD &=~(1<<F_D);PORTB &=~(1<<G_B);break;
    case 9: PORTD &=~(1<<A_D);PORTB &=~(1<<B_B);PORTC &=~(1<<C_C);PORTC &=~(1<<D_C);PORTB |=(1<<E_B);PORTD &=~(1<<F_D);PORTB &=~(1<<G_B);break;
    case 10: PORTD |=(1<<A_D);PORTB |=(1<<B_B);PORTC |=(1<<C_C);PORTC |=(1<<D_C);PORTB |=(1<<E_B);PORTD |=(1<<F_D);PORTB |=(1<<G_B);break;
    }}

void anod(){
switch(an){                                                             
    case 0: PORTB |=(1<<G3_B);PORTD &=~(1<<G0_D);break;
    case 1: PORTD |=(1<<G0_D);PORTB &=~(1<<G1_B);break;
    case 2: PORTB |=(1<<G1_B);PORTB &=~(1<<G2_B);break;
    case 3: PORTB |=(1<<G2_B);PORTB &=~(1<<G3_B);break;                                                     
    }}   

ISR(TIMER1_COMPA_vect){msec++;if(msec>9){msec=0;}}

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

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); // СТОП
  }

int i2c_read_1bit(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)|(1<<TWEA);
  while(~TWCR&(1<<TWINT));
    byte i2c_data = TWDR;
  TWCR = (1<<TWINT) | (1<<TWEN);
  while (!(TWCR & (1<<TWINT)));

   TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWSTO); // СТОП
  return i2c_data;
  }

// reset
uint8_t therm_reset(){
    uint8_t i;
    PORTC &= ~(1 << DS18B20);
    DDRC |= (1 << DS18B20);
    _delay_us(480);  
    DDRC &= ~(1 << DS18B20);
    _delay_us(60);
    i=((PINC >> DS18B20) & 1);
    _delay_us(420);
    return i;
}
// write bit
void therm_write_bit(uint8_t bit){
    PORTC &= ~(1 << DS18B20);
    DDRC |= (1 << DS18B20);
    _delay_us(1);
    if(bit) DDRC &= ~(1 << DS18B20);
    _delay_us(60);
    DDRC &= ~(1 << DS18B20);
}
// read bit
uint8_t therm_read_bit(void){
    uint8_t bit=0;
    PORTC &= ~(1 << DS18B20);
    DDRC |= (1 << DS18B20);
    _delay_us(1);
    DDRC &= ~(1 << DS18B20);
    _delay_us(14);
    if(PINC & (1 << DS18B20)) bit=1;
    _delay_us(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];
    float 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])/1.60;
    return (int)temper;
}  

10

Re: Часы на ИВЛ1-7/5 на Atmega8

27.5С такой бы формат температуры устроил. можно было бы без десятых пол шкалы где часы градусы в цифрах а пол школы верхний нолик и С большое.

11

Re: Часы на ИВЛ1-7/5 на Atmega8

21,51  это температура?

12

Re: Часы на ИВЛ1-7/5 на Atmega8

почему то время показывает и все. при прошивке засвет восьмерок подмаргивание. после пршивки только время идет и все.

13

Re: Часы на ИВЛ1-7/5 на Atmega8

http://forum.rcl-radio.ru/uploads/images/2023/03/bfa8e3c4b3e5616406ef31bb962e771d.png

14

Re: Часы на ИВЛ1-7/5 на Atmega8

Последний скетч загружали?

15 (2023-03-29 22:33:28 отредактировано klerik77)

Re: Часы на ИВЛ1-7/5 на Atmega8

да это я показал загруженный последний скетч, который в последнем окошке был.

16

Re: Часы на ИВЛ1-7/5 на Atmega8

Удалил вывод времени, должно показать температуру

// ATMEGA8 12MHz
#define G0_D       PD6
#define G1_B       PB1
#define G2_B       PB2
#define G3_B       PB5
#define DP_C       PC1 //PD SETKA 

#define E_B        PB3
#define G_B        PB4
#define B_B        PB0
#define F_D        PD7
#define A_D        PD5
#define D_C        PC2
#define C_C        PC3

#define SET        PD0
#define UP         PD1
#define DOWN       PD2

#define DS18B20    PC0

#include <avr/io.h>
#include <util/delay.h>

#define ADDR_DS1307    0b1101000
#define CPU_F          12000000 // Clock Speed
#define SCL_F          100000 // // I2C Speed

byte a[4],an,segm,i;
int msec,sec,minute,hour,set;
int set_sec,set_minute,set_hour;
int temp;

int main() {
  TWBR = (((CPU_F)/(SCL_F)-16 )/2) ;
  TWSR = 0;
// set_time(22,7,2,26,13,10,0);// год 00-99, ДН 1-7 (1=ВС), месяц 1-12, дата 1-31, час 0-23, минуты 0-59, секунды 0-59
  cli();
// (12000000/((18749+1)x64))=10 Hz
  OCR1A = 18749;
  TCCR1B |= (1 << WGM12);
// Prescaler 64
  TCCR1B |= (1 << CS11) | (1 << CS10);
  TIMSK |= (1 << OCIE1A);
// (12000000/((155+1)x256))=300.48076923077 Hz
  OCR2 = 155;
  TCCR2 |= (1 << WGM21);
// Prescaler 256
  TCCR2 |= (1 << CS22) | (1 << CS21);
  TIMSK |= (1 << OCIE2);
  sei();
  DDRB|=(1<<G1_B)|(1<<G2_B)|(1<<G3_B)|(1<<E_B)|(1<<G_B)|(1<<B_B);
  DDRC|=(1<<D_C)|(1<<C_C)|(1<<DP_C);
  DDRD|=(1<<G0_D)|(1<<F_D)|(1<<A_D);
  PORTD|=(1<<SET)|(1<<UP)|(1<<DOWN);


while(1) {
   sec =  (i2c_read_1bit(ADDR_DS1307,0) & 0x0F) + (((i2c_read_1bit(ADDR_DS1307,0) & 0x70) >> 4) * 10);
   minute =  (i2c_read_1bit(ADDR_DS1307,1) & 0x0F) + (((i2c_read_1bit(ADDR_DS1307,1) & 0x70) >> 4) * 10);
   hour = ((i2c_read_1bit(ADDR_DS1307,2) & 0x0F) + ((i2c_read_1bit(ADDR_DS1307,2) & 0x70) >> 4) * 10);   

   if(((PIND >> SET) & 1) == 0){set++;if(set>3){set=0;}_delay_ms(300);}

   set_sec=sec;
   set_minute=minute;
   set_hour=hour;

   if(((PIND >> UP) & 1) == 0 && set==1){set_hour++;if(set_hour>23){set_hour=23;}_delay_ms(300);set_time(255,255,255,255,set_hour,255,255);}
   if(((PIND >> DOWN) & 1) == 0 && set==1){set_hour--;if(set_hour<0){set_hour=0;}_delay_ms(300);set_time(255,255,255,255,set_hour,255,255);}

   if(((PIND >> UP) & 1) == 0 && set==2){set_minute++;if(set_minute>59){set_minute=59;}_delay_ms(300);set_time(255,255,255,255,255,set_minute,255);}
   if(((PIND >> DOWN) & 1) == 0 && set==2){set_minute--;if(set_minute<0){set_minute=0;}_delay_ms(300);set_time(255,255,255,255,255,set_minute,255);}

   if(((PIND >> UP) & 1) == 0 && set==3){set_sec=0;_delay_ms(300);set_time(255,255,255,255,255,255,set_sec);}
   if(((PIND >> DOWN) & 1) == 0 && set==3){set_sec=0;_delay_ms(300);set_time(255,255,255,255,255,255,set_sec);}

   temp=read_temp();
 
   a[3]=temp/1000%10;
   a[2]=temp/100%10;
   a[1]=temp/10%10;
   a[0]=temp%10;


   if(msec<5){PORTC |=(1<<DP_C);}
   if(msec>=5){PORTC &=~(1<<DP_C);}

_delay_ms(100);
}}

ISR(TIMER2_COMP_vect){
    switch(i){
    case 0:  segm=a[0];  an=0;  anod();segment(); break;
    case 1:  segm=a[1];  an=1;  anod();segment(); break;
    case 2:  segm=a[2];  an=2;  anod();segment(); break;
    case 3:  segm=a[3];  an=3;  anod();segment(); break;
    }i++;if(i>3){i=0;}}

void segment(){                                                               
   switch(segm){                                                                 
    case 0: PORTD &=~(1<<A_D);PORTB &=~(1<<B_B);PORTC &=~(1<<C_C);PORTC &=~(1<<D_C);PORTB &=~(1<<E_B);PORTD &=~(1<<F_D);PORTB |=(1<<G_B);break;
    case 1: PORTD |=(1<<A_D);PORTB &=~(1<<B_B);PORTC &=~(1<<C_C);PORTC |=(1<<D_C);PORTB |=(1<<E_B);PORTD |=(1<<F_D);PORTB |=(1<<G_B);break;
    case 2: PORTD &=~(1<<A_D);PORTB &=~(1<<B_B);PORTC |=(1<<C_C);PORTC &=~(1<<D_C);PORTB &=~(1<<E_B);PORTD |=(1<<F_D);PORTB &=~(1<<G_B);break;
    case 3: PORTD &=~(1<<A_D);PORTB &=~(1<<B_B);PORTC &=~(1<<C_C);PORTC &=~(1<<D_C);PORTB |=(1<<E_B);PORTD |=(1<<F_D);PORTB &=~(1<<G_B);break;
    case 4: PORTD |=(1<<A_D);PORTB &=~(1<<B_B);PORTC &=~(1<<C_C);PORTC |=(1<<D_C);PORTB |=(1<<E_B);PORTD &=~(1<<F_D);PORTB &=~(1<<G_B);break;   
    case 5: PORTD &=~(1<<A_D);PORTB |=(1<<B_B);PORTC &=~(1<<C_C);PORTC &=~(1<<D_C);PORTB |=(1<<E_B);PORTD &=~(1<<F_D);PORTB &=~(1<<G_B);break;
    case 6: PORTD &=~(1<<A_D);PORTB |=(1<<B_B);PORTC &=~(1<<C_C);PORTC &=~(1<<D_C);PORTB &=~(1<<E_B);PORTD &=~(1<<F_D);PORTB &=~(1<<G_B);break;
    case 7: PORTD &=~(1<<A_D);PORTB &=~(1<<B_B);PORTC &=~(1<<C_C);PORTC |=(1<<D_C);PORTB |=(1<<E_B);PORTD |=(1<<F_D);PORTB |=(1<<G_B);break;
    case 8: PORTD &=~(1<<A_D);PORTB &=~(1<<B_B);PORTC &=~(1<<C_C);PORTC &=~(1<<D_C);PORTB &=~(1<<E_B);PORTD &=~(1<<F_D);PORTB &=~(1<<G_B);break;
    case 9: PORTD &=~(1<<A_D);PORTB &=~(1<<B_B);PORTC &=~(1<<C_C);PORTC &=~(1<<D_C);PORTB |=(1<<E_B);PORTD &=~(1<<F_D);PORTB &=~(1<<G_B);break;
    case 10: PORTD |=(1<<A_D);PORTB |=(1<<B_B);PORTC |=(1<<C_C);PORTC |=(1<<D_C);PORTB |=(1<<E_B);PORTD |=(1<<F_D);PORTB |=(1<<G_B);break;
    }}

void anod(){
switch(an){                                                             
    case 0: PORTB |=(1<<G3_B);PORTD &=~(1<<G0_D);break;
    case 1: PORTD |=(1<<G0_D);PORTB &=~(1<<G1_B);break;
    case 2: PORTB |=(1<<G1_B);PORTB &=~(1<<G2_B);break;
    case 3: PORTB |=(1<<G2_B);PORTB &=~(1<<G3_B);break;                                                     
    }}   

ISR(TIMER1_COMPA_vect){msec++;if(msec>9){msec=0;}}

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

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); // СТОП
  }

int i2c_read_1bit(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)|(1<<TWEA);
  while(~TWCR&(1<<TWINT));
    byte i2c_data = TWDR;
  TWCR = (1<<TWINT) | (1<<TWEN);
  while (!(TWCR & (1<<TWINT)));

   TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWSTO); // СТОП
  return i2c_data;
  }

// reset
uint8_t therm_reset(){
    uint8_t i;
    PORTC &= ~(1 << DS18B20);
    DDRC |= (1 << DS18B20);
    _delay_us(480);  
    DDRC &= ~(1 << DS18B20);
    _delay_us(60);
    i=((PINC >> DS18B20) & 1);
    _delay_us(420);
    return i;
}
// write bit
void therm_write_bit(uint8_t bit){
    PORTC &= ~(1 << DS18B20);
    DDRC |= (1 << DS18B20);
    _delay_us(1);
    if(bit) DDRC &= ~(1 << DS18B20);
    _delay_us(60);
    DDRC &= ~(1 << DS18B20);
}
// read bit
uint8_t therm_read_bit(void){
    uint8_t bit=0;
    PORTC &= ~(1 << DS18B20);
    DDRC |= (1 << DS18B20);
    _delay_us(1);
    DDRC &= ~(1 << DS18B20);
    _delay_us(14);
    if(PINC & (1 << DS18B20)) bit=1;
    _delay_us(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];
    float 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])/1.60;
    return (int)temper;
}  

17

Re: Часы на ИВЛ1-7/5 на Atmega8

http://forum.rcl-radio.ru/uploads/images/2023/03/13a2086f0bf3bbce4666649d63ac7af2.jpg  Вот температура 26 градусов.

18

Re: Часы на ИВЛ1-7/5 на Atmega8

А если так:

// ATMEGA8 12MHz
#define G0_D       PD6
#define G1_B       PB1
#define G2_B       PB2
#define G3_B       PB5
#define DP_C       PC1 //PD SETKA 

#define E_B        PB3
#define G_B        PB4
#define B_B        PB0
#define F_D        PD7
#define A_D        PD5
#define D_C        PC2
#define C_C        PC3

#define SET        PD0
#define UP         PD1
#define DOWN       PD2

#define DS18B20    PC0

#include <avr/io.h>
#include <util/delay.h>

#define ADDR_DS1307    0b1101000
#define CPU_F          12000000 // Clock Speed
#define SCL_F          100000 // // I2C Speed

byte a[4],an,segm,i;
int msec,sec,minute,hour,set;
int set_sec,set_minute,set_hour;
int temp;

int main() {
  TWBR = (((CPU_F)/(SCL_F)-16 )/2) ;
  TWSR = 0;
// set_time(22,7,2,26,13,10,0);// год 00-99, ДН 1-7 (1=ВС), месяц 1-12, дата 1-31, час 0-23, минуты 0-59, секунды 0-59
  cli();
// (12000000/((18749+1)x64))=10 Hz
  OCR1A = 18749;
  TCCR1B |= (1 << WGM12);
// Prescaler 64
  TCCR1B |= (1 << CS11) | (1 << CS10);
  TIMSK |= (1 << OCIE1A);
// (12000000/((155+1)x256))=300.48076923077 Hz
  OCR2 = 155;
  TCCR2 |= (1 << WGM21);
// Prescaler 256
  TCCR2 |= (1 << CS22) | (1 << CS21);
  TIMSK |= (1 << OCIE2);
  sei();
  DDRB|=(1<<G1_B)|(1<<G2_B)|(1<<G3_B)|(1<<E_B)|(1<<G_B)|(1<<B_B);
  DDRC|=(1<<D_C)|(1<<C_C)|(1<<DP_C);
  DDRD|=(1<<G0_D)|(1<<F_D)|(1<<A_D);
  PORTD|=(1<<SET)|(1<<UP)|(1<<DOWN);


while(1) {
   sec =  (i2c_read_1bit(ADDR_DS1307,0) & 0x0F) + (((i2c_read_1bit(ADDR_DS1307,0) & 0x70) >> 4) * 10);
   minute =  (i2c_read_1bit(ADDR_DS1307,1) & 0x0F) + (((i2c_read_1bit(ADDR_DS1307,1) & 0x70) >> 4) * 10);
   hour = ((i2c_read_1bit(ADDR_DS1307,2) & 0x0F) + ((i2c_read_1bit(ADDR_DS1307,2) & 0x70) >> 4) * 10);   

   if(((PIND >> SET) & 1) == 0){set++;if(set>3){set=0;}_delay_ms(300);}

   set_sec=sec;
   set_minute=minute;
   set_hour=hour;

   if(((PIND >> UP) & 1) == 0 && set==1){set_hour++;if(set_hour>23){set_hour=23;}_delay_ms(300);set_time(255,255,255,255,set_hour,255,255);}
   if(((PIND >> DOWN) & 1) == 0 && set==1){set_hour--;if(set_hour<0){set_hour=0;}_delay_ms(300);set_time(255,255,255,255,set_hour,255,255);}

   if(((PIND >> UP) & 1) == 0 && set==2){set_minute++;if(set_minute>59){set_minute=59;}_delay_ms(300);set_time(255,255,255,255,255,set_minute,255);}
   if(((PIND >> DOWN) & 1) == 0 && set==2){set_minute--;if(set_minute<0){set_minute=0;}_delay_ms(300);set_time(255,255,255,255,255,set_minute,255);}

   if(((PIND >> UP) & 1) == 0 && set==3){set_sec=0;_delay_ms(300);set_time(255,255,255,255,255,255,set_sec);}
   if(((PIND >> DOWN) & 1) == 0 && set==3){set_sec=0;_delay_ms(300);set_time(255,255,255,255,255,255,set_sec);}

   temp=read_temp();
 
   a[3]=temp/1000%10;
   a[2]=temp/100%10;
   a[1]=temp/10%10;
   a[0]=temp%10;


   if(msec<5){PORTC |=(1<<DP_C);}
   if(msec>=5){PORTC &=~(1<<DP_C);}

_delay_ms(100);
}}

ISR(TIMER2_COMP_vect){
    switch(i){
    case 0:  segm=a[0];  an=0;  anod();segment(); break;
    case 1:  segm=a[1];  an=1;  anod();segment(); break;
    case 2:  segm=a[2];  an=2;  anod();segment(); break;
    case 3:  segm=a[3];  an=3;  anod();segment(); break;
    }i++;if(i>3){i=0;}}

void segment(){                                                               
   switch(segm){                                                                 
    case 0: PORTD &=~(1<<A_D);PORTB &=~(1<<B_B);PORTC &=~(1<<C_C);PORTC &=~(1<<D_C);PORTB &=~(1<<E_B);PORTD &=~(1<<F_D);PORTB |=(1<<G_B);break;
    case 1: PORTD |=(1<<A_D);PORTB &=~(1<<B_B);PORTC &=~(1<<C_C);PORTC |=(1<<D_C);PORTB |=(1<<E_B);PORTD |=(1<<F_D);PORTB |=(1<<G_B);break;
    case 2: PORTD &=~(1<<A_D);PORTB &=~(1<<B_B);PORTC |=(1<<C_C);PORTC &=~(1<<D_C);PORTB &=~(1<<E_B);PORTD |=(1<<F_D);PORTB &=~(1<<G_B);break;
    case 3: PORTD &=~(1<<A_D);PORTB &=~(1<<B_B);PORTC &=~(1<<C_C);PORTC &=~(1<<D_C);PORTB |=(1<<E_B);PORTD |=(1<<F_D);PORTB &=~(1<<G_B);break;
    case 4: PORTD |=(1<<A_D);PORTB &=~(1<<B_B);PORTC &=~(1<<C_C);PORTC |=(1<<D_C);PORTB |=(1<<E_B);PORTD &=~(1<<F_D);PORTB &=~(1<<G_B);break;   
    case 5: PORTD &=~(1<<A_D);PORTB |=(1<<B_B);PORTC &=~(1<<C_C);PORTC &=~(1<<D_C);PORTB |=(1<<E_B);PORTD &=~(1<<F_D);PORTB &=~(1<<G_B);break;
    case 6: PORTD &=~(1<<A_D);PORTB |=(1<<B_B);PORTC &=~(1<<C_C);PORTC &=~(1<<D_C);PORTB &=~(1<<E_B);PORTD &=~(1<<F_D);PORTB &=~(1<<G_B);break;
    case 7: PORTD &=~(1<<A_D);PORTB &=~(1<<B_B);PORTC &=~(1<<C_C);PORTC |=(1<<D_C);PORTB |=(1<<E_B);PORTD |=(1<<F_D);PORTB |=(1<<G_B);break;
    case 8: PORTD &=~(1<<A_D);PORTB &=~(1<<B_B);PORTC &=~(1<<C_C);PORTC &=~(1<<D_C);PORTB &=~(1<<E_B);PORTD &=~(1<<F_D);PORTB &=~(1<<G_B);break;
    case 9: PORTD &=~(1<<A_D);PORTB &=~(1<<B_B);PORTC &=~(1<<C_C);PORTC &=~(1<<D_C);PORTB |=(1<<E_B);PORTD &=~(1<<F_D);PORTB &=~(1<<G_B);break;
    case 10: PORTD |=(1<<A_D);PORTB |=(1<<B_B);PORTC |=(1<<C_C);PORTC |=(1<<D_C);PORTB |=(1<<E_B);PORTD |=(1<<F_D);PORTB |=(1<<G_B);break;
    }}

void anod(){
switch(an){                                                             
    case 0: PORTB |=(1<<G3_B);PORTD &=~(1<<G0_D);break;
    case 1: PORTD |=(1<<G0_D);PORTB &=~(1<<G1_B);break;
    case 2: PORTB |=(1<<G1_B);PORTB &=~(1<<G2_B);break;
    case 3: PORTB |=(1<<G2_B);PORTB &=~(1<<G3_B);break;                                                     
    }}   

ISR(TIMER1_COMPA_vect){msec++;if(msec>9){msec=0;}}

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

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); // СТОП
  }

int i2c_read_1bit(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)|(1<<TWEA);
  while(~TWCR&(1<<TWINT));
    byte i2c_data = TWDR;
  TWCR = (1<<TWINT) | (1<<TWEN);
  while (!(TWCR & (1<<TWINT)));

   TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWSTO); // СТОП
  return i2c_data;
  }

// reset
uint8_t therm_reset(){
    uint8_t i;
    PORTC &= ~(1 << DS18B20);
    DDRC |= (1 << DS18B20);
    _delay_us(480);  
    DDRC &= ~(1 << DS18B20);
    _delay_us(60);
    i=((PINC >> DS18B20) & 1);
    _delay_us(420);
    return i;
}
// write bit
void therm_write_bit(uint8_t bit){
    PORTC &= ~(1 << DS18B20);
    DDRC |= (1 << DS18B20);
    _delay_us(1);
    if(bit) DDRC &= ~(1 << DS18B20);
    _delay_us(60);
    DDRC &= ~(1 << DS18B20);
}
// read bit
uint8_t therm_read_bit(void){
    uint8_t bit=0;
    PORTC &= ~(1 << DS18B20);
    DDRC |= (1 << DS18B20);
    _delay_us(1);
    DDRC &= ~(1 << DS18B20);
    _delay_us(14);
    if(PINC & (1 << DS18B20)) bit=1;
    _delay_us(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];
    float 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])/0.16;
    return (int)temper;
}  

19

Re: Часы на ИВЛ1-7/5 на Atmega8

http://forum.rcl-radio.ru/uploads/images/2023/03/99c12d82700b606233fbccbf6a792546.jpg теперь так отображается уже лучше.25,87 градусов

20

Re: Часы на ИВЛ1-7/5 на Atmega8

Проверьте формат температуры 27*C, точка не должна гореть.

// ATMEGA8 12MHz
#define G0_D       PD6
#define G1_B       PB1
#define G2_B       PB2
#define G3_B       PB5
#define DP_C       PC1 //PD SETKA 

#define E_B        PB3
#define G_B        PB4
#define B_B        PB0
#define F_D        PD7
#define A_D        PD5
#define D_C        PC2
#define C_C        PC3

#define SET        PD0
#define UP         PD1
#define DOWN       PD2

#define DS18B20    PC0

#include <avr/io.h>
#include <util/delay.h>

#define ADDR_DS1307    0b1101000
#define CPU_F          12000000 // Clock Speed
#define SCL_F          100000 // // I2C Speed

byte a[4],an,segm,i;
int msec,sec,minute,hour,set;
int set_sec,set_minute,set_hour;
int temp;

int main() {
  TWBR = (((CPU_F)/(SCL_F)-16 )/2) ;
  TWSR = 0;
// set_time(22,7,2,26,13,10,0);// год 00-99, ДН 1-7 (1=ВС), месяц 1-12, дата 1-31, час 0-23, минуты 0-59, секунды 0-59
  cli();
// (12000000/((18749+1)x64))=10 Hz
  OCR1A = 18749;
  TCCR1B |= (1 << WGM12);
// Prescaler 64
  TCCR1B |= (1 << CS11) | (1 << CS10);
  TIMSK |= (1 << OCIE1A);
// (12000000/((155+1)x256))=300.48076923077 Hz
  OCR2 = 155;
  TCCR2 |= (1 << WGM21);
// Prescaler 256
  TCCR2 |= (1 << CS22) | (1 << CS21);
  TIMSK |= (1 << OCIE2);
  sei();
  DDRB|=(1<<G1_B)|(1<<G2_B)|(1<<G3_B)|(1<<E_B)|(1<<G_B)|(1<<B_B);
  DDRC|=(1<<D_C)|(1<<C_C)|(1<<DP_C);
  DDRD|=(1<<G0_D)|(1<<F_D)|(1<<A_D);
  PORTD|=(1<<SET)|(1<<UP)|(1<<DOWN);


while(1) {
   sec =  (i2c_read_1bit(ADDR_DS1307,0) & 0x0F) + (((i2c_read_1bit(ADDR_DS1307,0) & 0x70) >> 4) * 10);
   minute =  (i2c_read_1bit(ADDR_DS1307,1) & 0x0F) + (((i2c_read_1bit(ADDR_DS1307,1) & 0x70) >> 4) * 10);
   hour = ((i2c_read_1bit(ADDR_DS1307,2) & 0x0F) + ((i2c_read_1bit(ADDR_DS1307,2) & 0x70) >> 4) * 10);   

   if(((PIND >> SET) & 1) == 0){set++;if(set>3){set=0;}_delay_ms(300);}

   set_sec=sec;
   set_minute=minute;
   set_hour=hour;

   if(((PIND >> UP) & 1) == 0 && set==1){set_hour++;if(set_hour>23){set_hour=23;}_delay_ms(300);set_time(255,255,255,255,set_hour,255,255);}
   if(((PIND >> DOWN) & 1) == 0 && set==1){set_hour--;if(set_hour<0){set_hour=0;}_delay_ms(300);set_time(255,255,255,255,set_hour,255,255);}

   if(((PIND >> UP) & 1) == 0 && set==2){set_minute++;if(set_minute>59){set_minute=59;}_delay_ms(300);set_time(255,255,255,255,255,set_minute,255);}
   if(((PIND >> DOWN) & 1) == 0 && set==2){set_minute--;if(set_minute<0){set_minute=0;}_delay_ms(300);set_time(255,255,255,255,255,set_minute,255);}

   if(((PIND >> UP) & 1) == 0 && set==3){set_sec=0;_delay_ms(300);set_time(255,255,255,255,255,255,set_sec);}
   if(((PIND >> DOWN) & 1) == 0 && set==3){set_sec=0;_delay_ms(300);set_time(255,255,255,255,255,255,set_sec);}

   temp=read_temp();
     a[3]=temp/1000%10;
     a[2]=temp/100%10;
     a[1]=11;
     a[0]=12;
   PORTC |=(1<<DP_C);


   //if(msec<5){PORTC |=(1<<DP_C);}
   //if(msec>=5){PORTC &=~(1<<DP_C);}

_delay_ms(100);
}}

ISR(TIMER2_COMP_vect){
    switch(i){
    case 0:  segm=a[0];  an=0;  anod();segment(); break;
    case 1:  segm=a[1];  an=1;  anod();segment(); break;
    case 2:  segm=a[2];  an=2;  anod();segment(); break;
    case 3:  segm=a[3];  an=3;  anod();segment(); break;
    }i++;if(i>3){i=0;}}

void segment(){                                                               
   switch(segm){                                                                 
    case 0: PORTD &=~(1<<A_D);PORTB &=~(1<<B_B);PORTC &=~(1<<C_C);PORTC &=~(1<<D_C);PORTB &=~(1<<E_B);PORTD &=~(1<<F_D);PORTB |=(1<<G_B);break;
    case 1: PORTD |=(1<<A_D);PORTB &=~(1<<B_B);PORTC &=~(1<<C_C);PORTC |=(1<<D_C);PORTB |=(1<<E_B);PORTD |=(1<<F_D);PORTB |=(1<<G_B);break;
    case 2: PORTD &=~(1<<A_D);PORTB &=~(1<<B_B);PORTC |=(1<<C_C);PORTC &=~(1<<D_C);PORTB &=~(1<<E_B);PORTD |=(1<<F_D);PORTB &=~(1<<G_B);break;
    case 3: PORTD &=~(1<<A_D);PORTB &=~(1<<B_B);PORTC &=~(1<<C_C);PORTC &=~(1<<D_C);PORTB |=(1<<E_B);PORTD |=(1<<F_D);PORTB &=~(1<<G_B);break;
    case 4: PORTD |=(1<<A_D);PORTB &=~(1<<B_B);PORTC &=~(1<<C_C);PORTC |=(1<<D_C);PORTB |=(1<<E_B);PORTD &=~(1<<F_D);PORTB &=~(1<<G_B);break;   
    case 5: PORTD &=~(1<<A_D);PORTB |=(1<<B_B);PORTC &=~(1<<C_C);PORTC &=~(1<<D_C);PORTB |=(1<<E_B);PORTD &=~(1<<F_D);PORTB &=~(1<<G_B);break;
    case 6: PORTD &=~(1<<A_D);PORTB |=(1<<B_B);PORTC &=~(1<<C_C);PORTC &=~(1<<D_C);PORTB &=~(1<<E_B);PORTD &=~(1<<F_D);PORTB &=~(1<<G_B);break;
    case 7: PORTD &=~(1<<A_D);PORTB &=~(1<<B_B);PORTC &=~(1<<C_C);PORTC |=(1<<D_C);PORTB |=(1<<E_B);PORTD |=(1<<F_D);PORTB |=(1<<G_B);break;
    case 8: PORTD &=~(1<<A_D);PORTB &=~(1<<B_B);PORTC &=~(1<<C_C);PORTC &=~(1<<D_C);PORTB &=~(1<<E_B);PORTD &=~(1<<F_D);PORTB &=~(1<<G_B);break;
    case 9: PORTD &=~(1<<A_D);PORTB &=~(1<<B_B);PORTC &=~(1<<C_C);PORTC &=~(1<<D_C);PORTB |=(1<<E_B);PORTD &=~(1<<F_D);PORTB &=~(1<<G_B);break;
    case 10: PORTD |=(1<<A_D);PORTB |=(1<<B_B);PORTC |=(1<<C_C);PORTC |=(1<<D_C);PORTB |=(1<<E_B);PORTD |=(1<<F_D);PORTB |=(1<<G_B);break;// Пусто
    case 11: PORTD &=~(1<<A_D);PORTB &=~(1<<B_B);PORTC |=(1<<C_C);PORTC |=(1<<D_C);PORTB |=(1<<E_B);PORTD &=~(1<<F_D);PORTB &=~(1<<G_B);break;// Градус
    case 12: PORTD &=~(1<<A_D);PORTB |=(1<<B_B);PORTC |=(1<<C_C);PORTC &=~(1<<D_C);PORTB &=~(1<<E_B);PORTD &=~(1<<F_D);PORTB |=(1<<G_B);break;// С
    
    
    }}

void anod(){
switch(an){                                                             
    case 0: PORTB |=(1<<G3_B);PORTD &=~(1<<G0_D);break;
    case 1: PORTD |=(1<<G0_D);PORTB &=~(1<<G1_B);break;
    case 2: PORTB |=(1<<G1_B);PORTB &=~(1<<G2_B);break;
    case 3: PORTB |=(1<<G2_B);PORTB &=~(1<<G3_B);break;                                                     
    }}   

ISR(TIMER1_COMPA_vect){msec++;if(msec>9){msec=0;}}

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

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); // СТОП
  }

int i2c_read_1bit(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)|(1<<TWEA);
  while(~TWCR&(1<<TWINT));
    byte i2c_data = TWDR;
  TWCR = (1<<TWINT) | (1<<TWEN);
  while (!(TWCR & (1<<TWINT)));

   TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWSTO); // СТОП
  return i2c_data;
  }

// reset
uint8_t therm_reset(){
    uint8_t i;
    PORTC &= ~(1 << DS18B20);
    DDRC |= (1 << DS18B20);
    _delay_us(480);  
    DDRC &= ~(1 << DS18B20);
    _delay_us(60);
    i=((PINC >> DS18B20) & 1);
    _delay_us(420);
    return i;
}
// write bit
void therm_write_bit(uint8_t bit){
    PORTC &= ~(1 << DS18B20);
    DDRC |= (1 << DS18B20);
    _delay_us(1);
    if(bit) DDRC &= ~(1 << DS18B20);
    _delay_us(60);
    DDRC &= ~(1 << DS18B20);
}
// read bit
uint8_t therm_read_bit(void){
    uint8_t bit=0;
    PORTC &= ~(1 << DS18B20);
    DDRC |= (1 << DS18B20);
    _delay_us(1);
    DDRC &= ~(1 << DS18B20);
    _delay_us(14);
    if(PINC & (1 << DS18B20)) bit=1;
    _delay_us(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];
    float 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])/0.16;
    return (int)temper;
}  

21

Re: Часы на ИВЛ1-7/5 на Atmega8

http://forum.rcl-radio.ru/uploads/images/2023/03/9083523824fdb832ae0ee4bcdf7e5c82.jpg Вот это класно смотрится! мне нравится! терь бы 10 секунд время отображало а 3 сек температуру smile))

22

Re: Часы на ИВЛ1-7/5 на Atmega8

Протестируйте вывод температуры и времени:

// ATMEGA8 12MHz
#define G0_D       PD6
#define G1_B       PB1
#define G2_B       PB2
#define G3_B       PB5
#define DP_C       PC1 //PD SETKA 

#define E_B        PB3
#define G_B        PB4
#define B_B        PB0
#define F_D        PD7
#define A_D        PD5
#define D_C        PC2
#define C_C        PC3

#define SET        PD0
#define UP         PD1
#define DOWN       PD2

#define DS18B20    PC0

#include <avr/io.h>
#include <util/delay.h>

#define ADDR_DS1307    0b1101000
#define CPU_F          12000000 // Clock Speed
#define SCL_F          100000 // // I2C Speed

byte a[4],an,segm,i;
int msec,sec,minute,hour,set;
int set_sec,set_minute,set_hour;
int temp;
int w;

int main() {
  TWBR = (((CPU_F)/(SCL_F)-16 )/2) ;
  TWSR = 0;
// set_time(22,7,2,26,13,10,0);// год 00-99, ДН 1-7 (1=ВС), месяц 1-12, дата 1-31, час 0-23, минуты 0-59, секунды 0-59
  cli();
// (12000000/((18749+1)x64))=10 Hz
  OCR1A = 18749;
  TCCR1B |= (1 << WGM12);
// Prescaler 64
  TCCR1B |= (1 << CS11) | (1 << CS10);
  TIMSK |= (1 << OCIE1A);
// (12000000/((155+1)x256))=300.48076923077 Hz
  OCR2 = 155;
  TCCR2 |= (1 << WGM21);
// Prescaler 256
  TCCR2 |= (1 << CS22) | (1 << CS21);
  TIMSK |= (1 << OCIE2);
  sei();
  DDRB|=(1<<G1_B)|(1<<G2_B)|(1<<G3_B)|(1<<E_B)|(1<<G_B)|(1<<B_B);
  DDRC|=(1<<D_C)|(1<<C_C)|(1<<DP_C);
  DDRD|=(1<<G0_D)|(1<<F_D)|(1<<A_D);
  PORTD|=(1<<SET)|(1<<UP)|(1<<DOWN);


while(1) {
   sec =  (i2c_read_1bit(ADDR_DS1307,0) & 0x0F) + (((i2c_read_1bit(ADDR_DS1307,0) & 0x70) >> 4) * 10);
   minute =  (i2c_read_1bit(ADDR_DS1307,1) & 0x0F) + (((i2c_read_1bit(ADDR_DS1307,1) & 0x70) >> 4) * 10);
   hour = ((i2c_read_1bit(ADDR_DS1307,2) & 0x0F) + ((i2c_read_1bit(ADDR_DS1307,2) & 0x70) >> 4) * 10);   

   if(((PIND >> SET) & 1) == 0){set++;if(set>3){set=0;}_delay_ms(300);}

   set_sec=sec;
   set_minute=minute;
   set_hour=hour;

   if(((PIND >> UP) & 1) == 0 && set==1){set_hour++;if(set_hour>23){set_hour=23;}_delay_ms(300);set_time(255,255,255,255,set_hour,255,255);}
   if(((PIND >> DOWN) & 1) == 0 && set==1){set_hour--;if(set_hour<0){set_hour=0;}_delay_ms(300);set_time(255,255,255,255,set_hour,255,255);}

   if(((PIND >> UP) & 1) == 0 && set==2){set_minute++;if(set_minute>59){set_minute=59;}_delay_ms(300);set_time(255,255,255,255,255,set_minute,255);}
   if(((PIND >> DOWN) & 1) == 0 && set==2){set_minute--;if(set_minute<0){set_minute=0;}_delay_ms(300);set_time(255,255,255,255,255,set_minute,255);}

   if(((PIND >> UP) & 1) == 0 && set==3){set_sec=0;_delay_ms(300);set_time(255,255,255,255,255,255,set_sec);}
   if(((PIND >> DOWN) & 1) == 0 && set==3){set_sec=0;_delay_ms(300);set_time(255,255,255,255,255,255,set_sec);}

   if(sec%10>=0&&sec%10<=3){
    temp=read_temp();
     a[3]=temp/1000%10;
     a[2]=temp/100%10;
     a[1]=11;
     a[0]=12;
   PORTC |=(1<<DP_C);
    }
    else{
     a[3]=hour/10;
     a[2]=hour%10;
     a[1]=minute/10;
     a[0]=minute%10;
   if(msec<5){PORTC |=(1<<DP_C);}
   if(msec>=5){PORTC &=~(1<<DP_C);}
   }


_delay_ms(100);
}}

ISR(TIMER2_COMP_vect){
    switch(i){
    case 0:  segm=a[0];  an=0;  anod();segment(); break;
    case 1:  segm=a[1];  an=1;  anod();segment(); break;
    case 2:  segm=a[2];  an=2;  anod();segment(); break;
    case 3:  segm=a[3];  an=3;  anod();segment(); break;
    }i++;if(i>3){i=0;}}

void segment(){                                                               
   switch(segm){                                                                 
    case 0: PORTD &=~(1<<A_D);PORTB &=~(1<<B_B);PORTC &=~(1<<C_C);PORTC &=~(1<<D_C);PORTB &=~(1<<E_B);PORTD &=~(1<<F_D);PORTB |=(1<<G_B);break;
    case 1: PORTD |=(1<<A_D);PORTB &=~(1<<B_B);PORTC &=~(1<<C_C);PORTC |=(1<<D_C);PORTB |=(1<<E_B);PORTD |=(1<<F_D);PORTB |=(1<<G_B);break;
    case 2: PORTD &=~(1<<A_D);PORTB &=~(1<<B_B);PORTC |=(1<<C_C);PORTC &=~(1<<D_C);PORTB &=~(1<<E_B);PORTD |=(1<<F_D);PORTB &=~(1<<G_B);break;
    case 3: PORTD &=~(1<<A_D);PORTB &=~(1<<B_B);PORTC &=~(1<<C_C);PORTC &=~(1<<D_C);PORTB |=(1<<E_B);PORTD |=(1<<F_D);PORTB &=~(1<<G_B);break;
    case 4: PORTD |=(1<<A_D);PORTB &=~(1<<B_B);PORTC &=~(1<<C_C);PORTC |=(1<<D_C);PORTB |=(1<<E_B);PORTD &=~(1<<F_D);PORTB &=~(1<<G_B);break;   
    case 5: PORTD &=~(1<<A_D);PORTB |=(1<<B_B);PORTC &=~(1<<C_C);PORTC &=~(1<<D_C);PORTB |=(1<<E_B);PORTD &=~(1<<F_D);PORTB &=~(1<<G_B);break;
    case 6: PORTD &=~(1<<A_D);PORTB |=(1<<B_B);PORTC &=~(1<<C_C);PORTC &=~(1<<D_C);PORTB &=~(1<<E_B);PORTD &=~(1<<F_D);PORTB &=~(1<<G_B);break;
    case 7: PORTD &=~(1<<A_D);PORTB &=~(1<<B_B);PORTC &=~(1<<C_C);PORTC |=(1<<D_C);PORTB |=(1<<E_B);PORTD |=(1<<F_D);PORTB |=(1<<G_B);break;
    case 8: PORTD &=~(1<<A_D);PORTB &=~(1<<B_B);PORTC &=~(1<<C_C);PORTC &=~(1<<D_C);PORTB &=~(1<<E_B);PORTD &=~(1<<F_D);PORTB &=~(1<<G_B);break;
    case 9: PORTD &=~(1<<A_D);PORTB &=~(1<<B_B);PORTC &=~(1<<C_C);PORTC &=~(1<<D_C);PORTB |=(1<<E_B);PORTD &=~(1<<F_D);PORTB &=~(1<<G_B);break;
    case 10: PORTD |=(1<<A_D);PORTB |=(1<<B_B);PORTC |=(1<<C_C);PORTC |=(1<<D_C);PORTB |=(1<<E_B);PORTD |=(1<<F_D);PORTB |=(1<<G_B);break;// Пусто
    case 11: PORTD &=~(1<<A_D);PORTB &=~(1<<B_B);PORTC |=(1<<C_C);PORTC |=(1<<D_C);PORTB |=(1<<E_B);PORTD &=~(1<<F_D);PORTB &=~(1<<G_B);break;// Градус
    case 12: PORTD &=~(1<<A_D);PORTB |=(1<<B_B);PORTC |=(1<<C_C);PORTC &=~(1<<D_C);PORTB &=~(1<<E_B);PORTD &=~(1<<F_D);PORTB |=(1<<G_B);break;// С
    
    
    }}

void anod(){
switch(an){                                                             
    case 0: PORTB |=(1<<G3_B);PORTD &=~(1<<G0_D);break;
    case 1: PORTD |=(1<<G0_D);PORTB &=~(1<<G1_B);break;
    case 2: PORTB |=(1<<G1_B);PORTB &=~(1<<G2_B);break;
    case 3: PORTB |=(1<<G2_B);PORTB &=~(1<<G3_B);break;                                                     
    }}   

ISR(TIMER1_COMPA_vect){msec++;if(msec>9){msec=0;}}

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

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); // СТОП
  }

int i2c_read_1bit(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)|(1<<TWEA);
  while(~TWCR&(1<<TWINT));
    byte i2c_data = TWDR;
  TWCR = (1<<TWINT) | (1<<TWEN);
  while (!(TWCR & (1<<TWINT)));

   TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWSTO); // СТОП
  return i2c_data;
  }

// reset
uint8_t therm_reset(){
    uint8_t i;
    PORTC &= ~(1 << DS18B20);
    DDRC |= (1 << DS18B20);
    _delay_us(480);  
    DDRC &= ~(1 << DS18B20);
    _delay_us(60);
    i=((PINC >> DS18B20) & 1);
    _delay_us(420);
    return i;
}
// write bit
void therm_write_bit(uint8_t bit){
    PORTC &= ~(1 << DS18B20);
    DDRC |= (1 << DS18B20);
    _delay_us(1);
    if(bit) DDRC &= ~(1 << DS18B20);
    _delay_us(60);
    DDRC &= ~(1 << DS18B20);
}
// read bit
uint8_t therm_read_bit(void){
    uint8_t bit=0;
    PORTC &= ~(1 << DS18B20);
    DDRC |= (1 << DS18B20);
    _delay_us(1);
    DDRC &= ~(1 << DS18B20);
    _delay_us(14);
    if(PINC & (1 << DS18B20)) bit=1;
    _delay_us(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];
    float 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])/0.16;
    return (int)temper;
}  

23

Re: Часы на ИВЛ1-7/5 на Atmega8

пока только температуру выводит

24

Re: Часы на ИВЛ1-7/5 на Atmega8

Должно работать, Вы уверены что скетч загрузился?

25

Re: Часы на ИВЛ1-7/5 на Atmega8

да два раза загружал.