Subversion Repositories HomeAutomation

Rev

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

Rev 61 Rev 73
1
/**
1
/**
2
 * CAN test program.
2
 * CAN test program.
3
 *
3
 *
4
 * @target  AVR
4
 * @target  AVR
5
 * @date    2006-10-29
5
 * @date    2006-10-29
6
 * @author  Jimmy Myhrman
6
 * @author  Jimmy Myhrman
7
 *  
7
 *  
8
 */
8
 */
9
 
9
 
10
/*-----------------------------------------------------------------------------
10
/*-----------------------------------------------------------------------------
11
 * Includes
11
 * Includes
12
 *---------------------------------------------------------------------------*/
12
 *---------------------------------------------------------------------------*/
13
/* system files */
13
/* system files */
14
#include <avr/io.h>
14
#include <avr/io.h>
15
#include <avr/interrupt.h>
15
#include <avr/interrupt.h>
16
#include <avr/wdt.h>
16
#include <avr/wdt.h>
17
#include <stdio.h>
17
#include <stdio.h>
18
/* lib files */
18
/* lib files */
19
#include <can.h>
19
#include <can.h>
20
#include <uart.h>
20
#include <uart.h>
21
#include <timebase.h>
21
#include <timebase.h>
22
#include <protocol.h>
22
#include <protocol.h>
23
 
23
 
24
 
24
 
25
/*-----------------------------------------------------------------------------
25
/*-----------------------------------------------------------------------------
26
 * Main Program
26
 * Main Program
27
 *---------------------------------------------------------------------------*/
27
 *---------------------------------------------------------------------------*/
28
int main(void) {
28
int main(void) {
29
    TimebaseInit();
29
    TimebaseInit();
30
    UartInit();
30
    UartInit();
31
    sei();
31
    sei();
32
   
32
   
33
    printf("\n------------------------------\n");
33
    printf("\n------------------------------\n");
34
    printf("   CAN Monitor\n");
34
    printf("   CAN Monitor\n");
35
    printf("------------------------------\n");
35
    printf("------------------------------\n");
36
   
36
   
37
    printf("CanInit...");
37
    printf("CanInit...");
38
    if (CanInit(CAN_BITRATE_1M) != CAN_OK) {
38
    if (CanInit(CAN_BITRATE_1M) != CAN_OK) {
39
        printf("FAILED!\n");
39
        printf("FAILED!\n");
40
    }
40
    }
41
    else {
41
    else {
42
        printf("OK!\n");
42
        printf("OK!\n");
43
    }
43
    }
44
   
44
   
45
    uint32_t timeStamp = 0;
45
    uint32_t timeStamp = 0;
46
   
46
   
47
    CanMessage_t txMsg;
47
    CanMessage_t txMsg;
48
    CanMessage_t rxMsg;
48
    CanMessage_t rxMsg;
49
   
49
   
50
    txMsg.DataLength = 8;
50
    txMsg.DataLength = 8;
51
    txMsg.Data.bytes[0] = 7;
51
    txMsg.Data.bytes[0] = 7;
52
    txMsg.Id = 733;
52
    txMsg.Id = 733;
53
    txMsg.RemoteFlag = 0;
53
    txMsg.RemoteFlag = 0;
54
    txMsg.ExtendedFlag = 1;
54
    txMsg.ExtendedFlag = 1;
55
   
55
   
56
    /* main loop */
56
    /* main loop */
57
    while (1) {
57
    while (1) {
58
        /* any new CAN messages received? */
58
        /* any new CAN messages received? */
59
        if (CanReceive(&rxMsg) == CAN_OK) {
59
        if (CanReceive(&rxMsg) == CAN_OK) {
60
            ProtocolParseCanMessage(&rxMsg);
60
            ProtocolParseCanMessage(&rxMsg);
61
        }
61
        }
62
       
62
       
63
        /* send CAN message and check for CAN errors once every second */
63
        /* send CAN message and check for CAN errors once every second */
64
        if (TimebasePassedTimeMS(timeStamp) >= 1000) {
64
        if (TimebasePassedTimeMS(timeStamp) >= 1000) {
65
            /* increase ID and send txMsg */
65
            /* increase ID and send txMsg */
66
            txMsg.Id++;
66
            txMsg.Id++;
67
            CanSend(&txMsg);
67
            CanSend(&txMsg);
68
            /* check for errors */
68
            /* check for errors */
69
            timeStamp = TimebaseCurrentTime();
69
            timeStamp = TimebaseCurrentTime();
70
            if (CanCheckError() != CAN_OK) {
70
            if (CanCheckError() != CAN_OK) {
71
                printf("CAN ERROR detected!\n");
71
                printf("CAN ERROR detected!\n");
72
            }
72
            }
73
        }
73
        }
74
    }
74
    }
75
   
75
   
76
    return 0;
76
    return 0;
77
}
77
}
78
 
78