Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // zdefiniowane w makefile
- //#define F_CPU 1000000L
- #include <avr/io.h>
- #include <util/delay.h>
- #include <stdlib.h>
- #define SEGMENT_COUNT 8
- #define false 0
- #define true 1
- // liczby
- uint8_t liczby[10];
- void init(void)
- {
- // b0 jako wyjscie
- DDRB = _BV(0);
- // b0 na wysoki
- PORTB |= _BV(0);
- // b1 jako wejscie
- DDRB &= ~_BV(1);
- // b1 na wysoki
- PORTB |= _BV(1);
- // b2 jako wejscie
- DDRB &= ~_BV(2);
- // b2 na wysoki
- PORTB |= _BV(2);
- // b3 jako wejscie
- DDRB &= ~_BV(3);
- // b3 na wysoki
- PORTB |= _BV(3);
- // b4 jako wyjscie
- DDRB |= _BV(4);
- // b3 na wysoki
- PORTB |= _BV(4);
- // a jako wyjscie
- DDRA = 0xFF;
- // D jako wyjscie
- DDRD = 0xFF;
- }
- void powitanie(void)
- {
- PORTA = 0;
- PORTD = 0;
- _delay_ms(1000);
- PORTA = 0xFF;
- PORTD = 0xFF;
- _delay_ms(500);
- PORTA = 0;
- PORTD = 0;
- _delay_ms(1000);
- for (uint8_t i=0; i<SEGMENT_COUNT; ++i)
- {
- _delay_ms(100);
- // na wysoki
- PORTA |= _BV(i);
- PORTD |= _BV(i);
- }
- // zapalenie kropki
- PORTA &= ~_BV(3);
- PORTD &= ~_BV(3);
- }
- void flash(uint8_t **reg, uint8_t count, uint8_t delay)
- {
- uint8_t *before = malloc(sizeof(uint8_t) * count);
- for (uint8_t i = 0; i < count; ++i)
- {
- before[i] = *(reg[i]);
- *(reg[i]) = 0xFF;
- }
- _delay_ms(delay);
- for (uint8_t i = 0; i < count; ++i)
- {
- *(reg[i]) = before[i];
- }
- free(before);
- }
- void sound(uint16_t freq, uint32_t time_ms)
- {
- if (freq != 0)
- {
- uint16_t period = 1000000/freq;
- uint32_t time = 0;
- while(time < time_ms*1000)
- {
- PORTB ^= _BV(4);
- _delay_us(period);
- time += period;
- }
- }
- else
- {
- _delay_ms(time_ms);
- }
- }
- uint8_t menu(uint8_t max)
- {
- uint8_t wybor = 1;
- uint8_t clicked = false;
- while(1)
- {
- wybor %= max;
- PORTA = liczby[(wybor/10)%10];
- PORTD = liczby[wybor%10] & ~_BV(3);
- // b1 niski
- if (((PINB & _BV(3)) == 0) && !clicked)
- {
- _delay_ms(1);
- if ((PINB & _BV(3)) == 0)
- {
- ++wybor;
- clicked = true;
- }
- }
- else if (((PINB & _BV(2)) == 0) && !clicked)
- {
- _delay_ms(1);
- if ((PINB & _BV(2)) == 0)
- {
- --wybor;
- clicked = true;
- }
- }
- else if (((PINB & _BV(1)) == 0) && !clicked)
- {
- _delay_ms(1);
- if ((PINB & _BV(1)) == 0)
- {
- return wybor;
- }
- }
- else if (clicked)
- {
- _delay_ms(1);
- if ((PINB & _BV(3)) != 0 && (PINB & _BV(2)) != 0 && (PINB & _BV(3)) != 0)
- {
- clicked = false;
- }
- }
- }
- }
- int main(void)
- {
- liczby[0] = (uint8_t)~_BV(0) & ~_BV(1) & ~_BV(2) & ~_BV(4) & ~_BV(5) & ~_BV(6);
- liczby[1] = (uint8_t)~_BV(2) & ~_BV(4);
- liczby[2] = (uint8_t)~_BV(5) & ~_BV(4) & ~_BV(7) & ~_BV(0) & ~_BV(1);
- liczby[3] = (uint8_t)~_BV(1) & ~_BV(2) & ~_BV(4) & ~_BV(5) & ~_BV(7);
- liczby[4] = (uint8_t)~_BV(2) & ~_BV(4) & ~_BV(6) & ~_BV(7);
- liczby[5] = (uint8_t)~_BV(1) & ~_BV(2) & ~_BV(5) & ~_BV(6) & ~_BV(7);
- liczby[6] = (uint8_t)~_BV(0) & ~_BV(1) & ~_BV(2) & ~_BV(5) & ~_BV(6) & ~_BV(7);
- liczby[7] = (uint8_t)~_BV(2) & ~_BV(4) & ~_BV(5);
- liczby[8] = (uint8_t)_BV(3);
- liczby[9] = (uint8_t)_BV(3) | _BV(0);
- init();
- powitanie();
- #define PAUZA 0
- #define C 262
- #define D 294
- #define E 329
- #define F 350
- #define G 392
- #define A 440
- #define H 494
- #define C2 523
- #define D2 (D*2)
- #define G0 196
- #define ES 311
- #define GES 370
- #define HES 466
- #define E2S 621
- uint16_t tempo = 120;
- #define CWIERCNUTA 60000
- typedef struct nuta
- {
- uint16_t freq;
- uint16_t time_ms;
- };
- struct piosenki_T
- {
- uint8_t dlugosc;
- struct nuta* melodia;
- } piosenki[2];
- struct nuta koleda[] =
- {
- {C, CWIERCNUTA},
- {C, CWIERCNUTA/2},
- {G0, CWIERCNUTA/2},
- {C, CWIERCNUTA/2},
- {D, CWIERCNUTA/2},
- {E, CWIERCNUTA},
- {E, CWIERCNUTA/2},
- {D, CWIERCNUTA/2},
- {E, CWIERCNUTA/2},
- {F, CWIERCNUTA/2},
- {G, CWIERCNUTA/2},
- {A, CWIERCNUTA/2},
- {G, CWIERCNUTA},
- {F, CWIERCNUTA},
- {E, CWIERCNUTA},
- {D, CWIERCNUTA},
- {PAUZA, CWIERCNUTA},
- {C, CWIERCNUTA},
- {C, CWIERCNUTA/2},
- {G0, CWIERCNUTA/2},
- {C, CWIERCNUTA/2},
- {D, CWIERCNUTA/2},
- {E, CWIERCNUTA},
- {E, CWIERCNUTA/2},
- {D, CWIERCNUTA/2},
- {E, CWIERCNUTA/2},
- {F, CWIERCNUTA/2},
- {G, CWIERCNUTA/2},
- {A, CWIERCNUTA/2},
- {G, CWIERCNUTA},
- {F, CWIERCNUTA},
- {E, CWIERCNUTA},
- {D, CWIERCNUTA},
- {PAUZA, CWIERCNUTA},
- {G, CWIERCNUTA},
- {G, CWIERCNUTA/2},
- {F, CWIERCNUTA/2},
- {E, CWIERCNUTA/2},
- {D, CWIERCNUTA/2},
- {C, CWIERCNUTA},
- {C, CWIERCNUTA/2},
- {G0, CWIERCNUTA/2},
- {C, CWIERCNUTA/2},
- {D, CWIERCNUTA/2},
- {G, CWIERCNUTA},
- {G, CWIERCNUTA/2},
- {F, CWIERCNUTA/2},
- {E, CWIERCNUTA/2},
- {D, CWIERCNUTA/2},
- {C, CWIERCNUTA},
- {C, CWIERCNUTA/2},
- {G0, CWIERCNUTA/2},
- {C, CWIERCNUTA/2},
- {D, CWIERCNUTA/2},
- {G, CWIERCNUTA/2},
- {A, CWIERCNUTA/2},
- {G, CWIERCNUTA/2},
- {F, CWIERCNUTA/2},
- {E, CWIERCNUTA/2},
- {F, CWIERCNUTA/2},
- {G, CWIERCNUTA/2},
- {A, CWIERCNUTA/2},
- {G, CWIERCNUTA/2},
- {F, CWIERCNUTA/2},
- {E, CWIERCNUTA/2},
- {F, CWIERCNUTA/2},
- {G, CWIERCNUTA/2},
- {G, CWIERCNUTA/2},
- {A, CWIERCNUTA},
- {G, CWIERCNUTA},
- {F, CWIERCNUTA/2},
- {E, CWIERCNUTA/2},
- {D, CWIERCNUTA},
- {C, CWIERCNUTA}
- };
- piosenki[0].dlugosc = sizeof(koleda)/(sizeof(struct nuta));
- piosenki[0].melodia = koleda;
- struct nuta imperial[] =
- {
- {G, CWIERCNUTA},
- {G, CWIERCNUTA},
- {G, CWIERCNUTA},
- {ES, (CWIERCNUTA*3)/4},
- {HES, (CWIERCNUTA/4)},
- {G, CWIERCNUTA},
- {ES, (CWIERCNUTA*3)/4},
- {HES, (CWIERCNUTA/4)},
- {G, CWIERCNUTA*2},
- {D2, CWIERCNUTA},
- {D2, CWIERCNUTA},
- {D2, CWIERCNUTA},
- {E2S, (CWIERCNUTA*3)/4},
- {HES, (CWIERCNUTA/4)},
- {GES, (CWIERCNUTA*3)/2},
- {ES, (CWIERCNUTA*3)/4},
- {HES, (CWIERCNUTA/4)},
- {G, CWIERCNUTA*2}
- };
- piosenki[1].dlugosc = sizeof(imperial)/(sizeof(struct nuta));
- piosenki[1].melodia = imperial;
- while(1)
- {
- uint8_t wybor = menu(3);
- // tempo
- if (wybor == 0)
- {
- do
- {
- powitanie();
- tempo = menu(100) * 10;
- } while(tempo == 0);
- }
- else
- {
- uint16_t licznik = 1;
- while (1)
- {
- if (((PINB & _BV(3)) == 0))
- {
- _delay_ms(1);
- if ((PINB & _BV(3)) == 0)
- {
- ++tempo;
- }
- }
- else if ((PINB & _BV(2)) == 0)
- {
- _delay_ms(1);
- if ((PINB & _BV(2)) == 0)
- {
- ++tempo;
- }
- }
- if (licznik > piosenki[wybor-1].dlugosc)
- {
- break;
- }
- PORTA = liczby[(licznik/10)%10];
- PORTD = liczby[licznik%10];
- sound(piosenki[wybor-1].melodia[licznik-1].freq, piosenki[wybor-1].melodia[licznik-1].time_ms/tempo);
- ++licznik;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement