Subversion Repositories HomeAutomation

Rev

Rev 50 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

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