Subversion Repositories HomeAutomation

Rev

Rev 184 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

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