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