/* * pokus_hodiny_ds1307.c * * Created: 29.05.2021 10:39:46 * Author : Já */ #define F_CPU 8000000UL #include #include #include #include ////////////////////////// pridano #include #include #include #include ////////////////////////// konec //Definice pozic segmentů na pinech portu #define _s_A PD2 #define _s_B PD0 #define _s_C PD6 #define _s_D PD4 #define _s_E PD3 #define _s_F PD1 #define _s_G PD7 #define _s_dot PD5 /////////////////////////////////////// pridano ////////// i2c read and write command in readable format #define READ 1 #define WRITE 0 unsigned char bcd2bin(unsigned char); unsigned char bin2bcd(unsigned char); void i2c_init(void); unsigned char i2c_start(unsigned char address); void i2c_start_wait(unsigned char address); unsigned char i2c_rep_start(unsigned char address); void i2c_stop(void); unsigned char i2c_write( unsigned char data ); unsigned char i2c_readAck(void); unsigned char i2c_readNak(void); volatile unsigned char button_pressed=0,timer_0_i=0,timer_2_i=0,active_cathode=0,temp_digit=0; volatile unsigned char time[4]={16,16,16,16},can_change=0,min_plus=0,hour_plus; volatile unsigned char temp_time[4]={16,16,16,16},anim=0; ///////////////////////////////////////// konec*/ const unsigned char segs[] = //Definice číslic na 7segmentové displeji { _BV(_s_A) | _BV(_s_B) | _BV(_s_C) | _BV(_s_D) | _BV(_s_E) | _BV(_s_F), //0 _BV(_s_B) | _BV(_s_C), //1 _BV(_s_A) | _BV(_s_B) | _BV(_s_D) | _BV(_s_E) | _BV(_s_G), //2 _BV(_s_A) | _BV(_s_B) | _BV(_s_C) | _BV(_s_D) | _BV(_s_G), //3 _BV(_s_B) | _BV(_s_C) | _BV(_s_F) | _BV(_s_G), //4 _BV(_s_A) | _BV(_s_C) | _BV(_s_D) | _BV(_s_F) | _BV(_s_G), //5 _BV(_s_A) | _BV(_s_C) | _BV(_s_D) | _BV(_s_E) | _BV(_s_F) | _BV(_s_G), //6 _BV(_s_A) | _BV(_s_B) | _BV(_s_C), //7 _BV(_s_A) | _BV(_s_B) | _BV(_s_C) | _BV(_s_D) | _BV(_s_E) | _BV(_s_F) | _BV(_s_G), //8 _BV(_s_A) | _BV(_s_B) | _BV(_s_C) | _BV(_s_D) | _BV(_s_F) | _BV(_s_G), //9 _BV(_s_A) | _BV(_s_B) | _BV(_s_C) | _BV(_s_E) | _BV(_s_F) | _BV(_s_G), //A _BV(_s_C) | _BV(_s_D) | _BV(_s_E) | _BV(_s_F) | _BV(_s_G), //B _BV(_s_A) | _BV(_s_D) | _BV(_s_E) | _BV(_s_F), //C _BV(_s_B) | _BV(_s_C) | _BV(_s_D) | _BV(_s_E) | _BV(_s_G), //D _BV(_s_A) | _BV(_s_D) | _BV(_s_E) | _BV(_s_F) | _BV(_s_G), //E _BV(_s_A) | _BV(_s_E) | _BV(_s_F) | _BV(_s_G) //F }; /////////////////////////////////// pridano ISR (TIMER0_OVF_vect) { timer_0_i++; if(timer_0_i==10) { TIMSK &= ~(1<59){min_1++;sec=0;}; if(min_1>9){min_1=0;min_10++;}; if(min_10>5){min_10=0;hour_1++;}; if(hour_1>9){hour_1=0;hour_10++;}; if(hour_10>1 && hour_1>3){hour_1=0;hour_10=0;}; if(++show_t==4) show_t=0; switch(show_t) { case 0: //show minutes PORTC = 0x04; PORTD = (~segs[min_1]) & ~_BV(_s_dot); break; case 1: //show 10 minutes PORTC = 0x08; PORTD = (~segs[min_10]) & ~_BV(_s_dot); break; case 2: //show hours PORTC = 0x11; PORTD = (~segs[hour_1]);// & ~_BV(_s_dot); break; case 3: //show 10hours PORTC = 0x12; PORTD = (~segs[hour_10]);// & ~_BV(_s_dot); break; default: show_t = 0; break; } return; } #define B1() (bit_is_clear(PINB,0)) //3 #define B2() (bit_is_clear(PINB,1)) //4 #define B_WAIT 300 #define nop() asm volatile ("nop;") int main(void) { ////////////////////////////// pridano unsigned char numtemp=0; TCCR0 =0b00000101; TCCR2 =0b00000101; TIMSK |= 1<>4 == 0) time[0]=16; else time[0]=bcd2bin(numtemp)/10; time[1]=bcd2bin(numtemp)%10; _delay_ms(100); //some break... i2c_stop(); } } } ////////// converts a bcd number to binary unsigned char bcd2bin(unsigned char bcd) { unsigned char bin=0; bcd&=~(1<<7); bin+=bcd&0x0f; bcd>>=4; bin+=bcd*10; return bin; } ////////// converts a binary number to bcd unsigned char bin2bcd(unsigned char bin) { unsigned char bcd=0; bcd+=bin/10; bcd<<=4; bcd+=bin%10; return bcd; } void i2c_init(void) { TWSR = 0; TWBR = 32; }// i2c_init ////////// start i2c operation, parameter is the slave's address or'ed with the READ or WRITE command in the LSB bit ////////// returns with 0-val if the device was unreachable, 1 if OK unsigned char i2c_start(unsigned char address) { unsigned char twst; // start signal TWCR = (1<