Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- a.)
- #include <avr/io.h>
- #define TASTE PINB
- volatile int i,q,T;
- int s;
- int main(void) {
- s = 10; // s ist die Schrittweite fuer die Tastensteuerung
- T = 2000; // T ist die gesamte Periodendauer
- q = 1000; // q ist die aktuelle "An" Zeit
- PORTC = 0b00101010; // ein Anfangsmuster
- DDRC = 63; // Ausgaenge aktivieren
- int q1=1000;
- int q2=1000;
- // Beginn Hauptschleife
- for (;;) { // Eine Endlosschleife
- //PORTC ^= 63; // Invertieren der Lampen an Port B
- PORTC = 0b11;
- for (i=0;i<T;i++) { // eine Periode zaehlen
- if (i==q1){
- PORTC &=~1; //n1 ^=0b11111110
- }
- if (i==q2){
- PORTC &=~2; //n1 ^=0b11111110
- }
- }
- if (PINB &1){
- if(q1 > 0 && q2 > 0){
- q1-=s;
- q2-=s;
- }
- }
- if (PINB &4){
- q1+=s;
- q2+=s;
- }
- //if (TASTE & 4) q-=s; if (q<0) q=0; // Pulsbreite erniedrigen
- }
- }
- --------------------------------
- b.)
- // Program dimmer_irq_c SK
- #include <avr/io.h>
- #include <avr/interrupt.h>
- // Interruptroutine fuer PWM A
- ISR(TIMER1_COMPA_vect) {
- PORTC &= ~1; // Lampe aus
- }
- ISR(TIMER1_COMPB_vect) {
- PORTC &= ~2; // Lampe aus
- }
- // Interruptroutine fuer Overflow
- ISR(TIMER1_OVF_vect) {
- PORTC |= 3; // Lampe wieder an
- if (PINB &4){
- if(OCR1A<65535-100){
- OCR1A += 100; // Vergleichswert erhoehen (Wozu ? :-)
- OCR1B += 100; // Vergleichswert erhoehen (Wozu ? :-)
- }
- }
- }
- // Hauptprogramm
- int main(void) {
- // Timer Initialisieren
- TCCR1B=(1 << CS10); // Prescaler: 1, Mode: 0 ("normal Mode")
- OCR1A = 32767; // Comparewert A, 50 %
- OCR1B = 32767; // Comparewert A, 50 %
- // Interrupt fuer Timer 1 compare A,und Overflow aktivieren
- TIMSK1 |= (1 << OCIE1A) | (1 << TOIE1) | (1 << OCIE1B);
- DDRC = 63; // LEDs aktivieren
- PORTC = 0; // Alle LEDs aus
- sei(); // Interrupts global aktivieren
- while(1) {
- if(PINB&1){
- OCR1A = 1;
- OCR1B = 1;
- }
- }; // Endlosschleife
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement