Go to most recent revision | Details | 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 |
||
167 | UBRR= (SYSCLK / (BAUD_RATE * 8L) -1 ); |
||
168 | //öffnet einen Kanal für printf (STDOUT) |
||
169 | fdevopen (uart_putchar, NULL); |
||
170 | Debug_Timer = SetDelay(200); |
||
171 | UDR = '*'; |
||
172 | } |
||
173 | |||
174 | |||
175 | |||
176 | |||
177 | //--------------------------------------------------------------------------------------------- |
||
178 | void DatenUebertragung(void) |
||
179 | { |
||
180 | if((CheckDelay(Debug_Timer) && UebertragungAbgeschlossen)) // im Singlestep-Betrieb in jedem Schtitt senden |
||
181 | { |
||
182 | SendOutData('D',MeineSlaveAdresse,(unsigned char *) &DebugOut,sizeof(DebugOut)); |
||
183 | Debug_Timer = SetDelay(50); // Sendeintervall |
||
184 | } |
||
185 | } |