Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- uprava 6.2.2016
- */
- #define F_CPU 16000000UL //clock speed 16MHz
- #include <avr/io.h>
- #include <util/delay.h>
- #include <avr/interrupt.h>
- #include <avr/io.h>
- void delay(int t)
- {
- static int elapsedDelay=0;
- for(int j=elapsedDelay;j<t;j++)
- {
- if ((bit_is_clear(PINC, 0)) || (bit_is_clear(PINC, 1)) || (bit_is_clear(PINC, 2)) || (bit_is_clear(PINC, 3)) || (bit_is_clear(PINC, 4)) || (bit_is_clear(PINC, 5)))
- {
- elapsedDelay = j;
- return;
- }
- else
- {
- elapsedDelay=0;
- }
- _delay_ms(1);
- }
- }
- volatile int tot_overflow; //rozdiel medzi uint8_t a int je, ze int ma o 24 bytov viac
- int main(void)
- {
- /* na zaciatku sa musime vzdy rozhodnut, ci budu vystupne registre DDRx ako output alebo input */
- DDRC &= ~(1 << PINC0); // -> input (0) - tlacitko 1
- PORTC |= 1 << PINC0; // -> high level (1) - tlacitko 1 je na high urovni
- DDRC &= ~(1 << PINC1); // -> input (0) - tlacitko 2
- PORTC |= 1 << PINC1; // -> high level (1) - tlacitko 2 je na high urovni
- DDRC &= ~(1 << PINC2); // -> input (0) - tlacitko 3
- PORTC |= 1 << PINC2; // -> high level (1) - tlacitko 3 je na high urovni
- DDRC &= ~(1 << PINC3); // -> input (0) - tlacitko 4
- PORTC |= 1 << PINC3; // -> high level (1) - tlacitko 4 je na high urovni
- DDRC &= ~(1 << PINC4); // -> input (0) - tlacitko 5
- PORTC |= 1 << PINC4; // -> high level (1) - tlacitko 5 je na high urovni
- DDRC &= ~(1 << PINC5); // -> input (0) - tlacitko 6
- PORTC |= 1 << PINC5; // -> high level (1) - tlacitko 6 je na high urovni
- int Stlacene_off_0percent = 0;
- int Stlacene_100percent = 0;
- int Stlacene_66percent = 0;
- int Stlacene_33percent = 0;
- int Stlacene_pulse = 0;
- int Stlacene_blik = 0;
- int tlacitko = 0;
- int i=0;
- DDRD |= 0xFF; //vsetky piny na porte D ako vystup
- // prescaler = 256
- TCCR2 |= (1 << CS22)|(1 << CS21);
- // initialize counter
- TCNT2 = 0;
- // enable overflow interrupt
- TIMSK |= (1 << TOIE2);
- // enable global interrupts
- sei();
- // initialize overflow counter variable
- tot_overflow = 0;
- TCCR1A |= 1<<WGM11 | 1<<COM1A1 | 1<<COM1A0; //Set OC1A/OC1B on compare match, clear OC1A/OC1B at BOTTOM, (inverting mode), PWM, Phase Correct, 9-bit
- TCCR1B |= 1<<WGM12 | 1<<WGM13 | 1<<CS10; //| 1<<CS11; //cs10 ON -> prescaler off cs10, cs11 ON -> prescaler 64, clear on timer compare, top value ICR1
- ICR1 = 1000; //maximalna hodnota z 65535 ... (16MHz/64) /1000= 250Hz co je nasa PWM frekvencia fpwm=fclkIO/(N*(1+TOP))
- while (1)
- {
- /* PWM 0% */
- if (bit_is_clear(PINC, 0))
- {
- if (Stlacene_off_0percent == 0)
- {
- tlacitko = 1;
- Stlacene_off_0percent = 1;
- }
- }
- else
- {
- Stlacene_off_0percent = 0;
- }
- /* PWM 100% */
- if (bit_is_clear(PINC, 1))
- {
- if (Stlacene_100percent == 0)
- {
- tlacitko = 2;
- Stlacene_100percent = 1;
- }
- }
- else
- {
- Stlacene_100percent = 0;
- }
- /* PWM 66% */
- if (bit_is_clear(PINC, 2))
- {
- if (Stlacene_66percent == 0)
- {
- tlacitko = 3;
- Stlacene_66percent = 1;
- }
- }
- else
- {
- Stlacene_66percent = 0;
- }
- /* PWM 33% */
- if (bit_is_clear(PINC, 3))
- {
- if (Stlacene_33percent == 0)
- {
- tlacitko = 4;
- Stlacene_33percent = 1;
- }
- }
- else
- {
- Stlacene_33percent = 0;
- }
- /* pulse na PINC4 tlacitku*/
- if (bit_is_clear(PINC, 4))
- {
- if (Stlacene_pulse == 0)
- {
- tlacitko = 5;
- Stlacene_pulse = 0;
- }
- }
- else
- {
- Stlacene_pulse = 0;
- }
- /* BLIK */
- if (bit_is_clear(PINC, 5))
- {
- if (Stlacene_blik == 0)
- {
- tlacitko = 6;
- Stlacene_blik = 1;
- }
- }
- else
- {
- Stlacene_blik = 0;
- }
- switch (tlacitko)
- {
- case 1: //turn off
- OCR1A=ICR1;
- break;
- case 2: //100%
- OCR1A=ICR1*0;
- break;
- case 3: //66%
- OCR1A=ICR1*0.66;
- break;
- case 4: //33%
- OCR1A=ICR1*0.33;
- break;
- case 5: //pulse
- for(i=0;i<50;i++)
- {
- OCR1A = ICR1 - i*20;
- delay(20);
- }
- for(i=50;i>=0;i--)
- {
- OCR1A = ICR1 - i*20;
- delay(20);
- }
- break;
- case 6: //blik
- if (tot_overflow >= 24)
- {
- OCR1A ^= (1 << ICR1); // toggles the led, to iste ako PORTB ^= (1 << 0);
- TCNT2 = 0; // reset counter
- tot_overflow = 0; // reset overflow counter
- }
- break;
- default:
- OCR1A=ICR1;
- }
- }
- }
- ISR(TIMER2_OVF_vect)
- {
- // keep a track of number of overflows
- tot_overflow++;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement