1 | //RS485 Definitionen***********************************************
|
2 |
|
3 |
|
4 | #define TRANSMIT_CONF DDRA|= (1<<DDA4)
|
5 | #define TRANSMIT_ON PORTA |= (1<<PA4)
|
6 | #define TRANSMIT_OFF PORTA &= ~(1<<PA4)
|
7 |
|
8 | volatile uint8_t uart_transmit_progress = 0;
|
9 | volatile uint8_t uart_r_array[10];
|
10 | volatile uint8_t uart_back_read = 0;
|
11 | volatile uint8_t u_rx_start = 0;
|
12 | volatile uint8_t receive_max = 6;
|
13 | volatile char u_receive_string[15];
|
14 | volatile uint8_t u_r_c = 0; //Daten an UART empfangen
|
15 |
|
16 | //************** RS485 Funktionen *********************************************************
|
17 |
|
18 |
|
19 | void USART_Transmit( char data )
|
20 | {
|
21 |
|
22 | /* Wait for empty transmit buffer */
|
23 | while ( !( UCSRA & (1<<UDRE)) )
|
24 | ;
|
25 | /* Put data into buffer, sends the data */
|
26 | UDR = data;
|
27 | }
|
28 |
|
29 | void uart_puts(char *s,uint8_t count)
|
30 | {
|
31 |
|
32 | uint8_t i;
|
33 | uart_transmit_progress = 1;
|
34 | for (i=0;i<count;i++)
|
35 | {
|
36 | TRANSMIT_ON;
|
37 | USART_Transmit(s[i]);
|
38 | while(!(uart_back_read))
|
39 | ;
|
40 | uart_back_read = 0;
|
41 | u_rx_start=0;
|
42 | }
|
43 | uart_transmit_progress= 0;
|
44 |
|
45 | }
|
46 |
|
47 | ISR(USART_TXC_vect)
|
48 | {
|
49 | TRANSMIT_OFF;
|
50 | }
|
51 |
|
52 | void uart_putc(char c)
|
53 | {
|
54 | TRANSMIT_ON;
|
55 | USART_Transmit(c);
|
56 | }
|
57 |
|
58 | ISR(USART_RXC_vect)
|
59 | {
|
60 | uint8_t i;
|
61 | if(uart_transmit_progress)
|
62 | {
|
63 | uart_r_array[u_rx_start] = UDR;
|
64 | uart_back_read=1;
|
65 |
|
66 | u_rx_start++;
|
67 | }
|
68 | else
|
69 | {
|
70 |
|
71 |
|
72 | //USART_Transmit(receive_string[i]);
|
73 | //USART_Transmit(i);
|
74 | i = u_rx_start;
|
75 | u_receive_string[i] = UDR;
|
76 | //lcd_putc(i);
|
77 | /*if (i==1)
|
78 | {
|
79 | if((u_receive_string[i]>>4)== 16)
|
80 | {
|
81 | receive_max = 10;
|
82 | }
|
83 | else
|
84 | {
|
85 | receive_max = 9;
|
86 | }
|
87 | }
|
88 | */
|
89 | if(i == receive_max)
|
90 | {
|
91 | u_rx_start=0;
|
92 | LED3_ON ;
|
93 | u_r_c = receive_max;
|
94 | //USART_Transmit('Z');
|
95 |
|
96 | }
|
97 | else
|
98 | {
|
99 |
|
100 | i++;
|
101 | u_rx_start = i;
|
102 | }
|
103 | }
|
104 | }
|
105 |
|
106 | int main(void)
|
107 | {
|
108 |
|
109 | //Interrupt-Eingänge konfigurieren
|
110 | PORTD |= (1<<2);
|
111 | PORTD |= (1<<3);
|
112 | PORTB |= (1<<2) | (1<<3);
|
113 |
|
114 |
|
115 | //Taster pullup akitivieren
|
116 | TASTER_PULLUP |= (1<<TASTER_PIN);
|
117 |
|
118 | //LED-Ausgänge konfigurieren
|
119 | DDRA |= (1<<DDA7) | (1<<DDA6) | (1<<DDA5);
|
120 |
|
121 |
|
122 | //USART initialisieren
|
123 |
|
124 | TRANSMIT_CONF; // Transmit Pin als Ausgang
|
125 | //USART0
|
126 | UBRRH = UBRR_VAL_UART >> 8;
|
127 | UBRRL = UBRR_VAL_UART& 0xFF;
|
128 |
|
129 | //USART0
|
130 | UCSRB |= (1<<TXEN); // UART TX einschalten
|
131 | UCSRB |= (1<<RXEN);
|
132 | UCSRB |= (1<<RXCIE);
|
133 | UCSRB |= (1<<TXCIE);
|
134 | UCSRC |= (1<<URSEL)|(1<<UCSZ0)| (1<<UCSZ1); // Asynchron 8N1
|
135 |
|
136 | //Sensor-Interrupts aktivieren
|
137 | // MCUCR |= (1<<ISC11) | (1<<ISC01); //fallende Flanke auf INT0 und INT1
|
138 | // MCUCSR &= ~(1<<ISC2);
|
139 | // GICR |= (1<<INT0) | (1<<INT1) | (1<<INT2);
|
140 |
|
141 | sei();
|
142 | //uart_putc((uint8_t) receive_max);
|
143 | //uart_putc('O');
|
144 | //uart_putc('K');
|
145 | //Master_send_command(1,16,12312);
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 | /*********************************************************************/
|
154 |
|
155 | // Messung der Interruptdauer
|
156 | tmp =0;
|
157 | tmp =0;
|
158 | tmp =0;
|
159 |
|
160 | // Debug
|
161 |
|
162 |
|
163 | int16_t be3;
|
164 | uint8_t testv;
|
165 | testv = 0;
|
166 | uint8_t direction;
|
167 | direction = 1;
|
168 |
|
169 | while(1)
|
170 | {
|
171 | if(u_r_c) //Es wurden Daten empfangen
|
172 | {
|
173 |
|
174 |
|
175 |
|
176 | uart_putc((uint8_t) u_r_c);
|
177 | u_r_c = 0;
|
178 | }
|
179 | }
|
180 | }
|