Subversion Repositories HomeAutomation

Rev

Rev 190 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 190 Rev 192
Line 13... Line 13...
13
 *  Transmitts: DATA: <temperature low byte> <temperature high byte> <relay status> <failsafe mode>
13
 *  Transmitts: DATA: <temperature low byte> <temperature high byte> <relay status> <failsafe mode>
14
 *
14
 *
15
 *
15
 *
16
 */
16
 */
17
 
17
 
18
#define DEBUG   0 /* Prints debug messages to uart */
18
#define DEBUG           1 /* Prints debug messages to uart */
-
 
19
#define TWO_BUTTON_MODE 1 /* If using two (or just one but nothing more) auxiliary buttons */
19
 
20
 
20
/*-----------------------------------------------------------------------------
21
/*-----------------------------------------------------------------------------
21
 * Includes
22
 * Includes
22
 *---------------------------------------------------------------------------*/
23
 *---------------------------------------------------------------------------*/
23
/* system files */
24
/* system files */
Line 45... Line 46...
45
#define RELAY_CMD_TOGGLE    0x03
46
#define RELAY_CMD_TOGGLE    0x03
46
#define RELAY_CMD_STATUS    0x04
47
#define RELAY_CMD_STATUS    0x04
47
#define RELAY_CMD_RESET     0x05
48
#define RELAY_CMD_RESET     0x05
48
#define STATUS_SEND_PERIOD  10000 /* milliseconds */
49
#define STATUS_SEND_PERIOD  10000 /* milliseconds */
49
#define FAILSAFE_SEND_PERIOD  10000 /* milliseconds */
50
#define FAILSAFE_SEND_PERIOD    10000 /* milliseconds */
-
 
51
#define BOUNCE_TIME         10 /* milliseconds */
-
 
52
#define BUTTON_ID           0x1202
-
 
53
#define BUTTON1             1
-
 
54
#define BUTTON2             2
-
 
55
 
-
 
56
/*-------------------------------------------------------------------------
-
 
57
 * Global variables
-
 
58
 * -----------------------------------------------------------------------*/
-
 
59
#if TWO_BUTTON_MODE
-
 
60
Can_Message_t txMsg_Button;
-
 
61
#endif
50
 
62
 
51
/*----------------------------------------------------------------------------
63
/*----------------------------------------------------------------------------
52
 * Functions
64
 * Functions
53
 * -------------------------------------------------------------------------*/
65
 * -------------------------------------------------------------------------*/
54
uint8_t relayOff();
66
uint8_t relayOff();
55
uint8_t relayOn();
67
uint8_t relayOn();
56
void relayFailsafe();
68
void relayFailsafe();
-
 
69
 
-
 
70
/*----------------------------------------------------------------------------
-
 
71
 * Interrupt routines
-
 
72
 * -------------------------------------------------------------------------*/
-
 
73
#if TWO_BUTTON_MODE
-
 
74
ISR( PCINT2_vect )
-
 
75
{
-
 
76
    /*
-
 
77
     * Auxiliary button pressed and released
-
 
78
     * Check time between press and release to eliminate bounces
-
 
79
     */
-
 
80
    static uint32_t buttonTime[2];
-
 
81
 
-
 
82
    txMsg_Button.RemoteFlag = 0;
-
 
83
    txMsg_Button.ExtendedFlag = 1;
-
 
84
 
-
 
85
    if( (PIND & (1<<PD6)) == 0){
-
 
86
        /* Button pressed */
-
 
87
        buttonTime[0] = Timebase_CurrentTime();
-
 
88
#if DEBUG
-
 
89
        printf("Button 1 pressed\n");
-
 
90
#endif
-
 
91
    }else if( (PIND & (1<<PD6)) == 1){
-
 
92
        if( Timebase_PassedTimeMillis( buttonTime[0] ) >= BOUNCE_TIME ){
-
 
93
            /* No bounce, send message */
-
 
94
            txMsg_Button.Id = BUTTON_ID;
-
 
95
            txMsg_Button.DataLength = 1;
-
 
96
            txMsg_Button.Data.bytes[0] = BUTTON1;
-
 
97
#if DEBUG
-
 
98
            printf("Button 1 pressed: message sent\n");
-
 
99
#endif
-
 
100
            return;
-
 
101
        }else{
-
 
102
            /* Just bounces, ignore it */
-
 
103
#if DEBUG
-
 
104
            printf("Button 1 just bounced\n");
-
 
105
#endif
-
 
106
            return;
-
 
107
        }
-
 
108
    }
-
 
109
    if( (PIND & (1<<PD7)) == 0){
-
 
110
        /* Button pressed */
-
 
111
        buttonTime[1] = Timebase_CurrentTime();
-
 
112
#if DEBUG
-
 
113
        printf("Button 2 pressed\n");
-
 
114
#endif
-
 
115
    }else if( (PIND & (1<<PD7)) == 1){
-
 
116
        if( Timebase_PassedTimeMillis( buttonTime[1] ) >= BOUNCE_TIME ){
-
 
117
            /* No bounce, send message */
-
 
118
            txMsg_Button.Id = BUTTON_ID;
-
 
119
            txMsg_Button.DataLength = 1;
-
 
120
            txMsg_Button.Data.bytes[0] = BUTTON2;
-
 
121
#if DEBUG
-
 
122
            printf("Button 2 pressed: message sent\n");
-
 
123
#endif
-
 
124
            return;
-
 
125
        }else{
-
 
126
            /* Just bounces, ignore it */
-
 
127
#if DEBUG
-
 
128
            printf("Button 2 just bounced\n");
-
 
129
#endif
-
 
130
            return;
-
 
131
        }
-
 
132
    }
-
 
133
}
-
 
134
#endif
-
 
135
 
-
 
136
//ISR( ADC_vect )
-
 
137
//{
-
 
138
    /*
-
 
139
     * ADC conversion completed
-
 
140
     */
-
 
141
    // TODO använd denna rutin för att läsa värde när omvandlingen är klar
-
 
142
 
-
 
143
//}
57
 
144
 
58
/*-----------------------------------------------------------------------------
145
/*-----------------------------------------------------------------------------
59
 * Main Program
146
 * Main Program
60
 *---------------------------------------------------------------------------*/
147
 *---------------------------------------------------------------------------*/
61
int main(void) {
148
int main(void) {
Line 67... Line 154...
67
 
154
 
68
    Timebase_Init();
155
    Timebase_Init();
69
#if DEBUG
156
#if DEBUG
70
    Serial_Init();
157
    Serial_Init();
71
#endif
158
#endif
-
 
159
 
-
 
160
#if TWO_BUTTON_MODE
-
 
161
    /*
-
 
162
     * Initialize buttons
-
 
163
     * It is possible to use ie. sensors instead
-
 
164
     * but then change this
-
 
165
     */
-
 
166
    /* Set as input and enable pull-up */
-
 
167
    DDRD &= ~( (1<<DDD6)|(1<<DDD7) );
-
 
168
    PORTD |= (1<<PD6)|(1<<PD7);
-
 
169
 
-
 
170
    /* Enable IO-pin interrupt */
-
 
171
    PCICR |= (1<<PCIE1);
-
 
172
    /* Unmask PD6 and PD7 */
-
 
173
    PCMSK2 |= (1<<PCINT22) | (1<<PCINT23);
-
 
174
        /* Button pressed */
-
 
175
#endif
-
 
176
 
-
 
177
 
72
    sei();
178
    sei();
73
 
179
 
74
    /* Turn relay off */
180
    /* Turn relay off */
75
    relayStatus = relayOff();
181
    relayStatus = relayOff();
76
 
182
 
Line 88... Line 194...
88
    }
194
    }
89
#else
195
#else
90
    Can_Init();
196
    Can_Init();
91
#endif
197
#endif
92
    uint32_t timeStamp = 0;
198
    uint32_t timeStamp = 0;
93
 
199
 
94
    Can_Message_t txMsg;
200
    Can_Message_t txMsg;
95
    Can_Message_t rxMsg;
201
    Can_Message_t rxMsg;
96
    txMsg.Id = RELAY_CMD_SEND;
202
    txMsg.Id = RELAY_CMD_SEND;
97
    txMsg.RemoteFlag = 0;
203
    txMsg.RemoteFlag = 0;
98
    txMsg.ExtendedFlag = 1;
204
    txMsg.ExtendedFlag = 1;
99
    txMsg.DataLength = 3;
205
    txMsg.DataLength = 3;
100
 
206
 
101
    /* main loop */
207
    /* main loop */
102
    while (1) {
208
    while (1) {
103
        /* service the CAN routines */
209
        /* service the CAN routines */
104
        Can_Service();
210
        Can_Service();
105
 
211
 
106
        /* check if any messages have been received */
212
        /* check if any messages have been received */
107
        while (Can_Receive(&rxMsg) == CAN_OK) {
213
        while (Can_Receive(&rxMsg) == CAN_OK) {
108
            /* This relay is adressed*/
214
            /* This relay is adressed*/
109
            if( rxMsg.Id == RELAY_CMD_GET ){
215
            if( rxMsg.Id == RELAY_CMD_GET ){
110
 
216
 
111
                if( rxMsg.Data.bytes[0] == RELAY_CMD_ON ){
217
                if( rxMsg.Data.bytes[0] == RELAY_CMD_ON ){
112
                    /* Turn relay on */
218
                    /* Turn relay on */
113
                    relayStatus = relayOn();
219
                    relayStatus = relayOn();
114
 
220
 
115
                }else if(rxMsg.Data.bytes[0] == RELAY_CMD_OFF){
221
                }else if(rxMsg.Data.bytes[0] == RELAY_CMD_OFF){
116
                    /* Turn relay off */
222
                    /* Turn relay off */
117
                    relayStatus = relayOff();
223
                    relayStatus = relayOff();
118
 
224
 
119
                }else if(rxMsg.Data.bytes[0] == RELAY_CMD_TOGGLE ){
225
                }else if(rxMsg.Data.bytes[0] == RELAY_CMD_TOGGLE ){
120
                    /* Toggle relay */
226
                    /* Toggle relay */
121
 
227
 
122
                    if(relayStatus == RELAY_OFF){
228
                    if(relayStatus == RELAY_OFF){
123
                        relayStatus = relayOn();
229
                        relayStatus = relayOn();
124
                    }else{
230
                    }else{
125
                        relayStatus = relayOff();
231
                        relayStatus = relayOff();
126
                    }
232
                    }
127
 
233
 
128
                }else if(rxMsg.Data.bytes[0] == RELAY_CMD_STATUS){
234
                }else if(rxMsg.Data.bytes[0] == RELAY_CMD_STATUS){
129
                    /* Send relay status to CAN */
235
                    /* Send relay status to CAN */
130
 
236
 
131
                    boardTemperature = getTC1047temperature();
237
                    boardTemperature = getTC1047temperature();
132
 
238
 
133
                    if( (int32_t)boardTemperature >= FAILSAFE_TRESHOLD ){
239
                    if( (int32_t)boardTemperature >= FAILSAFE_TRESHOLD ){
134
                        relayFailsafe();
240
                        relayFailsafe();
135
                    }else{
241
                    }else{
136
                        txMsg.Data.bytes[0] = boardTemperature & 0x00FF;
242
                        txMsg.Data.bytes[0] = boardTemperature & 0x00FF;
137
                        txMsg.Data.bytes[1] = (boardTemperature & 0xFF00)>>8;
243
                        txMsg.Data.bytes[1] = (boardTemperature & 0xFF00)>>8;
138
                        txMsg.Data.bytes[2] = relayStatus;
244
                        txMsg.Data.bytes[2] = relayStatus;
139
 
245
 
140
                        Can_Send( &txMsg );
246
                        Can_Send( &txMsg );
141
                    }
247
                    }
142
                }
248
                }
143
            }
249
            }
144
        }
250
        }
145
 
251
 
146
        if( Timebase_PassedTimeMillis(timeStamp) >= STATUS_SEND_PERIOD ){
252
        if( Timebase_PassedTimeMillis(timeStamp) >= STATUS_SEND_PERIOD ){
147
            timeStamp = Timebase_CurrentTime();
253
            timeStamp = Timebase_CurrentTime();
148
            /* Send relay status to CAN */
254
            /* Send relay status to CAN */
149
 
255
 
150
            boardTemperature = getTC1047temperature();
256
            boardTemperature = getTC1047temperature();
151
 
257
 
152
            if( (int32_t)boardTemperature >= FAILSAFE_TRESHOLD ){
258
            if( (int32_t)boardTemperature >= FAILSAFE_TRESHOLD ){
153
                relayFailsafe();
259
                relayFailsafe();
154
            }else{
260
            }else{
155
                txMsg.Data.bytes[0] = boardTemperature & 0x00FF;
261
                txMsg.Data.bytes[0] = boardTemperature & 0x00FF;
156
                txMsg.Data.bytes[1] = (boardTemperature & 0xFF00)>>8;
262
                txMsg.Data.bytes[1] = (boardTemperature & 0xFF00)>>8;
157
                txMsg.Data.bytes[2] = relayStatus;
263
                txMsg.Data.bytes[2] = relayStatus;
158
 
264
 
159
                Can_Send( &txMsg );
265
                Can_Send( &txMsg );
160
#if DEBUG
266
#if DEBUG
161
                printf("Periodic message sent...\n");
267
                printf("Periodic message sent...\n");
162
                printf("Temperature: %d\nRelay status: %u\n", boardTemperature, relayStatus);
268
                printf("Temperature: %d\nRelay status: %u\n", boardTemperature, relayStatus);
163
#endif
269
#endif
164
            }
270
            }
165
        }
271
        }
166
    }
272
    }
167
    return 0;
273
    return 0;
168
}
274
}
169
 
275
 
170
uint8_t relayOff()
276
uint8_t relayOff()
171
{
277
{
Line 173... Line 279...
173
    PORTC &= ~(1<<PC1);
279
    PORTC &= ~(1<<PC1);
174
    DDRC |= (1<<DDC1);
280
    DDRC |= (1<<DDC1);
175
 
281
 
176
#if DEBUG
282
#if DEBUG
177
    printf("relay turned off\n");
283
    printf("relay turned off\n");
178
#endif
284
#endif
179
 
285
 
180
    return RELAY_OFF;
286
    return RELAY_OFF;
181
}
287
}
182
 
288
 
183
uint8_t relayOn()
289
uint8_t relayOn()
Line 215... Line 321...
215
        while (Can_Receive(&rxMsg) == CAN_OK) {
321
        while (Can_Receive(&rxMsg) == CAN_OK) {
216
            /* This relay is adressed*/
322
            /* This relay is adressed*/
217
            if( rxMsg.Id == RELAY_CMD_GET ){
323
            if( rxMsg.Id == RELAY_CMD_GET ){
218
                if( rxMsg.Data.bytes[0] == RELAY_CMD_RESET ){
324
                if( rxMsg.Data.bytes[0] == RELAY_CMD_RESET ){
219
                    /* Return from failsafe mode */
325
                    /* Return from failsafe mode */
220
                    return 0;
326
                    return;
221
                }
327
                }
222
            }
328
            }
223
        }
329
        }
224
 
330
 
225
        if( Timebase_PassedTimeMillis(timeStamp) >= FAILSAFE_SEND_PERIOD ){
331
        if( Timebase_PassedTimeMillis(timeStamp) >= FAILSAFE_SEND_PERIOD ){