Subversion Repositories HomeAutomation

Rev

Rev 185 | Rev 190 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 185 Rev 187
Line 10... Line 10...
10
 *   ADC=Vin*1024/Vref
10
 *   ADC=Vin*1024/Vref
11
 *   Vout=( 10 mV/C )( Temperature C ) + 500 mV
11
 *   Vout=( 10 mV/C )( Temperature C ) + 500 mV
12
 *
12
 *
13
 *  Transmitts: DATA: <temperature low byte> <temperature high byte> <relay status>
13
 *  Transmitts: DATA: <temperature low byte> <temperature high byte> <relay status>
14
 *
14
 *
15
 *  TODO fixa så status/temp ligger i en funktion
15
 *  TODO
16
 *  temperaturomvandlingen fungerar fint
-
 
17
 *  fixa så adcn inte pollas hela tiden, det tar för mycket tid
16
 *  fixa så adcn inte pollas hela tiden, det tar för mycket tid
18
 *  kunna använda Vref = 1.1
17
 *  kunna använda Vref = 1.1
-
 
18
 *
19
 *
19
 *
20
 */
20
 */
21
 
21
 
22
/*
-
 
23
 * For testing with ATmega88 PDIP enable this fetaure
-
 
24
 * Since PDIP doesn't have ADC7 it will use the ADC0 instead
-
 
25
 */
-
 
26
#define PDIP    1
-
 
27
#define DEBUG   0 /* Prints debug messages to uart */
22
#define DEBUG   0 /* Prints debug messages to uart */
28
 
23
 
29
/*-----------------------------------------------------------------------------
24
/*-----------------------------------------------------------------------------
30
 * Includes
25
 * Includes
31
 *---------------------------------------------------------------------------*/
26
 *---------------------------------------------------------------------------*/
32
/* system files */
27
/* system files */
33
#include <avr/io.h>
28
#include <avr/io.h>
Line 36... Line 31...
36
#include <stdio.h>
31
#include <stdio.h>
37
/* lib files */
32
/* lib files */
38
#include <can.h>
33
#include <can.h>
39
#include <serial.h>
34
#include <serial.h>
40
#include <timebase.h>
35
#include <timebase.h>
-
 
36
 
-
 
37
#include <tc1047.h>
-
 
38
 
41
/* defines */
39
/* defines */
42
#define OFF     0x00
40
#define RELAY_OFF           0x01
-
 
41
#define RELAY_ON            0x02
-
 
42
#define FAILSAFE_MODE       0x0F
-
 
43
#define FAILSAFE_TRESHOLD   60  /* Degrees when nodeRelay goes into failsafe mode */
-
 
44
 
-
 
45
#define RELAY_CMD_GET       0x1200
-
 
46
#define RELAY_CMD_SEND      0x1201
43
#define ON      0x01
47
#define RELAY_CMD_ON        0x01
-
 
48
#define RELAY_CMD_OFF       0x02
-
 
49
#define RELAY_CMD_TOGGLE    0x03
-
 
50
#define RELAY_CMD_STATUS    0x04
-
 
51
#define RELAY_CMD_RESET     0x05
-
 
52
#define STATUS_SEND_PERIOD  10000 /* milliseconds */
44
#define Vref    5 /* For Aref use 5, internal ref 1.1 */
53
#define FAILSAFE_SEND_PERIOD  10000 /* milliseconds */
-
 
54
 
-
 
55
/*----------------------------------------------------------------------------
-
 
56
 * Functions
-
 
57
 * -------------------------------------------------------------------------*/
-
 
58
uint8_t relayOff();
-
 
59
uint8_t relayOn();
-
 
60
void relayFailsafe();
45
 
61
 
46
/*-----------------------------------------------------------------------------
62
/*-----------------------------------------------------------------------------
47
 * Main Program
63
 * Main Program
48
 *---------------------------------------------------------------------------*/
64
 *---------------------------------------------------------------------------*/
49
int main(void) {
65
int main(void) {
50
 
66
 
51
    uint8_t relayStatus;
67
    uint8_t relayStatus;
52
    uint32_t boardTemperature;
68
    uint32_t boardTemperature = 0;
-
 
69
 
-
 
70
    adcTemperatureInit();
53
 
71
 
54
    Timebase_Init();
72
    Timebase_Init();
55
#if DEBUG
73
#if DEBUG
56
    Serial_Init();
74
    Serial_Init();
57
#endif
75
#endif
58
    sei();
76
    sei();
59
 
77
 
60
    /* Turn relay off */
78
    /* Turn relay off */
61
    PORTC &= ~(1<<PC1);
-
 
62
    DDRC |= (1<<DDC1);
-
 
63
 
-
 
64
    relayStatus = OFF;
79
    relayStatus = relayOff();
65
 
-
 
66
    /* Initiate ADC for reading temperature sensor */
-
 
67
 
-
 
68
    /* Wake up ADC */
-
 
69
    PRR &= ~(1<<PRADC);
-
 
70
#if PDIP
-
 
71
    /* Enable AREF and ADC0 (For PDIP package) */
-
 
72
    ADMUX = 0x00; // TODO 0x00 för aref, 0xc0 för internal 1,1
-
 
73
#else
-
 
74
    /* Enable AREF and ADC7 (For TQFP package) */
-
 
75
    ADMUX = 0x07;
-
 
76
#endif
-
 
77
    ADCSRA |= (1<<ADEN);
-
 
78
 
80
 
79
#if DEBUG
81
#if DEBUG
80
    printf("\n------------------------------------------------------------\n");
82
    printf("\n------------------------------------------------------------\n");
81
    printf(  "   CAN: Relay\n");
83
    printf(  "   CAN: Relay\n");
82
    printf(  "------------------------------------------------------------\n");
84
    printf(  "------------------------------------------------------------\n");
Line 93... Line 95...
93
#endif
95
#endif
94
    uint32_t timeStamp = 0;
96
    uint32_t timeStamp = 0;
95
 
97
 
96
    Can_Message_t txMsg;
98
    Can_Message_t txMsg;
97
    Can_Message_t rxMsg;
99
    Can_Message_t rxMsg;
98
    txMsg.Id = 0;
100
    txMsg.Id = RELAY_CMD_SEND;
99
    txMsg.RemoteFlag = 0;
101
    txMsg.RemoteFlag = 0;
100
    txMsg.ExtendedFlag = 1;
102
    txMsg.ExtendedFlag = 1;
-
 
103
    txMsg.DataLength = 3;
101
 
104
 
102
    /* main loop */
105
    /* main loop */
103
    while (1) {
106
    while (1) {
104
        /* service the CAN routines */
107
        /* service the CAN routines */
105
        Can_Service();
108
        Can_Service();
106
 
109
 
107
        /* check if any messages have been received */
110
        /* check if any messages have been received */
108
        while (Can_Receive(&rxMsg) == CAN_OK) {
111
        while (Can_Receive(&rxMsg) == CAN_OK) {
109
            /* This relay is adressed*/
112
            /* This relay is adressed*/
110
            if( rxMsg.Id == 0x1200 ){
113
            if( rxMsg.Id == RELAY_CMD_GET ){
111
 
114
 
112
                if( rxMsg.Data.bytes[0] == 0x01 ){
115
                if( rxMsg.Data.bytes[0] == RELAY_CMD_ON ){
113
                    /* Turn relay on */
116
                    /* Turn relay on */
-
 
117
                    relayStatus = relayOn();
-
 
118
 
-
 
119
                }else if(rxMsg.Data.bytes[0] == RELAY_CMD_OFF){
-
 
120
                    /* Turn relay off */
-
 
121
                    relayStatus = relayOff();
-
 
122
 
-
 
123
                }else if(rxMsg.Data.bytes[0] == RELAY_CMD_TOGGLE ){
-
 
124
                    /* Toggle relay */
-
 
125
 
-
 
126
                    if(relayStatus == RELAY_OFF){
-
 
127
                        relayStatus = relayOn();
-
 
128
                    }else{
-
 
129
                        relayStatus = relayOff();
-
 
130
                    }
-
 
131
 
-
 
132
                }else if(rxMsg.Data.bytes[0] == RELAY_CMD_STATUS){
-
 
133
                    /* Send relay status to CAN */
-
 
134
 
-
 
135
                    boardTemperature = getTC1047temperature();
-
 
136
 
-
 
137
                    if( boardTemperature >= FAILSAFE_TRESHOLD ){
-
 
138
                        relayFailsafe();
-
 
139
                    }else{
-
 
140
                        txMsg.Data.bytes[0] = boardTemperature & 0x00FF;
-
 
141
                        txMsg.Data.bytes[1] = (boardTemperature & 0xFF00)>>8;
-
 
142
                        txMsg.Data.bytes[2] = relayStatus;
-
 
143
 
-
 
144
                        Can_Send( &txMsg );
-
 
145
                    }
-
 
146
                }
-
 
147
            }
-
 
148
        }
-
 
149
 
-
 
150
        if( Timebase_PassedTimeMillis(timeStamp) >= STATUS_SEND_PERIOD ){
-
 
151
            timeStamp = Timebase_CurrentTime();
-
 
152
            /* Send relay status to CAN */
-
 
153
 
-
 
154
            boardTemperature = getTC1047temperature();
-
 
155
 
-
 
156
            if( boardTemperature >= FAILSAFE_TRESHOLD ){
-
 
157
                relayFailsafe();
-
 
158
            }else{
-
 
159
                txMsg.Data.bytes[0] = boardTemperature & 0x00FF;
-
 
160
                txMsg.Data.bytes[1] = (boardTemperature & 0xFF00)>>8;
-
 
161
                txMsg.Data.bytes[2] = relayStatus;
-
 
162
 
-
 
163
                Can_Send( &txMsg );
-
 
164
#if DEBUG
-
 
165
                printf("Periodic message sent...\n");
-
 
166
                printf("Temperature: %d\nRelay status: %u\n", boardTemperature, relayStatus);
-
 
167
#endif
-
 
168
            }
-
 
169
        }
-
 
170
    }
-
 
171
    return 0;
-
 
172
}
-
 
173
 
-
 
174
uint8_t relayOff()
-
 
175
{
-
 
176
    /* Set as output and sink */
-
 
177
    PORTC &= ~(1<<PC1);
-
 
178
    DDRC |= (1<<DDC1);
-
 
179
 
-
 
180
#if DEBUG
-
 
181
    printf("relay turned off\n");
-
 
182
#endif
-
 
183
 
-
 
184
    return RELAY_OFF;
-
 
185
}
-
 
186
 
-
 
187
uint8_t relayOn()
-
 
188
{
-
 
189
    /* Set as input and enable pull-up */
-
 
190
    DDRC &= ~(1<<DDC1);
-
 
191
    PORTC |= (1<<PC1);
-
 
192
 
-
 
193
#if DEBUG
-
 
194
    printf("relay turned on\n");
-
 
195
#endif
-
 
196
 
-
 
197
    return RELAY_ON;
-
 
198
}
-
 
199
 
-
 
200
void relayFailsafe()
-
 
201
{
-
 
202
    uint8_t relayStatus;
-
 
203
    uint32_t boardTemperature = 0, timeStamp = 0;
-
 
204
 
-
 
205
    Can_Message_t txMsg;
-
 
206
    Can_Message_t rxMsg;
-
 
207
    txMsg.Id = RELAY_CMD_SEND;
-
 
208
    txMsg.RemoteFlag = 0;
-
 
209
    txMsg.ExtendedFlag = 1;
-
 
210
    txMsg.DataLength = 4;
-
 
211
 
-
 
212
    relayStatus = relayOff();
-
 
213
 
-
 
214
    while(1){
-
 
215
        /* service the CAN routines */
-
 
216
        Can_Service();
-
 
217
 
-
 
218
        /* check if any messages have been received */
-
 
219
        while (Can_Receive(&rxMsg) == CAN_OK) {
-
 
220
            /* This relay is adressed*/
-
 
221
            if( rxMsg.Id == RELAY_CMD_GET ){
-
 
222
                if( rxMsg.Data.bytes[0] == RELAY_CMD_RESET ){
-
 
223
                    /* Return from failsafe mode */
-
 
224
                    return 0;
-
 
225
                }
-
 
226
            }
-
 
227
        }
114
 
228
 
115
                    /* Set as input and enable pull-up */
229
        if( Timebase_PassedTimeMillis(timeStamp) >= FAILSAFE_SEND_PERIOD ){
116
                    DDRC &= ~(1<<DDC1);
-
 
117
                    PORTC |= (1<<PC1);
-
 
118
 
-
 
119
                    relayStatus = ON;
-
 
120
#if DEBUG
-
 
121
                    printf("turn on\n");
230
            timeStamp = Timebase_CurrentTime();
122
#endif
-
 
123
 
-
 
124
                }else if(rxMsg.Data.bytes[0] == 0x02){
-
 
125
                    /* Turn relay off */
-
 
126
 
-
 
127
                    PORTC &= ~(1<<PC1);
-
 
128
                    DDRC |= (1<<DDC1);
-
 
129
 
-
 
130
                    relayStatus = OFF;
231
            /* Send relay status to CAN */
131
#if DEBUG
-
 
132
                    printf("turn off\n");
-
 
133
#endif
-
 
134
 
-
 
135
                }else if(rxMsg.Data.bytes[0] == 0x03){
-
 
136
                    /* Toggle relay */
-
 
137
 
232
 
138
                    if(relayStatus == ON){
-
 
139
                        PORTC &= ~(1<<PC1);
-
 
140
                        DDRC |= (1<<DDC1);
-
 
141
#if DEBUG
-
 
142
                        printf("toggle: turn off\n");
-
 
143
#endif
-
 
144
 
-
 
145
                        relayStatus = OFF;
-
 
146
 
-
 
147
                    }else{
-
 
148
                        DDRC &= ~(1<<DDC1);
-
 
149
                        PORTC |= (1<<PC1);
-
 
150
#if DEBUG
-
 
151
                        printf("toggle: turn on\n");
-
 
152
#endif
-
 
153
 
-
 
154
                        relayStatus = ON;
-
 
155
                    }
-
 
156
 
-
 
157
                }else if(rxMsg.Data.bytes[0] == 0x04){
-
 
158
                    /* Get relay status (ON/OFF?, temperature) */
-
 
159
                    ADCSRA |= (1<<ADSC);
-
 
160
 
-
 
161
                    /* Wait for conversion to complete */
-
 
162
                    while( ADCSRA & (1<<ADSC) ){}
-
 
163
 
-
 
164
                    txMsg.Id = 0x1201; // temporary ID
233
            boardTemperature = getTC1047temperature();
165
 
234
 
166
                    txMsg.Data.bytes[2] = relayStatus;
-
 
167
 
-
 
168
                    /* Convert voltage to temperature */
-
 
169
 
-
 
170
                    boardTemperature = ADCW;
-
 
171
 
-
 
172
                    boardTemperature = boardTemperature*Vref;
-
 
173
                    boardTemperature = (boardTemperature*100/1024)-50;
-
 
174
 
-
 
175
                    /* Save and send */
-
 
176
                    txMsg.Data.bytes[0]= boardTemperature & 0x00FF;
235
            txMsg.Data.bytes[0] = boardTemperature & 0x00FF;
177
                    txMsg.Data.bytes[1]= (boardTemperature & 0xFF00)>>8;
236
            txMsg.Data.bytes[1] = (boardTemperature & 0xFF00)>>8;
178
 
-
 
179
                    txMsg.DataLength = 3;
-
 
180
 
-
 
181
                    Can_Send( &txMsg );
-
 
182
                }
-
 
183
            }
-
 
184
        }
-
 
185
 
-
 
186
        // TODO
-
 
187
        // ska status på relä och temperatur skickas periodiskt?
-
 
188
        // tätare intervall om temperaturen kommer över en viss gräns?
-
 
189
 
-
 
190
        if( Timebase_PassedTimeMillis(timeStamp) >=10000){
-
 
191
            timeStamp = Timebase_CurrentTime();
-
 
192
            /* Get relay status (ON/OFF?, temperature) and send */
-
 
193
 
-
 
194
            ADCSRA |= (1<<ADSC);
-
 
195
 
-
 
196
            /* Wait for conversion to complete */
-
 
197
            while( ADCSRA & (1<<ADSC) ){}
-
 
198
 
-
 
199
            txMsg.Id = 0x1201; // temporary ID
-
 
200
 
-
 
201
            txMsg.Data.bytes[2] = relayStatus;
237
            txMsg.Data.bytes[2] = relayStatus;
202
 
-
 
203
            /* Convert voltage to temperature */
-
 
204
 
-
 
205
            boardTemperature = ADCW;
-
 
206
 
-
 
207
            boardTemperature = boardTemperature*Vref;
-
 
208
            boardTemperature = (boardTemperature*100/1024)-50;
-
 
209
 
-
 
210
            /* Save and send */
-
 
211
            txMsg.Data.bytes[0]= boardTemperature & 0x00FF;
238
            txMsg.Data.bytes[3] = FAILSAFE_MODE;
212
            txMsg.Data.bytes[1]= (boardTemperature & 0xFF00)>>8;
-
 
213
 
239
 
214
            txMsg.DataLength = 3;
-
 
215
            Can_Send( &txMsg );
240
            Can_Send( &txMsg );
216
#if DEBUG
-
 
217
            printf("Message sent...\n");
-
 
218
            printf("Temperature: %d\nRelay status: %x, %u\n", boardTemperature, relayStatus, relayStatus);
-
 
219
#endif
-
 
220
 
-
 
221
        }
241
        }
222
    }
242
    }
223
    return 0;
-
 
224
}
243
}
225
 
244