Subversion Repositories HomeAutomation

Rev

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

Rev 163 Rev 164
1
/**
1
/**
2
 * CAN Test. This program sends a CAN message once every second. The ID of the
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.
3
 * message is increased each time for testing purposes.
4
 * Added LCD output
4
 * Added LCD output
5
 *
5
 *
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
 */
9
 */
10
 
10
 
11
/*-----------------------------------------------------------------------------
11
/*-----------------------------------------------------------------------------
12
 * Includes
12
 * Includes
13
 *---------------------------------------------------------------------------*/
13
 *---------------------------------------------------------------------------*/
14
/* system files */
14
/* system files */
15
#include <avr/io.h>
15
#include <avr/io.h>
16
#include <avr/interrupt.h>
16
#include <avr/interrupt.h>
17
#include <avr/wdt.h>
17
#include <avr/wdt.h>
18
#include <stdio.h>
18
#include <stdio.h>
19
/* lib files */
19
/* lib files */
20
#include <can.h>
20
#include <can.h>
21
#include <serial.h>
21
#include <serial.h>
22
#include <timebase.h>
22
#include <timebase.h>
23
#include <lcd_HD44780.h>
23
#include <lcd_HD44780.h>
-
 
24
 
-
 
25
#define BUFFER_SIZE 20
24
 
26
 
25
/*-----------------------------------------------------------------------------
27
/*-----------------------------------------------------------------------------
26
 * Main Program
28
 * Main Program
27
 *---------------------------------------------------------------------------*/
29
 *---------------------------------------------------------------------------*/
28
int main(void) {
30
int main(void) {
-
 
31
 
-
 
32
    char buffer[ BUFFER_SIZE ];
-
 
33
    int num=1234;
-
 
34
 
29
    Timebase_Init();
35
    Timebase_Init();
30
    Serial_Init();
36
    Serial_Init();
31
    sei();
37
    sei();
32
    lcd_init( LCD_DISP_ON );
38
    lcd_init( LCD_DISP_ON );
33
 
39
 
34
    printf("\n------------------------------------------------------------\n");
40
    printf("\n------------------------------------------------------------\n");
35
    printf(  "   CAN Test: Periodic Transmission\n");
41
    printf(  "   CAN Test: Periodic Transmission\n");
36
    printf(  "------------------------------------------------------------\n");
42
    printf(  "------------------------------------------------------------\n");
37
 
43
 
38
    lcd_clrscr();
44
    lcd_clrscr();
39
    lcd_puts("CAN LCD test\n");
45
    lcd_puts("CAN LCD test\n");
40
 
46
 
41
    printf("CanInit...");
47
    printf("CanInit...");
42
    lcd_puts("CanInit... ");
48
    lcd_puts("CanInit... ");
43
    if (Can_Init() != CAN_OK) {
49
    if (Can_Init() != CAN_OK) {
44
        printf("FAILED!\n");
50
        printf("FAILED!\n");
45
        lcd_puts("FAILED!\n");
51
        lcd_puts("FAILED!\n");
46
    }
52
    }
47
    else {
53
    else {
48
        printf("OK!\n");
54
        printf("OK!\n");
49
        lcd_puts("OK!\n");
55
        lcd_puts("OK!\n");
50
    }
56
    }
-
 
57
   
-
 
58
//    snprintf(buffer, BUFFER_SIZE, "yo %d 98765432109\n", num);
-
 
59
//    lcd_puts(buffer);
-
 
60
//    lcd_puts("test\n");
-
 
61
//    lcd_puts(buffer);
51
   
62
 
52
    uint32_t timeStamp = 0;
63
    uint32_t timeStamp = 0;
53
   
64
   
54
    Can_Message_t txMsg;
65
    Can_Message_t txMsg;
55
    Can_Message_t rxMsg;
66
    Can_Message_t rxMsg;
56
    txMsg.Id = 0;
67
    txMsg.Id = 0;
57
    txMsg.RemoteFlag = 0;
68
    txMsg.RemoteFlag = 0;
58
    txMsg.ExtendedFlag = 1; //DataLength and the databytes are just what happens to be in the memory. They are never set.
69
    txMsg.ExtendedFlag = 1; //DataLength and the databytes are just what happens to be in the memory. They are never set.
59
   
70
   
60
    /* main loop */
71
    /* main loop */
61
    while (1) {
72
    while (1) {
62
        /* service the CAN routines */
73
        /* service the CAN routines */
63
        Can_Service();
74
        Can_Service();
64
       
75
       
65
        /* send CAN message and check for CAN errors once every second */
76
        /* send CAN message and check for CAN errors once every second */
66
        if (Timebase_PassedTimeMillis(timeStamp) >= 1000) {
77
        if (Timebase_PassedTimeMillis(timeStamp) >= 1000) {
67
            timeStamp = Timebase_CurrentTime();
78
            timeStamp = Timebase_CurrentTime();
68
            /* send txMsg */
79
            /* send txMsg */
69
            txMsg.Id++;
80
            txMsg.Id++;
70
            Can_Send(&txMsg);
81
            Can_Send(&txMsg);
71
        }
82
        }
72
       
83
       
73
        /* check if any messages have been received */
84
        /* check if any messages have been received */
74
        while (Can_Receive(&rxMsg) == CAN_OK) {
85
        while (Can_Receive(&rxMsg) == CAN_OK) {
75
            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("MSG Received: ID=%lx, DLC=%u, EXT=%u, RTR=%u, ", rxMsg.Id, (uint16_t)(rxMsg.DataLength), (uint16_t)(rxMsg.ExtendedFlag), (uint16_t)(rxMsg.RemoteFlag));
76
            printf("data={ ");
87
            printf("data={ ");
-
 
88
 
77
            for (uint8_t i=0; i<rxMsg.DataLength; i++) {
89
            for (uint8_t i=0; i<rxMsg.DataLength; i++) {
78
                printf("%x ", rxMsg.Data.bytes[i]);
90
                printf("%x ", rxMsg.Data.bytes[i]);
79
            }
91
            }
80
            printf("}\n");
92
            printf("}\n");
-
 
93
 
-
 
94
            if( rxMsg.Id == 0x3 ){
-
 
95
//                lcd_gotoxy(0,1);
-
 
96
                lcd_clrscr();
-
 
97
 //               lcd_putc(0xdf);
-
 
98
                lcd_puts("Temperature data:\n");
-
 
99
 
-
 
100
                for( uint8_t i=0; i<(rxMsg.DataLength/2); i++ ){
-
 
101
                        snprintf( buffer, BUFFER_SIZE, "Sensor %i: %d,%d%cC", i, rxMsg.Data.bytes[i*2], rxMsg.Data.bytes[i*2+1], 0xdf );
-
 
102
                        lcd_gotoxy(0,1+i);
-
 
103
                        lcd_puts( buffer );
-
 
104
                }
-
 
105
            }
81
        }
106
        }
82
    }
107
    }
83
   
108
   
84
    return 0;
109
    return 0;
85
}
110
}
86
 
111