Difference between revisions of "U23 2008-2/Gruppe4"
(→2. Projektabend (27. Oktober)) |
m (Reverted edits by Oxudocopaj (talk) to last revision by 134.147.35.46) |
||
(9 intermediate revisions by 5 users not shown) | |||
Line 97: | Line 97: | ||
'''Code:''' | '''Code:''' | ||
<source lang ="c"> | <source lang ="c"> | ||
+ | #include <avr/io.h> | ||
+ | #include <string.h> | ||
+ | #include <stdio.h> | ||
+ | #include <avr/pgmspace.h> | ||
+ | #include "uart.h" | ||
+ | |||
+ | static void zeitmessen(); | ||
+ | |||
+ | int main(void) | ||
+ | { | ||
+ | uint8_t status; | ||
+ | status = 0; | ||
+ | |||
+ | /* initialize serial uart */ | ||
+ | uart_init(); | ||
+ | |||
+ | /* configure irrx as input */ | ||
+ | DDRC &= ~_BV(PC3); | ||
+ | |||
+ | /* init led pin as output */ | ||
+ | DDRD |= _BV(PD3); | ||
+ | |||
+ | |||
+ | |||
+ | while(1) { | ||
+ | /* if ir rx is high, turn off led */ | ||
+ | if (PINC & _BV(PC3)) | ||
+ | { | ||
+ | /* if ir was on, display "aus" to indicate it's been turned off */ | ||
+ | if (status==1) | ||
+ | { | ||
+ | uart_printf("aus"); | ||
+ | status=0; | ||
+ | } | ||
+ | |||
+ | PORTD &= ~_BV(PD3); | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | /* if ir was off, display "an" to indicate it's been turned on */ | ||
+ | if (status==0) | ||
+ | { | ||
+ | uart_printf("an"); | ||
+ | status=1; | ||
+ | } | ||
+ | |||
+ | PORTD |= _BV(PD3); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | /* static void zeitmessen() | ||
+ | { | ||
+ | |||
+ | } | ||
+ | */ | ||
</source> | </source> | ||
+ | |||
+ | Unser Ergebnis vom 2. Abend: das Programm fragt den Infrarot Empfänger ab, und gibt über die Schnittstelle eine Meldung aus, wenn sich der Zustand ändert. | ||
=== Aufgabe zur Vorbereitung === | === Aufgabe zur Vorbereitung === | ||
Line 115: | Line 173: | ||
'''Code:''' | '''Code:''' | ||
<source lang ="c"> | <source lang ="c"> | ||
+ | #include <stdio.h> | ||
+ | #include <stdlib.h> | ||
+ | |||
+ | int zahlengen( int a, int e) | ||
+ | { | ||
+ | double r = e - a + 1; | ||
+ | return (a + (int)(r * rand() /(RAND_MAX+1.0))); | ||
+ | } | ||
+ | |||
+ | int main(void) { | ||
+ | srand(time(0)); //Seed initialisieren | ||
+ | int min=1, max=7,i=0; | ||
+ | for(i=0;i<=100;i++){ | ||
+ | printf("%i\n",zahlengen(min,max));} | ||
+ | getchar(); | ||
+ | return EXIT_SUCCESS; | ||
+ | } | ||
</source> | </source> | ||
− | |||
=== Aufgabe zur Vorbereitung === | === Aufgabe zur Vorbereitung === | ||
Line 130: | Line 204: | ||
=== Aufgabe === | === Aufgabe === | ||
+ | Um den Jammer zu realisieren, haben wir die folgenden beiden Interrupts aus dem vorgegebenen Code geändert geändert: | ||
'''Code:''' | '''Code:''' | ||
+ | |||
<source lang ="c"> | <source lang ="c"> | ||
+ | |||
+ | ISR(PCINT1_vect) | ||
+ | 100 { | ||
+ | 101 /* do nothing if we are just processing a code in the main loop, | ||
+ | 102 * or no more space is available for a timer value */ | ||
+ | 103 if (done || pos == MAX) | ||
+ | 104 return; | ||
+ | 105 | ||
+ | 106 /* if this would be the first timing value ever recorded, and the | ||
+ | 107 * state before was high (=idle), do not record the timing value | ||
+ | 108 * and just reset the timer */ | ||
+ | 109 if (state && pos == 0) { | ||
+ | 110 TCNT1 = 0; | ||
+ | 111 /* else record the timing value */ | ||
+ | 112 } else { | ||
+ | 113 /* store current timer value in code[] | ||
+ | 114 * and reset the timer */ | ||
+ | 115 code[pos++] = TCNT1; | ||
+ | 116 TCNT1 = 0; | ||
+ | 117 } | ||
+ | 118 | ||
+ | 119 if (mode == MODE_JAM) | ||
+ | 120 { | ||
+ | 121 if (pos % 3 == 0) | ||
+ | 122 { | ||
+ | 123 /* turn off interrupt */ | ||
+ | 124 PCICR &= ~_BV(PCIE1); | ||
+ | 125 /* change interrupt trigger value to 2 * last timing */ | ||
+ | 126 OCR1A = (code[pos-1] * 2); | ||
+ | 127 /* set TIMER1 to 0 to count jam time */ | ||
+ | 128 TCNT1 = 0; | ||
+ | 129 /* turn on ir */ | ||
+ | 130 ir_enable(PWM_FREQ); | ||
+ | 131 } | ||
+ | 132 } | ||
+ | |||
+ | |||
+ | |||
+ | /* timer 1 compare A interrupt service function */ | ||
+ | 145 ISR(TIMER1_COMPA_vect) | ||
+ | 146 { | ||
+ | 147 /* do nothing if we are just processing a code in the main loop */ | ||
+ | 148 if (done) | ||
+ | 149 return; | ||
+ | 150 | ||
+ | 151 /* if some code has been received */ | ||
+ | 152 if (pos > 0) { | ||
+ | 153 /* if pos is odd, one last 'off'-timing is missing, fill with zero */ | ||
+ | 154 if (pos % 2 == 1) | ||
+ | 155 code[pos++] = 0; | ||
+ | 156 | ||
+ | 157 /* signal main */ | ||
+ | 158 done = 1; | ||
+ | 159 | ||
+ | 160 /* turn on third led */ | ||
+ | 161 PORTD |= _BV(PD6); | ||
+ | 162 } | ||
+ | 163 | ||
+ | 164 if (mode == MODE_JAM) | ||
+ | 165 { | ||
+ | 166 /* turn off ir */ | ||
+ | 167 ir_disable(); | ||
+ | 168 /* reset interrupt trigger */ | ||
+ | 169 OCR1A = F_CPU/5/64; | ||
+ | 170 /* reset TIMER1 */ | ||
+ | 171 TCNT1 = 0; | ||
+ | 172 /* turn on interrrupt */ | ||
+ | 173 PCICR |= _BV(PCIE1); | ||
+ | 174 } | ||
+ | 175 } | ||
+ | |||
+ | |||
</source> | </source> | ||
− | |||
=== Aufgabe zur Vorbereitung === | === Aufgabe zur Vorbereitung === | ||
Line 153: | Line 300: | ||
'''Code:''' | '''Code:''' | ||
<source lang ="c"> | <source lang ="c"> | ||
+ | #include <avr/io.h> | ||
+ | #include <string.h> | ||
+ | #include <stdio.h> | ||
+ | #include <avr/pgmspace.h> | ||
+ | #include <avr/interrupt.h> | ||
+ | |||
+ | #include "uart.h" | ||
+ | |||
+ | volatile uint8_t data[100]; | ||
+ | volatile uint8_t pos = 0; | ||
+ | volatile uint8_t bit = 0; | ||
+ | |||
+ | enum { | ||
+ | IDLE = 0, | ||
+ | HEADER_ON = 1, | ||
+ | HEADER_OFF = 2, | ||
+ | DATA_HIGH = 3, | ||
+ | DATA_LOW = 4, | ||
+ | DONE = 5, | ||
+ | } state = IDLE; | ||
+ | |||
+ | static void reset_irrx(void) | ||
+ | { | ||
+ | state = IDLE; | ||
+ | pos = 0; | ||
+ | bit = 0; | ||
+ | data[0] = 0; | ||
+ | } | ||
+ | |||
+ | |||
+ | ISR(PCINT1_vect) { | ||
+ | uint16_t value = TCNT1; | ||
+ | TCNT1 = 0; | ||
+ | |||
+ | if (state == IDLE) { | ||
+ | state = HEADER_ON; | ||
+ | } else if (state == HEADER_ON) { | ||
+ | /* check if header on is ~9ms */ | ||
+ | if (value >= 2500 && value <= 3300) { | ||
+ | state = HEADER_OFF; | ||
+ | } else { | ||
+ | reset_irrx(); | ||
+ | } | ||
+ | } else if (state == HEADER_OFF) { | ||
+ | /* check if header off is ~4.5ms */ | ||
+ | if (value >= 1000 && value <= 1800) { | ||
+ | state = DATA_HIGH; | ||
+ | } else { | ||
+ | uart_printf("r%u %u\n",state,value); | ||
+ | reset_irrx(); | ||
+ | } | ||
+ | } else if (state == DATA_HIGH) { | ||
+ | /* check if data1 is ~560us */ | ||
+ | if (value >= 140 && value <= 270) { | ||
+ | state = DATA_LOW; | ||
+ | } else { | ||
+ | uart_printf("r%u %u\n",state,value); | ||
+ | reset_irrx(); | ||
+ | } | ||
+ | } else if (state == DATA_LOW) { | ||
+ | state = DATA_HIGH; | ||
+ | |||
+ | /* check if data1 is ~560us -> value = 0 */ | ||
+ | if (value >= 140 && value <= 260) { | ||
+ | bit++; | ||
+ | } | ||
+ | /* check if data2 is ~1690us -> value = 1 */ | ||
+ | else if (value >= 400 && value <= 600) { | ||
+ | data[pos] |= _BV(bit); | ||
+ | bit++; | ||
+ | } else { | ||
+ | uart_printf("r%u %u\n",state,value); | ||
+ | reset_irrx(); | ||
+ | } | ||
+ | |||
+ | if (bit == 8) | ||
+ | { | ||
+ | pos++; | ||
+ | data[pos + 0] = 0; | ||
+ | data[pos + 1] = 0; | ||
+ | bit = 0; | ||
+ | } | ||
+ | |||
+ | } | ||
+ | } | ||
+ | |||
+ | ISR(TIMER1_COMPA_vect) | ||
+ | { | ||
+ | TCNT1 = 0; | ||
+ | if (state == DONE || state == IDLE) | ||
+ | return; | ||
+ | |||
+ | if (pos > 0) | ||
+ | { | ||
+ | state = DONE; | ||
+ | } else { | ||
+ | reset_irrx(); | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | 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 */ | ||
+ | OCR1A = 4800; | ||
+ | /* enable OCR1A interrupt */ | ||
+ | TIMSK1 = _BV(OCIE1A); | ||
+ | |||
+ | sei(); | ||
+ | |||
+ | while(1) { | ||
+ | /* if ir rx is high, turn off led */ | ||
+ | if (PINC & _BV(PC3)) { | ||
+ | PORTD &= ~_BV(PD3); | ||
+ | } else { | ||
+ | PORTD |= _BV(PD3); | ||
+ | } | ||
+ | |||
+ | if (state == DONE) | ||
+ | { | ||
+ | uart_printf("->%s\n",data); | ||
+ | reset_irrx(); | ||
+ | } | ||
+ | |||
+ | } | ||
+ | } | ||
</source> | </source> | ||
− | |||
=== Aufgabe zur Vorbereitung === | === Aufgabe zur Vorbereitung === |
Latest revision as of 17:36, 24 November 2010
Contents
Mitglieder
- Christian
- Steffen
- Stefan
- Martin
1. Projektabend (20. Oktober)
Aufgabe
Eine LED soll zum leuchten gebracht werden.
Code:
<source lang ="c">
- include <avr/io.h>
int main(void) {
/* PC4 als Ausgang konfig. */ DDRC = &b10000; /* PC4 auf high setzen */ PORTC = &b10000; /* Endlosschleife */ while(1){ }
}
</source>
Aufgabe zur Vorbereitung
Eine LED Sequenz
Code:
<source lang ="c">
- include <avr/io.h>
- include <util/delay.h>
static void led_ausgabe(uint8_t led, uint16_t zeit);
int main(void)
{
uint16_t anzeigedauer = 1000; /* Ausgaenge konfig. */ DDRC = (1 << DDC4); DDRD = (1 << DDD3) | (1 << DDD6) | (1 << DDD7); /* Animation */ while(1){ led_ausgabe (0b1000,anzeigedauer); led_ausgabe (0b0100,anzeigedauer); led_ausgabe (0b0010,anzeigedauer); led_ausgabe (0b0001,anzeigedauer); }
}
static void led_ausgabe(uint8_t led, uint16_t zeit) {
if (led & 0b1){ PORTC |= (1 << DDC4); } else{ PORTC &= ~(1 << DDC4);}
if (led & 0b10){ PORTD |= (1 << DDD3);} else{ PORTD &= ~(1 << DDD3);} if (led & 0b100){ PORTD |= (1 << DDD6);} else{ PORTD &= ~(1 << DDD6);} if (led & 0b1000){ PORTD |= (1 << DDD7);} else{ PORTD &= ~(1 << DDD7);} _delay_ms(zeit);
} </source>
2. Projektabend (27. Oktober)
Aufgabe
Code: <source lang ="c">
- include <avr/io.h>
- include <string.h>
- include <stdio.h>
- include <avr/pgmspace.h>
- include "uart.h"
static void zeitmessen();
int main(void) {
uint8_t status; status = 0;
/* initialize serial uart */ uart_init();
/* configure irrx as input */ DDRC &= ~_BV(PC3);
/* init led pin as output */ DDRD |= _BV(PD3);
while(1) { /* if ir rx is high, turn off led */ if (PINC & _BV(PC3))
{
/* if ir was on, display "aus" to indicate it's been turned off */
if (status==1) { uart_printf("aus"); status=0; }
PORTD &= ~_BV(PD3); } else { /* if ir was off, display "an" to indicate it's been turned on */ if (status==0) { uart_printf("an"); status=1; }
PORTD |= _BV(PD3); }
}
}
/* static void zeitmessen() {
}
- /
</source>
Unser Ergebnis vom 2. Abend: das Programm fragt den Infrarot Empfänger ab, und gibt über die Schnittstelle eine Meldung aus, wenn sich der Zustand ändert.
Aufgabe zur Vorbereitung
Code: <source lang ="c"> </source>
3. Projektabend (03. November)
Aufgabe
Code: <source lang ="c">
- include <stdio.h>
- include <stdlib.h>
int zahlengen( int a, int e) {
double r = e - a + 1; return (a + (int)(r * rand() /(RAND_MAX+1.0)));
}
int main(void) { srand(time(0)); //Seed initialisieren int min=1, max=7,i=0; for(i=0;i<=100;i++){ printf("%i\n",zahlengen(min,max));} getchar(); return EXIT_SUCCESS; } </source>
Aufgabe zur Vorbereitung
Code: <source lang ="c"> </source>
4. Projektabend (10. November)
Aufgabe
Um den Jammer zu realisieren, haben wir die folgenden beiden Interrupts aus dem vorgegebenen Code geändert geändert:
Code:
<source lang ="c">
ISR(PCINT1_vect) 100 { 101 /* do nothing if we are just processing a code in the main loop, 102 * or no more space is available for a timer value */ 103 if (done || pos == MAX) 104 return; 105 106 /* if this would be the first timing value ever recorded, and the 107 * state before was high (=idle), do not record the timing value 108 * and just reset the timer */ 109 if (state && pos == 0) { 110 TCNT1 = 0; 111 /* else record the timing value */ 112 } else { 113 /* store current timer value in code[] 114 * and reset the timer */ 115 code[pos++] = TCNT1; 116 TCNT1 = 0; 117 } 118 119 if (mode == MODE_JAM) 120 { 121 if (pos % 3 == 0) 122 { 123 /* turn off interrupt */ 124 PCICR &= ~_BV(PCIE1); 125 /* change interrupt trigger value to 2 * last timing */ 126 OCR1A = (code[pos-1] * 2); 127 /* set TIMER1 to 0 to count jam time */ 128 TCNT1 = 0; 129 /* turn on ir */ 130 ir_enable(PWM_FREQ); 131 } 132 }
/* timer 1 compare A interrupt service function */
145 ISR(TIMER1_COMPA_vect) 146 { 147 /* do nothing if we are just processing a code in the main loop */ 148 if (done) 149 return; 150 151 /* if some code has been received */ 152 if (pos > 0) { 153 /* if pos is odd, one last 'off'-timing is missing, fill with zero */ 154 if (pos % 2 == 1) 155 code[pos++] = 0; 156 157 /* signal main */ 158 done = 1; 159 160 /* turn on third led */ 161 PORTD |= _BV(PD6); 162 } 163 164 if (mode == MODE_JAM) 165 { 166 /* turn off ir */ 167 ir_disable(); 168 /* reset interrupt trigger */ 169 OCR1A = F_CPU/5/64; 170 /* reset TIMER1 */ 171 TCNT1 = 0; 172 /* turn on interrrupt */ 173 PCICR |= _BV(PCIE1); 174 } 175 }
</source>
Aufgabe zur Vorbereitung
Code: <source lang ="c"> </source>
5. Projektabend (17. November)
Aufgabe
Code: <source lang ="c">
- include <avr/io.h>
- include <string.h>
- include <stdio.h>
- include <avr/pgmspace.h>
- include <avr/interrupt.h>
- include "uart.h"
volatile uint8_t data[100]; volatile uint8_t pos = 0; volatile uint8_t bit = 0;
enum {
IDLE = 0, HEADER_ON = 1, HEADER_OFF = 2, DATA_HIGH = 3, DATA_LOW = 4, DONE = 5,
} state = IDLE;
static void reset_irrx(void) {
state = IDLE; pos = 0; bit = 0; data[0] = 0;
}
ISR(PCINT1_vect) {
uint16_t value = TCNT1; TCNT1 = 0;
if (state == IDLE) { state = HEADER_ON; } else if (state == HEADER_ON) { /* check if header on is ~9ms */ if (value >= 2500 && value <= 3300) { state = HEADER_OFF; } else { reset_irrx(); } } else if (state == HEADER_OFF) { /* check if header off is ~4.5ms */ if (value >= 1000 && value <= 1800) { state = DATA_HIGH; } else { uart_printf("r%u %u\n",state,value); reset_irrx(); } } else if (state == DATA_HIGH) { /* check if data1 is ~560us */ if (value >= 140 && value <= 270) { state = DATA_LOW; } else { uart_printf("r%u %u\n",state,value); reset_irrx(); } } else if (state == DATA_LOW) { state = DATA_HIGH;
/* check if data1 is ~560us -> value = 0 */ if (value >= 140 && value <= 260) { bit++; } /* check if data2 is ~1690us -> value = 1 */ else if (value >= 400 && value <= 600) { data[pos] |= _BV(bit); bit++; } else { uart_printf("r%u %u\n",state,value); reset_irrx(); }
if (bit == 8) { pos++; data[pos + 0] = 0; data[pos + 1] = 0; bit = 0; }
}
}
ISR(TIMER1_COMPA_vect) {
TCNT1 = 0; if (state == DONE || state == IDLE) return;
if (pos > 0) { state = DONE; } else { reset_irrx(); }
}
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 */ OCR1A = 4800; /* enable OCR1A interrupt */ TIMSK1 = _BV(OCIE1A);
sei();
while(1) { /* if ir rx is high, turn off led */ if (PINC & _BV(PC3)) { PORTD &= ~_BV(PD3); } else { PORTD |= _BV(PD3); }
if (state == DONE) { uart_printf("->%s\n",data); reset_irrx(); }
}
} </source>
Aufgabe zur Vorbereitung
Code: <source lang ="c"> </source>
6. Projektabend (24. November)
Aufgabe
Code: <source lang ="c"> </source>
Aufgabe zur Vorbereitung
Code: <source lang ="c"> </source>
Abschlussabend (vorrausichtlich 27. November)
Vorstellung der Ergebnisse