Skip to content

Commit 1ea031d

Browse files
committed
Revised version of L15
1 parent 028dd94 commit 1ea031d

23 files changed

Lines changed: 1219 additions & 20 deletions

15_TimerUndInterrupts.md

Lines changed: 478 additions & 8 deletions
Large diffs are not rendered by default.

exampleCode/14_ADC/ReadingLightSensor/ReadingLightSensor.ino

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,35 @@
33
#endif
44

55
#include <avr/io.h>
6-
#include <util/delay.h>
76

87
int readADC(int channel) {
98
int i; int result = 0;
109
// Den ADC aktivieren und Teilungsfaktor auf 64 stellen
1110
ADCSRA = (1<<ADEN) | (1<<ADPS2) | (1<<ADPS1);
1211
// Kanal des Multiplexers & Interne Referenzspannung (2,56 V)
13-
ADMUX = channel | (1<<REFS1) | (1<<REFS0);
12+
ADMUX = channel | (1<<REFS0) | (1<<REFS1);
1413
// Den ADC initialisieren und einen sog. Dummyreadout machen
1514
ADCSRA |= (1<<ADSC);
1615
while(ADCSRA & (1<<ADSC));
17-
//4 Leseoperationen
18-
for(i=0; i<4; i++) {
19-
ADCSRA |= (1<<ADSC);
20-
while(ADCSRA & (1<<ADSC)); // Auf Ergebnis warten...
21-
result += ADCW; }
16+
ADCSRA |= (1<<ADSC);
17+
while(ADCSRA & (1<<ADSC)); // Auf Ergebnis warten...
18+
result = ADCW;
2219
// ADC wieder deaktivieren
23-
ADCSRA &= ~(1<<ADEN);
24-
return result>>2;
20+
ADCSRA = 0;
21+
return result;
2522
}
2623

2724
int main(void)
2825
{
2926
Serial.begin(9600);
3027
while (1) //infinite loop
3128
{
32-
int result = readADC(0);
33-
Serial.println(result);
29+
int result_individual = readADC(0);
30+
//Serial.print(1023);
31+
//Serial.print(",");
32+
Serial.println(result_individual);
3433
Serial.flush();
35-
_delay_ms(10); //1 second delay
34+
_delay_ms(20);
3635
}
3736
return 0; // wird nie erreicht
3837
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#ifndef F_CPU
2+
#define F_CPU 16000000UL // 16 MHz clock speed
3+
#endif
4+
5+
#include <avr/io.h>
6+
#include <util/delay.h>
7+
8+
int main(void)
9+
{
10+
Serial.begin(9600);
11+
DDRB = 0;
12+
PORTB = 0xFF;
13+
14+
TCCR1A = 0; // Normal Mode
15+
16+
TCCR1B = 0;
17+
// 1024 als Prescale-Wert Rising edge
18+
TCCR1B = (1 << CS12) | (1 <<CS10) | (1 << ICES1);
19+
TIFR1 = (1<<ICF1);
20+
//TIMSK1 |= (1 << ICIE1); // enable Interrupt
21+
22+
while (1) //infinite loop
23+
{
24+
Serial.println("Waiting for Button push");
25+
Serial.flush();
26+
TCNT1 = 0;
27+
while ((TIFR1 & (1<<ICF1)) == 0);
28+
TIFR1 = (1<<ICF1);
29+
Serial.println(ICR1);
30+
Serial.flush();
31+
_delay_ms(500);
32+
}
33+
return 0;
34+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#define F_CPU 16000000UL
2+
3+
#include <avr/io.h>
4+
#include <avr/interrupt.h>
5+
6+
ISR(INT0_vect) {
7+
// Interrupt Service Routine für INT0
8+
PORTB |= (1 << PB5);
9+
Serial.print("Here!");
10+
}
11+
12+
int main (void) {
13+
Serial.begin(9600);
14+
DDRB |= (1 << PB5);
15+
DDRD &= ~(1 << DDD2);
16+
PORTD |= (1 << PORTD2);
17+
EIMSK |= ( 1 << INT0);
18+
EICRA |= ( 1 << ISC01);
19+
sei();
20+
21+
int i = 0;
22+
while (1) {
23+
Serial.print("Ich rechne fleißig ... ");
24+
Serial.println(i++);
25+
_delay_ms(50);
26+
}
27+
return 0;
28+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
TARGET = main
2+
3+
CTRL = atmega328p
4+
STK = arduino
5+
6+
CC = avr-gcc
7+
OC = avr-objcopy
8+
LDFLAGS = -j .text -j .data
9+
10+
AVRDUDE = avrdude
11+
AVRDUDE_FLAGS = -P /dev/ttyUSB0
12+
AVRDUDE_FLAGS += -B 57600
13+
AVRDUDE_FLAGS += -c $(STK)
14+
AVRDUDE_FLAGS += -p m328p
15+
AVRDUDE_FLAGS += -v -D -U flash:w:$(TARGET).hex:i
16+
17+
.PHONY: all
18+
19+
all:
20+
@echo "compiling"
21+
$(CC) -c -g -o $(TARGET).o -Os -mmcu=$(CTRL) $(TARGET).c -fverbose-asm -save-temps
22+
@echo "linking"
23+
@$(CC) -mmcu=$(CTRL) $(TARGET).o -o $(TARGET).elf
24+
@echo "generate properly output format"
25+
@$(OC) $(LDFLAGS) -O ihex $(TARGET).elf $(TARGET).hex
26+
avr-size --mcu=$(CTRL) $(TARGET).elf
27+
28+
program: all
29+
@echo "Programing the device"
30+
@echo $(AVRDUDE_FLAGS)
31+
$(AVRDUDE) $(AVRDUDE_FLAGS)
32+
33+
asm: all
34+
@echo "Generating assembler code"
35+
avr-objdump -d -S $(TARGET).elf > $(TARGET).asm
36+
37+
clean:
38+
@echo "Cleaning"
39+
@rm -rf $(TARGET).hex $(TARGET).o $(TARGET).elf *~
40+
41+
com: program
42+
@echo "Opening Serial connection"
43+
gtkterm -p /dev/ttyACM0 -s 9600 -r "-"
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
2+
main.elf: file format elf32-avr
3+
4+
5+
Disassembly of section .text:
6+
7+
00000000 <__vectors>:
8+
0: 0c 94 34 00 jmp 0x68 ; 0x68 <__ctors_end>
9+
4: 0c 94 40 00 jmp 0x80 ; 0x80 <__vector_1>
10+
8: 0c 94 3e 00 jmp 0x7c ; 0x7c <__bad_interrupt>
11+
c: 0c 94 3e 00 jmp 0x7c ; 0x7c <__bad_interrupt>
12+
10: 0c 94 3e 00 jmp 0x7c ; 0x7c <__bad_interrupt>
13+
14: 0c 94 3e 00 jmp 0x7c ; 0x7c <__bad_interrupt>
14+
18: 0c 94 3e 00 jmp 0x7c ; 0x7c <__bad_interrupt>
15+
1c: 0c 94 3e 00 jmp 0x7c ; 0x7c <__bad_interrupt>
16+
20: 0c 94 3e 00 jmp 0x7c ; 0x7c <__bad_interrupt>
17+
24: 0c 94 3e 00 jmp 0x7c ; 0x7c <__bad_interrupt>
18+
28: 0c 94 3e 00 jmp 0x7c ; 0x7c <__bad_interrupt>
19+
2c: 0c 94 3e 00 jmp 0x7c ; 0x7c <__bad_interrupt>
20+
30: 0c 94 3e 00 jmp 0x7c ; 0x7c <__bad_interrupt>
21+
34: 0c 94 3e 00 jmp 0x7c ; 0x7c <__bad_interrupt>
22+
38: 0c 94 3e 00 jmp 0x7c ; 0x7c <__bad_interrupt>
23+
3c: 0c 94 3e 00 jmp 0x7c ; 0x7c <__bad_interrupt>
24+
40: 0c 94 3e 00 jmp 0x7c ; 0x7c <__bad_interrupt>
25+
44: 0c 94 3e 00 jmp 0x7c ; 0x7c <__bad_interrupt>
26+
48: 0c 94 3e 00 jmp 0x7c ; 0x7c <__bad_interrupt>
27+
4c: 0c 94 3e 00 jmp 0x7c ; 0x7c <__bad_interrupt>
28+
50: 0c 94 3e 00 jmp 0x7c ; 0x7c <__bad_interrupt>
29+
54: 0c 94 3e 00 jmp 0x7c ; 0x7c <__bad_interrupt>
30+
58: 0c 94 3e 00 jmp 0x7c ; 0x7c <__bad_interrupt>
31+
5c: 0c 94 3e 00 jmp 0x7c ; 0x7c <__bad_interrupt>
32+
60: 0c 94 3e 00 jmp 0x7c ; 0x7c <__bad_interrupt>
33+
64: 0c 94 3e 00 jmp 0x7c ; 0x7c <__bad_interrupt>
34+
35+
00000068 <__ctors_end>:
36+
68: 11 24 eor r1, r1
37+
6a: 1f be out 0x3f, r1 ; 63
38+
6c: cf ef ldi r28, 0xFF ; 255
39+
6e: d8 e0 ldi r29, 0x08 ; 8
40+
70: de bf out 0x3e, r29 ; 62
41+
72: cd bf out 0x3d, r28 ; 61
42+
74: 0e 94 4b 00 call 0x96 ; 0x96 <main>
43+
78: 0c 94 56 00 jmp 0xac ; 0xac <_exit>
44+
45+
0000007c <__bad_interrupt>:
46+
7c: 0c 94 00 00 jmp 0 ; 0x0 <__vectors>
47+
48+
00000080 <__vector_1>:
49+
#define F_CPU 16000000UL
50+
51+
#include <avr/io.h>
52+
#include <avr/interrupt.h>
53+
54+
ISR(INT0_vect) {
55+
80: 1f 92 push r1
56+
82: 0f 92 push r0
57+
84: 0f b6 in r0, 0x3f ; 63
58+
86: 0f 92 push r0
59+
88: 11 24 eor r1, r1
60+
PORTB |= (1 << PB5);
61+
8a: 2d 9a sbi 0x05, 5 ; 5
62+
}
63+
8c: 0f 90 pop r0
64+
8e: 0f be out 0x3f, r0 ; 63
65+
90: 0f 90 pop r0
66+
92: 1f 90 pop r1
67+
94: 18 95 reti
68+
69+
00000096 <main>:
70+
71+
int main (void) {
72+
DDRB |= (1 << PB5);
73+
96: 25 9a sbi 0x04, 5 ; 4
74+
DDRD &= ~(1 << DDD2);
75+
98: 52 98 cbi 0x0a, 2 ; 10
76+
PORTD |= (1 << PORTD2);
77+
9a: 5a 9a sbi 0x0b, 2 ; 11
78+
EIMSK |= ( 1 << INT0);
79+
9c: e8 9a sbi 0x1d, 0 ; 29
80+
EICRA |= ( 1 << ISC01);
81+
9e: 80 91 69 00 lds r24, 0x0069 ; 0x800069 <__DATA_REGION_ORIGIN__+0x9>
82+
a2: 82 60 ori r24, 0x02 ; 2
83+
a4: 80 93 69 00 sts 0x0069, r24 ; 0x800069 <__DATA_REGION_ORIGIN__+0x9>
84+
sei();
85+
a8: 78 94 sei
86+
aa: ff cf rjmp .-2 ; 0xaa <main+0x14>
87+
88+
000000ac <_exit>:
89+
ac: f8 94 cli
90+
91+
000000ae <__stop_program>:
92+
ae: ff cf rjmp .-2 ; 0xae <__stop_program>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#define F_CPU 16000000UL
2+
3+
#include <avr/io.h>
4+
#include <avr/interrupt.h>
5+
6+
ISR(INT0_vect) {
7+
PORTB |= (1 << PB5);
8+
}
9+
10+
int main (void) {
11+
DDRB |= (1 << PB5);
12+
DDRD &= ~(1 << DDD2);
13+
PORTD |= (1 << PORTD2);
14+
EIMSK |= ( 1 << INT0);
15+
EICRA |= ( 1 << ISC01);
16+
sei();
17+
while (1);
18+
return 0;
19+
}
11 KB
Binary file not shown.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
:100000000C9434000C9440000C943E000C943E0080
2+
:100010000C943E000C943E000C943E000C943E0068
3+
:100020000C943E000C943E000C943E000C943E0058
4+
:100030000C943E000C943E000C943E000C943E0048
5+
:100040000C943E000C943E000C943E000C943E0038
6+
:100050000C943E000C943E000C943E000C943E0028
7+
:100060000C943E000C943E0011241FBECFEFD8E04C
8+
:10007000DEBFCDBF0E944B000C9456000C940000D4
9+
:100080001F920F920FB60F9211242D9A0F900FBE50
10+
:100090000F901F901895259A52985A9AE89A809135
11+
:1000A00069008260809369007894FFCFF894FFCF55
12+
:00000001FF

0 commit comments

Comments
 (0)