Subversion Repositories HomeAutomation

Rev

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

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