Subversion Repositories HomeAutomation

Rev

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

Rev 176 Rev 177
Line 1... Line 1...
1
/**
1
/**
2
 * CAN Test. This program sends a CAN message once every second. The ID of the
-
 
3
 * message is increased each time for testing purposes.
-
 
4
 * Added LCD output
2
 * CAN Relay.
5
 *
3
 *
6
 * @date    2006-11-21, LCD addition 2006-12-11
-
 
7
 * @author  Jimmy Myhrman, Erik Larsson
-
 
8
 *  
4
 *
-
 
5
 * @date    2006-12-17
-
 
6
 * @author  Erik Larsson
-
 
7
 *
9
 *   Observera! Applikationen är helt otestad
8
 *   Observera! Applikationen är helt otestad!
-
 
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
 *
14
 *
15
 */
15
 */
-
 
16
 
-
 
17
/*
-
 
18
 * For testing with ATmega88 PDIP enable this fetaure
-
 
19
 * Since PDIP doesn't have ADC7 it will use the ADC0 instead
-
 
20
 */
-
 
21
#define PDIP    0
16
 
22
 
17
/*-----------------------------------------------------------------------------
23
/*-----------------------------------------------------------------------------
18
 * Includes
24
 * Includes
19
 *---------------------------------------------------------------------------*/
25
 *---------------------------------------------------------------------------*/
20
/* system files */
26
/* system files */
Line 24... Line 30...
24
#include <stdio.h>
30
#include <stdio.h>
25
/* lib files */
31
/* lib files */
26
#include <can.h>
32
#include <can.h>
27
#include <serial.h>
33
#include <serial.h>
28
#include <timebase.h>
34
#include <timebase.h>
29
 
35
/* defines */
30
#define OFF     0x00
36
#define OFF     0x00
31
#define ON      0x01
37
#define ON      0x01
32
#define Vref    5
38
#define Vref    5
33
 
39
 
34
/*------------------------------------------------------------------------------
40
/*------------------------------------------------------------------------------
35
 * Read relay state (OPEN/CLOSED) and temperature
41
 * Read and send relay state (OPEN/CLOSED) and temperature
36
 * ---------------------------------------------------------------------------*/
42
 * ---------------------------------------------------------------------------*/
37
void sendRelayStatus( Can_Message_t* msg, uint8_t state )
43
void sendRelayStatus( Can_Message_t* msg, uint8_t state )
38
{
44
{
39
    int16_t temp;
45
    int16_t temp;
40
 
46
 
Line 79... Line 85...
79
 
85
 
80
    /* Initiate ADC for reading temperature sensor */
86
    /* Initiate ADC for reading temperature sensor */
81
 
87
 
82
    /* Wake up ADC */
88
    /* Wake up ADC */
83
    PRR &= ~(1<<PRADC);
89
    PRR &= ~(1<<PRADC);
-
 
90
#if PDIP
-
 
91
    /* Enable AREF and ADC0 (For PDIP package) */
-
 
92
    ADMUX = 0x00;
-
 
93
#else
84
    /* Enable AREF and ADC7 */
94
    /* Enable AREF and ADC7 (For TQFP package) */
85
    ADMUX = 0x07;
95
    ADMUX = 0x07;
-
 
96
#endif
-
 
97
 
86
    ADCSRA |= (1<<ADEN);
98
    ADCSRA |= (1<<ADEN);
87
 
99
 
88
    printf("\n------------------------------------------------------------\n");
100
    printf("\n------------------------------------------------------------\n");
89
    printf(  "   CAN: Relay\n");
101
    printf(  "   CAN: Relay\n");
90
    printf(  "------------------------------------------------------------\n");
102
    printf(  "------------------------------------------------------------\n");
Line 102... Line 114...
102
    Can_Message_t txMsg;
114
    Can_Message_t txMsg;
103
    Can_Message_t rxMsg;
115
    Can_Message_t rxMsg;
104
    txMsg.Id = 0;
116
    txMsg.Id = 0;
105
    txMsg.RemoteFlag = 0;
117
    txMsg.RemoteFlag = 0;
106
    txMsg.ExtendedFlag = 1;
118
    txMsg.ExtendedFlag = 1;
107
 
119
 
108
 
120
 
109
    /* main loop */
121
    /* main loop */
110
    while (1) {
122
    while (1) {
111
        /* service the CAN routines */
123
        /* service the CAN routines */
112
        Can_Service();
124
        Can_Service();
113
 
125
 
114
        /* check if any messages have been received */
126
        /* check if any messages have been received */
115
        while (Can_Receive(&rxMsg) == CAN_OK) {
127
        while (Can_Receive(&rxMsg) == CAN_OK) {
116
            /* This relay is adressed*/
128
            /* This relay is adressed*/
117
            if( rxMsg.Id == 0x1200 ){
129
            if( rxMsg.Id == 0x1200 ){
118
 
130
 
119
                if( rxMsg.Data.bytes[0] == 0x01 ){
131
                if( rxMsg.Data.bytes[0] == 0x01 ){
120
                    /* Turn relay on */
132
                    /* Turn relay on */
121
 
133
 
122
                    /* Set as input and enable pull-up */
134
                    /* Set as input and enable pull-up */
123
                    DDRC &= ~(1<<DDC1);
135
                    DDRC &= ~(1<<DDC1);
124
                    PORTC |= (1<<PC1);
136
                    PORTC |= (1<<PC1);
125
 
137
 
126
                    relayStatus = ON;
138
                    relayStatus = ON;
127
 
139
 
128
                }else if(rxMsg.Data.bytes[0] == 0x02){
140
                }else if(rxMsg.Data.bytes[0] == 0x02){
129
                    /* Turn relay off */
141
                    /* Turn relay off */
130
 
142
 
131
                    PORTC &= ~(1<<PC1);
143
                    PORTC &= ~(1<<PC1);
132
                    DDRC |= (1<<DDC1);
144
                    DDRC |= (1<<DDC1);
Line 160... Line 172...
160
        // ska status på relä och temperatur skickas periodiskt?
172
        // ska status på relä och temperatur skickas periodiskt?
161
        // tätare intervall om temperaturen kommer över en viss gräns?
173
        // tätare intervall om temperaturen kommer över en viss gräns?
162
 
174
 
163
        if( Timebase_PassedTimeMillis(timeStamp) >=5000){
175
        if( Timebase_PassedTimeMillis(timeStamp) >=5000){
164
            timeStamp = Timebase_CurrentTime();
176
            timeStamp = Timebase_CurrentTime();
165
            /* Get relay status (ON/OFF?, temperature) */
177
            /* Get relay status (ON/OFF?, temperature) and send */
166
 
178
 
167
             sendRelayStatus( &txMsg, relayStatus );
179
             sendRelayStatus( &txMsg, relayStatus );
168
        }
180
        }
169
    }
181
    }
170
    return 0;
182
    return 0;