Subversion Repositories HomeAutomation

Rev

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

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