Subversion Repositories HomeAutomation

Rev

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

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