Rev 53 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 53 | Rev 56 | ||
|---|---|---|---|
| 1 | /********************************************************************* |
1 | /********************************************************************* |
| 2 | * |
2 | * |
| 3 | * Main application |
3 | * Main application |
| 4 | * |
4 | * |
| 5 | ********************************************************************* |
5 | ********************************************************************* |
| 6 | * FileName: Main.c |
6 | * FileName: Main.c |
| 7 | * |
7 | * |
| 8 | * Author Date Comment |
8 | * Author Date Comment |
| 9 | *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
9 | *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 10 | * Johan Böhlin 21-10-06 Original (Rev 1.0) |
10 | * Johan Böhlin 21-10-06 Original (Rev 1.0) |
| 11 | ********************************************************************/ |
11 | ********************************************************************/ |
| 12 | 12 | ||
| 13 | #include <compiler.h> |
13 | #include <compiler.h> |
| 14 | #include <stackTasks.h> |
14 | #include <stackTasks.h> |
| 15 | #include <Tick.h> |
15 | #include <Tick.h> |
| 16 | 16 | ||
| 17 | #ifdef USE_CAN |
17 | #ifdef USE_CAN |
| 18 | #include <CAN.h> |
18 | #include <CAN.h> |
| 19 | #endif |
19 | #endif |
| 20 | 20 | ||
| 21 | static void mainInit(void); |
21 | static void mainInit(void); |
| - | 22 | ||
| - | 23 | unsigned int heartcnt=0; |
|
| 22 | 24 | ||
| 23 | void main() |
25 | void main() |
| 24 | { |
26 | { |
| 25 | static TICK t = 0; |
27 | static TICK t = 0; |
| 26 | 28 | ||
| 27 | // Inits |
29 | // Inits |
| 28 | 30 | ||
| 29 | mainInit(); |
31 | mainInit(); |
| 30 | 32 | ||
| 31 | #ifdef USE_CAN |
33 | #ifdef USE_CAN |
| 32 | canInit(); |
34 | canInit(); |
| 33 | #endif |
35 | #endif |
| 34 | 36 | ||
| 35 | tickInit(); |
37 | tickInit(); |
| 36 | 38 | ||
| 37 | while(1) |
39 | while(1) |
| 38 | { |
40 | { |
| 39 | #ifdef USE_CAN |
41 | #ifdef USE_CAN |
| 40 | if ((tickGet()-t)>=5*TICK_SECOND) |
42 | if ((tickGet()-t)>=5*TICK_SECOND) |
| 41 | { |
43 | { |
| 42 | CAN_MESSAGE cm; |
44 | CAN_MESSAGE cm; |
| 43 | 45 | ||
| 44 | t = tickGet(); |
46 | t = tickGet(); |
| 45 | 47 | ||
| 46 | // Send heartbeat |
48 | // Send heartbeat |
| 47 | cm.ident=0x00000666; |
49 | cm.ident=0x00000666; |
| 48 | cm.extended=FALSE; |
50 | cm.extended=FALSE; |
| 49 | cm.data_length= |
51 | cm.data_length=5; |
| 50 | cm.data[0]='H'; |
52 | cm.data[0]='H'; |
| - | 53 | cm.data[1]=(BYTE)((heartcnt & 0x000000FF)); |
|
| - | 54 | cm.data[2]=(BYTE)((heartcnt & 0x0000FF00)>>1); |
|
| - | 55 | cm.data[3]=(BYTE)((heartcnt & 0x00FF0000)>>2); |
|
| - | 56 | cm.data[4]=(BYTE)((heartcnt & 0xFF000000)>>3); |
|
| - | 57 | ||
| - | 58 | heartcnt++; |
|
| 51 | 59 | ||
| 52 | while(!canSendMessage(cm)); |
60 | while(!canSendMessage(cm,PRIO_LOW)); |
| 53 | } |
61 | } |
| 54 | #endif |
62 | #endif |
| 55 | } |
63 | } |
| 56 | } |
64 | } |
| 57 | 65 | ||
| 58 | 66 | ||
| 59 | #pragma interruptlow HighISR |
67 | #pragma interruptlow HighISR |
| 60 | void HighISR(void) |
68 | void HighISR(void) |
| 61 | { |
69 | { |
| 62 | 70 | ||
| 63 | 71 | ||
| 64 | #ifdef USE_CAN |
72 | #ifdef USE_CAN |
| 65 | canISR(); |
73 | canISR(); |
| 66 | #endif |
74 | #endif |
| 67 | 75 | ||
| 68 | 76 | ||
| 69 | if (INTCONbits.INT0IE && INTCONbits.INT0IF) |
77 | if (INTCONbits.INT0IE && INTCONbits.INT0IF) |
| 70 | { |
78 | { |
| 71 | static TICK t = 0; |
79 | static TICK t = 0; |
| 72 | CAN_MESSAGE cm; |
80 | CAN_MESSAGE cm; |
| 73 | 81 | ||
| 74 | if ((tickGet()-t)>TICK_SECOND/2) |
82 | if ((tickGet()-t)>TICK_SECOND/2) |
| 75 | { |
83 | { |
| 76 | 84 | ||
| 77 | LED0_IO=~LED0_IO; |
85 | LED0_IO=~LED0_IO; |
| 78 | 86 | ||
| 79 | // Send heartbeat |
87 | // Send heartbeat |
| 80 | cm.ident=0x1F910091; |
88 | cm.ident=0x1F910091; |
| 81 | cm.extended=TRUE; |
89 | cm.extended=TRUE; |
| - | 90 | cm.remote_request=TRUE; |
|
| 82 | cm.data_length=1; |
91 | cm.data_length=1; |
| 83 | cm.data[0]=0x01; |
92 | cm.data[0]=0x01; |
| 84 | cm.data[1]=0x07; |
93 | cm.data[1]=0x07; |
| 85 | cm.data[2]=0x09; |
94 | cm.data[2]=0x09; |
| 86 | cm.data[3]=0x08; |
95 | cm.data[3]=0x08; |
| 87 | cm.data[4]=0x03; |
96 | cm.data[4]=0x03; |
| 88 | 97 | ||
| 89 | while(!canSendMessage(cm)); |
98 | while(!canSendMessage(cm,PRIO_LOW)); |
| 90 | 99 | ||
| 91 | t=tickGet(); |
100 | t=tickGet(); |
| 92 | } |
101 | } |
| 93 | 102 | ||
| 94 | INTCONbits.INT0IF=0; |
103 | INTCONbits.INT0IF=0; |
| 95 | } |
104 | } |
| 96 | 105 | ||
| 97 | 106 | ||
| 98 | tickUpdate(); |
107 | tickUpdate(); |
| 99 | } |
108 | } |
| 100 | #pragma code highVector=0x08 |
109 | #pragma code highVector=0x08 |
| 101 | void HighVector (void) |
110 | void HighVector (void) |
| 102 | { |
111 | { |
| 103 | _asm goto HighISR _endasm |
112 | _asm goto HighISR _endasm |
| 104 | } |
113 | } |
| 105 | #pragma code // Return to default code section |
114 | #pragma code // Return to default code section |
| 106 | 115 | ||
| 107 | 116 | ||
| 108 | 117 | ||
| 109 | #pragma interruptlow LowISR |
118 | #pragma interruptlow LowISR |
| 110 | void LowISR(void) |
119 | void LowISR(void) |
| 111 | { |
120 | { |
| 112 | 121 | ||
| 113 | } |
122 | } |
| 114 | #pragma code lowVector=0x18 |
123 | #pragma code lowVector=0x18 |
| 115 | void LowVector (void) |
124 | void LowVector (void) |
| 116 | { |
125 | { |
| 117 | _asm goto LowISR _endasm |
126 | _asm goto LowISR _endasm |
| 118 | } |
127 | } |
| 119 | #pragma code // Return to default code section |
128 | #pragma code // Return to default code section |
| 120 | 129 | ||
| 121 | 130 | ||
| 122 | /* |
131 | /* |
| 123 | * Function: mainInit |
132 | * Function: mainInit |
| 124 | * |
133 | * |
| 125 | * Input: none |
134 | * Input: none |
| 126 | * Output: none |
135 | * Output: none |
| 127 | * Pre-conditions: none |
136 | * Pre-conditions: none |
| 128 | * Affects: Se code |
137 | * Affects: Se code |
| 129 | * Depends: none. |
138 | * Depends: none. |
| 130 | */ |
139 | */ |
| 131 | void mainInit() |
140 | void mainInit() |
| 132 | { |
141 | { |
| 133 | LED0_TRIS=0; |
142 | LED0_TRIS=0; |
| 134 | 143 | ||
| 135 | // Enable internal PORTB pull-ups |
144 | // Enable internal PORTB pull-ups |
| 136 | INTCON2bits.RBPU = 0; |
145 | INTCON2bits.RBPU = 0; |
| 137 | 146 | ||
| 138 | // Enable Interrupts |
147 | // Enable Interrupts |
| 139 | INTCONbits.GIEH = 1; |
148 | INTCONbits.GIEH = 1; |
| 140 | INTCONbits.GIEL = 1; |
149 | INTCONbits.GIEL = 1; |
| 141 | 150 | ||
| 142 | 151 | ||
| 143 | // RB0 interrupt |
152 | // RB0 interrupt |
| 144 | INTCON2bits.INTEDG0 = 1; //rising edge |
153 | INTCON2bits.INTEDG0 = 1; //rising edge |
| 145 | INTCONbits.INT0IE = 1; |
154 | INTCONbits.INT0IE = 1; |
| 146 | 155 | ||
| 147 | // Enable interrupt prirority |
156 | // Enable interrupt prirority |
| 148 | RCONbits.IPEN=1; |
157 | RCONbits.IPEN=1; |
| 149 | 158 | ||
| 150 | 159 | ||
| 151 | } |
160 | } |
| 152 | 161 | ||
| 153 | /* |
162 | /* |
| 154 | * Function: canParse |
163 | * Function: canParse |
| 155 | * |
164 | * |
| 156 | * Input: Received can message |
165 | * Input: Received can message |
| 157 | * Output: none |
166 | * Output: none |
| 158 | * Pre-conditions: canInit and received packet. |
167 | * Pre-conditions: canInit and received packet. |
| 159 | * Affects: Sensors/actuators/etc. See code. |
168 | * Affects: Sensors/actuators/etc. See code. |
| 160 | * Depends: none. |
169 | * Depends: none. |
| 161 | */ |
170 | */ |
| 162 | #ifdef USE_CAN |
171 | #ifdef USE_CAN |
| 163 | void canParse(CAN_MESSAGE cm) |
172 | void canParse(CAN_MESSAGE cm) |
| 164 | { |
173 | { |
| 165 | if (cm.ident==0x00000304) |
174 | if (cm.ident==0x00000304) |
| 166 | { |
175 | { |
| 167 | if (cm.data[0]==0x20) LED0_IO=1; |
176 | if (cm.data[0]==0x20) LED0_IO=1; |
| 168 | if (cm.data[0]==0x40) LED0_IO=0; |
177 | if (cm.data[0]==0x40) LED0_IO=0; |
| 169 | if (cm.data[0]==0x80) |
178 | if (cm.data[0]==0x80) |
| 170 | { |
179 | { |
| 171 | cm.ident=0x00000303; |
180 | cm.ident=0x00000303; |
| 172 | cm.extended=FALSE; |
181 | cm.extended=FALSE; |
| 173 | cm.data_length=1; |
182 | cm.data_length=1; |
| 174 | cm.data[0]=LED0_IO; |
183 | cm.data[0]=LED0_IO; |
| 175 | while(!canSendMessage(cm)); |
184 | while(!canSendMessage(cm,PRIO_LOW)); |
| 176 | 185 | ||
| 177 | } |
186 | } |
| 178 | } |
187 | } |
| 179 | 188 | ||
| 180 | 189 | ||
| 181 | } |
190 | } |
| 182 | #endif |
191 | #endif |
| 183 | 192 | ||
| 184 | 193 | ||