Rev 29 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1 | ingob | 1 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
2 | // + Regler für Brushless-Motoren |
||
3 | // + ATMEGA8 mit 8MHz |
||
4 | // + (c) 01.2007 Holger Buss |
||
5 | // + Nur für den privaten Gebrauch |
||
6 | // + Keine Garantie auf Fehlerfreiheit |
||
7 | // + Kommerzielle Nutzung nur mit meiner Zustimmung |
||
8 | // + Der Code ist für die Hardware BL_Ctrl V1.0 entwickelt worden |
||
9 | // + www.mikrocontroller.com |
||
10 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
11 | |||
12 | #include "main.h" |
||
13 | #include "uart.h" |
||
14 | |||
15 | #define MAX_SENDE_BUFF 100 |
||
16 | #define MAX_EMPFANGS_BUFF 100 |
||
17 | |||
18 | unsigned volatile char SIO_Sollwert = 0; |
||
19 | unsigned volatile char SioTmp = 0; |
||
20 | unsigned volatile char SendeBuffer[MAX_SENDE_BUFF]; |
||
21 | unsigned volatile char RxdBuffer[MAX_EMPFANGS_BUFF]; |
||
22 | unsigned volatile char NeuerDatensatzEmpfangen = 0; |
||
23 | unsigned volatile char UebertragungAbgeschlossen = 1; |
||
24 | unsigned char MeineSlaveAdresse; |
||
25 | |||
26 | struct str_DebugOut DebugOut; |
||
27 | |||
28 | |||
29 | int Debug_Timer; |
||
30 | |||
31 | |||
32 | SIGNAL(INT_VEC_TX) |
||
33 | { |
||
34 | } |
||
35 | |||
36 | void SendUart(void) |
||
37 | { |
||
38 | static unsigned int ptr = 0; |
||
39 | unsigned char tmp_tx; |
||
40 | if(!(UCSRA & 0x40)) return; |
||
41 | if(!UebertragungAbgeschlossen) |
||
42 | { |
||
43 | ptr++; // die [0] wurde schon gesendet |
||
44 | tmp_tx = SendeBuffer[ptr]; |
||
45 | if((tmp_tx == '\r') || (ptr == MAX_SENDE_BUFF)) |
||
46 | { |
||
47 | ptr = 0; |
||
48 | UebertragungAbgeschlossen = 1; |
||
49 | } |
||
50 | USR |= (1<TXC); |
||
51 | UDR = tmp_tx; |
||
52 | } |
||
53 | else ptr = 0; |
||
54 | } |
||
55 | |||
56 | |||
57 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
58 | //++ Empfangs-Part der Datenübertragung |
||
59 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
60 | SIGNAL(INT_VEC_RX) |
||
61 | { |
||
62 | static unsigned char serPacketCounter = 100; |
||
63 | SioTmp = UDR; |
||
64 | #if X3D_SIO == 1 |
||
65 | if(SioTmp == 0xF5) // Startzeichen |
||
66 | { |
||
67 | serPacketCounter = 0; |
||
68 | } |
||
69 | else |
||
70 | { |
||
71 | if(++serPacketCounter == MOTORADRESSE) // (1-4) |
||
72 | { |
||
73 | SIO_Sollwert = SioTmp; |
||
74 | SIO_Timeout = 200; // werte für 200ms gültig |
||
75 | } |
||
76 | else |
||
77 | { |
||
78 | if(serPacketCounter > 100) serPacketCounter = 100; |
||
79 | } |
||
80 | } |
||
81 | #endif |
||
82 | }; |
||
83 | |||
84 | |||
85 | // -------------------------------------------------------------------------- |
||
86 | void AddCRC(unsigned int wieviele) |
||
87 | { |
||
88 | unsigned int tmpCRC = 0,i; |
||
89 | for(i = 0; i < wieviele;i++) |
||
90 | { |
||
91 | tmpCRC += SendeBuffer[i]; |
||
92 | } |
||
93 | tmpCRC %= 4096; |
||
94 | SendeBuffer[i++] = '=' + tmpCRC / 64; |
||
95 | SendeBuffer[i++] = '=' + tmpCRC % 64; |
||
96 | SendeBuffer[i++] = '\r'; |
||
97 | UebertragungAbgeschlossen = 0; |
||
98 | UDR = SendeBuffer[0]; |
||
99 | } |
||
100 | |||
101 | |||
102 | // -------------------------------------------------------------------------- |
||
103 | void SendOutData(unsigned char cmd,unsigned char modul, unsigned char *snd, unsigned char len) |
||
104 | { |
||
105 | unsigned int pt = 0; |
||
106 | unsigned char a,b,c; |
||
107 | unsigned char ptr = 0; |
||
108 | |||
109 | |||
110 | SendeBuffer[pt++] = '#'; // Startzeichen |
||
111 | SendeBuffer[pt++] = modul; // Adresse (a=0; b=1,...) |
||
112 | SendeBuffer[pt++] = cmd; // Commando |
||
113 | |||
114 | while(len) |
||
115 | { |
||
116 | if(len) { a = snd[ptr++]; len--;} else a = 0; |
||
117 | if(len) { b = snd[ptr++]; len--;} else b = 0; |
||
118 | if(len) { c = snd[ptr++]; len--;} else c = 0; |
||
119 | SendeBuffer[pt++] = '=' + (a >> 2); |
||
120 | SendeBuffer[pt++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4)); |
||
121 | SendeBuffer[pt++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6)); |
||
122 | SendeBuffer[pt++] = '=' + ( c & 0x3f); |
||
123 | } |
||
124 | AddCRC(pt); |
||
125 | } |
||
126 | |||
127 | |||
128 | |||
129 | //############################################################################ |
||
130 | //Routine für die Serielle Ausgabe |
||
131 | int uart_putchar (char c) |
||
132 | //############################################################################ |
||
133 | { |
||
134 | if (c == '\n') |
||
135 | uart_putchar('\r'); |
||
136 | //Warten solange bis Zeichen gesendet wurde |
||
137 | loop_until_bit_is_set(USR, UDRE); |
||
138 | //Ausgabe des Zeichens |
||
139 | UDR = c; |
||
140 | |||
141 | return (0); |
||
142 | } |
||
143 | |||
144 | // -------------------------------------------------------------------------- |
||
145 | void WriteProgramData(unsigned int pos, unsigned char wert) |
||
146 | { |
||
147 | } |
||
148 | |||
149 | //############################################################################ |
||
150 | //INstallation der Seriellen Schnittstelle |
||
151 | void UART_Init (void) |
||
152 | //############################################################################ |
||
153 | { |
||
154 | //Enable TXEN im Register UCR TX-Data Enable & RX Enable |
||
155 | |||
156 | UCR=(1 << TXEN) | (1 << RXEN); |
||
157 | // UART Double Speed (U2X) |
||
158 | USR |= (1<<U2X); |
||
159 | // RX-Interrupt Freigabe |
||
160 | #if X3D_SIO == 1 |
||
161 | UCSRB |= (1<<RXCIE); // serieller Empfangsinterrupt |
||
162 | #endif |
||
163 | // TX-Interrupt Freigabe |
||
164 | // UCSRB |= (1<<TXCIE); |
||
165 | |||
166 | //Teiler wird gesetzt |
||
31 | Nick666 | 167 | //UBRR= (SYSCLK / (BAUD_RATE * 8L) -1 ); |
168 | #if X3D_SIO == 1 |
||
169 | UBRR = 12; //38400 Baud @ 4 MHz |
||
170 | #else |
||
171 | UBRR = 8; //57600 Baud @ 4 MHz |
||
172 | #endif |
||
173 | |||
1 | ingob | 174 | //öffnet einen Kanal für printf (STDOUT) |
175 | fdevopen (uart_putchar, NULL); |
||
176 | Debug_Timer = SetDelay(200); |
||
177 | UDR = '*'; |
||
178 | } |
||
179 | |||
180 | |||
181 | |||
182 | |||
183 | //--------------------------------------------------------------------------------------------- |
||
184 | void DatenUebertragung(void) |
||
185 | { |
||
186 | if((CheckDelay(Debug_Timer) && UebertragungAbgeschlossen)) // im Singlestep-Betrieb in jedem Schtitt senden |
||
187 | { |
||
188 | SendOutData('D',MeineSlaveAdresse,(unsigned char *) &DebugOut,sizeof(DebugOut)); |
||
189 | Debug_Timer = SetDelay(50); // Sendeintervall |
||
190 | } |
||
191 | } |