#define F_CPU 1000000UL #include #include #include #include #include #include // 5 digit 7 segment display, common anode, connected to the PB0-PB7 // Interrupt on change signals connected to the PD2-PD3 // A button is pulled up, connected to PD0 // PNP transistors as the emitter keys for the common anodes of the digits, connected to the PC0-PC4 volatile float N = 0; volatile uint8_t ibutt = 0; void PrintNum(float N) // Head 5 digit 7-seg function, 2 decimal precision, common anode { int32_t n = round(N*100); if (n<0){n *= -1;} while (n>99999){n = n-100000;} uint8_t numd[5] = {0,0,0,0,0}; numd[0]=n/10000; numd[1]=n%10000/1000; numd[2]=n%1000/100; numd[3]=n%100/10; numd[4]=n%10; uint8_t digit[5] = {0b11111110, 0b11111101, 0b11111011, 0b11110111, 0b11101111}; //Digit mask const uint8_t dmask[10] = {0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8, 0x80, 0x90}; //digits 0-9, "0 = led on". PORTB = dmask[numd[0]]; if(numd[0] == 0){PORTB = 0xFF;} if(N<0){PORTB &= 0xBF;} PORTC = digit[0]; _delay_us(250); if(numd[0] == 0 && numd[1] == 0){PORTB = 0xFF;} else{PORTB = dmask[numd[1]];} PORTC = digit[1]; _delay_us(250); PORTB = dmask[numd[2]]; PORTB &= 0b01111111; PORTC = digit[2]; _delay_us(250); PORTB = dmask[numd[3]]; PORTC = digit[3]; _delay_us(250); PORTB = dmask[numd[4]]; PORTC = digit[4]; _delay_us(250); PORTC = 0xFF; } void buttoncheck(void) { if(!(PIND&0x01)) { if (ibutt < 3) {ibutt++;} else { N = 0; // Do something if button pressed ibutt = 0; } } else {ibutt = 0;} } ISR(INT0_vect) // { if(PIND&0x04) //PD2 is up { if(PIND&0x08) //PD3 is also up {N += 0.0423333333333;} else {N -= 0.0423333333333;} } else { if(PIND&0x08) //PD3 is up {N -= 0.0423333333333;} else {N += 0.0423333333333;} } } ISR(INT1_vect) // { if(PIND&0x08) //PD2 is up { if(PIND&0x04) //PD3 is also up {N -= 0.0423333333333;} else {N += 0.0423333333333;} } else { if(PIND&0x04) //PD3 is up {N += 0.0423333333333;} else {N -= 0.0423333333333;} } } main() { DDRB = 0xFF; // output port DDRC = 0xFF; // output port DDRD = 0x00; // input port PORTB = 0xFF; PORTC = 0x00; PORTD = 0x0D; // pull-up resistors on PD0, PD2, PD3 GICR |= (1<