Subversion Repositories HomeAutomation

Rev

Details | Last modification | View Log | SVN | RSS feed

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