1 | #define F_CPU 12000000
|
2 | #include <avr/io.h>
|
3 | #include <util/delay.h>
|
4 | #include <string.h>
|
5 | #include <stdlib.h>
|
6 |
|
7 | #define WDDR DDRA
|
8 | #define WPORT PORTA
|
9 | #define WPIN 0
|
10 |
|
11 | char seg[] = {0x20, 0x40, 0x10, 0x80, 0x08, 0x00};
|
12 | char zahl[] = {0xF9, 0x88, 0xB5, 0x9D, 0xCC, 0x5D, 0x7D, 0x89, 0xFD, 0xDD, 0x00};
|
13 | char sonder[] = {0x04, 0xC5, 0x71, 0x02, 0x00}; // 0x04 = minus; 0xC5 = Gradzeichen; 0x71 = C; 0x02 = PUNKT
|
14 | //char codes[] = {0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01, 0x00};
|
15 |
|
16 |
|
17 |
|
18 | void wire_high (void) {
|
19 | WDDR &= ~(1<<WPIN);
|
20 | }
|
21 |
|
22 | void wire_low (void) {
|
23 | WDDR |= (1<<WPIN);
|
24 | WPORT &= ~(1<<WPIN);
|
25 | }
|
26 |
|
27 | int wire_reset (void) {
|
28 |
|
29 | wire_low();
|
30 | _delay_us(500);
|
31 | wire_high();
|
32 |
|
33 | _delay_us(100);
|
34 |
|
35 | if (PINA & (1<<WPIN)) {
|
36 | return 0;
|
37 | } else {
|
38 | _delay_us(400);
|
39 | return 1;
|
40 | }
|
41 |
|
42 | }
|
43 |
|
44 | void senden (unsigned int wert) {
|
45 | int i = 0;
|
46 | int code = 0;
|
47 | for (i=0; i<8; i++) {
|
48 | code = ((wert >> i) & 1); //geht so!!!
|
49 | if (code == 1) {
|
50 | wire_low();
|
51 | _delay_us(90); //0 senden (60-120 us)
|
52 | wire_high();
|
53 | } else {
|
54 | wire_low();
|
55 | _delay_us(7); //1 senden (1-15 us)
|
56 | wire_high();
|
57 | }
|
58 | _delay_us(20);
|
59 | }
|
60 |
|
61 | }
|
62 |
|
63 | int empfang (void) {
|
64 | int i = 0;
|
65 | unsigned int zahl1 = 0;
|
66 | for (i=0; i<8; i++) {
|
67 |
|
68 | wire_low();
|
69 | _delay_us(1),
|
70 | wire_high();
|
71 | _delay_us(4);
|
72 |
|
73 | if (PINA & (1<<WPIN))
|
74 | zahl1 = zahl1 + 1;
|
75 | //else
|
76 | //zahl1 = 0; <-- überflüssig
|
77 |
|
78 | zahl1 = zahl1 * 2; //2 für binär
|
79 |
|
80 | while (!(PINA & (1<<WPIN))) {
|
81 | _delay_us(2);
|
82 | }
|
83 | //_delay_us(80); ???
|
84 | }
|
85 |
|
86 | return zahl1;
|
87 | }
|
88 |
|
89 |
|
90 | void ausgeben (unsigned int wert) {
|
91 | int i, code = 0;
|
92 |
|
93 | for (i=0; i<8; i++) {
|
94 | code = (wert & codes[i]);
|
95 | if (code) {
|
96 | PORTC = zahl[1];
|
97 | } else {
|
98 | PORTC = zahl[0];
|
99 | }
|
100 | _delay_ms(500);
|
101 | PORTC = 0x00;
|
102 | _delay_ms(100);
|
103 | }
|
104 | }
|
105 |
|
106 |
|
107 | int main (void) {
|
108 |
|
109 | DDRD = 0xFF;
|
110 | DDRC = 0xFF;
|
111 | PORTD = seg[2];
|
112 |
|
113 | int ergebnis = wire_reset();
|
114 | if (ergebnis == 1) {
|
115 | PORTC = zahl[4];
|
116 | senden (0xCC);
|
117 | _delay_us(10);
|
118 | senden (0x44);
|
119 | _delay_us(100);
|
120 | wire_reset();
|
121 | senden (0xCC);
|
122 | _delay_us(10);
|
123 | senden (0xBE);
|
124 | _delay_ms(760);
|
125 | unsigned int wert1 = empfang();
|
126 | //_delay_us(60);
|
127 | unsigned int wert2 = empfang();
|
128 | //_delay_us(3);
|
129 | ausgeben (wert1);
|
130 | _delay_ms(500);
|
131 | ausgeben (wert2);
|
132 |
|
133 | } else {
|
134 | PORTC = zahl[9];
|
135 | }
|
136 |
|
137 | return 0;
|
138 | }
|
139 | // 0x20 = 1. Segment
|
140 | // 0x40 = 2. Segment
|
141 | // 0x10 = 3. Segment
|
142 | // 0x80 = 4. Segment
|
143 | // 0x08 = 5. Segment
|
144 | //PORTD = SEGMENTE
|
145 | //PORTC = ZEICHEN
|
146 | //-------------------------
|
147 | // 0x01 = PIN0 = mitte oben
|
148 | // 0x02 = PUNKT
|
149 | // 0x04 = PIN2 = mittelstrich
|
150 | // 0x08 = PIN3 = unten rechts
|
151 | // 0x10 = PIN4 = mitte unten
|
152 | // 0x20 = PIN5 = unten links
|
153 | // 0x40 = PIN6 = oben links
|
154 | // 0x80 = PIN7 = oben rechts
|
155 | //----------------------------
|
156 | // F9 = 0
|
157 | // 88 = 1
|
158 | // B5 = 2
|
159 | // 9D = 3
|
160 | // CC = 4
|
161 | // 5D = 5
|
162 | // 7D = 6
|
163 | // 89 = 7
|
164 | // FD = 8
|
165 | // DD = 9
|