Rev 48 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 48 | Rev 50 | ||
|---|---|---|---|
| Line 15... | Line 15... | ||
| 15 | #include "../Include/Tick.h" |
15 | #include "../Include/Tick.h" |
| 16 | 16 | ||
| 17 | #ifdef USE_CAN |
17 | #ifdef USE_CAN |
| 18 | #include "../Include/CAN.h" |
18 | #include "../Include/CAN.h" |
| 19 | #endif |
19 | #endif |
| 20 | - | ||
| 21 | #ifdef USE_UART |
- | |
| 22 | #include "../Include/uart.h" |
- | |
| 23 | #endif |
- | |
| 24 | - | ||
| 25 | - | ||
| 26 | 20 | ||
| 27 | static void mainInit(void); |
21 | static void mainInit(void); |
| 28 | - | ||
| 29 | static BOOL heartBeatEnabled = FALSE; |
- | |
| 30 | BYTE ldc = 50; |
- | |
| 31 | 22 | ||
| 32 | void main() |
23 | void main() |
| 33 | { |
24 | { |
| 34 | static TICK t = 0; |
25 | static TICK t = 0; |
| 35 | 26 | ||
| 36 | // Inits |
27 | // Inits |
| 37 | 28 | ||
| 38 | mainInit(); |
29 | mainInit(); |
| 39 | 30 | ||
| 40 | #ifdef USE_CAN |
31 | #ifdef USE_CAN |
| 41 | canInit(); |
32 | canInit(); |
| 42 | #endif |
- | |
| 43 | - | ||
| 44 | #ifdef USE_UART |
- | |
| 45 | uartInit(); |
- | |
| 46 | #endif |
33 | #endif |
| 47 | 34 | ||
| 48 | tickInit(); |
35 | tickInit(); |
| 49 | 36 | ||
| 50 | while(1) |
37 | while(1) |
| 51 | { |
38 | { |
| 52 | #ifdef USE_CAN |
39 | #ifdef USE_CAN |
| 53 | if ((tickGet()-t)>=5*TICK_SECOND |
40 | if ((tickGet()-t)>=5*TICK_SECOND) |
| 54 | { |
41 | { |
| 55 | CAN_MESSAGE cm; |
42 | CAN_MESSAGE cm; |
| 56 | 43 | ||
| 57 | t = tickGet(); |
44 | t = tickGet(); |
| 58 | 45 | ||
| 59 | // Send heartbeat |
46 | // Send heartbeat |
| 60 | cm.ident=0x00000666; |
47 | cm.ident=0x00000666; |
| 61 | cm.extended=FALSE; |
48 | cm.extended=FALSE; |
| 62 | cm.data_length=1; |
49 | cm.data_length=1; |
| 63 | cm.data[0]='H'; |
50 | cm.data[0]='H'; |
| 64 | 51 | ||
| 65 | while(!canSendMessage(cm)); |
52 | while(!canSendMessage(cm)); |
| 66 | - | ||
| 67 | - | ||
| 68 | } |
53 | } |
| 69 | #endif |
54 | #endif |
| 70 | } |
55 | } |
| 71 | } |
56 | } |
| 72 | 57 | ||
| 73 | 58 | ||
| 74 | #pragma interruptlow HighISR |
59 | #pragma interruptlow HighISR |
| 75 | void HighISR(void) |
60 | void HighISR(void) |
| 76 | { |
61 | { |
| 77 | //if(ldc--<=1) {LED0_IO=~LED0_IO; ldc=50; } |
- | |
| - | 62 | ||
| 78 | 63 | ||
| 79 | #ifdef USE_CAN |
64 | #ifdef USE_CAN |
| 80 | canISR(); |
65 | canISR(); |
| 81 | #endif |
66 | #endif |
| 82 | 67 | ||
| 83 | #ifdef USE_UART |
- | |
| 84 | uartISR(); |
- | |
| 85 | #endif |
- | |
| 86 | 68 | ||
| 87 | if (INTCONbits.INT0IE && INTCONbits.INT0IF) |
69 | if (INTCONbits.INT0IE && INTCONbits.INT0IF) |
| 88 | { |
70 | { |
| 89 | static TICK t = 0; |
71 | static TICK t = 0; |
| 90 | CAN_MESSAGE cm; |
72 | CAN_MESSAGE cm; |
| 91 | 73 | ||
| 92 | if ((tickGet()-t)>TICK_SECOND/2) |
74 | if ((tickGet()-t)>TICK_SECOND/2) |
| 93 | { |
75 | { |
| 94 | 76 | ||
| 95 | LED0_IO=~LED0_IO; |
77 | LED0_IO=~LED0_IO; |
| 96 | 78 | ||
| 97 | // Send heartbeat |
79 | // Send heartbeat |
| 98 | cm.ident= |
80 | cm.ident=0x1F910091; |
| 99 | cm.extended= |
81 | cm.extended=TRUE; |
| 100 | cm.data_length=1; |
82 | cm.data_length=1; |
| 101 | cm.data[0]= |
83 | cm.data[0]=0x01; |
| - | 84 | cm.data[1]=0x07; |
|
| - | 85 | cm.data[2]=0x09; |
|
| - | 86 | cm.data[3]=0x08; |
|
| - | 87 | cm.data[4]=0x03; |
|
| 102 | 88 | ||
| 103 | while(!canSendMessage(cm)); |
89 | while(!canSendMessage(cm)); |
| 104 | 90 | ||
| 105 | t=tickGet(); |
91 | t=tickGet(); |
| 106 | } |
92 | } |
| 107 | 93 | ||
| 108 | INTCONbits.INT0IF=0; |
94 | INTCONbits.INT0IF=0; |
| 109 | } |
95 | } |
| 110 | 96 | ||
| 111 | //if (RCSTAbits.OERR) { RCSTAbits.CREN=0; RCSTAbits.CREN=1; } |
- | |
| 112 | 97 | ||
| 113 | tickUpdate(); |
98 | tickUpdate(); |
| 114 | } |
99 | } |
| 115 | #pragma code highVector=0x08 |
100 | #pragma code highVector=0x08 |
| 116 | void HighVector (void) |
101 | void HighVector (void) |
| 117 | { |
102 | { |
| 118 | _asm goto HighISR _endasm |
103 | _asm goto HighISR _endasm |
| 119 | } |
104 | } |
| 120 | #pragma code // Return to default code section |
105 | #pragma code // Return to default code section |
| 121 | 106 | ||
| 122 | 107 | ||
| 123 | 108 | ||
| 124 | #pragma interruptlow LowISR |
109 | #pragma interruptlow LowISR |
| 125 | void LowISR(void) |
110 | void LowISR(void) |
| 126 | { |
111 | { |
| 127 | 112 | ||
| 128 | } |
113 | } |
| 129 | #pragma code lowVector=0x18 |
114 | #pragma code lowVector=0x18 |
| 130 | void LowVector (void) |
115 | void LowVector (void) |
| 131 | { |
116 | { |
| 132 | _asm goto LowISR _endasm |
117 | _asm goto LowISR _endasm |
| 133 | } |
118 | } |
| 134 | #pragma code // Return to default code section |
119 | #pragma code // Return to default code section |
| 135 | 120 | ||
| 136 | 121 | ||
| 137 | /* |
122 | /* |
| 138 | * Function: mainInit |
123 | * Function: mainInit |
| 139 | * |
124 | * |
| 140 | * Input: none |
125 | * Input: none |
| 141 | * Output: none |
126 | * Output: none |
| Line 172... | Line 157... | ||
| 172 | * Output: none |
157 | * Output: none |
| 173 | * Pre-conditions: canInit and received packet. |
158 | * Pre-conditions: canInit and received packet. |
| 174 | * Affects: Sensors/actuators/etc. See code. |
159 | * Affects: Sensors/actuators/etc. See code. |
| 175 | * Depends: none. |
160 | * Depends: none. |
| 176 | */ |
161 | */ |
| 177 | #ifdef USE_CAN |
162 | #ifdef USE_CAN |
| 178 | void canParse(CAN_MESSAGE cm) |
163 | void canParse(CAN_MESSAGE cm) |
| 179 | { |
- | |
| 180 | int i=0; |
- | |
| 181 | - | ||
| 182 | uartPutc(UART_START_BYTE); |
- | |
| 183 | uartPutc((BYTE)(cm.ident)); |
- | |
| 184 | uartPutc((BYTE)(cm.ident>>8)); |
- | |
| 185 | uartPutc((BYTE)(cm.ident>>16)); |
- | |
| 186 | uartPutc((BYTE)(cm.ident>>24)); |
- | |
| 187 | uartPutc(cm.extended); |
- | |
| 188 | uartPutc(cm.data_length); |
- | |
| 189 | for(i=0;i<8;i++) uartPutc(cm.data[i]); |
- | |
| 190 | uartPutc(UART_END_BYTE); |
- | |
| 191 | - | ||
| 192 | } |
- | |
| 193 | #endif |
- | |
| 194 | - | ||
| 195 | - | ||
| 196 | /* |
- | |
| 197 | * Function: uartParse |
- | |
| 198 | * |
- | |
| 199 | * Input: Received uart message |
- | |
| 200 | * Output: none |
- | |
| 201 | * Pre-conditions: uartInit and received byte. |
- | |
| 202 | * Affects: Sensors/actuators/etc. See code. |
- | |
| 203 | * Depends: none. |
- | |
| 204 | */ |
- | |
| 205 | #ifdef USE_UART |
- | |
| 206 | void uartParse(BYTE c) |
- | |
| 207 | { |
164 | { |
| 208 | BYTE i; |
- | |
| 209 | unsigned int wait = 0; |
- | |
| 210 | CAN_MESSAGE cm; |
- | |
| 211 | static BOOL waitingMessage = FALSE; |
- | |
| 212 | static TICK timeout=0; |
- | |
| 213 | static BYTE count=0; |
- | |
| 214 | - | ||
| 215 | - | ||
| 216 | if (waitingMessage==TRUE && (tickGet()-timeout)>TICK_SECOND/20) |
- | |
| 217 | { |
- | |
| 218 | waitingMessage=FALSE; |
- | |
| 219 | } |
- | |
| 220 | - | ||
| 221 | - | ||
| 222 | if (waitingMessage==TRUE) |
- | |
| 223 | { |
- | |
| 224 | - | ||
| 225 | timeout=tickGet(); |
- | |
| 226 | - | ||
| 227 | // UART END |
- | |
| 228 | if(count>=14) |
- | |
| 229 | { |
- | |
| 230 | if (c==UART_END_BYTE) |
- | |
| 231 | { |
- | |
| 232 | while(!canSendMessage(cm)); |
- | |
| 233 | } |
- | |
| 234 | waitingMessage=FALSE; |
- | |
| 235 | return; |
- | |
| 236 | } |
- | |
| 237 | - | ||
| 238 | // data |
- | |
| 239 | if(count>=6) |
- | |
| 240 | { |
- | |
| 241 | cm.data[count-6]=c; |
- | |
| 242 | count++; |
- | |
| 243 | return; |
- | |
| 244 | } |
- | |
| 245 | - | ||
| 246 | // data length |
- | |
| 247 | if(count>=5) |
- | |
| 248 | { |
- | |
| 249 | cm.data_length=c; |
- | |
| 250 | count++; |
- | |
| 251 | return; |
- | |
| 252 | } |
- | |
| 253 | - | ||
| 254 | // extended |
- | |
| 255 | if(count>=4) |
- | |
| 256 | { |
- | |
| 257 | cm.extended=c; |
- | |
| 258 | count++; |
- | |
| 259 | return; |
- | |
| 260 | } |
- | |
| 261 | - | ||
| 262 | // ident |
- | |
| 263 | if(count>=0) |
- | |
| 264 | { |
- | |
| 265 | cm.ident+=((DWORD)c<<(count*8)); |
- | |
| 266 | count++; |
- | |
| 267 | return; |
- | |
| 268 | } |
- | |
| 269 | - | ||
| 270 | } |
- | |
| 271 | - | ||
| 272 | - | ||
| 273 | if (c==UART_START_BYTE && waitingMessage==FALSE) |
- | |
| 274 | { |
- | |
| 275 | waitingMessage=TRUE; |
- | |
| 276 | timeout=tickGet(); |
- | |
| 277 | count=0; |
- | |
| 278 | cm.ident=0; |
- | |
| 279 | return; |
- | |
| 280 | } |
- | |
| 281 | - | ||
| 282 | if(c=='1' && waitingMessage==FALSE) |
- | |
| 283 | { |
- | |
| 284 |
|
165 | if (cm.ident==0x00000304) |
| 285 | cm.extended=FALSE; |
- | |
| 286 | cm.data_length=4; |
- | |
| 287 | cm.data[3]='H'; |
- | |
| 288 | cm.data[2]='e'; |
- | |
| 289 | cm.data[1]='j'; |
- | |
| 290 | cm.data[0]='!'; |
- | |
| 291 | - | ||
| 292 | while(!canSendMessage(cm)); |
- | |
| 293 | } |
- | |
| 294 | - | ||
| 295 | if(c=='2' && waitingMessage==FALSE) |
- | |
| 296 | { |
166 | { |
| 297 | cm.ident=0x0ABCDEF0; |
- | |
| 298 | cm.extended=TRUE; |
- | |
| 299 | cm.data_length=8; |
- | |
| 300 | cm.data[7]='G'; |
- | |
| 301 | cm.data[6]='o'; |
- | |
| 302 | cm.data[5]='d'; |
- | |
| 303 | cm.data |
167 | if (cm.data[0]==0x20) LED0_IO=1; |
| 304 | cm.data[3]='d'; |
- | |
| 305 | cm.data[2]='a'; |
- | |
| 306 | cm.data |
168 | if (cm.data[0]==0x40) LED0_IO=0; |
| 307 | cm.data |
169 | if (cm.data[0]==0x80) |
| 308 | - | ||
| 309 | while(!canSendMessage(cm)); |
- | |
| 310 | } |
- | |
| 311 | - | ||
| 312 | if(c=='3' && waitingMessage==FALSE) |
- | |
| 313 | { |
170 | { |
| 314 | cm.ident= |
171 | cm.ident=0x00000303; |
| 315 | cm.extended=FALSE; |
172 | cm.extended=FALSE; |
| 316 | cm.data_length= |
173 | cm.data_length=1; |
| 317 | cm.data[5]='5'; |
- | |
| 318 | cm.data[4]='4'; |
- | |
| 319 | cm.data[3]='3'; |
- | |
| 320 | cm.data[2]='2'; |
- | |
| 321 | cm.data[1]='1'; |
- | |
| 322 | cm.data[0]= |
174 | cm.data[0]=LED0_IO; |
| 323 | - | ||
| 324 | while(!canSendMessage(cm)); |
175 | while(!canSendMessage(cm)); |
| - | 176 | ||
| - | 177 | } |
|
| 325 | } |
178 | } |
| 326 | - | ||
| 327 | 179 | ||
| 328 | 180 | ||
| 329 | } |
181 | } |
| 330 | #endif |
182 | #endif |
| - | 183 | ||