Difference between revisions of "U23 2008-2/Gruppe3"

From C4 Wiki
Jump to: navigation, search
(Teilnehmer:)
(Abend 2:)
Line 48: Line 48:
 
}
 
}
 
</source>
 
</source>
 +
Programmbeschreibung:
 +
LED leuchtet sobald IR-Signal empfangen wird!
  
 
+
'''IR LED-Blinker mit seriell output'''
 
<source lang=c>
 
<source lang=c>
 
#include <avr/io.h>
 
#include <avr/io.h>
Line 89: Line 91:
 
}
 
}
 
</source>
 
</source>
 +
Programmbeschreibung:
 +
LED leuchtet sobald IR-Signal empfangen wird und gibt eine Meldung über die serielle Schnittstelle aus!
  
 
== '''Abend 3:''' ==
 
== '''Abend 3:''' ==

Revision as of 19:37, 4 November 2008

Teilnehmer:

  • David (der von weit her, FSC Laptop)
  • Vincent (das schwarze Schaf der Apple-Familie)
  • Christian (ASUS Notebook)
  • André (der ohne Beschreibung)


thx @ Christian für das code hochladen!

Abend 1:

Abend 2:

Binäre Operatoren (einzelne Bits modifizieren):
Memo was DDRC &= ~_BV(PC3) bedeutet:
Legende: ~ ist Negation, & logisches UND, | logisches ODER

Beispiel: <source lang=c>

PC3  00100000

~PC3 11011111 Alter Beispielwert: 10100110 Ergebnis Alt & ~PC3: 10000110 </source>

IR LED-Blinker <source lang=c>

  1. include <avr/io.h>

int main(void) { /* unset PC3 -> IR_IN ist Input */

 	DDRC &= ~_BV(PC3);

/* set PC4 -> Output für LED1 */ DDRC |= _BV(PC4);

while (1) { /* Wenn PIN kein Eingang verzeichnet :wird */ if (PINC & _BV(PC3)) { /* unset PC4 -> LED1 aus */ PORTC &= ~_BV(PC4); } /* Wenn PIN EIngang verzeichnet */ else { PORTC |= _BV(PC4); } } } </source> Programmbeschreibung: LED leuchtet sobald IR-Signal empfangen wird!

IR LED-Blinker mit seriell output <source lang=c>

  1. include <avr/io.h>
  2. include <avr/pgmspace.h>
  3. include <string.h>
  4. include <stdio.h>
  5. include <avr/pgmspace.h>
  6. include "uart.h"


int main(void) { uart_init(); /* unset PC3 -> IR_IN ist Input */

 	DDRC &= ~_BV(PC3);

/* set PC4 -> Output für LED1 */ DDRC |= _BV(PC4);

int led = 0;

while (1) { /* Wenn PIN kein Eingang verzeichnet :wird */ if (PINC & _BV(PC3)) { if(led == 1) { uart_printf("LED1 aus\n"); /* unset PC4 -> LED1 aus */ PORTC &= ~_BV(PC4); led = 0; } } /* Wenn PIN EIngang verzeichnet */ else { if(led == 0) { uart_printf("LED1 an\n"); PORTC |= _BV(PC4); led = 1; } } } } </source> Programmbeschreibung: LED leuchtet sobald IR-Signal empfangen wird und gibt eine Meldung über die serielle Schnittstelle aus!

Abend 3:

<source lang=c>

  1. include <avr/io.h>
  2. include <avr/interrupt.h>
  3. include "uart.h"

/* constants */

  1. define MAX 160

/* 38khz: freq = F_CPU/(2*prescaler*frequency)

* 20mhz/(2*8*38khz) = ~33
* real frequency: 20mhz/(2*8*33) = ~37878Hz */
  1. define PWM_FREQ 33

uint8_t sending_active;

/* enum {

   MODE_IDLE = 0,
   MODE_SENDING = 1,
   MODE_SLEEP = 2,

} mode = MODE_IDLE;

  • /

/* set up timer 0 to generate a carrier using pwm at freq on pin OC0B (PD5) */ static void ir_enable(uint8_t freq) {

   /* timer 0: fast pwm mode, clear OC0B on match, prescaler 8 */
   TCCR0A = _BV(WGM00) | _BV(COM0B1);
   TCCR0B = _BV(CS01) | _BV(WGM02);
   /* set frequency */
   OCR0A = freq;
   /* set duty-cycle to 25% */
   OCR0B = freq/4;

}

/* disable timer 0 and pwm generation */ static void ir_disable(void) {

   TCCR0A = 0;
   TCCR0B = 0;

}


/* pin change interrupt 1 service function */ ISR(PCINT1_vect) {

   /* if this would be the first timing value ever recorded, and the
    * state before was high (=idle), do not record the timing value
    * and just reset the timer */
   
   
           /* store current timer value in code[]
        * and reset the timer */
       //code[pos++] = TCNT1;
       //TCNT1 = 0;
   /* toggle second led */
   PORTD ^= _BV(PD3);
   
   TCNT1 = 0;
   
   /* disable IR interrupt */
   PCMSK1 &= ~_BV(PCINT11);
   /* set global var */
   sending_active = 1;    
   
   ir_enable(PWM_FREQ);
   
   uart_printf("jamming activated\n");

}

/* timer 1 compare A interrupt service function */ ISR(TIMER1_COMPA_vect) {

   TCNT1 = 0;
   if(sending_active) {
       uart_printf("jamming deactivated\n");
       ir_disable();
       sending_active = 0;
       
       /* enable IR interrupt */
       PCMSK1 |= _BV(PCINT11);
   }

}

int main(void) {

   /* initialize uart */
   uart_init();
   uart_printf("rumpus ir-jammer\n");
   /* configure led pins as outputs and turn leds off */
   DDRC |= _BV(PC4);
   DDRD |= _BV(PD3) | _BV(PD6) | _BV(PD7);
   PORTC &= ~_BV(PC4);
   PORTD &= ~(_BV(PD3) | _BV(PD6) | _BV(PD7));
   /* configure ir input pin, with pullup */
   DDRC &= ~_BV(PC3);
   PORTC |= _BV(PC3);
   /* configure ir send pin as output, set low */
   DDRD |= _BV(PD5);
   PORTD &= ~_BV(PD5);
   /* set global var */
   sending_active = 0;
   /* wait until pin is high (no ir carrier is detected) */
   while(!(PINC & _BV(PC3)));
   /* enable pin change interrupt 1 for ir input pin (PC3/PCINT11) */
   PCMSK1 |= _BV(PCINT11);
   PCICR |= _BV(PCIE1);
   /* configure timer1 with prescaler 64 and CTC for measuring ir timings */
   TCCR1B = _BV(CS11) | _BV(CS10) | _BV(WGM12);
   /* configure timer action after 200ms: 20mhz/64/5 */
   OCR1A = F_CPU/5/64;
   /* enable OCR1A interrupt */
   TIMSK1 = _BV(OCIE1A);
   /* signal user availability by turning on led 1 */
   PORTC |= _BV(PC4);
   /* enable interrupts */
   sei();
   /* signal the user that the analyzer part has started by turning led 1 on */
   PORTC |= _BV(PC3);
   /* clear interrupt flags, enable timer1 and pin change interrupts */
   TIFR1 = _BV(OCIE1A);
   TIMSK1 |= _BV(OCIE1A);
   PCMSK1 |= _BV(PCINT11);
   while(1) {
   }

} </source>

Abend 4:

Abend 5:

Abend 6: