26

Re: Часы на игп-17 (разработка)

Датчик в виде модуля или без него?

27

Re: Часы на игп-17 (разработка)

Датчик подключен по стандартной схеме к входу РD0 (вывод 9 микросхемы в корпусе TQFP), с подтягивающим резистором 4,7 кОм

28

Re: Часы на игп-17 (разработка)

Датчик без модуля.

29

Re: Часы на игп-17 (разработка)

Pinout в Arduino IDE - standart?

30

Re: Часы на игп-17 (разработка)

да, установки по умолчанию

31

Re: Часы на игп-17 (разработка)

Датчик точно исправен, есть возможность его проверить?

32

Re: Часы на игп-17 (разработка)

Датчик проверил, исправен

33

Re: Часы на игп-17 (разработка)

if(sec==10||sec==40) temper = dsRead(0)-TEMP_KORR ;

Если закомментировать эту строку зависание уходит?

34 (2024-01-20 23:38:20 отредактировано aprdetali)

Re: Часы на игп-17 (разработка)

Да, при закомментированной строке зависание пропало. Появилась децимальная точка между второй и третьей цифрами значения температуры: t 00.0“C

35

Re: Часы на игп-17 (разработка)

// ATMEGA8535 16 MHZ
#define ADDR    0x68
#define CPU_F   16000000 // Clock Speed
#define SCL_F   400000 // // I2C Speed
#define TEMP_KORR 20 // -2гр.Цельсия

#include <OneWire.h>            // http://rcl-radio.ru/wp-content/uploads/2018/07/OneWire.zip
#include <avr/io.h>
#include <util/delay.h>
OneWire  ds(8); // Вход датчика 18b20  PD8 PIN_14

byte a[16],an,segm,i;
bool w=1,w1;
byte sec,min_,hour,datat,mont,year;
int temper,sett,times;
int set_hour,set_min,set_sec,set_datat,set_mont,set_year;


void setup() {
  DDRA = 0xFF;
  DDRB = 0xFF;
  DDRC = 0xFF;
  DDRD |= (1 << PD6)|(1 << PD7);
  PORTD |= (1 << PD2)|(1 << PD3)|(1 << PD4);
  TWBR = (((CPU_F)/(SCL_F)-16 )/2) ;
  TWSR = 0;
  DDRD |= (1 << PD5); // PWM +180V TIMER_1
  cli();
// TIMER_2  F = 16000000/128/100=1250 Hz (800 uS)
  OCR2 = 100;
  TCCR2 |= (1 << WGM21);
  TCCR2 |= (1 << CS22) | (1 << CS20);
  TIMSK |= (1 << OCIE2);
// TIMER_1  F = 16000000/1024/1/1=15625 Hz
  TCCR1A |= (1 << COM1A1);
  TCCR1B |= (1 << WGM12);
  TCCR1A |= (1 << WGM11) | (1 << WGM10);
  TCCR1B |= (1 << CS10); 
  ICR1 = 1023; 
  OCR1A = 350; 
  sei();
 // set_time(22,2,2,4,22,23,0);// год 00-99, ДН 1-7 (1=ВС), месяц 1-12, дата 1-31, час 0-23, минуты 0-59, секунды 0-59
  _delay_ms(200); 
}   

void loop() { 
  datat = ((i2c_read(ADDR,4) & 0x0F) + ((i2c_read(ADDR,4) & 0x70) >> 4) * 10);
  mont = ((i2c_read(ADDR,5) & 0x0F) + ((i2c_read(ADDR,5) & 0x70) >> 4) * 10);
  year = ((i2c_read(ADDR,6) & 0x0F) + ((i2c_read(ADDR,6) & 0x70) >> 4) * 10); 
  hour = (i2c_read(ADDR,2) & 0x0F) + (((i2c_read(ADDR,2) & 0x70) >> 4) * 10);
  min_ =  (i2c_read(ADDR,1) & 0x0F) + (((i2c_read(ADDR,1) & 0x70) >> 4) * 10);
  if(sec==10||sec==40) {temper = dsRead(0)-TEMP_KORR ;}
  sec =  (i2c_read(ADDR,0) & 0x0F) + (((i2c_read(ADDR,0) & 0x70) >> 4) * 10);
  set_year = year;set_mont = mont;set_datat = datat;set_hour = hour;set_min = min_;set_sec = sec;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/// BUTTON /////////////  
  if(((PIND >> PD2) & 1) == 0){sett++;times=0;if(sett>6){sett=0;}wr();_delay_ms(300);}
  
  if(((PIND >> PD3) & 1) == 0 && sett==1){set_hour++;w1=1; if(set_hour>23){set_hour=23;} set_time(set_year,0,set_mont,set_datat,set_hour,set_min,set_sec);wr();_delay_ms(300);}
  if(((PIND >> PD4) & 1) == 0 && sett==1){set_hour--;w1=1; if(set_hour<0){set_hour=0;} set_time(set_year,0,set_mont,set_datat,set_hour,set_min,set_sec);wr();_delay_ms(300);}

  if(((PIND >> PD3) & 1) == 0 && sett==2){set_min++;w1=1; if(set_mont>59){set_min=59;} set_time(set_year,0,set_mont,set_datat,set_hour,set_min,set_sec);wr();_delay_ms(300);}
  if(((PIND >> PD4) & 1) == 0 && sett==2){set_min--;w1=1; if(set_mont<0){set_min=0;} set_time(set_year,0,set_mont,set_datat,set_hour,set_min,set_sec);wr();_delay_ms(300);}

  if(((PIND >> PD3) & 1) == 0 && sett==3){set_sec=0;w1=1; set_time(set_year,0,set_mont,set_datat,set_hour,set_min,set_sec);wr();_delay_ms(300);}
  if(((PIND >> PD4) & 1) == 0 && sett==3){set_sec=0;w1=1; set_time(set_year,0,set_mont,set_datat,set_hour,set_min,set_sec);wr();_delay_ms(300);}

  if(((PIND >> PD3) & 1) == 0 && sett==4){set_datat++;w1=1; if(set_datat>31){set_datat=31;} set_time(set_year,0,set_mont,set_datat,set_hour,set_min,set_sec);wr();_delay_ms(300);}
  if(((PIND >> PD4) & 1) == 0 && sett==4){set_datat--;w1=1; if(set_datat<0){set_datat=0;} set_time(set_year,0,set_mont,set_datat,set_hour,set_min,set_sec);wr();_delay_ms(300);}

  if(((PIND >> PD3) & 1) == 0 && sett==5){set_mont++;w1=1; if(set_mont>12){set_mont=12;} set_time(set_year,0,set_mont,set_datat,set_hour,set_min,set_sec);wr();_delay_ms(300);}
  if(((PIND >> PD4) & 1) == 0 && sett==5){set_mont--;w1=1; if(set_mont<0){set_mont=0;} set_time(set_year,0,set_mont,set_datat,set_hour,set_min,set_sec);wr();_delay_ms(300);} 

  if(((PIND >> PD3) & 1) == 0 && sett==6){set_year++;w1=1; if(set_year>50){set_year=50;} set_time(set_year,0,set_mont,set_datat,set_hour,set_min,set_sec);wr();_delay_ms(300);}
  if(((PIND >> PD4) & 1) == 0 && sett==6){set_year--;w1=1; if(set_year<22){set_year=22;} set_time(set_year,0,set_mont,set_datat,set_hour,set_min,set_sec);wr();_delay_ms(300);}      
 
 if(sett==0){
 if(min_==30&&sec<10){a[0]=sec%10+20;a[1]=sec%10+20;a[2]=sec%10+20;a[3]=sec%10+20;a[4]=sec%10+20;a[5]=sec%10+20;a[6]=sec%10+20;a[7]=sec%10+20;
  a[8]=sec%10+20;a[9]=sec%10+20;a[10]=sec%10+20;a[11]=sec%10+20;a[12]=sec%10+20;a[13]=sec%10+20;a[14]=sec%10+20;a[15]=sec%10+20;}
 else{ 
  if((sec>=10&&sec<=20)||(sec>=40&&sec<=50)){
  a[0]=14;
  a[1]=13;
  a[2]=temper%10;
  a[3]=temper/10%10+20;
  a[4]=temper/100;
  a[5]=10;
  a[6]=12;  
  }
  else{
  a[0]=10;
  a[1]=year%10;
  a[2]=year/10;
  a[3]=mont%10+20;
  a[4]=mont/10;
  a[5]=datat%10+20;
  a[6]=datat/10;
  }
  a[7]=10;
  a[8]=sec%10;
  a[9]=sec/10;
  a[10]=11;
  a[11]=min_%10;
  a[12]=min_/10;
  a[13]=11;
  a[14]=hour%10;
  a[15]=hour/10;
}
}// sett=0

 if(sett>0){
  a[0]=10;
  if(sett==6){if(times<=500||w1==1){w1=0;a[1]=year%10+20;a[2]=year/10;}else{a[1]=11;a[2]=11;}}
  if(sett==5){if(times<=500||w1==1){w1=0;a[3]=mont%10+20;a[4]=mont/10;}else{a[3]=11;a[4]=11;}}
  if(sett==4){if(times<=500||w1==1){w1=0;a[5]=datat%10+20;a[6]=datat/10;}else{a[5]=11;a[6]=11;}}
  a[7]=10;
  if(sett==3){if(times<=500||w1==1){w1=0;a[8]=sec%10;a[9]=sec/10;}else{a[8]=11;a[9]=11;}}
  a[10]=11;
  if(sett==2){if(times<=500||w1==1){w1=0;a[11]=min_%10;a[12]=min_/10;}else{a[11]=11;a[12]=11;}}
  a[13]=11;
  if(sett==1){if(times<=500||w1==1){w1=0;a[14]=hour%10;a[15]=hour/10;}else{a[14]=11;a[15]=11;}}
  if(sett!=3){a[8]=sec%10;a[9]=sec/10;}
}// sett>0

if(sett==0){_delay_ms(100);}
 }

void wr(){a[0]=10;a[1]=year%10; a[2]=year/10;a[3]=mont%10+20;a[4]=mont/10;a[5]=datat%10+20;a[6]=datat/10;a[7]=10;a[8]=sec%10;
a[9]=sec/10;a[10]=11;a[11]=min_%10;a[12]=min_/10;a[13]=11;a[14]=hour%10;a[15]=hour/10;}


ISR(TIMER2_COMP_vect){
  switch(i){
    case 0:  segm=a[0];  an=16;  anod(); _delay_us(600); segment(); if(a[0]==10)an=16;else an=0;  anod(); break;
    case 1:  segm=a[1];  an=16;  anod(); _delay_us(600); segment(); if(a[1]==10)an=16;else an=1;  anod(); break;
    case 2:  segm=a[2];  an=16;  anod(); _delay_us(600); segment(); if(a[2]==10)an=16;else an=2;  anod(); break;
    case 3:  segm=a[3];  an=16;  anod(); _delay_us(600); segment(); if(a[3]==10)an=16;else an=3;  anod(); break;
    case 4:  segm=a[4];  an=16;  anod(); _delay_us(600); segment(); if(a[4]==10)an=16;else an=4;  anod(); break;
    case 5:  segm=a[5];  an=16;  anod(); _delay_us(600); segment(); if(a[5]==10)an=16;else an=5;  anod(); break;
    case 6:  segm=a[6];  an=16;  anod(); _delay_us(600); segment(); if(a[6]==10)an=16;else an=6;  anod(); break;
    case 7:  segm=a[7];  an=16;  anod(); _delay_us(600); segment(); if(a[7]==10)an=16;else an=7;  anod(); break;
    case 8:  segm=a[8];  an=16;  anod(); _delay_us(400); segment(); an=8;  anod(); break;
    case 9:  segm=a[9];  an=16;  anod(); _delay_us(400); segment(); an=9;  anod(); break;
   case 10:  segm=a[10]; an=16;  anod(); _delay_us(400); segment(); an=10; anod(); break;
   case 11:  segm=a[11]; an=16;  anod(); _delay_us(400); segment(); an=11; anod(); break;
   case 12:  segm=a[12]; an=16;  anod(); _delay_us(400); segment(); an=12; anod(); break;
   case 13:  segm=a[13]; an=16;  anod(); _delay_us(400); segment(); an=13; anod(); break;
   case 14:  segm=a[14]; an=16;  anod(); _delay_us(400); segment(); an=14; anod(); break;
   case 15:  segm=a[15]; an=16;  anod(); _delay_us(400); segment(); an=15; anod(); break;
    }i++;if(i>15){i=0;}times++;if(times>600){times=0;}}

void segment(){                                                                
   switch(segm){                                                                 
             //       GECDXBFA
    case 0: PORTA = 0b01110111;break; 
    case 1: PORTA = 0b00100100;break; 
    case 2: PORTA = 0b11010101;break; 
    case 3: PORTA = 0b10110101;break; 
    case 4: PORTA = 0b10100110;break; 
    case 5: PORTA = 0b10110011;break; 
    case 6: PORTA = 0b11110011;break; 
    case 7: PORTA = 0b00100101;break; 
    case 8: PORTA = 0b11110111;break;          
    case 9: PORTA = 0b10110111;break;
              //       GECDXBFA
    case 20: PORTA = 0b01111111;break; 
    case 21: PORTA = 0b00101100;break; 
    case 22: PORTA = 0b11011101;break; 
    case 23: PORTA = 0b10111101;break; 
    case 24: PORTA = 0b10101110;break; 
    case 25: PORTA = 0b10111011;break; 
    case 26: PORTA = 0b11111011;break; 
    case 27: PORTA = 0b00101101;break; 
    case 28: PORTA = 0b11111111;break;          
    case 29: PORTA = 0b10111111;break;
              //       GECDXBFA
    case 10: PORTA = 0b00000000;break; // пусто 
    case 11: PORTA = 0b10000000;break; // -
    case 12: PORTA = 0b11010010;break; // t
    case 13: PORTA = 0b10000111;break; // гр
    case 14: PORTA = 0b01010011;break; // C
  }}
    

void anod(){
 switch(an){                                                             
    case 0: PORTC &=~(1<<PC7);PORTB |=(1<<PB0);break;
    case 1: PORTB &=~(1<<PB0);PORTB |=(1<<PB1);break; 
    case 2: PORTB &=~(1<<PB1);PORTB |=(1<<PB2);break; 
    case 3: PORTB &=~(1<<PB2);PORTB |=(1<<PB3);break; 
    case 4: PORTB &=~(1<<PB3);PORTB |=(1<<PB4);break; 
    case 5: PORTB &=~(1<<PB4);PORTB |=(1<<PB5);break; 
    case 6: PORTB &=~(1<<PB5);PORTB |=(1<<PB6);break; 
    case 7: PORTB &=~(1<<PB6);PORTB |=(1<<PB7);break;  
    case 8: PORTB &=~(1<<PB7);PORTD |=(1<<PD6);break;
    case 9: PORTD &=~(1<<PD6);PORTD |=(1<<PD7);break; 
   case 10: PORTD &=~(1<<PD7);PORTC |=(1<<PC2);break; 
   case 11: PORTC &=~(1<<PC2);PORTC |=(1<<PC3);break; 
   case 12: PORTC &=~(1<<PC3);PORTC |=(1<<PC4);break; 
   case 13: PORTC &=~(1<<PC4);PORTC |=(1<<PC5);break; 
   case 14: PORTC &=~(1<<PC5);PORTC |=(1<<PC6);break; 
   case 15: PORTC &=~(1<<PC6);PORTC |=(1<<PC7);break;       
   case 16: PORTB &=~(1<<PB0);PORTB &=~(1<<PB1);PORTB &=~(1<<PB2);PORTB &=~(1<<PB3);PORTB &=~(1<<PB4);PORTB &=~(1<<PB5);PORTB &=~(1<<PB6);PORTB &=~(1<<PB7);
   PORTD &=~(1<<PD6);PORTD &=~(1<<PD7);PORTC &=~(1<<PC2);PORTC &=~(1<<PC3);PORTC &=~(1<<PC4);PORTC &=~(1<<PC5);PORTC &=~(1<<PC6);PORTC &=~(1<<PC7);break;                                                     
  }} 

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

float dsRead(byte x) {
  byte data[2], addr[8][8], kol = 0;
  while (ds.search(addr[kol])) {  // поиск датчиков, определение адреса и кол-ва датчиков
    kol++;
  } 
  ds.reset_search();  // Сброс поиска датчика
  ds.reset();         // Инициализация, выполняется сброс шины
  ds.select(addr[x]); // Обращение к датчику по адресу
  ds.write(0x44, 0);  // Измерение температуры с переносом данных в память
  ds.reset();         // Инициализация, выполняется сброс шины
  ds.select(addr[x]); // Обращение к датчику по адресу
  ds.write(0xBE);     // Обращение памяти
  for (byte i=0; i<9; i++) data[i]=ds.read();
  int raw=(data[1]<<8)|data[0];
  float value = (float)raw / 1.60; return value; // Расчет температуры и вывод  
}

36

Re: Часы на игп-17 (разработка)

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

37

Re: Часы на игп-17 (разработка)

Постоянное мерцание или в момент запроса температуры?

38 (2024-01-21 00:35:23 отредактировано aprdetali)

Re: Часы на игп-17 (разработка)

Постоянное мерцание, характер не меняется при отображении даты или температуры. Я сначала подумал, что вышел из строя БП

39

Re: Часы на игп-17 (разработка)

// ATMEGA8535 16 MHZ
#define ADDR    0x68
#define CPU_F   16000000 // Clock Speed
#define SCL_F   400000 // // I2C Speed
#define TEMP_KORR 20 // -2гр.Цельсия

#include <OneWire.h>            // http://rcl-radio.ru/wp-content/uploads/2018/07/OneWire.zip
#include <avr/io.h>
#include <util/delay.h>
OneWire  ds(8); // Вход датчика 18b20  PD8 PIN_14

byte a[16],an,segm,i;
bool w=1,w1;
byte sec,min_,hour,datat,mont,year;
int temper,sett,times;
int set_hour,set_min,set_sec,set_datat,set_mont,set_year;


void setup() {
  DDRA = 0xFF;
  DDRB = 0xFF;
  DDRC = 0xFF;
  DDRD |= (1 << PD6)|(1 << PD7);
  PORTD |= (1 << PD2)|(1 << PD3)|(1 << PD4);
  TWBR = (((CPU_F)/(SCL_F)-16 )/2) ;
  TWSR = 0;
  DDRD |= (1 << PD5); // PWM +180V TIMER_1
  cli();
// TIMER_2  F = 16000000/128/100=1250 Hz (800 uS)
  OCR2 = 100;
  TCCR2 |= (1 << WGM21);
  TCCR2 |= (1 << CS22) | (1 << CS20);
  TIMSK |= (1 << OCIE2);
// TIMER_1  F = 16000000/1024/1/1=15625 Hz
  TCCR1A |= (1 << COM1A1);
  TCCR1B |= (1 << WGM12);
  TCCR1A |= (1 << WGM11) | (1 << WGM10);
  TCCR1B |= (1 << CS10); 
  ICR1 = 1023; 
  OCR1A = 350; 
  sei();
 // set_time(22,2,2,4,22,23,0);// год 00-99, ДН 1-7 (1=ВС), месяц 1-12, дата 1-31, час 0-23, минуты 0-59, секунды 0-59
  _delay_ms(200); 
}   

void loop() { 
  datat = ((i2c_read(ADDR,4) & 0x0F) + ((i2c_read(ADDR,4) & 0x70) >> 4) * 10);
  mont = ((i2c_read(ADDR,5) & 0x0F) + ((i2c_read(ADDR,5) & 0x70) >> 4) * 10);
  year = ((i2c_read(ADDR,6) & 0x0F) + ((i2c_read(ADDR,6) & 0x70) >> 4) * 10); 
  hour = (i2c_read(ADDR,2) & 0x0F) + (((i2c_read(ADDR,2) & 0x70) >> 4) * 10);
  min_ =  (i2c_read(ADDR,1) & 0x0F) + (((i2c_read(ADDR,1) & 0x70) >> 4) * 10);
  if(sec==10||sec==40) {temper = dsRead(0)-TEMP_KORR ;}
  sec =  (i2c_read(ADDR,0) & 0x0F) + (((i2c_read(ADDR,0) & 0x70) >> 4) * 10);
  set_year = year;set_mont = mont;set_datat = datat;set_hour = hour;set_min = min_;set_sec = sec;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/// BUTTON /////////////  
  if(((PIND >> PD2) & 1) == 0){sett++;times=0;if(sett>6){sett=0;}wr();_delay_ms(300);}
  
  if(((PIND >> PD3) & 1) == 0 && sett==1){set_hour++;w1=1; if(set_hour>23){set_hour=23;} set_time(set_year,0,set_mont,set_datat,set_hour,set_min,set_sec);wr();_delay_ms(300);}
  if(((PIND >> PD4) & 1) == 0 && sett==1){set_hour--;w1=1; if(set_hour<0){set_hour=0;} set_time(set_year,0,set_mont,set_datat,set_hour,set_min,set_sec);wr();_delay_ms(300);}

  if(((PIND >> PD3) & 1) == 0 && sett==2){set_min++;w1=1; if(set_mont>59){set_min=59;} set_time(set_year,0,set_mont,set_datat,set_hour,set_min,set_sec);wr();_delay_ms(300);}
  if(((PIND >> PD4) & 1) == 0 && sett==2){set_min--;w1=1; if(set_mont<0){set_min=0;} set_time(set_year,0,set_mont,set_datat,set_hour,set_min,set_sec);wr();_delay_ms(300);}

  if(((PIND >> PD3) & 1) == 0 && sett==3){set_sec=0;w1=1; set_time(set_year,0,set_mont,set_datat,set_hour,set_min,set_sec);wr();_delay_ms(300);}
  if(((PIND >> PD4) & 1) == 0 && sett==3){set_sec=0;w1=1; set_time(set_year,0,set_mont,set_datat,set_hour,set_min,set_sec);wr();_delay_ms(300);}

  if(((PIND >> PD3) & 1) == 0 && sett==4){set_datat++;w1=1; if(set_datat>31){set_datat=31;} set_time(set_year,0,set_mont,set_datat,set_hour,set_min,set_sec);wr();_delay_ms(300);}
  if(((PIND >> PD4) & 1) == 0 && sett==4){set_datat--;w1=1; if(set_datat<0){set_datat=0;} set_time(set_year,0,set_mont,set_datat,set_hour,set_min,set_sec);wr();_delay_ms(300);}

  if(((PIND >> PD3) & 1) == 0 && sett==5){set_mont++;w1=1; if(set_mont>12){set_mont=12;} set_time(set_year,0,set_mont,set_datat,set_hour,set_min,set_sec);wr();_delay_ms(300);}
  if(((PIND >> PD4) & 1) == 0 && sett==5){set_mont--;w1=1; if(set_mont<0){set_mont=0;} set_time(set_year,0,set_mont,set_datat,set_hour,set_min,set_sec);wr();_delay_ms(300);} 

  if(((PIND >> PD3) & 1) == 0 && sett==6){set_year++;w1=1; if(set_year>50){set_year=50;} set_time(set_year,0,set_mont,set_datat,set_hour,set_min,set_sec);wr();_delay_ms(300);}
  if(((PIND >> PD4) & 1) == 0 && sett==6){set_year--;w1=1; if(set_year<22){set_year=22;} set_time(set_year,0,set_mont,set_datat,set_hour,set_min,set_sec);wr();_delay_ms(300);}      
 
 if(sett==0){
 if(min_==30&&sec<10){a[0]=sec%10+20;a[1]=sec%10+20;a[2]=sec%10+20;a[3]=sec%10+20;a[4]=sec%10+20;a[5]=sec%10+20;a[6]=sec%10+20;a[7]=sec%10+20;
  a[8]=sec%10+20;a[9]=sec%10+20;a[10]=sec%10+20;a[11]=sec%10+20;a[12]=sec%10+20;a[13]=sec%10+20;a[14]=sec%10+20;a[15]=sec%10+20;}
 else{ 
  if((sec>=10&&sec<=20)||(sec>=40&&sec<=50)){
  a[0]=14;
  a[1]=13;
  a[2]=temper%10;
  a[3]=temper/10%10+20;
  a[4]=temper/100;
  a[5]=10;
  a[6]=12;  
  }
  else{
  a[0]=10;
  a[1]=year%10;
  a[2]=year/10;
  a[3]=mont%10+20;
  a[4]=mont/10;
  a[5]=datat%10+20;
  a[6]=datat/10;
  }
  a[7]=10;
  a[8]=sec%10;
  a[9]=sec/10;
  a[10]=11;
  a[11]=min_%10;
  a[12]=min_/10;
  a[13]=11;
  a[14]=hour%10;
  a[15]=hour/10;
}
}// sett=0

 if(sett>0){
  a[0]=10;
  if(sett==6){if(times<=500||w1==1){w1=0;a[1]=year%10+20;a[2]=year/10;}else{a[1]=11;a[2]=11;}}
  if(sett==5){if(times<=500||w1==1){w1=0;a[3]=mont%10+20;a[4]=mont/10;}else{a[3]=11;a[4]=11;}}
  if(sett==4){if(times<=500||w1==1){w1=0;a[5]=datat%10+20;a[6]=datat/10;}else{a[5]=11;a[6]=11;}}
  a[7]=10;
  if(sett==3){if(times<=500||w1==1){w1=0;a[8]=sec%10;a[9]=sec/10;}else{a[8]=11;a[9]=11;}}
  a[10]=11;
  if(sett==2){if(times<=500||w1==1){w1=0;a[11]=min_%10;a[12]=min_/10;}else{a[11]=11;a[12]=11;}}
  a[13]=11;
  if(sett==1){if(times<=500||w1==1){w1=0;a[14]=hour%10;a[15]=hour/10;}else{a[14]=11;a[15]=11;}}
  if(sett!=3){a[8]=sec%10;a[9]=sec/10;}
}// sett>0

if(sett==0){_delay_ms(100);}
 }

void wr(){a[0]=10;a[1]=year%10; a[2]=year/10;a[3]=mont%10+20;a[4]=mont/10;a[5]=datat%10+20;a[6]=datat/10;a[7]=10;a[8]=sec%10;
a[9]=sec/10;a[10]=11;a[11]=min_%10;a[12]=min_/10;a[13]=11;a[14]=hour%10;a[15]=hour/10;}


ISR(TIMER2_COMP_vect){
  switch(i){
    case 0:  segm=a[0];  an=16;  anod(); _delay_us(300); segment(); if(a[0]==10)an=16;else an=0;  anod(); break;
    case 1:  segm=a[1];  an=16;  anod(); _delay_us(300); segment(); if(a[1]==10)an=16;else an=1;  anod(); break;
    case 2:  segm=a[2];  an=16;  anod(); _delay_us(300); segment(); if(a[2]==10)an=16;else an=2;  anod(); break;
    case 3:  segm=a[3];  an=16;  anod(); _delay_us(300); segment(); if(a[3]==10)an=16;else an=3;  anod(); break;
    case 4:  segm=a[4];  an=16;  anod(); _delay_us(300); segment(); if(a[4]==10)an=16;else an=4;  anod(); break;
    case 5:  segm=a[5];  an=16;  anod(); _delay_us(300); segment(); if(a[5]==10)an=16;else an=5;  anod(); break;
    case 6:  segm=a[6];  an=16;  anod(); _delay_us(300); segment(); if(a[6]==10)an=16;else an=6;  anod(); break;
    case 7:  segm=a[7];  an=16;  anod(); _delay_us(300); segment(); if(a[7]==10)an=16;else an=7;  anod(); break;
    case 8:  segm=a[8];  an=16;  anod(); _delay_us(200); segment(); an=8;  anod(); break;
    case 9:  segm=a[9];  an=16;  anod(); _delay_us(200); segment(); an=9;  anod(); break;
   case 10:  segm=a[10]; an=16;  anod(); _delay_us(200); segment(); an=10; anod(); break;
   case 11:  segm=a[11]; an=16;  anod(); _delay_us(200); segment(); an=11; anod(); break;
   case 12:  segm=a[12]; an=16;  anod(); _delay_us(200); segment(); an=12; anod(); break;
   case 13:  segm=a[13]; an=16;  anod(); _delay_us(200); segment(); an=13; anod(); break;
   case 14:  segm=a[14]; an=16;  anod(); _delay_us(200); segment(); an=14; anod(); break;
   case 15:  segm=a[15]; an=16;  anod(); _delay_us(200); segment(); an=15; anod(); break;
    }i++;if(i>15){i=0;}times++;if(times>600){times=0;}}

void segment(){                                                                
   switch(segm){                                                                 
             //       GECDXBFA
    case 0: PORTA = 0b01110111;break; 
    case 1: PORTA = 0b00100100;break; 
    case 2: PORTA = 0b11010101;break; 
    case 3: PORTA = 0b10110101;break; 
    case 4: PORTA = 0b10100110;break; 
    case 5: PORTA = 0b10110011;break; 
    case 6: PORTA = 0b11110011;break; 
    case 7: PORTA = 0b00100101;break; 
    case 8: PORTA = 0b11110111;break;          
    case 9: PORTA = 0b10110111;break;
              //       GECDXBFA
    case 20: PORTA = 0b01111111;break; 
    case 21: PORTA = 0b00101100;break; 
    case 22: PORTA = 0b11011101;break; 
    case 23: PORTA = 0b10111101;break; 
    case 24: PORTA = 0b10101110;break; 
    case 25: PORTA = 0b10111011;break; 
    case 26: PORTA = 0b11111011;break; 
    case 27: PORTA = 0b00101101;break; 
    case 28: PORTA = 0b11111111;break;          
    case 29: PORTA = 0b10111111;break;
              //       GECDXBFA
    case 10: PORTA = 0b00000000;break; // пусто 
    case 11: PORTA = 0b10000000;break; // -
    case 12: PORTA = 0b11010010;break; // t
    case 13: PORTA = 0b10000111;break; // гр
    case 14: PORTA = 0b01010011;break; // C
  }}
    

void anod(){
 switch(an){                                                             
    case 0: PORTC &=~(1<<PC7);PORTB |=(1<<PB0);break;
    case 1: PORTB &=~(1<<PB0);PORTB |=(1<<PB1);break; 
    case 2: PORTB &=~(1<<PB1);PORTB |=(1<<PB2);break; 
    case 3: PORTB &=~(1<<PB2);PORTB |=(1<<PB3);break; 
    case 4: PORTB &=~(1<<PB3);PORTB |=(1<<PB4);break; 
    case 5: PORTB &=~(1<<PB4);PORTB |=(1<<PB5);break; 
    case 6: PORTB &=~(1<<PB5);PORTB |=(1<<PB6);break; 
    case 7: PORTB &=~(1<<PB6);PORTB |=(1<<PB7);break;  
    case 8: PORTB &=~(1<<PB7);PORTD |=(1<<PD6);break;
    case 9: PORTD &=~(1<<PD6);PORTD |=(1<<PD7);break; 
   case 10: PORTD &=~(1<<PD7);PORTC |=(1<<PC2);break; 
   case 11: PORTC &=~(1<<PC2);PORTC |=(1<<PC3);break; 
   case 12: PORTC &=~(1<<PC3);PORTC |=(1<<PC4);break; 
   case 13: PORTC &=~(1<<PC4);PORTC |=(1<<PC5);break; 
   case 14: PORTC &=~(1<<PC5);PORTC |=(1<<PC6);break; 
   case 15: PORTC &=~(1<<PC6);PORTC |=(1<<PC7);break;       
   case 16: PORTB &=~(1<<PB0);PORTB &=~(1<<PB1);PORTB &=~(1<<PB2);PORTB &=~(1<<PB3);PORTB &=~(1<<PB4);PORTB &=~(1<<PB5);PORTB &=~(1<<PB6);PORTB &=~(1<<PB7);
   PORTD &=~(1<<PD6);PORTD &=~(1<<PD7);PORTC &=~(1<<PC2);PORTC &=~(1<<PC3);PORTC &=~(1<<PC4);PORTC &=~(1<<PC5);PORTC &=~(1<<PC6);PORTC &=~(1<<PC7);break;                                                     
  }} 

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

float dsRead(byte x) {
  byte data[2], addr[8][8], kol = 0;
  while (ds.search(addr[kol])) {  // поиск датчиков, определение адреса и кол-ва датчиков
    kol++;
  } 
  ds.reset_search();  // Сброс поиска датчика
  ds.reset();         // Инициализация, выполняется сброс шины
  ds.select(addr[x]); // Обращение к датчику по адресу
  ds.write(0x44, 0);  // Измерение температуры с переносом данных в память
  ds.reset();         // Инициализация, выполняется сброс шины
  ds.select(addr[x]); // Обращение к датчику по адресу
  ds.write(0xBE);     // Обращение памяти
  for (byte i=0; i<9; i++) data[i]=ds.read();
  int raw=(data[1]<<8)|data[0];
  float value = (float)raw / 1.60; return value; // Расчет температуры и вывод  
}

40

Re: Часы на игп-17 (разработка)

К сожалению мерцание индикатора не пропало

41

Re: Часы на игп-17 (разработка)

Я поднял частоту динамической индикации

// ATMEGA8535 16 MHZ
#define ADDR    0x68
#define CPU_F   16000000 // Clock Speed
#define SCL_F   400000 // // I2C Speed
#define TEMP_KORR 20 // -2гр.Цельсия

#include <OneWire.h>            // http://rcl-radio.ru/wp-content/uploads/2018/07/OneWire.zip
#include <avr/io.h>
#include <util/delay.h>
OneWire  ds(8); // Вход датчика 18b20  PD8 PIN_14

byte a[16],an,segm,i;
bool w=1,w1;
byte sec,min_,hour,datat,mont,year;
int temper,sett,times;
int set_hour,set_min,set_sec,set_datat,set_mont,set_year;


void setup() {
  DDRA = 0xFF;
  DDRB = 0xFF;
  DDRC = 0xFF;
  DDRD |= (1 << PD6)|(1 << PD7);
  PORTD |= (1 << PD2)|(1 << PD3)|(1 << PD4);
  TWBR = (((CPU_F)/(SCL_F)-16 )/2) ;
  TWSR = 0;
  DDRD |= (1 << PD5); // PWM +180V TIMER_1
  cli();
// TIMER_2  F = 16000000/128/100=1250 Hz (800 uS)
  OCR2 = 50;
  TCCR2 |= (1 << WGM21);
  TCCR2 |= (1 << CS22) | (1 << CS20);
  TIMSK |= (1 << OCIE2);
// TIMER_1  F = 16000000/1024/1/1=15625 Hz
  TCCR1A |= (1 << COM1A1);
  TCCR1B |= (1 << WGM12);
  TCCR1A |= (1 << WGM11) | (1 << WGM10);
  TCCR1B |= (1 << CS10); 
  ICR1 = 1023; 
  OCR1A = 350; 
  sei();
 // set_time(22,2,2,4,22,23,0);// год 00-99, ДН 1-7 (1=ВС), месяц 1-12, дата 1-31, час 0-23, минуты 0-59, секунды 0-59
  _delay_ms(200); 
}   

void loop() { 
  datat = ((i2c_read(ADDR,4) & 0x0F) + ((i2c_read(ADDR,4) & 0x70) >> 4) * 10);
  mont = ((i2c_read(ADDR,5) & 0x0F) + ((i2c_read(ADDR,5) & 0x70) >> 4) * 10);
  year = ((i2c_read(ADDR,6) & 0x0F) + ((i2c_read(ADDR,6) & 0x70) >> 4) * 10); 
  hour = (i2c_read(ADDR,2) & 0x0F) + (((i2c_read(ADDR,2) & 0x70) >> 4) * 10);
  min_ =  (i2c_read(ADDR,1) & 0x0F) + (((i2c_read(ADDR,1) & 0x70) >> 4) * 10);
  if(sec==10||sec==40) {temper = dsRead(0)-TEMP_KORR ;}
  sec =  (i2c_read(ADDR,0) & 0x0F) + (((i2c_read(ADDR,0) & 0x70) >> 4) * 10);
  set_year = year;set_mont = mont;set_datat = datat;set_hour = hour;set_min = min_;set_sec = sec;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/// BUTTON /////////////  
  if(((PIND >> PD2) & 1) == 0){sett++;times=0;if(sett>6){sett=0;}wr();_delay_ms(300);}
  
  if(((PIND >> PD3) & 1) == 0 && sett==1){set_hour++;w1=1; if(set_hour>23){set_hour=23;} set_time(set_year,0,set_mont,set_datat,set_hour,set_min,set_sec);wr();_delay_ms(300);}
  if(((PIND >> PD4) & 1) == 0 && sett==1){set_hour--;w1=1; if(set_hour<0){set_hour=0;} set_time(set_year,0,set_mont,set_datat,set_hour,set_min,set_sec);wr();_delay_ms(300);}

  if(((PIND >> PD3) & 1) == 0 && sett==2){set_min++;w1=1; if(set_mont>59){set_min=59;} set_time(set_year,0,set_mont,set_datat,set_hour,set_min,set_sec);wr();_delay_ms(300);}
  if(((PIND >> PD4) & 1) == 0 && sett==2){set_min--;w1=1; if(set_mont<0){set_min=0;} set_time(set_year,0,set_mont,set_datat,set_hour,set_min,set_sec);wr();_delay_ms(300);}

  if(((PIND >> PD3) & 1) == 0 && sett==3){set_sec=0;w1=1; set_time(set_year,0,set_mont,set_datat,set_hour,set_min,set_sec);wr();_delay_ms(300);}
  if(((PIND >> PD4) & 1) == 0 && sett==3){set_sec=0;w1=1; set_time(set_year,0,set_mont,set_datat,set_hour,set_min,set_sec);wr();_delay_ms(300);}

  if(((PIND >> PD3) & 1) == 0 && sett==4){set_datat++;w1=1; if(set_datat>31){set_datat=31;} set_time(set_year,0,set_mont,set_datat,set_hour,set_min,set_sec);wr();_delay_ms(300);}
  if(((PIND >> PD4) & 1) == 0 && sett==4){set_datat--;w1=1; if(set_datat<0){set_datat=0;} set_time(set_year,0,set_mont,set_datat,set_hour,set_min,set_sec);wr();_delay_ms(300);}

  if(((PIND >> PD3) & 1) == 0 && sett==5){set_mont++;w1=1; if(set_mont>12){set_mont=12;} set_time(set_year,0,set_mont,set_datat,set_hour,set_min,set_sec);wr();_delay_ms(300);}
  if(((PIND >> PD4) & 1) == 0 && sett==5){set_mont--;w1=1; if(set_mont<0){set_mont=0;} set_time(set_year,0,set_mont,set_datat,set_hour,set_min,set_sec);wr();_delay_ms(300);} 

  if(((PIND >> PD3) & 1) == 0 && sett==6){set_year++;w1=1; if(set_year>50){set_year=50;} set_time(set_year,0,set_mont,set_datat,set_hour,set_min,set_sec);wr();_delay_ms(300);}
  if(((PIND >> PD4) & 1) == 0 && sett==6){set_year--;w1=1; if(set_year<22){set_year=22;} set_time(set_year,0,set_mont,set_datat,set_hour,set_min,set_sec);wr();_delay_ms(300);}      
 
 if(sett==0){
 if(min_==30&&sec<10){a[0]=sec%10+20;a[1]=sec%10+20;a[2]=sec%10+20;a[3]=sec%10+20;a[4]=sec%10+20;a[5]=sec%10+20;a[6]=sec%10+20;a[7]=sec%10+20;
  a[8]=sec%10+20;a[9]=sec%10+20;a[10]=sec%10+20;a[11]=sec%10+20;a[12]=sec%10+20;a[13]=sec%10+20;a[14]=sec%10+20;a[15]=sec%10+20;}
 else{ 
  if((sec>=10&&sec<=20)||(sec>=40&&sec<=50)){
  a[0]=14;
  a[1]=13;
  a[2]=temper%10;
  a[3]=temper/10%10+20;
  a[4]=temper/100;
  a[5]=10;
  a[6]=12;  
  }
  else{
  a[0]=10;
  a[1]=year%10;
  a[2]=year/10;
  a[3]=mont%10+20;
  a[4]=mont/10;
  a[5]=datat%10+20;
  a[6]=datat/10;
  }
  a[7]=10;
  a[8]=sec%10;
  a[9]=sec/10;
  a[10]=11;
  a[11]=min_%10;
  a[12]=min_/10;
  a[13]=11;
  a[14]=hour%10;
  a[15]=hour/10;
}
}// sett=0

 if(sett>0){
  a[0]=10;
  if(sett==6){if(times<=500||w1==1){w1=0;a[1]=year%10+20;a[2]=year/10;}else{a[1]=11;a[2]=11;}}
  if(sett==5){if(times<=500||w1==1){w1=0;a[3]=mont%10+20;a[4]=mont/10;}else{a[3]=11;a[4]=11;}}
  if(sett==4){if(times<=500||w1==1){w1=0;a[5]=datat%10+20;a[6]=datat/10;}else{a[5]=11;a[6]=11;}}
  a[7]=10;
  if(sett==3){if(times<=500||w1==1){w1=0;a[8]=sec%10;a[9]=sec/10;}else{a[8]=11;a[9]=11;}}
  a[10]=11;
  if(sett==2){if(times<=500||w1==1){w1=0;a[11]=min_%10;a[12]=min_/10;}else{a[11]=11;a[12]=11;}}
  a[13]=11;
  if(sett==1){if(times<=500||w1==1){w1=0;a[14]=hour%10;a[15]=hour/10;}else{a[14]=11;a[15]=11;}}
  if(sett!=3){a[8]=sec%10;a[9]=sec/10;}
}// sett>0

if(sett==0){_delay_ms(100);}
 }

void wr(){a[0]=10;a[1]=year%10; a[2]=year/10;a[3]=mont%10+20;a[4]=mont/10;a[5]=datat%10+20;a[6]=datat/10;a[7]=10;a[8]=sec%10;
a[9]=sec/10;a[10]=11;a[11]=min_%10;a[12]=min_/10;a[13]=11;a[14]=hour%10;a[15]=hour/10;}


ISR(TIMER2_COMP_vect){
  switch(i){
    case 0:  segm=a[0];  an=16;  anod(); _delay_us(10); segment(); if(a[0]==10)an=16;else an=0;  anod(); break;
    case 1:  segm=a[1];  an=16;  anod(); _delay_us(10); segment(); if(a[1]==10)an=16;else an=1;  anod(); break;
    case 2:  segm=a[2];  an=16;  anod(); _delay_us(10); segment(); if(a[2]==10)an=16;else an=2;  anod(); break;
    case 3:  segm=a[3];  an=16;  anod(); _delay_us(10); segment(); if(a[3]==10)an=16;else an=3;  anod(); break;
    case 4:  segm=a[4];  an=16;  anod(); _delay_us(10); segment(); if(a[4]==10)an=16;else an=4;  anod(); break;
    case 5:  segm=a[5];  an=16;  anod(); _delay_us(10); segment(); if(a[5]==10)an=16;else an=5;  anod(); break;
    case 6:  segm=a[6];  an=16;  anod(); _delay_us(10); segment(); if(a[6]==10)an=16;else an=6;  anod(); break;
    case 7:  segm=a[7];  an=16;  anod(); _delay_us(10); segment(); if(a[7]==10)an=16;else an=7;  anod(); break;
    case 8:  segm=a[8];  an=16;  anod(); _delay_us(10); segment(); an=8;  anod(); break;
    case 9:  segm=a[9];  an=16;  anod(); _delay_us(10); segment(); an=9;  anod(); break;
   case 10:  segm=a[10]; an=16;  anod(); _delay_us(10); segment(); an=10; anod(); break;
   case 11:  segm=a[11]; an=16;  anod(); _delay_us(10); segment(); an=11; anod(); break;
   case 12:  segm=a[12]; an=16;  anod(); _delay_us(10); segment(); an=12; anod(); break;
   case 13:  segm=a[13]; an=16;  anod(); _delay_us(10); segment(); an=13; anod(); break;
   case 14:  segm=a[14]; an=16;  anod(); _delay_us(10); segment(); an=14; anod(); break;
   case 15:  segm=a[15]; an=16;  anod(); _delay_us(10); segment(); an=15; anod(); break;
    }i++;if(i>15){i=0;}times++;if(times>600){times=0;}}

void segment(){                                                                
   switch(segm){                                                                 
             //       GECDXBFA
    case 0: PORTA = 0b01110111;break; 
    case 1: PORTA = 0b00100100;break; 
    case 2: PORTA = 0b11010101;break; 
    case 3: PORTA = 0b10110101;break; 
    case 4: PORTA = 0b10100110;break; 
    case 5: PORTA = 0b10110011;break; 
    case 6: PORTA = 0b11110011;break; 
    case 7: PORTA = 0b00100101;break; 
    case 8: PORTA = 0b11110111;break;          
    case 9: PORTA = 0b10110111;break;
              //       GECDXBFA
    case 20: PORTA = 0b01111111;break; 
    case 21: PORTA = 0b00101100;break; 
    case 22: PORTA = 0b11011101;break; 
    case 23: PORTA = 0b10111101;break; 
    case 24: PORTA = 0b10101110;break; 
    case 25: PORTA = 0b10111011;break; 
    case 26: PORTA = 0b11111011;break; 
    case 27: PORTA = 0b00101101;break; 
    case 28: PORTA = 0b11111111;break;          
    case 29: PORTA = 0b10111111;break;
              //       GECDXBFA
    case 10: PORTA = 0b00000000;break; // пусто 
    case 11: PORTA = 0b10000000;break; // -
    case 12: PORTA = 0b11010010;break; // t
    case 13: PORTA = 0b10000111;break; // гр
    case 14: PORTA = 0b01010011;break; // C
  }}
    

void anod(){
 switch(an){                                                             
    case 0: PORTC &=~(1<<PC7);PORTB |=(1<<PB0);break;
    case 1: PORTB &=~(1<<PB0);PORTB |=(1<<PB1);break; 
    case 2: PORTB &=~(1<<PB1);PORTB |=(1<<PB2);break; 
    case 3: PORTB &=~(1<<PB2);PORTB |=(1<<PB3);break; 
    case 4: PORTB &=~(1<<PB3);PORTB |=(1<<PB4);break; 
    case 5: PORTB &=~(1<<PB4);PORTB |=(1<<PB5);break; 
    case 6: PORTB &=~(1<<PB5);PORTB |=(1<<PB6);break; 
    case 7: PORTB &=~(1<<PB6);PORTB |=(1<<PB7);break;  
    case 8: PORTB &=~(1<<PB7);PORTD |=(1<<PD6);break;
    case 9: PORTD &=~(1<<PD6);PORTD |=(1<<PD7);break; 
   case 10: PORTD &=~(1<<PD7);PORTC |=(1<<PC2);break; 
   case 11: PORTC &=~(1<<PC2);PORTC |=(1<<PC3);break; 
   case 12: PORTC &=~(1<<PC3);PORTC |=(1<<PC4);break; 
   case 13: PORTC &=~(1<<PC4);PORTC |=(1<<PC5);break; 
   case 14: PORTC &=~(1<<PC5);PORTC |=(1<<PC6);break; 
   case 15: PORTC &=~(1<<PC6);PORTC |=(1<<PC7);break;       
   case 16: PORTB &=~(1<<PB0);PORTB &=~(1<<PB1);PORTB &=~(1<<PB2);PORTB &=~(1<<PB3);PORTB &=~(1<<PB4);PORTB &=~(1<<PB5);PORTB &=~(1<<PB6);PORTB &=~(1<<PB7);
   PORTD &=~(1<<PD6);PORTD &=~(1<<PD7);PORTC &=~(1<<PC2);PORTC &=~(1<<PC3);PORTC &=~(1<<PC4);PORTC &=~(1<<PC5);PORTC &=~(1<<PC6);PORTC &=~(1<<PC7);break;                                                     
  }} 

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

float dsRead(byte x) {
  byte data[2], addr[8][8], kol = 0;
  while (ds.search(addr[kol])) {  // поиск датчиков, определение адреса и кол-ва датчиков
    kol++;
  } 
  ds.reset_search();  // Сброс поиска датчика
  ds.reset();         // Инициализация, выполняется сброс шины
  ds.select(addr[x]); // Обращение к датчику по адресу
  ds.write(0x44, 0);  // Измерение температуры с переносом данных в память
  ds.reset();         // Инициализация, выполняется сброс шины
  ds.select(addr[x]); // Обращение к датчику по адресу
  ds.write(0xBE);     // Обращение памяти
  for (byte i=0; i<9; i++) data[i]=ds.read();
  int raw=(data[1]<<8)|data[0];
  float value = (float)raw / 1.60; return value; // Расчет температуры и вывод  
}

42 (2024-01-21 01:00:38 отредактировано aprdetali)

Re: Часы на игп-17 (разработка)

Александр, к сожалению с такой прошивкой отображение ухудшилось: мерцание не изменилось, но дополнительно появилась засветка неиспользуемых сегментов индикатора, раньше ее практически не было заметно

43

Re: Часы на игп-17 (разработка)

Я полностью поменял код опроса ds18B20

// ATMEGA8535 16 MHZ
#define ADDR    0x68
#define CPU_F   16000000 // Clock Speed
#define SCL_F   400000 // // I2C Speed
#define TEMP_KORR 20 // -2гр.Цельсия

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

byte a[16],an,segm,i;
bool w=1,w1;
byte sec,min_,hour,datat,mont,year;
int temper,sett,times;
int set_hour,set_min,set_sec,set_datat,set_mont,set_year;


void setup() {
  DDRA = 0xFF;
  DDRB = 0xFF;
  DDRC = 0xFF;
  DDRD |= (1 << PD6)|(1 << PD7);
  PORTD |= (1 << PD2)|(1 << PD3)|(1 << PD4);
  TWBR = (((CPU_F)/(SCL_F)-16 )/2) ;
  TWSR = 0;
  DDRD |= (1 << PD5); // PWM +180V TIMER_1
  cli();
// TIMER_2  F = 16000000/128/100=1250 Hz (800 uS)
  OCR2 = 100;
  TCCR2 |= (1 << WGM21);
  TCCR2 |= (1 << CS22) | (1 << CS20);
  TIMSK |= (1 << OCIE2);
// TIMER_1  F = 16000000/1024/1/1=15625 Hz
  TCCR1A |= (1 << COM1A1);
  TCCR1B |= (1 << WGM12);
  TCCR1A |= (1 << WGM11) | (1 << WGM10);
  TCCR1B |= (1 << CS10); 
  ICR1 = 1023; 
  OCR1A = 350; 
  sei();
 // set_time(22,2,2,4,22,23,0);// год 00-99, ДН 1-7 (1=ВС), месяц 1-12, дата 1-31, час 0-23, минуты 0-59, секунды 0-59
  _delay_ms(200); 
}   

void loop() { 
  datat = ((i2c_read(ADDR,4) & 0x0F) + ((i2c_read(ADDR,4) & 0x70) >> 4) * 10);
  mont = ((i2c_read(ADDR,5) & 0x0F) + ((i2c_read(ADDR,5) & 0x70) >> 4) * 10);
  year = ((i2c_read(ADDR,6) & 0x0F) + ((i2c_read(ADDR,6) & 0x70) >> 4) * 10); 
  hour = (i2c_read(ADDR,2) & 0x0F) + (((i2c_read(ADDR,2) & 0x70) >> 4) * 10);
  min_ =  (i2c_read(ADDR,1) & 0x0F) + (((i2c_read(ADDR,1) & 0x70) >> 4) * 10);
  if(sec==10||sec==40) {temper = read_temp()-TEMP_KORR ;}
  sec =  (i2c_read(ADDR,0) & 0x0F) + (((i2c_read(ADDR,0) & 0x70) >> 4) * 10);
  set_year = year;set_mont = mont;set_datat = datat;set_hour = hour;set_min = min_;set_sec = sec;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/// BUTTON /////////////  
  if(((PIND >> PD2) & 1) == 0){sett++;times=0;if(sett>6){sett=0;}wr();_delay_ms(300);}
  
  if(((PIND >> PD3) & 1) == 0 && sett==1){set_hour++;w1=1; if(set_hour>23){set_hour=23;} set_time(set_year,0,set_mont,set_datat,set_hour,set_min,set_sec);wr();_delay_ms(300);}
  if(((PIND >> PD4) & 1) == 0 && sett==1){set_hour--;w1=1; if(set_hour<0){set_hour=0;} set_time(set_year,0,set_mont,set_datat,set_hour,set_min,set_sec);wr();_delay_ms(300);}

  if(((PIND >> PD3) & 1) == 0 && sett==2){set_min++;w1=1; if(set_mont>59){set_min=59;} set_time(set_year,0,set_mont,set_datat,set_hour,set_min,set_sec);wr();_delay_ms(300);}
  if(((PIND >> PD4) & 1) == 0 && sett==2){set_min--;w1=1; if(set_mont<0){set_min=0;} set_time(set_year,0,set_mont,set_datat,set_hour,set_min,set_sec);wr();_delay_ms(300);}

  if(((PIND >> PD3) & 1) == 0 && sett==3){set_sec=0;w1=1; set_time(set_year,0,set_mont,set_datat,set_hour,set_min,set_sec);wr();_delay_ms(300);}
  if(((PIND >> PD4) & 1) == 0 && sett==3){set_sec=0;w1=1; set_time(set_year,0,set_mont,set_datat,set_hour,set_min,set_sec);wr();_delay_ms(300);}

  if(((PIND >> PD3) & 1) == 0 && sett==4){set_datat++;w1=1; if(set_datat>31){set_datat=31;} set_time(set_year,0,set_mont,set_datat,set_hour,set_min,set_sec);wr();_delay_ms(300);}
  if(((PIND >> PD4) & 1) == 0 && sett==4){set_datat--;w1=1; if(set_datat<0){set_datat=0;} set_time(set_year,0,set_mont,set_datat,set_hour,set_min,set_sec);wr();_delay_ms(300);}

  if(((PIND >> PD3) & 1) == 0 && sett==5){set_mont++;w1=1; if(set_mont>12){set_mont=12;} set_time(set_year,0,set_mont,set_datat,set_hour,set_min,set_sec);wr();_delay_ms(300);}
  if(((PIND >> PD4) & 1) == 0 && sett==5){set_mont--;w1=1; if(set_mont<0){set_mont=0;} set_time(set_year,0,set_mont,set_datat,set_hour,set_min,set_sec);wr();_delay_ms(300);} 

  if(((PIND >> PD3) & 1) == 0 && sett==6){set_year++;w1=1; if(set_year>50){set_year=50;} set_time(set_year,0,set_mont,set_datat,set_hour,set_min,set_sec);wr();_delay_ms(300);}
  if(((PIND >> PD4) & 1) == 0 && sett==6){set_year--;w1=1; if(set_year<22){set_year=22;} set_time(set_year,0,set_mont,set_datat,set_hour,set_min,set_sec);wr();_delay_ms(300);}      
 
 if(sett==0){
 if(min_==30&&sec<10){a[0]=sec%10+20;a[1]=sec%10+20;a[2]=sec%10+20;a[3]=sec%10+20;a[4]=sec%10+20;a[5]=sec%10+20;a[6]=sec%10+20;a[7]=sec%10+20;
  a[8]=sec%10+20;a[9]=sec%10+20;a[10]=sec%10+20;a[11]=sec%10+20;a[12]=sec%10+20;a[13]=sec%10+20;a[14]=sec%10+20;a[15]=sec%10+20;}
 else{ 
  if((sec>=10&&sec<=20)||(sec>=40&&sec<=50)){
  a[0]=14;
  a[1]=13;
  a[2]=temper%10;
  a[3]=temper/10%10+20;
  a[4]=temper/100;
  a[5]=10;
  a[6]=12;  
  }
  else{
  a[0]=10;
  a[1]=year%10;
  a[2]=year/10;
  a[3]=mont%10+20;
  a[4]=mont/10;
  a[5]=datat%10+20;
  a[6]=datat/10;
  }
  a[7]=10;
  a[8]=sec%10;
  a[9]=sec/10;
  a[10]=11;
  a[11]=min_%10;
  a[12]=min_/10;
  a[13]=11;
  a[14]=hour%10;
  a[15]=hour/10;
}
}// sett=0

 if(sett>0){
  a[0]=10;
  if(sett==6){if(times<=500||w1==1){w1=0;a[1]=year%10+20;a[2]=year/10;}else{a[1]=11;a[2]=11;}}
  if(sett==5){if(times<=500||w1==1){w1=0;a[3]=mont%10+20;a[4]=mont/10;}else{a[3]=11;a[4]=11;}}
  if(sett==4){if(times<=500||w1==1){w1=0;a[5]=datat%10+20;a[6]=datat/10;}else{a[5]=11;a[6]=11;}}
  a[7]=10;
  if(sett==3){if(times<=500||w1==1){w1=0;a[8]=sec%10;a[9]=sec/10;}else{a[8]=11;a[9]=11;}}
  a[10]=11;
  if(sett==2){if(times<=500||w1==1){w1=0;a[11]=min_%10;a[12]=min_/10;}else{a[11]=11;a[12]=11;}}
  a[13]=11;
  if(sett==1){if(times<=500||w1==1){w1=0;a[14]=hour%10;a[15]=hour/10;}else{a[14]=11;a[15]=11;}}
  if(sett!=3){a[8]=sec%10;a[9]=sec/10;}
}// sett>0

if(sett==0){_delay_ms(100);}
 }

void wr(){a[0]=10;a[1]=year%10; a[2]=year/10;a[3]=mont%10+20;a[4]=mont/10;a[5]=datat%10+20;a[6]=datat/10;a[7]=10;a[8]=sec%10;
a[9]=sec/10;a[10]=11;a[11]=min_%10;a[12]=min_/10;a[13]=11;a[14]=hour%10;a[15]=hour/10;}


ISR(TIMER2_COMP_vect){
  switch(i){
    case 0:  segm=a[0];  an=16;  anod(); _delay_us(600); segment(); if(a[0]==10)an=16;else an=0;  anod(); break;
    case 1:  segm=a[1];  an=16;  anod(); _delay_us(600); segment(); if(a[1]==10)an=16;else an=1;  anod(); break;
    case 2:  segm=a[2];  an=16;  anod(); _delay_us(600); segment(); if(a[2]==10)an=16;else an=2;  anod(); break;
    case 3:  segm=a[3];  an=16;  anod(); _delay_us(600); segment(); if(a[3]==10)an=16;else an=3;  anod(); break;
    case 4:  segm=a[4];  an=16;  anod(); _delay_us(600); segment(); if(a[4]==10)an=16;else an=4;  anod(); break;
    case 5:  segm=a[5];  an=16;  anod(); _delay_us(600); segment(); if(a[5]==10)an=16;else an=5;  anod(); break;
    case 6:  segm=a[6];  an=16;  anod(); _delay_us(600); segment(); if(a[6]==10)an=16;else an=6;  anod(); break;
    case 7:  segm=a[7];  an=16;  anod(); _delay_us(600); segment(); if(a[7]==10)an=16;else an=7;  anod(); break;
    case 8:  segm=a[8];  an=16;  anod(); _delay_us(400); segment(); an=8;  anod(); break;
    case 9:  segm=a[9];  an=16;  anod(); _delay_us(400); segment(); an=9;  anod(); break;
   case 10:  segm=a[10]; an=16;  anod(); _delay_us(400); segment(); an=10; anod(); break;
   case 11:  segm=a[11]; an=16;  anod(); _delay_us(400); segment(); an=11; anod(); break;
   case 12:  segm=a[12]; an=16;  anod(); _delay_us(400); segment(); an=12; anod(); break;
   case 13:  segm=a[13]; an=16;  anod(); _delay_us(400); segment(); an=13; anod(); break;
   case 14:  segm=a[14]; an=16;  anod(); _delay_us(400); segment(); an=14; anod(); break;
   case 15:  segm=a[15]; an=16;  anod(); _delay_us(400); segment(); an=15; anod(); break;
    }i++;if(i>15){i=0;}times++;if(times>600){times=0;}}

void segment(){                                                                
   switch(segm){                                                                 
             //       GECDXBFA
    case 0: PORTA = 0b01110111;break; 
    case 1: PORTA = 0b00100100;break; 
    case 2: PORTA = 0b11010101;break; 
    case 3: PORTA = 0b10110101;break; 
    case 4: PORTA = 0b10100110;break; 
    case 5: PORTA = 0b10110011;break; 
    case 6: PORTA = 0b11110011;break; 
    case 7: PORTA = 0b00100101;break; 
    case 8: PORTA = 0b11110111;break;          
    case 9: PORTA = 0b10110111;break;
              //       GECDXBFA
    case 20: PORTA = 0b01111111;break; 
    case 21: PORTA = 0b00101100;break; 
    case 22: PORTA = 0b11011101;break; 
    case 23: PORTA = 0b10111101;break; 
    case 24: PORTA = 0b10101110;break; 
    case 25: PORTA = 0b10111011;break; 
    case 26: PORTA = 0b11111011;break; 
    case 27: PORTA = 0b00101101;break; 
    case 28: PORTA = 0b11111111;break;          
    case 29: PORTA = 0b10111111;break;
              //       GECDXBFA
    case 10: PORTA = 0b00000000;break; // пусто 
    case 11: PORTA = 0b10000000;break; // -
    case 12: PORTA = 0b11010010;break; // t
    case 13: PORTA = 0b10000111;break; // гр
    case 14: PORTA = 0b01010011;break; // C
  }}
    

void anod(){
 switch(an){                                                             
    case 0: PORTC &=~(1<<PC7);PORTB |=(1<<PB0);break;
    case 1: PORTB &=~(1<<PB0);PORTB |=(1<<PB1);break; 
    case 2: PORTB &=~(1<<PB1);PORTB |=(1<<PB2);break; 
    case 3: PORTB &=~(1<<PB2);PORTB |=(1<<PB3);break; 
    case 4: PORTB &=~(1<<PB3);PORTB |=(1<<PB4);break; 
    case 5: PORTB &=~(1<<PB4);PORTB |=(1<<PB5);break; 
    case 6: PORTB &=~(1<<PB5);PORTB |=(1<<PB6);break; 
    case 7: PORTB &=~(1<<PB6);PORTB |=(1<<PB7);break;  
    case 8: PORTB &=~(1<<PB7);PORTD |=(1<<PD6);break;
    case 9: PORTD &=~(1<<PD6);PORTD |=(1<<PD7);break; 
   case 10: PORTD &=~(1<<PD7);PORTC |=(1<<PC2);break; 
   case 11: PORTC &=~(1<<PC2);PORTC |=(1<<PC3);break; 
   case 12: PORTC &=~(1<<PC3);PORTC |=(1<<PC4);break; 
   case 13: PORTC &=~(1<<PC4);PORTC |=(1<<PC5);break; 
   case 14: PORTC &=~(1<<PC5);PORTC |=(1<<PC6);break; 
   case 15: PORTC &=~(1<<PC6);PORTC |=(1<<PC7);break;       
   case 16: PORTB &=~(1<<PB0);PORTB &=~(1<<PB1);PORTB &=~(1<<PB2);PORTB &=~(1<<PB3);PORTB &=~(1<<PB4);PORTB &=~(1<<PB5);PORTB &=~(1<<PB6);PORTB &=~(1<<PB7);
   PORTD &=~(1<<PD6);PORTD &=~(1<<PD7);PORTC &=~(1<<PC2);PORTC &=~(1<<PC3);PORTC &=~(1<<PC4);PORTC &=~(1<<PC5);PORTC &=~(1<<PC6);PORTC &=~(1<<PC7);break;                                                     
  }} 

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

// reset
uint8_t therm_reset(){
    uint8_t i;
    PORTD &= ~(1 << PD0);
    DDRD |= (1 << PD0);
    delayMicroseconds(480);  
    DDRD &= ~(1 << PD0);
    delayMicroseconds(60);
    i=((PIND >> PD0) & 1);
    delayMicroseconds(420);
    return i;
}
// write bit
void therm_write_bit(uint8_t bit){
    PORTD &= ~(1 << PD0);
    DDRD |= (1 << PD0);
    delayMicroseconds(1);
    if(bit) DDRD &= ~(1 << PD0);
    delayMicroseconds(60);
    DDRD &= ~(1 << PD0);
}
// read bit
uint8_t therm_read_bit(void){
    uint8_t bit=0;
    PORTD &= ~(1 << PD0);
    DDRD |= (1 << PD0);
    delayMicroseconds(1);
    DDRD &= ~(1 << PD0);
    delayMicroseconds(14);
    if(PIND & (1 << PD0)) 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];
    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;
}

44

Re: Часы на игп-17 (разработка)

Александр, смогу проверить через полчаса-час, обязательно напишу

45

Re: Часы на игп-17 (разработка)

Проверил. Температура отображается, мерцание индикатора сохраняется без изменений

46

Re: Часы на игп-17 (разработка)

через несколько опросов датчика температура перестала отображаться корректно, затем стали отображаться нули:http://forum.rcl-radio.ru/uploads/images/2024/01/101dd6db071246b9e0086d934b51c261.jpg

47 (2024-01-21 02:43:32 отредактировано aprdetali)

Re: Часы на игп-17 (разработка)

Как я понимаю, проблема с протоколом 1wire. А если попробовать в качестве  выносного датчика использовать LM75 , он подключается по I2C?

48

Re: Часы на игп-17 (разработка)

да, через i2c

49

Re: Часы на игп-17 (разработка)

у меня есть датчики LM75, смогу проверить в железе

50

Re: Часы на игп-17 (разработка)

http://rcl-radio.ru/?p=83500

Начальный адрес I2C 0x48, перемычки адреса позволяют менять три младших разряда адреса:

BIN 1001000 = 0x48 | A0 = 0, A1 = 0, A2 = 0

// ATMEGA8535 16 MHZ
#define ADDR    0x68
#define CPU_F   16000000 // Clock Speed
#define SCL_F   400000 // // I2C Speed
#define TEMP_KORR 0 // гр.Цельсия

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

byte a[16],an,segm,i;
bool w=1,w1;
byte sec,min_,hour,datat,mont,year;
int temper,sett,times;
int set_hour,set_min,set_sec,set_datat,set_mont,set_year;


void setup() {
  DDRA = 0xFF;
  DDRB = 0xFF;
  DDRC = 0xFF;
  DDRD |= (1 << PD6)|(1 << PD7);
  PORTD |= (1 << PD2)|(1 << PD3)|(1 << PD4);
  TWBR = (((CPU_F)/(SCL_F)-16 )/2) ;
  TWSR = 0;
  DDRD |= (1 << PD5); // PWM +180V TIMER_1
  cli();
// TIMER_2  F = 16000000/128/100=1250 Hz (800 uS)
  OCR2 = 100;
  TCCR2 |= (1 << WGM21);
  TCCR2 |= (1 << CS22) | (1 << CS20);
  TIMSK |= (1 << OCIE2);
// TIMER_1  F = 16000000/1024/1/1=15625 Hz
  TCCR1A |= (1 << COM1A1);
  TCCR1B |= (1 << WGM12);
  TCCR1A |= (1 << WGM11) | (1 << WGM10);
  TCCR1B |= (1 << CS10); 
  ICR1 = 1023; 
  OCR1A = 350; 
  sei();
 // set_time(22,2,2,4,22,23,0);// год 00-99, ДН 1-7 (1=ВС), месяц 1-12, дата 1-31, час 0-23, минуты 0-59, секунды 0-59
  i2c_write(0x48,0x01,0);
  _delay_ms(200); 
}   

void loop() { 
  datat = ((i2c_read(ADDR,4) & 0x0F) + ((i2c_read(ADDR,4) & 0x70) >> 4) * 10);
  mont = ((i2c_read(ADDR,5) & 0x0F) + ((i2c_read(ADDR,5) & 0x70) >> 4) * 10);
  year = ((i2c_read(ADDR,6) & 0x0F) + ((i2c_read(ADDR,6) & 0x70) >> 4) * 10); 
  hour = (i2c_read(ADDR,2) & 0x0F) + (((i2c_read(ADDR,2) & 0x70) >> 4) * 10);
  min_ =  (i2c_read(ADDR,1) & 0x0F) + (((i2c_read(ADDR,1) & 0x70) >> 4) * 10);
  if(sec==10||sec==40) {temper = i2c_read_i2c_2bit(0x48,0)-TEMP_KORR ;}
  sec =  (i2c_read(ADDR,0) & 0x0F) + (((i2c_read(ADDR,0) & 0x70) >> 4) * 10);
  set_year = year;set_mont = mont;set_datat = datat;set_hour = hour;set_min = min_;set_sec = sec;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/// BUTTON /////////////  
  if(((PIND >> PD2) & 1) == 0){sett++;times=0;if(sett>6){sett=0;}wr();_delay_ms(300);}
  
  if(((PIND >> PD3) & 1) == 0 && sett==1){set_hour++;w1=1; if(set_hour>23){set_hour=23;} set_time(set_year,0,set_mont,set_datat,set_hour,set_min,set_sec);wr();_delay_ms(300);}
  if(((PIND >> PD4) & 1) == 0 && sett==1){set_hour--;w1=1; if(set_hour<0){set_hour=0;} set_time(set_year,0,set_mont,set_datat,set_hour,set_min,set_sec);wr();_delay_ms(300);}

  if(((PIND >> PD3) & 1) == 0 && sett==2){set_min++;w1=1; if(set_mont>59){set_min=59;} set_time(set_year,0,set_mont,set_datat,set_hour,set_min,set_sec);wr();_delay_ms(300);}
  if(((PIND >> PD4) & 1) == 0 && sett==2){set_min--;w1=1; if(set_mont<0){set_min=0;} set_time(set_year,0,set_mont,set_datat,set_hour,set_min,set_sec);wr();_delay_ms(300);}

  if(((PIND >> PD3) & 1) == 0 && sett==3){set_sec=0;w1=1; set_time(set_year,0,set_mont,set_datat,set_hour,set_min,set_sec);wr();_delay_ms(300);}
  if(((PIND >> PD4) & 1) == 0 && sett==3){set_sec=0;w1=1; set_time(set_year,0,set_mont,set_datat,set_hour,set_min,set_sec);wr();_delay_ms(300);}

  if(((PIND >> PD3) & 1) == 0 && sett==4){set_datat++;w1=1; if(set_datat>31){set_datat=31;} set_time(set_year,0,set_mont,set_datat,set_hour,set_min,set_sec);wr();_delay_ms(300);}
  if(((PIND >> PD4) & 1) == 0 && sett==4){set_datat--;w1=1; if(set_datat<0){set_datat=0;} set_time(set_year,0,set_mont,set_datat,set_hour,set_min,set_sec);wr();_delay_ms(300);}

  if(((PIND >> PD3) & 1) == 0 && sett==5){set_mont++;w1=1; if(set_mont>12){set_mont=12;} set_time(set_year,0,set_mont,set_datat,set_hour,set_min,set_sec);wr();_delay_ms(300);}
  if(((PIND >> PD4) & 1) == 0 && sett==5){set_mont--;w1=1; if(set_mont<0){set_mont=0;} set_time(set_year,0,set_mont,set_datat,set_hour,set_min,set_sec);wr();_delay_ms(300);} 

  if(((PIND >> PD3) & 1) == 0 && sett==6){set_year++;w1=1; if(set_year>50){set_year=50;} set_time(set_year,0,set_mont,set_datat,set_hour,set_min,set_sec);wr();_delay_ms(300);}
  if(((PIND >> PD4) & 1) == 0 && sett==6){set_year--;w1=1; if(set_year<22){set_year=22;} set_time(set_year,0,set_mont,set_datat,set_hour,set_min,set_sec);wr();_delay_ms(300);}      
 
 if(sett==0){
 if(min_==30&&sec<10){a[0]=sec%10+20;a[1]=sec%10+20;a[2]=sec%10+20;a[3]=sec%10+20;a[4]=sec%10+20;a[5]=sec%10+20;a[6]=sec%10+20;a[7]=sec%10+20;
  a[8]=sec%10+20;a[9]=sec%10+20;a[10]=sec%10+20;a[11]=sec%10+20;a[12]=sec%10+20;a[13]=sec%10+20;a[14]=sec%10+20;a[15]=sec%10+20;}
 else{ 
  if((sec>=10&&sec<=20)||(sec>=40&&sec<=50)){
  a[0]=14;
  a[1]=13;
  a[2]=temper%10;
  a[3]=temper/10%10+20;
  a[4]=temper/100;
  a[5]=10;
  a[6]=12;  
  }
  else{
  a[0]=10;
  a[1]=year%10;
  a[2]=year/10;
  a[3]=mont%10+20;
  a[4]=mont/10;
  a[5]=datat%10+20;
  a[6]=datat/10;
  }
  a[7]=10;
  a[8]=sec%10;
  a[9]=sec/10;
  a[10]=11;
  a[11]=min_%10;
  a[12]=min_/10;
  a[13]=11;
  a[14]=hour%10;
  a[15]=hour/10;
}
}// sett=0

 if(sett>0){
  a[0]=10;
  if(sett==6){if(times<=500||w1==1){w1=0;a[1]=year%10+20;a[2]=year/10;}else{a[1]=11;a[2]=11;}}
  if(sett==5){if(times<=500||w1==1){w1=0;a[3]=mont%10+20;a[4]=mont/10;}else{a[3]=11;a[4]=11;}}
  if(sett==4){if(times<=500||w1==1){w1=0;a[5]=datat%10+20;a[6]=datat/10;}else{a[5]=11;a[6]=11;}}
  a[7]=10;
  if(sett==3){if(times<=500||w1==1){w1=0;a[8]=sec%10;a[9]=sec/10;}else{a[8]=11;a[9]=11;}}
  a[10]=11;
  if(sett==2){if(times<=500||w1==1){w1=0;a[11]=min_%10;a[12]=min_/10;}else{a[11]=11;a[12]=11;}}
  a[13]=11;
  if(sett==1){if(times<=500||w1==1){w1=0;a[14]=hour%10;a[15]=hour/10;}else{a[14]=11;a[15]=11;}}
  if(sett!=3){a[8]=sec%10;a[9]=sec/10;}
}// sett>0

if(sett==0){_delay_ms(100);}
 }

void wr(){a[0]=10;a[1]=year%10; a[2]=year/10;a[3]=mont%10+20;a[4]=mont/10;a[5]=datat%10+20;a[6]=datat/10;a[7]=10;a[8]=sec%10;
a[9]=sec/10;a[10]=11;a[11]=min_%10;a[12]=min_/10;a[13]=11;a[14]=hour%10;a[15]=hour/10;}


ISR(TIMER2_COMP_vect){
  switch(i){
    case 0:  segm=a[0];  an=16;  anod(); _delay_us(600); segment(); if(a[0]==10)an=16;else an=0;  anod(); break;
    case 1:  segm=a[1];  an=16;  anod(); _delay_us(600); segment(); if(a[1]==10)an=16;else an=1;  anod(); break;
    case 2:  segm=a[2];  an=16;  anod(); _delay_us(600); segment(); if(a[2]==10)an=16;else an=2;  anod(); break;
    case 3:  segm=a[3];  an=16;  anod(); _delay_us(600); segment(); if(a[3]==10)an=16;else an=3;  anod(); break;
    case 4:  segm=a[4];  an=16;  anod(); _delay_us(600); segment(); if(a[4]==10)an=16;else an=4;  anod(); break;
    case 5:  segm=a[5];  an=16;  anod(); _delay_us(600); segment(); if(a[5]==10)an=16;else an=5;  anod(); break;
    case 6:  segm=a[6];  an=16;  anod(); _delay_us(600); segment(); if(a[6]==10)an=16;else an=6;  anod(); break;
    case 7:  segm=a[7];  an=16;  anod(); _delay_us(600); segment(); if(a[7]==10)an=16;else an=7;  anod(); break;
    case 8:  segm=a[8];  an=16;  anod(); _delay_us(400); segment(); an=8;  anod(); break;
    case 9:  segm=a[9];  an=16;  anod(); _delay_us(400); segment(); an=9;  anod(); break;
   case 10:  segm=a[10]; an=16;  anod(); _delay_us(400); segment(); an=10; anod(); break;
   case 11:  segm=a[11]; an=16;  anod(); _delay_us(400); segment(); an=11; anod(); break;
   case 12:  segm=a[12]; an=16;  anod(); _delay_us(400); segment(); an=12; anod(); break;
   case 13:  segm=a[13]; an=16;  anod(); _delay_us(400); segment(); an=13; anod(); break;
   case 14:  segm=a[14]; an=16;  anod(); _delay_us(400); segment(); an=14; anod(); break;
   case 15:  segm=a[15]; an=16;  anod(); _delay_us(400); segment(); an=15; anod(); break;
    }i++;if(i>15){i=0;}times++;if(times>600){times=0;}}

void segment(){                                                                
   switch(segm){                                                                 
             //       GECDXBFA
    case 0: PORTA = 0b01110111;break; 
    case 1: PORTA = 0b00100100;break; 
    case 2: PORTA = 0b11010101;break; 
    case 3: PORTA = 0b10110101;break; 
    case 4: PORTA = 0b10100110;break; 
    case 5: PORTA = 0b10110011;break; 
    case 6: PORTA = 0b11110011;break; 
    case 7: PORTA = 0b00100101;break; 
    case 8: PORTA = 0b11110111;break;          
    case 9: PORTA = 0b10110111;break;
              //       GECDXBFA
    case 20: PORTA = 0b01111111;break; 
    case 21: PORTA = 0b00101100;break; 
    case 22: PORTA = 0b11011101;break; 
    case 23: PORTA = 0b10111101;break; 
    case 24: PORTA = 0b10101110;break; 
    case 25: PORTA = 0b10111011;break; 
    case 26: PORTA = 0b11111011;break; 
    case 27: PORTA = 0b00101101;break; 
    case 28: PORTA = 0b11111111;break;          
    case 29: PORTA = 0b10111111;break;
              //       GECDXBFA
    case 10: PORTA = 0b00000000;break; // пусто 
    case 11: PORTA = 0b10000000;break; // -
    case 12: PORTA = 0b11010010;break; // t
    case 13: PORTA = 0b10000111;break; // гр
    case 14: PORTA = 0b01010011;break; // C
  }}
    

void anod(){
 switch(an){                                                             
    case 0: PORTC &=~(1<<PC7);PORTB |=(1<<PB0);break;
    case 1: PORTB &=~(1<<PB0);PORTB |=(1<<PB1);break; 
    case 2: PORTB &=~(1<<PB1);PORTB |=(1<<PB2);break; 
    case 3: PORTB &=~(1<<PB2);PORTB |=(1<<PB3);break; 
    case 4: PORTB &=~(1<<PB3);PORTB |=(1<<PB4);break; 
    case 5: PORTB &=~(1<<PB4);PORTB |=(1<<PB5);break; 
    case 6: PORTB &=~(1<<PB5);PORTB |=(1<<PB6);break; 
    case 7: PORTB &=~(1<<PB6);PORTB |=(1<<PB7);break;  
    case 8: PORTB &=~(1<<PB7);PORTD |=(1<<PD6);break;
    case 9: PORTD &=~(1<<PD6);PORTD |=(1<<PD7);break; 
   case 10: PORTD &=~(1<<PD7);PORTC |=(1<<PC2);break; 
   case 11: PORTC &=~(1<<PC2);PORTC |=(1<<PC3);break; 
   case 12: PORTC &=~(1<<PC3);PORTC |=(1<<PC4);break; 
   case 13: PORTC &=~(1<<PC4);PORTC |=(1<<PC5);break; 
   case 14: PORTC &=~(1<<PC5);PORTC |=(1<<PC6);break; 
   case 15: PORTC &=~(1<<PC6);PORTC |=(1<<PC7);break;       
   case 16: PORTB &=~(1<<PB0);PORTB &=~(1<<PB1);PORTB &=~(1<<PB2);PORTB &=~(1<<PB3);PORTB &=~(1<<PB4);PORTB &=~(1<<PB5);PORTB &=~(1<<PB6);PORTB &=~(1<<PB7);
   PORTD &=~(1<<PD6);PORTD &=~(1<<PD7);PORTC &=~(1<<PC2);PORTC &=~(1<<PC3);PORTC &=~(1<<PC4);PORTC &=~(1<<PC5);PORTC &=~(1<<PC6);PORTC &=~(1<<PC7);break;                                                     
  }} 

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

int i2c_read_i2c_2bit(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));
   int i2c_data0 = TWDR; 
 
   TWCR = (1<<TWINT) | (1<<TWEN);
  while(~TWCR&(1<<TWINT));
  int i2c_data1 = TWDR; 
 
   TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWSTO); // СТОП
  int i2c_data = (i2c_data0*10)+(i2c_data1>>5)*1.25;
  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);}
  }