Subversion Repositories HomeAutomation

Rev

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

Rev 175 Rev 176
Line 6... Line 6...
6
 * @date    2006-11-21, LCD addition 2006-12-11
6
 * @date    2006-11-21, LCD addition 2006-12-11
7
 * @author  Jimmy Myhrman, Erik Larsson
7
 * @author  Jimmy Myhrman, Erik Larsson
8
 *  
8
 *  
9
 *   Observera! Applikationen är helt otestad
9
 *   Observera! Applikationen är helt otestad
10
 *   ADC=Vin*1024/Vref
10
 *   ADC=Vin*1024/Vref
-
 
11
 *   Vout=( 10 mV/C )( Temperature C ) + 500 mV
-
 
12
 *
-
 
13
 *  Transmitts: DATA: <temperature high byte> <temperature low byte> <relay status>
11
 *
14
 *
12
 */
15
 */
13
 
16
 
14
/*-----------------------------------------------------------------------------
17
/*-----------------------------------------------------------------------------
15
 * Includes
18
 * Includes
Line 22... Line 25...
22
/* lib files */
25
/* lib files */
23
#include <can.h>
26
#include <can.h>
24
#include <serial.h>
27
#include <serial.h>
25
#include <timebase.h>
28
#include <timebase.h>
26
 
29
 
27
//#define RELAY_PORT  PORTC
-
 
28
//#define RELAY_PIN   PC1
-
 
29
#define OFF     0x00
30
#define OFF     0x00
30
#define ON      0x01
31
#define ON      0x01
-
 
32
#define Vref    5
31
 
33
 
32
/*------------------------------------------------------------------------------
34
/*------------------------------------------------------------------------------
33
 * Read relay state (OPEN/CLOSED) and temperature
35
 * Read relay state (OPEN/CLOSED) and temperature
34
 * ---------------------------------------------------------------------------*/
36
 * ---------------------------------------------------------------------------*/
35
void readRelayStatus( Can_Message_t* msg, uint8_t state )
37
void sendRelayStatus( Can_Message_t* msg, uint8_t state )
36
{
38
{
-
 
39
    int16_t temp;
-
 
40
 
37
            /* Start measureing */
41
    /* Start measureing */
38
            ADCSRA |= (1<<ADSC);
42
    ADCSRA |= (1<<ADSC);
39
 
43
 
40
            /* Wait for conversion to complete */
44
    /* Wait for conversion to complete */
41
            while( ADCSRA & (1<<ADSC) ){}
45
    while( ADCSRA & (1<<ADSC) ){}
42
 
46
 
43
            msg->Id = 0x1201; // temporary ID
47
    msg->Id = 0x1201; // temporary ID
44
 
48
 
45
            msg->Data.bytes[0] = state;
49
    msg->Data.bytes[0] = state;
46
            msg->Data.bytes[1]= ADCL;
-
 
47
            msg->Data.bytes[2]= ADCH;
-
 
48
            msg->DataLength = 3;
-
 
49
 
50
 
-
 
51
    /* Convert voltage to temperature */
-
 
52
    temp = (ADCL & (ADCH<<8));
-
 
53
    temp = temp*Vref/1024;
-
 
54
    temp = (temp - 0.5)/(0.01);
-
 
55
 
-
 
56
    msg->Data.bytes[1]= temp;
-
 
57
    msg->Data.bytes[2]= (temp>>8);
-
 
58
    msg->DataLength = 3;
-
 
59
 
50
            Can_Send( msg );
60
    Can_Send( &msg );
51
}
61
}
52
 
62
 
53
/*-----------------------------------------------------------------------------
63
/*-----------------------------------------------------------------------------
54
 * Main Program
64
 * Main Program
55
 *---------------------------------------------------------------------------*/
65
 *---------------------------------------------------------------------------*/
56
int main(void) {
66
int main(void) {
Line 58... Line 68...
58
    uint8_t relayStatus;
68
    uint8_t relayStatus;
59
 
69
 
60
    Timebase_Init();
70
    Timebase_Init();
61
    Serial_Init();
71
    Serial_Init();
62
    sei();
72
    sei();
63
 
73
 
64
    /* Enable RELAY_PIN as output */
-
 
65
    DDRC |= (1<<DDC1);
-
 
66
 
-
 
67
    /* Turn relay off */
74
    /* Turn relay off */
68
    PORTC &= ~(1<<PC1);
75
    PORTC &= ~(1<<PC1);
-
 
76
    DDRC |= (1<<DDC1);
-
 
77
 
69
    relayStatus = OFF;
78
    relayStatus = OFF;
70
 
79
 
71
    /* Initiate ADC for reading temperature sensor */
80
    /* Initiate ADC for reading temperature sensor */
72
 
81
 
73
    /* Wake up ADC */
82
    /* Wake up ADC */
Line 104... Line 113...
104
 
113
 
105
        /* check if any messages have been received */
114
        /* check if any messages have been received */
106
        while (Can_Receive(&rxMsg) == CAN_OK) {
115
        while (Can_Receive(&rxMsg) == CAN_OK) {
107
            /* This relay is adressed*/
116
            /* This relay is adressed*/
108
            if( rxMsg.Id == 0x1200 ){
117
            if( rxMsg.Id == 0x1200 ){
109
 
118
 
110
                if( rxMsg.Data.bytes[0] == 0x01 ){
119
                if( rxMsg.Data.bytes[0] == 0x01 ){
111
                    /* Turn relay on */
120
                    /* Turn relay on */
112
 
121
 
113
                    /* Set as input and enable pull-up */
122
                    /* Set as input and enable pull-up */
114
                    DDRC &= ~(1<<DDC1);
123
                    DDRC &= ~(1<<DDC1);
115
                    PORTC |= (1<<PC1);
124
                    PORTC |= (1<<PC1);
116
 
125
 
117
                    relayStatus = ON;
126
                    relayStatus = ON;
118
 
127
 
119
                }else if(rxMsg.Data.bytes[0] == 0x02){
128
                }else if(rxMsg.Data.bytes[0] == 0x02){
120
                    /* Turn relay off */
129
                    /* Turn relay off */
121
 
130
 
122
                    DDRC |= (1<<DDC1);
131
                    PORTC &= ~(1<<PC1);
123
                    PORTC &= ~(1<<PC1);
132
                    DDRC |= (1<<DDC1);
124
 
133
 
125
                    relayStatus = OFF;
134
                    relayStatus = OFF;
126
 
135
 
127
                }else if(rxMsg.Data.bytes[0] == 0x03){
136
                }else if(rxMsg.Data.bytes[0] == 0x03){
128
                    /* Toggle relay */
137
                    /* Toggle relay */
129
 
138
 
130
                    if(relayStatus == ON){
139
                    if(relayStatus == ON){
131
                       
-
 
132
                        DDRC |= (1<<DDC1);
-
 
133
                        PORTC &= ~(1<<PC1);
140
                        PORTC &= ~(1<<PC1);
-
 
141
                        DDRC |= (1<<DDC1);
-
 
142
 
134
                        relayStatus = OFF;
143
                        relayStatus = OFF;
135
 
144
 
136
                    }else{
145
                    }else{
137
 
-
 
138
                        DDRC &= ~(1<<DDC1);
146
                        DDRC &= ~(1<<DDC1);
139
                        PORTC |= (1<<PC1);
147
                        PORTC |= (1<<PC1);
-
 
148
 
140
                        relayStatus = ON;
149
                        relayStatus = ON;
141
                    }
150
                    }
142
 
151
 
143
                }else if(rxMsg.Data.bytes[0] == 0x04){
152
                }else if(rxMsg.Data.bytes[0] == 0x04){
144
                    /* Get relay status (ON/OFF?, temperature) */
153
                    /* Get relay status (ON/OFF?, temperature) */
145
                    readRelayStatus( &txMsg, relayStatus );
154
                    sendRelayStatus( &txMsg, relayStatus );
146
                }
155
                }
147
            }
156
            }
148
        }
157
        }
149
 
158
 
150
        // TODO
159
        // TODO
Line 153... Line 162...
153
 
162
 
154
        if( Timebase_PassedTimeMillis(timeStamp) >=5000){
163
        if( Timebase_PassedTimeMillis(timeStamp) >=5000){
155
            timeStamp = Timebase_CurrentTime();
164
            timeStamp = Timebase_CurrentTime();
156
            /* Get relay status (ON/OFF?, temperature) */
165
            /* Get relay status (ON/OFF?, temperature) */
157
 
166
 
158
             readRelayStatus( &txMsg, relayStatus );
167
             sendRelayStatus( &txMsg, relayStatus );
159
        }
168
        }
160
    }
169
    }
161
    return 0;
170
    return 0;
162
}
171
}
163
 
172