U23 2008-2/Gruppe3

From C4 Wiki
< U23 2008-2
Revision as of 17:35, 24 November 2010 by Tobias (talk | contribs) (Reverted edits by Oxudocopaj (talk) to last revision by ME)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Teilnehmer:

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


thx 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:

<source lang=c>

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

volatile uint8_t data[100]; volatile uint8_t pos = 0; volatile uint8_t bit = 0; volatile uint8_t byte = 0;

enum {

   IDLE       = 0,
   HEADER_ON  = 1,
   HEADER_OFF = 2,
   DATA_HIGH  = 3,
   DATA_LOW   = 4,
   DONE       = 5,

} state;

ISR(PCINT1_vect) {

   if (state == IDLE) {
       state = HEADER_ON;
       TCNT1 = 0;
   } else if (state == HEADER_ON) {
       uint16_t value = TCNT1;
       TCNT1 = 0;
       /* check if header on is ~9ms */
       if (value >= 2500 && value <= 3300) {
           state = HEADER_OFF;
       } else {
           state = IDLE;
           pos = 0;
           bit = 0;
       }
   } else if (state == HEADER_OFF) {
       uint16_t value = TCNT1;
       TCNT1 = 0;
       /* check if header off is ~4.5ms */
       if (value >= 1000 && value <= 1800) {
           state = DATA_HIGH;
       } else {
           state = IDLE;
           pos = 0;
           bit = 0;
       }
   } else if (state == DATA_HIGH) {
       /* INSERT CODE HERE */
       uint16_t value = TCNT1;
       TCNT1 = 0;
       
       /* IR "Licht" ~560µs */
       if (value >= 150  && value <= 200) {
           state = DATA_LOW;
       } else {
           state = IDLE;
           pos = 0;
           bit = 0;
       }
   } else if (state == DATA_LOW){
   
       uint16_t value = TCNT1;
       TCNT1 = 0;
       
       /* IR "aus lang" 3x~560µs => 1*/
       if (value >= 490  && value <= 550) {
           state = DATA_HIGH;
           uart_printf("1");
           
           byte |= (1<<bit);
           bit++;
           if(bit == 8){
               bit = 0;
               data[pos] = byte;
               ++pos;
               byte = 0;
           }
       }
       /* IR "aus kurz" ~560µs => 0*/
       else if (value >= 150  && value <= 200) {
           state = DATA_HIGH;
           uart_printf("0");
           
           bit++;
           if(bit == 8){
               bit = 0;
               data[pos] = byte;
               ++pos;
               byte = 0;
           }        
       } else {
           state = IDLE;
           pos = 0;
           bit = 0;
       }            
   }

}

int main(void) {

   /* initialize serial uart */
   uart_init();
   uart_printf("booting\n");
   /* configure irrx as input */
   DDRC &= ~_BV(PC3);
   /* init led pin as output */
   DDRD |= _BV(PD3);
   /* configure ir input pin, with pullup */
   DDRC &= ~_BV(PC3);
   PORTC |= _BV(PC3);
   /* 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);
   sei();
   state = IDLE;
   while(1) {
       /* if ir rx is high, turn off led */
       if (PINC & _BV(PC3)) {
           PORTD &= ~_BV(PD3);
       } else {
           PORTD |= _BV(PD3);
       }


       if (uart_done == 1) {
           uart_printf("\n%s\n", uart_input);
           uart_len = 0;
           uart_done = 0;
       }
   }

} </source>

Abend 6: