#define F_CPU 128000UL #include // regerald, 20171110 struct bits { uint8_t b0:1; uint8_t b1:1; uint8_t b2:1; uint8_t b3:1; uint8_t b4:1; uint8_t b5:1; uint8_t b6:1; uint8_t b7:1; } __attribute__((__packed__)); #define SBIT(port,pin) ((*(volatile struct bits*)&port).b##pin) #define DIO SBIT( PORTB, 0 ) #define DIO_DDR SBIT( DDRB, 0 ) #define SCK SBIT( PORTB, 2 ) #define SCK_DDR SBIT( DDRB, 2 ) #define RCK SBIT( PORTB, 1 ) #define RCK_DDR SBIT( DDRB, 1 ) void spi_wr8( uint8_t val ) // function to write a byte to software SPI { for( uint8_t i = 8; i; i-- ){ SCK = 0; DIO = 0; if( val & 0x80 ) // MSB first DIO = 1; val <<= 1; SCK = 1; } } void displ( int16_t t) //function to print value on the Led display with two shift registers { if (t<0){t = -1*t;}; //no negative values allowed uint8_t numdigit[5]; //splitingt number into digits numdigit[0]=t/1000; numdigit[1]=t%1000/100; numdigit[2]=t%100/10; numdigit[3]=t%10; numdigit[4]=0; const uint8_t chr[5] = { //digit mask 0b00001000, 0b00000100, 0b00000010, 0b00000001, 0b00000000 //Display of the fifth digit is a walkaround. Otherwise last digit would be brighter than the others. }; const uint8_t digit[10] = { // 7 segment mask 0b11000000, // 0 0b11111001, // 1 0b10100100, // 2 0b10110000, // 3 0b10011001, // 4 0b10010010, // 5 0b10000010, // 6 0b11111000, // 7 0b10000000, // 8 0b10010000 // 9 }; for(uint8_t i = 0; i <= 4; i++){ RCK = 0; spi_wr8(digit[numdigit[i]]); spi_wr8(chr[i]); RCK = 1; } } main() { DDRB =0b00000111; // PB0, PB1, PB2, will now be the output pins for the display int16_t t = 0; //this number is our turn count uint8_t ca = 0; //"case variable", needed to see the direction of turn while(1){ if(bit_is_set(PINB,3) && bit_is_clear(PINB,4) && ca == 0){ //PIN is "clear" when light do not pass ca = 1; } if(bit_is_set(PINB, 3) && bit_is_set(PINB, 4) && ca == 1){ ca = 2; } if(bit_is_set(PINB, 4) && bit_is_clear(PINB,3) && ca == 2){ ca = 3; } if(bit_is_set(PINB,4) && bit_is_clear(PINB,3) && ca == 0){ ca = 4; } if(bit_is_set(PINB, 3) && bit_is_set(PINB, 4) && ca == 4){ ca = 5; } if(bit_is_clear(PINB, 4) && bit_is_set(PINB,3) && ca == 5){ ca = 6; } if(bit_is_clear(PINB, 3) && bit_is_clear(PINB, 4)){ ca = 0; } if (ca == 3){ //turned into ++ direction t++; ca = 0; //reset the "case" } if (ca == 6){ //turned into ++ direction t--; ca = 0; //reset the "case" } displ(t); } }