1 | /* Nötigen Includes.....*/
|
2 | #include <avr/io.h>
|
3 | #include <avr/interrupt.h>
|
4 | #include <avr/signal.h>
|
5 | #include <avr/pgmspace.h>
|
6 | #include <stdlib.h>
|
7 | #include <string.h>
|
8 | #include <stdio.h>
|
9 | #include <util/delay.h>
|
10 |
|
11 | #include "uart.h"
|
12 |
|
13 |
|
14 |
|
15 | /* DEFINES */
|
16 |
|
17 | // Für Schrittmotor:
|
18 | #define S_PORT PORTC
|
19 | #define S_REG DDRC
|
20 | #define S_ENABLE PC1
|
21 | #define S_DIR PC2
|
22 | #define S_MODE PC4
|
23 | #define S_CLOCK PC3
|
24 |
|
25 | // Für UART
|
26 | #define UART_BAUD_RATE 9600
|
27 |
|
28 |
|
29 |
|
30 | int n; /* Laufvariable für die For-Schleife, gibt an wie viele
|
31 | Schritte der Motor schon hatte....
|
32 | */
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | unsigned int c;
|
39 |
|
40 | // Fürs splitten....
|
41 | char delim[] = ";";
|
42 | char *result = NULL;
|
43 | int i = 0;
|
44 | int richtung, schritte;
|
45 |
|
46 | // Prototypen
|
47 | void init_usart(void);
|
48 | void usart_receive(void);
|
49 | void init_motor(void);
|
50 | void motor(int schritte, int richtung, int modus);
|
51 |
|
52 |
|
53 |
|
54 | /* ****************************** UART ******************************/
|
55 | void init_usart()
|
56 | {
|
57 | uart_init( UART_BAUD_SELECT(UART_BAUD_RATE,F_CPU) );
|
58 | }
|
59 |
|
60 |
|
61 |
|
62 | void usart_receive(void)
|
63 | {
|
64 |
|
65 | while(1)
|
66 | {
|
67 | c = uart_getc();
|
68 |
|
69 | if ( c & UART_NO_DATA )
|
70 | {
|
71 | // Keine Daten verfügbar
|
72 | }
|
73 | else
|
74 | {
|
75 | // Daten verfügbar...
|
76 | if ( c & UART_FRAME_ERROR )
|
77 | {
|
78 | uart_puts_P("UART Frame Error: ");
|
79 | }
|
80 | if ( c & UART_OVERRUN_ERROR )
|
81 | {
|
82 | uart_puts_P("UART Overrun Error: ");
|
83 | }
|
84 |
|
85 | //uart_putc( (unsigned char)c );
|
86 |
|
87 | char command = (char)c;
|
88 |
|
89 | motor(48,(int)command,1);
|
90 | //i = 0;
|
91 | // uart_putc(command);
|
92 | /*result = strtok_r( command, delim );
|
93 | while( result != NULL )
|
94 | {
|
95 | i=i+1;
|
96 | if (i=1){richtung = (int)result;}
|
97 | if (i=2){schritte = (int)result;}
|
98 | }
|
99 | uart_putc( (unsigned char)richtung );
|
100 | uart_putc( (unsigned char)schritte );*/
|
101 |
|
102 | }
|
103 | }
|
104 |
|
105 | }
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 | /* Motor initialiseren... (Enable auf High setzen) */
|
112 | void init_motor()
|
113 | {
|
114 | S_REG = 0xFF; // Alle Pins von Port C auf Ausgänge schalten
|
115 | S_PORT |= (1<<S_ENABLE);
|
116 | }
|
117 |
|
118 |
|
119 |
|
120 |
|
121 | /* DEN MOTOR STEUERN*/
|
122 | void motor(int schritte, int richtung, int modus)
|
123 | {
|
124 |
|
125 | // Richtung bestimmen....
|
126 | if (richtung=1)
|
127 | {
|
128 | S_PORT |= (1<<S_DIR);
|
129 | }
|
130 | else
|
131 | {
|
132 | S_PORT &= ~(1<<S_DIR);
|
133 | }
|
134 |
|
135 |
|
136 | // Modus auswählen.....
|
137 | if (modus=1)
|
138 | {
|
139 | S_PORT |= (1<<S_MODE);
|
140 | }
|
141 | else
|
142 | {
|
143 | S_PORT &= ~(1<<S_MODE);
|
144 | }
|
145 |
|
146 | // Motor drehen lassen...
|
147 | for(n = 0; n <= schritte; n++)
|
148 | {
|
149 | // Clock immer auf High und Low setzen, damit der Motor sich ein Schritt bewegt....
|
150 | S_PORT ^= (1<<S_CLOCK);
|
151 | _delay_ms(2);
|
152 | S_PORT ^= (1<<S_CLOCK);
|
153 | _delay_ms(2);
|
154 | }
|
155 |
|
156 |
|
157 | }
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 | int main(void)
|
175 | {
|
176 |
|
177 | init_usart();
|
178 | sei();
|
179 | init_motor();
|
180 | usart_receive();
|
181 | return 0;
|
182 | }
|