Created
May 14, 2019 11:07
-
-
Save avr-programmierung/d01d006d8359d95313bd761bd57f7400 to your computer and use it in GitHub Desktop.
ATmega88 @ 8MHz Monoflop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* monoflop_01.c ATmega88 @ 8MHz */ | |
#include <avr/io.h> | |
#include <avr/interrupt.h> | |
#define led1 (1<<PB0) | |
#define led2 (1<<PB1) | |
uint8_t glowtime = 10, glowtime_led_2, taster_flag = 0; | |
uint16_t timer_overflow_counter = 0; | |
void init_timer_0 (void) | |
{ | |
TCCR0B = (1<<CS01) + (1<<CS00); // Prescaler = 64 -> 8Mhz/64 = 125000 clockspeed | |
TIMSK0 = (1<<TOIE0); // Timer0 overflow interrupt enable | |
TCNT0 = 6; // Timer mit 6 vorladen | |
} | |
int main(void) | |
{ | |
DDRB = 0xFF; // PORTB Richtungsregister = Ausgang | |
DDRC = 0xF0; // PC0...PC3 auf Eingang | |
PORTC = 0xF1; // PC0 = Pull up | |
init_timer_0(); // Timer 0 initialisieren | |
sei(); // Interrupts einschalten | |
while(1) | |
{ | |
if (PINC &(1<<PINC0)) // Wenn Taster gedrückt wurde... | |
{ | |
taster_flag = 1; // Flag für Taster wurde gedrückt setzen | |
glowtime_led_2 = glowtime; // glowtime_led_2 auf Anfangswert 10 setzen | |
} | |
} | |
} | |
ISR(TIMER0_OVF_vect) | |
{ | |
if (timer_overflow_counter <= 499) // Zählvariable 499 mal hochzählen | |
timer_overflow_counter ++; | |
else // Den else-Zweig beim jedem 500, Durchlauf ausführen | |
{ | |
TCNT0 = 6; // reload timer0 | |
PORTB ^= led1; // LED1 toggeln | |
timer_overflow_counter = 0; // Zähler zurücksetzen | |
if (taster_flag == 1) // Taster wurde gedrückt | |
{ | |
PORTB |= led2; // LED ON | |
glowtime_led_2 --; // glowtime_led_2 pro Sekunde um 1 runter zählen | |
if (glowtime_led_2 == 0) | |
{ | |
PORTB &= ~led2; // LED OFF | |
glowtime_led_2 = glowtime; // glowtime_led_2 auf Anfangswert 10 setzen | |
taster_flag = 0; // Taster flag zurücksetzen | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment