51

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

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

52

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

Скорость i2c надо понизить

#define SCL_F   400000 // // I2C Speed

замените на

#define SCL_F   100000 // // I2C Speed

53

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

Заменил. Не помогло...

54

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

// 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;
int uuu;


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() { 
  if(uuu==0){
  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 ;}
  }
  uuu++;if(uuu>100){uuu=0;}
  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);}
  } 

55

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

Мерцание индикатора осталось. Перестал опрашиваться датчик, отображаются нули

56

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

Частота кварца 16 МГц?

57

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

Да, кварц 16 МГц, неизменно с начала опытов

58

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

if(sec==10||sec==40) {temper = i2c_read_i2c_2bit(0x48,0)-TEMP_KORR ;}

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

59

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

При выборе частоты кварца в 16 МГц нажимали "Записать загрузчик"?

60

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

Нет.

61

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

Нажмите "Записать загрузчик" и повторите загрузку скетча.

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

62

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

Да, все так и сделал. Температура отображается, но индикатор по прежнему мерцает. Может проблема в том, что я использую не плату ардуино, а просто контроллер? Вот фьюзы на всякий случайhttp://forum.rcl-radio.ru/uploads/images/2024/01/49e0f3401b1bafdc71259c8aefa790c6.png

63

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

Это после записи крайней версии скетча

64

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

Прошивайте через Arduino IDE используя USBasp.

http://forum.rcl-radio.ru/uploads/images/2024/01/73767aa7dfb5610f4aca7cf9aab423b2.png


http://forum.rcl-radio.ru/uploads/images/2024/01/e6d49f813975c62956949a6558b3b8fb.png

65

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

Я именно так и делаюhttp://forum.rcl-radio.ru/uploads/images/2024/01/21d1f30d9ffdfa6c7615e0d6a51c26c0.jpg

66

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

if(sec==10||sec==40) {temper = i2c_read_i2c_2bit(0x48,0)-TEMP_KORR ;}

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

67

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

При выборе частоты кварца в 16 МГц нажимали "Записать загрузчик"?

68

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

Нет, мерцание не пропадает. Температура - отображаются нули. Вопрос: Вы тестировали в железе скетч с датчиком DHT, там  не было мерцания индикатора? по сути дела датчик LM75 это аналогичное доп.устройство, подключенное по I2c

69

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

Прошу прощения, нужно отлучиться на пару часов

70

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

Вы тестировали в железе скетч с датчиком DHT, там  не было мерцания индикатора?

Мерцания не было.

71

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

Хотел для эксперимента загрузить Ваш скетч для датчика с первой страницы:
http://forum.rcl-radio.ru/viewtopic.php?pid=5383#p5383
но при проверке/компиляции выдает ошибку:
Compilation error: 'byte min' redeclared as different kind of symbol

72

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

// ATMEGA8535 16 MHZ
#define ADDR_HTU21D  0x40
#define ADDR    0x68
#define CPU_F   16000000 // Clock Speed
#define SCL_F   100000 // // I2C Speed

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

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


int main(){
  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(ADDR_HTU21D,0xE6,0b10000001); // 11 bit
  _delay_ms(200); 
   

while(1) { 
  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); 
  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);

  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;
  
  if(sec==10&&f2==0){f2=1;w2=1;}if(sec>10){f2=0;}
  if(sec==20&&f3==0){f3=1;w3=1;}if(sec>20){f3=0;}
  if(w2==1) {w2=0;temper = (-46.85+175.72*(i2c_read_2byte(ADDR_HTU21D,0xE3)/65536.00))*10;}
  if(w3==1) {w3=0;humidity = (-6+125*(i2c_read_2byte(ADDR_HTU21D,0xE5)/65536.00))*10;}
  
  
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

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

 }}

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(); if(a[8]==10)an=16;else an=8;  anod(); break;
    case 9:  segm=a[9];  an=16;  anod(); _delay_us(600); segment(); if(a[9]==10)an=16;else an=9;  anod(); break;
   case 10:  segm=a[10]; an=16;  anod(); _delay_us(600); segment(); if(a[10]==10)an=16;else an=10; anod(); break;
   case 11:  segm=a[11]; an=16;  anod(); _delay_us(600); segment(); if(a[11]==10)an=16;else an=11; anod(); break;
   case 12:  segm=a[12]; an=16;  anod(); _delay_us(600); segment(); if(a[12]==10)an=16;else an=12; anod(); break;
   case 13:  segm=a[13]; an=16;  anod(); _delay_us(600); segment(); if(a[13]==10)an=16;else an=13; anod(); break;
   case 14:  segm=a[14]; an=16;  anod(); _delay_us(600); segment(); if(a[14]==10)an=16;else an=14; anod(); break;
   case 15:  segm=a[15]; an=16;  anod(); _delay_us(600); segment(); if(a[15]==10)an=16;else 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
    case 15: PORTA = 0b11100110;break; // H
    case 16: PORTA = 0b11110000;break; // o
  }}
    

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

int i2c_read_2byte(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<<8)+i2c_data1;
  return i2c_data;
  }  

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

73

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

Проверил. Понятно, что контроллер виснет при обращении к датчику температуры. Но появляется чуть заметное мерцание индикатора, значительно слабее, чем при работе со скетчами для DS18b20 и LM75

74

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

Я понизил динамическую индикацию до 60 Гц, посмотрите мерцание, что поменялось

// 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 = 255;
  TCCR2 |= (1 << WGM21);
  TCCR2 |= (1 << CS22) | | (1 << CS21)|(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);}
  } 

75

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

выдает ошибку при компиляции:
Compilation error: expected primary-expression before '|' token