Subversion Repositories HomeAutomation

Rev

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

Rev 167 Rev 178
1
/**
1
/**
-
 
2
 * CAN LCD.
2
 * CAN Test. This program sends a CAN message once every second. The ID of the
3
 * For HD44780- and KS0073-based LCD displays
3
 * message is increased each time for testing purposes.
4
 * This is the early version that only prints temperature sensor data.
4
 * Added LCD output
-
 
5
 *
5
 *
6
 * @date    2006-11-21, LCD addition 2006-12-11
6
 * @date    2006-12-11
7
 * @author  Jimmy Myhrman, Erik Larsson
7
 * @author  Erik Larsson
-
 
8
 *
-
 
9
 * TODO Fix more types of output: relay status, mail recieved etc.
-
 
10
 * Make it configurable to use UART, because it's not necessary on the final PCB
8
 *  
11
 *
9
 */
12
 */
-
 
13
 
-
 
14
/* For eq's testing purpose */
-
 
15
#define EQLAZER 0
10
 
16
 
11
/*-----------------------------------------------------------------------------
17
/*-----------------------------------------------------------------------------
12
 * Includes
18
 * Includes
13
 *---------------------------------------------------------------------------*/
19
 *---------------------------------------------------------------------------*/
14
/* system files */
20
/* system files */
15
#include <avr/io.h>
21
#include <avr/io.h>
16
#include <avr/interrupt.h>
22
#include <avr/interrupt.h>
17
#include <avr/wdt.h>
23
#include <avr/wdt.h>
18
#include <stdio.h>
24
#include <stdio.h>
19
/* lib files */
25
/* lib files */
20
#include <can.h>
26
#include <can.h>
21
#include <serial.h>
27
#include <serial.h>
22
#include <timebase.h>
28
#include <timebase.h>
23
#include <lcd_HD44780.h>
29
#include <lcd_HD44780.h>
24
 
30
 
-
 
31
/* defines */
-
 
32
//#include <global_funcdefs> // TODO in future version use this (first fix the defs)
-
 
33
 
-
 
34
#ifdef EQLAZER
25
#include <eqlazer_funcdefs.h>
35
#include <eqlazer_funcdefs.h> // for eq's personal defs
-
 
36
#endif
26
 
37
 
27
#define BUFFER_SIZE 20
38
#define BUFFER_SIZE 20
28
 
39
 
29
/*-----------------------------------------------------------------------------
40
/*-----------------------------------------------------------------------------
30
 * Main Program
41
 * Main Program
31
 *---------------------------------------------------------------------------*/
42
 *---------------------------------------------------------------------------*/
32
int main(void) {
43
int main(void) {
33
 
44
 
34
    char buffer[ BUFFER_SIZE ];
45
    char buffer[ BUFFER_SIZE ];
35
    uint8_t subzero;
46
    uint8_t subzero;
36
 
47
 
37
    Timebase_Init();
48
    Timebase_Init();
38
    Serial_Init();
49
    Serial_Init();
39
    sei();
50
    sei();
40
    lcd_init( LCD_DISP_ON );
51
    lcd_init( LCD_DISP_ON );
41
 
52
 
42
    printf("\n------------------------------------------------------------\n");
53
    printf("\n------------------------------------------------------------\n");
43
    printf(  "   CAN Test: LCD\n");
54
    printf(  "   CAN LCD\n");
44
    printf(  "------------------------------------------------------------\n");
55
    printf(  "------------------------------------------------------------\n");
45
 
56
 
46
    lcd_clrscr();
57
    lcd_clrscr();
47
    lcd_puts("CAN LCD test\n");
58
    lcd_puts("CAN LCD test\n");
48
 
59
 
49
    printf("CanInit...");
60
    printf("CanInit...");
50
    lcd_puts("CanInit... ");
61
    lcd_puts("CanInit... ");
51
    if (Can_Init() != CAN_OK) {
62
    if (Can_Init() != CAN_OK) {
52
        printf("FAILED!\n");
63
        printf("FAILED!\n");
53
        lcd_puts("FAILED!\n");
64
        lcd_puts("FAILED!\n");
54
    }
65
    }
55
    else {
66
    else {
56
        printf("OK!\n");
67
        printf("OK!\n");
57
        lcd_puts("OK!\n");
68
        lcd_puts("OK!\n");
58
    }
69
    }
59
   
70
   
60
    uint32_t timeStamp = 0;
71
    uint32_t timeStamp = 0;
61
   
72
   
62
    Can_Message_t txMsg;
73
    Can_Message_t txMsg;
63
    Can_Message_t rxMsg;
74
    Can_Message_t rxMsg;
64
    txMsg.Id = 0;
75
    txMsg.Id = 0;
65
    txMsg.RemoteFlag = 0;
76
    txMsg.RemoteFlag = 0;
66
    txMsg.ExtendedFlag = 1; //DataLength and the databytes are just what happens to be in the memory. They are never set.
77
    txMsg.ExtendedFlag = 1;
67
 
78
 
68
    lcd_clrscr();
79
    lcd_clrscr();
69
 
80
 
70
    /* main loop */
81
    /* main loop */
71
    while (1) {
82
    while (1) {
72
        /* service the CAN routines */
83
        /* service the CAN routines */
73
        Can_Service();
84
        Can_Service();
74
       
85
 
75
        /* send CAN message and check for CAN errors once every second */
-
 
76
//      if (Timebase_PassedTimeMillis(timeStamp) >= 1000) {
-
 
77
//          timeStamp = Timebase_CurrentTime();
-
 
78
            /* send txMsg */
-
 
79
//          txMsg.Id++;
-
 
80
//          Can_Send(&txMsg);
-
 
81
//      }
-
 
82
 
-
 
83
        /* check if any messages have been received */
86
        /* check if any messages have been received and print to uart */
84
        while (Can_Receive(&rxMsg) == CAN_OK) {
87
        while (Can_Receive(&rxMsg) == CAN_OK) {
85
            printf("MSG Received: ID=%lx, DLC=%u, EXT=%u, RTR=%u, ", rxMsg.Id, (uint16_t)(rxMsg.DataLength), (uint16_t)(rxMsg.ExtendedFlag), (uint16_t)(rxMsg.RemoteFlag));
88
            printf("MSG Received: ID=%lx, DLC=%u, EXT=%u, RTR=%u, ", rxMsg.Id, (uint16_t)(rxMsg.DataLength), (uint16_t)(rxMsg.ExtendedFlag), (uint16_t)(rxMsg.RemoteFlag));
86
            printf("data={ ");
89
            printf("data={ ");
87
 
90
 
88
            for (uint8_t i=0; i<rxMsg.DataLength; i++) {
91
            for (uint8_t i=0; i<rxMsg.DataLength; i++) {
89
                printf("%x ", rxMsg.Data.bytes[i]);
92
                printf("%x ", rxMsg.Data.bytes[i]);
90
            }
93
            }
91
            printf("}\n");
94
            printf("}\n");
92
 
95
 
-
 
96
#ifdef EQLAZER /* More personalized code */
-
 
97
            if( (rxMsg.Id & 0x1E000000)>>25 == FUNCT_SENSORS){
93
 
98
 
94
            if( (rxMsg.Id & 0x1E000000)>>25 == FUNCT_SENSORS){ // if sensor data received
99
// TODO fixa rätt umatning av sensordata: negativa tal och decimaldelen.
-
 
100
// Skriva om hela skiten
95
 
101
//
96
                /* which temperature sensor? */
102
                /* which temperature sensor? */
97
                if( (rxMsg.Id & 0x01FF8000)>>15 == FUNCC_SENSORS_TEMPERATURE_INSIDE ){
103
                if( (rxMsg.Id & 0x01FF8000)>>15 == FUNCC_SENSORS_TEMPERATURE_INSIDE ){
98
                    /* Below 0 degrees celsius? */
104
                    /* Below 0 degrees celsius? */
99
                    if(rxMsg.Data.bytes[0]&0x80){
105
                    if(rxMsg.Data.bytes[0]&0x80){
100
                        rxMsg.Data.bytes[0]= -rxMsg.Data.bytes[0];
106
                        rxMsg.Data.bytes[0]= -rxMsg.Data.bytes[0];
101
                        subzero = 1;
107
                        subzero = 1;
102
                    }else{
108
                    }else{
103
                        subzero = 0;
109
                        subzero = 0;
104
                    }
110
                    }
105
                        snprintf( buffer, BUFFER_SIZE, (subzero)?"-%d,%d%cC":"%d,%d%cC", rxMsg.Data.bytes[0], rxMsg.Data.bytes[1], 0xdf );
111
                        snprintf( buffer, BUFFER_SIZE, (subzero)?"-%d,%d%cC":"%d,%d%cC", rxMsg.Data.bytes[0], rxMsg.Data.bytes[1], 0xdf );
106
                        lcd_gotoxy( LCD_LINE0_0 );
112
                        lcd_gotoxy( LCD_LINE0_0 );
107
                        lcd_puts( LCD_SENSOR_TEMPERATURE_INSIDE );
113
                        lcd_puts( LCD_SENSOR_TEMPERATURE_INSIDE );
108
                        lcd_gotoxy( LCD_LINE1_0 );
114
                        lcd_gotoxy( LCD_LINE1_0 );
109
                        lcd_puts( buffer );
115
                        lcd_puts( buffer );
110
                }else if( (rxMsg.Id & 0x01FF8000)>>15 == FUNCC_SENSORS_TEMPERATURE_OUTSIDE ){
116
                }else if( (rxMsg.Id & 0x01FF8000)>>15 == FUNCC_SENSORS_TEMPERATURE_OUTSIDE ){
111
                    if(rxMsg.Data.bytes[0]&0x80){
117
                    if(rxMsg.Data.bytes[0]&0x80){
112
                        rxMsg.Data.bytes[0]= -rxMsg.Data.bytes[0];
118
                        rxMsg.Data.bytes[0]= -rxMsg.Data.bytes[0];
113
                        subzero = 1;
119
                        subzero = 1;
114
                    }else{
120
                    }else{
115
                        subzero = 0;
121
                        subzero = 0;
116
                    }
122
                    }
117
                        snprintf( buffer, BUFFER_SIZE, (subzero)?"-%d,%d%cC":"%d,%d%cC", rxMsg.Data.bytes[0], rxMsg.Data.bytes[1], 0xdf );
123
                        snprintf( buffer, BUFFER_SIZE, (subzero)?"-%d,%d%cC":"%d,%d%cC", rxMsg.Data.bytes[0], rxMsg.Data.bytes[1], 0xdf );
118
                        lcd_gotoxy( LCD_LINE2_0 );
124
                        lcd_gotoxy( LCD_LINE2_0 );
119
                        lcd_puts( LCD_SENSOR_TEMPERATURE_OUTSIDE );
125
                        lcd_puts( LCD_SENSOR_TEMPERATURE_OUTSIDE );
120
                        lcd_gotoxy( LCD_LINE3_0 );
126
                        lcd_gotoxy( LCD_LINE3_0 );
121
                        lcd_puts( buffer );
127
                        lcd_puts( buffer );
122
                }else if( (rxMsg.Id & 0x01FF8000)>>15 == FUNCC_SENSORS_TEMPERATURE_FREEZER ){
128
                }else if( (rxMsg.Id & 0x01FF8000)>>15 == FUNCC_SENSORS_TEMPERATURE_FREEZER ){
123
                    if(rxMsg.Data.bytes[0]&0x80){
129
                    if(rxMsg.Data.bytes[0]&0x80){
124
                        rxMsg.Data.bytes[0]= -rxMsg.Data.bytes[0];
130
                        rxMsg.Data.bytes[0]= -rxMsg.Data.bytes[0];
125
                        subzero = 1;
131
                        subzero = 1;
126
                    }else{
132
                    }else{
127
                        subzero = 0;
133
                        subzero = 0;
128
                    }
134
                    }
129
                        snprintf( buffer, BUFFER_SIZE, (subzero)?"-%d,%d%cC":"%d,%d%cC", rxMsg.Data.bytes[0], rxMsg.Data.bytes[1], 0xdf );
135
                        snprintf( buffer, BUFFER_SIZE, (subzero)?"-%d,%d%cC":"%d,%d%cC", rxMsg.Data.bytes[0], rxMsg.Data.bytes[1], 0xdf );
130
                        lcd_gotoxy( LCD_LINE2_2 );
136
                        lcd_gotoxy( LCD_LINE2_2 );
131
                        lcd_puts( LCD_SENSOR_TEMPERATURE_FREEZER );
137
                        lcd_puts( LCD_SENSOR_TEMPERATURE_FREEZER );
132
                        lcd_gotoxy( LCD_LINE3_2 );
138
                        lcd_gotoxy( LCD_LINE3_2 );
133
                        lcd_puts( buffer );
139
                        lcd_puts( buffer );
134
                }else{
140
                }else{
135
                    lcd_clrscr();
141
                    lcd_clrscr();
136
                    lcd_puts("Sensors: No config.");
142
                    lcd_puts("Sensors: No config.");
137
                }
143
                }
-
 
144
            }
-
 
145
#else /* Generally code */
-
 
146
 
-
 
147
        /* If temperature sensor data received print to LCD */
-
 
148
        if( rxMsg.Id  == 0x300 ){
138
/*
149
 
139
                for( uint8_t i=0; i<(rxMsg.DataLength/2); i++ ){
150
                for( uint8_t i=0; i<(rxMsg.DataLength/2); i++ ){
140
                        snprintf( buffer, BUFFER_SIZE, "Sensor %i: %d,%d%cC", i, rxMsg.Data.bytes[i*2], rxMsg.Data.bytes[i*2+1], 0xdf );
151
                        snprintf( buffer, BUFFER_SIZE, "Sensor %i: %d,%d%cC", i, rxMsg.Data.bytes[i*2], rxMsg.Data.bytes[i*2+1], 0xdf ); // TODO fixa korrekt utmatning av negativa tal och decimaldel
141
                        lcd_gotoxy(0,1+i);
152
                        lcd_gotoxy(0,1+i);
142
                        lcd_puts( buffer );
153
                        lcd_puts( buffer );
143
                }*/
-
 
144
            }
154
                }
-
 
155
            }
-
 
156
#endif
145
        }
157
        }
146
    }
158
    }
147
   
159
   
148
    return 0;
160
    return 0;
149
}
161
}
150
 
162