1 | #include <avr/io.h>
|
2 | #include <avr/interrupt.h>
|
3 | #ifndef F_CPU
|
4 | #define F_CPU 8000000UL
|
5 | #endif
|
6 | #include <stdio.h>
|
7 | #define BAUD 9600L
|
8 | #define UBRR_VAL ((F_CPU+BAUD*8)/(BAUD*16)-1)
|
9 | #define BAUD_REAL (F_CPU/(16*(UBRR_VAL+1)))
|
10 | #define BAUD_ERROR ((BAUD_REAL*1000)/BAUD-1000)
|
11 | ////////////////////////////////////////////////////////////////////
|
12 | int uart_putchar(char c, FILE *stream)
|
13 | {
|
14 | loop_until_bit_is_set(UCSRA, UDRE);
|
15 | UDR = c;
|
16 | return 0;
|
17 | }
|
18 | static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL, _FDEV_SETUP_WRITE);
|
19 | ////////////////////////////////////////////////////////////////////
|
20 | #include <util/delay.h>
|
21 | ////////////////////////////////////////////////////////////////////
|
22 | #define DATA595_PIN PC0
|
23 | #define DATA595_PORT PORTC
|
24 | #define DATA595_DDR DDRC
|
25 | ///////
|
26 | #define DATA595_high() (DATA595_PORT |= _BV(DATA595_PIN))
|
27 | #define DATA595_low() (DATA595_PORT &= ~_BV(DATA595_PIN))
|
28 | ////////////////
|
29 | #define RCLOCK595_PIN PC2
|
30 | #define RCLOCK595_PORT PORTC
|
31 | #define RCLOCK595_DDR DDRC
|
32 | ///////
|
33 | #define RCLOCK595_high() (RCLOCK595_PORT |= _BV(RCLOCK595_PIN))
|
34 | #define RCLOCK595_low() (RCLOCK595_PORT &= ~_BV(RCLOCK595_PIN))
|
35 | ////////////////
|
36 | #define CLOCK595_PIN PC1
|
37 | #define CLOCK595_PORT PORTC
|
38 | #define CLOCK595_DDR DDRC
|
39 | ///////
|
40 | #define CLOCK595_high() (CLOCK595_PORT |= _BV(CLOCK595_PIN))
|
41 | #define CLOCK595_low() (CLOCK595_PORT &= ~_BV(CLOCK595_PIN))
|
42 | ////////////////////////////////////////////////////////////////////
|
43 | #define DATA165_PIN PC5
|
44 | #define DATA165_PORT PORTC
|
45 | #define DATA165_DDR DDRC
|
46 | ////////////////////////////
|
47 | #define CLOCK165_PIN PC4
|
48 | #define CLOCK165_PORT PORTC
|
49 | #define CLOCK165_DDR DDRC
|
50 | #define CLOCK165_high() (CLOCK165_PORT |= _BV(CLOCK165_PIN))
|
51 | #define CLOCK165_low() (CLOCK165_PORT &= ~_BV(CLOCK165_PIN))
|
52 | ////////////////////////////
|
53 | #define PARALLEL165_PIN PC3
|
54 | #define PARALLEL165_PORT PORTC
|
55 | #define PARALLEL165_DDR DDRC
|
56 | #define PARALLEL165_high() (PARALLEL165_PORT |= _BV(PARALLEL165_PIN))
|
57 | #define PARALLEL165_low() (PARALLEL165_PORT &= ~_BV(PARALLEL165_PIN))
|
58 | ////////////////////////////////////////////////////////////////////
|
59 | uint8_t Encoder1AS;
|
60 | uint8_t Encoder1BS;
|
61 | uint8_t encoderState;
|
62 | uint8_t oldEncoderState;
|
63 | uint8_t encoderValue;
|
64 | ////////////////////////////////////////////////////////////////////
|
65 |
|
66 | void get_Encoder_data(int v165)
|
67 | {
|
68 | Encoder1AS = (v165 & _BV(1)) ? 1:0;
|
69 | Encoder1BS = (v165 & _BV(2)) ? 1:0;
|
70 | printf("\n");
|
71 | printf("test encodder A =");
|
72 | printf("%d", Encoder1AS);
|
73 | printf("\n");
|
74 | printf("test encodder B =");
|
75 | printf("%d", Encoder1BS);
|
76 | printf("\n");
|
77 | }
|
78 |
|
79 | ////////////////////////////
|
80 | int wertSkalieren(uint8_t *value, int offset)
|
81 | {
|
82 | *value = value+offset;
|
83 | return (int)value;
|
84 | }
|
85 |
|
86 | ////////////////////////////
|
87 |
|
88 | void setup_Encoder()
|
89 | {
|
90 | oldEncoderState = encoderState = (Encoder1AS << 1) | Encoder1BS;
|
91 | encoderValue = 0;
|
92 | }
|
93 | ////////////////////////////
|
94 |
|
95 | void read_Encoder()
|
96 | {
|
97 | encoderState = (Encoder1AS<<1) | Encoder1BS;
|
98 | if (encoderState != oldEncoderState)
|
99 | {
|
100 | uint8_t transition = (oldEncoderState << 2) | encoderState;
|
101 |
|
102 | switch (transition) {
|
103 | case 0b0001: wertSkalieren( &encoderValue, -1 ); break;
|
104 | case 0b0010: wertSkalieren( &encoderValue, +1 ); break;
|
105 |
|
106 | case 0b0100: wertSkalieren( &encoderValue, +1 ); break;
|
107 | case 0b0111: wertSkalieren( &encoderValue, -1 ); break;
|
108 |
|
109 | case 0b1000: wertSkalieren( &encoderValue, -1 ); break;
|
110 | case 0b1011: wertSkalieren( &encoderValue, +1 ); break;
|
111 |
|
112 | case 0b1101: wertSkalieren( &encoderValue, +1 ); break;
|
113 | case 0b1110: wertSkalieren( &encoderValue, -1 ); break;
|
114 | }
|
115 | oldEncoderState = encoderState;
|
116 | }
|
117 | }
|
118 |
|
119 |
|
120 | ////////////////////////////////////////////////////////////////////
|
121 | void USART_Init( unsigned int baud )
|
122 | {
|
123 | UBRRH = UBRR_VAL >>8;
|
124 | UBRRL = UBRR_VAL & 0xFF;
|
125 | UCSRB = (1<<RXEN)|(1<<TXEN);
|
126 | UCSRC = (1<<URSEL)|(1<<USBS)|(3<<UCSZ0);
|
127 | }
|
128 | //////////////////////////////////////////////////////////////////
|
129 | void InitShiftOut()
|
130 | {
|
131 | RCLOCK595_DDR |= _BV(RCLOCK595_PIN);
|
132 | CLOCK595_DDR |= _BV(CLOCK595_PIN);
|
133 | DATA595_DDR |= _BV(DATA595_PIN);
|
134 | }
|
135 | void InitShiftIn()
|
136 | {
|
137 | PARALLEL165_DDR |= _BV(PARALLEL165_PIN);
|
138 | CLOCK165_DDR |= _BV(CLOCK165_PIN);
|
139 | DATA165_DDR &= ~_BV(DATA165_PIN);
|
140 | DATA165_PORT &= ~_BV(DATA165_PIN);
|
141 | }
|
142 | ////////////////////////////////////////////////////////////////////
|
143 | void Pulse595Clock()
|
144 | {
|
145 | CLOCK595_high();
|
146 | CLOCK595_low();
|
147 | }
|
148 | void Pulse165Clock()
|
149 | {
|
150 | CLOCK165_low();
|
151 | CLOCK165_high();
|
152 | }
|
153 |
|
154 | ////////////////////////////////////////////////////////////////////
|
155 |
|
156 | void Pulse595Rclock()
|
157 | {
|
158 | RCLOCK595_high();
|
159 | RCLOCK595_low();
|
160 | }
|
161 | void Pulse165Parallel()
|
162 | {
|
163 | PARALLEL165_low();
|
164 | PARALLEL165_high();
|
165 | }
|
166 |
|
167 | ////////////////////////////////////////////////////////////////////
|
168 |
|
169 | void ShiftOut(uint8_t var)
|
170 | {
|
171 | for (uint8_t i =0; i<8; i++)
|
172 | {
|
173 | if(var & 0b10000000)
|
174 | {
|
175 | DATA595_high();
|
176 | }
|
177 | else
|
178 | {
|
179 | DATA595_low();
|
180 | }
|
181 | Pulse595Clock();
|
182 | var=var<<1;
|
183 | DATA595_low();
|
184 | }
|
185 | Pulse595Rclock();
|
186 | }
|
187 | ////////////////////////////////////////////////////////////////////
|
188 | #if 0
|
189 | void ShiftIn(int *var)
|
190 | {
|
191 | CLOCK165_high();
|
192 | Pulse165Parallel();
|
193 | for (unsigned char i = 0; i < 8; ++i)
|
194 | {
|
195 | *var <<= 1;
|
196 | if (DATA165_read())
|
197 | *var |= 0x01;
|
198 | Pulse165Clock();
|
199 | }
|
200 | CLOCK165_low();
|
201 | }
|
202 | #endif
|
203 | static uint8_t DATA165_read(void)
|
204 | {
|
205 | if (PINC & _BV(DATA165_PIN))
|
206 | printf("1");
|
207 | else
|
208 | printf("0");
|
209 | return (PINC & _BV(DATA165_PIN)) ? 1 : 0;
|
210 | }
|
211 |
|
212 | uint8_t ShiftIn(void)
|
213 | {
|
214 | uint8_t shiftResult;
|
215 |
|
216 | CLOCK165_high();
|
217 | Pulse165Parallel();
|
218 |
|
219 | for (unsigned char i = 0; i < 8; ++i) {
|
220 | shiftResult |= DATA165_read();
|
221 | shiftResult <<= 1;
|
222 | Pulse165Clock();
|
223 | }
|
224 | CLOCK165_low();
|
225 | return shiftResult;
|
226 | }
|
227 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
228 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
229 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
230 |
|
231 | int main(void)
|
232 | {
|
233 | stdout = &mystdout;
|
234 | long baud = 9600UL;
|
235 | USART_Init(baud);
|
236 | InitShiftIn();
|
237 | int v165 = 0;
|
238 | InitShiftOut();
|
239 | while(1)
|
240 | {
|
241 | //ShiftIn(var);
|
242 | printf("\n Binärcode : ");
|
243 | v165 = ShiftIn();
|
244 | ShiftOut(v165);
|
245 | printf("\n Variable: ");
|
246 | printf("%d", v165);
|
247 | setup_Encoder();
|
248 | get_Encoder_data(v165);
|
249 | read_Encoder();
|
250 | printf("%d", encoderValue);
|
251 | }
|
252 | }
|