1 | #include <avr/io.h>
|
2 | #include "config.h"
|
3 | #include "io.h"
|
4 |
|
5 | uint8_t io_info(uint8_t ionr, uint8_t var)
|
6 | { /* returns the requested variable for the pin io_nr;
|
7 | * io_nr: pin
|
8 | * var: 0 <portnumber>
|
9 | * 1 returns number of ADC-port, if it isn't an ADC-port, 255 is returned
|
10 | * 2 returns number of servo, if it isn't a defined servo, 255 is returned
|
11 | */
|
12 |
|
13 | switch(var)
|
14 | {
|
15 | case 0:
|
16 | switch(ionr)
|
17 | {
|
18 | case 1 : case 9 : case 12: return 6;
|
19 | case 2 : case 14: case 23: return 0;
|
20 | case 3 : case 15: case 24: return 1;
|
21 | case 4 : case 16: case 25: return 2;
|
22 | case 5 : case 17: case 26: return 3;
|
23 | case 6 : case 18: case 27: return 4;
|
24 | case 11: case 19: case 28: return 5;
|
25 | case 10: case 13: return 7;
|
26 | }
|
27 | break;
|
28 | case 1:
|
29 | if (ionr >= 23 && ionr <= 28)
|
30 | return ionr-23;
|
31 | break;
|
32 | case 2:
|
33 | if (ionr == 255) return 255;
|
34 | else if (ionr == SERVO0) return 0;
|
35 | else if (ionr == SERVO1) return 1;
|
36 | else if (ionr == SERVO2) return 2;
|
37 | else if (ionr == SERVO3) return 3;
|
38 | else if (ionr == SERVO4) return 4;
|
39 | else if (ionr == SERVO5) return 5;
|
40 | }
|
41 | return 255;
|
42 | }
|
43 |
|
44 | /*******************************************************************
|
45 | * io_set_
|
46 | *
|
47 | * Wird intern verwendet
|
48 | *******************************************************************/
|
49 | inline void io_set_(volatile uint8_t *ddr, volatile uint8_t *port, uint8_t pinnr, uint8_t status)
|
50 | {
|
51 | switch(status)
|
52 | {
|
53 | case 0:
|
54 | *ddr |= (1 << pinnr);
|
55 | *port &= ~(1 << pinnr);
|
56 | break;
|
57 | case 1:
|
58 | default:
|
59 | *ddr |= (1 << pinnr);
|
60 | *port |= (1 << pinnr);
|
61 | break;
|
62 | case 2:
|
63 | *ddr |= (1 << pinnr);
|
64 | break;
|
65 | case 3:
|
66 | *ddr &= ~(1 << pinnr);
|
67 | *port |= (1 << pinnr);
|
68 | break;
|
69 | case 4:
|
70 | *ddr &= ~(1 << pinnr);
|
71 | *port &= ~(1 << pinnr);
|
72 | }
|
73 | }
|
74 |
|
75 | /*******************************************************************
|
76 | * set_io
|
77 | *
|
78 | * Setzen eines Pins in einen bestimmten Status
|
79 | * status = 0 Ausgang deaktivieren
|
80 | * 1 Ausgang aktivieren
|
81 | * 2 Ausgang
|
82 | * 3 Eingang aktivieren mit internem Pullup
|
83 | * 4 Eingang aktivieren ohne internem Pullup
|
84 | *******************************************************************/
|
85 | void set_io(uint8_t pin, uint8_t status)
|
86 | {
|
87 | switch (pin)
|
88 | {
|
89 | case 1: case 23: case 24: case 25: case 26: case 27: case 28:
|
90 | io_set_(&DDRC,&PORTC,io_info(pin,0),status);
|
91 | break;
|
92 | case 2: case 3: case 4: case 5: case 6: case 11: case 12: case 13:
|
93 | io_set_(&DDRD,&PORTD,io_info(pin,0),status);
|
94 | break;
|
95 | case 9: case 10: case 14: case 15: case 16: case 17: case 18: case 19:
|
96 | io_set_(&DDRB,&PORTB,io_info(pin,0),status);
|
97 | break;
|
98 | }
|
99 | }
|
100 |
|
101 | /*******************************************************************
|
102 | * io_get_
|
103 | *
|
104 | * Wird intern verwendet
|
105 | *******************************************************************/
|
106 | inline uint8_t io_get_(volatile uint8_t *pin, uint8_t pinnr)
|
107 | {
|
108 | if (*pin & (1 << pinnr)) return 0;
|
109 | return 1;
|
110 | }
|
111 |
|
112 | /*******************************************************************
|
113 | * get_io
|
114 | *
|
115 | * Auslesen eines Pins
|
116 | * Rueckgabe 0 = low
|
117 | * 1 = high
|
118 | *******************************************************************/
|
119 | uint8_t get_io(uint8_t pin)
|
120 | {
|
121 | switch (pin)
|
122 | {
|
123 | case 1: case 23: case 24: case 25: case 26: case 27: case 28:
|
124 | return io_get_(&PINC, io_info(pin,0));
|
125 | break;
|
126 | case 2: case 3: case 4: case 5: case 6: case 11: case 12: case 13:
|
127 | return io_get_(&PIND, io_info(pin,0));
|
128 | break;
|
129 | case 9: case 10: case 14: case 15: case 16: case 17: case 18: case 19:
|
130 | return io_get_(&PINB, io_info(pin,0));
|
131 | break;
|
132 | }
|
133 | return 255;
|
134 | }
|