Details | Last modification | View Log | SVN | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 61 | jimmy | 1 | /** |
| 144 | jimmy | 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. |
||
| 61 | jimmy | 4 | * |
| 144 | jimmy | 5 | * @date 2006-11-21 |
| 61 | jimmy | 6 | * @author Jimmy Myhrman |
| 7 | * |
||
| 8 | */ |
||
| 9 | |||
| 10 | /*----------------------------------------------------------------------------- |
||
| 11 | * Includes |
||
| 12 | *---------------------------------------------------------------------------*/ |
||
| 13 | /* system files */ |
||
| 14 | #include <avr/io.h> |
||
| 15 | #include <avr/interrupt.h> |
||
| 16 | #include <avr/wdt.h> |
||
| 17 | #include <stdio.h> |
||
| 18 | /* lib files */ |
||
| 19 | #include <can.h> |
||
| 149 | jimmy | 20 | #include <serial.h> |
| 61 | jimmy | 21 | #include <timebase.h> |
| 22 | |||
| 23 | |||
| 24 | /*----------------------------------------------------------------------------- |
||
| 25 | * Main Program |
||
| 26 | *---------------------------------------------------------------------------*/ |
||
| 27 | int main(void) { |
||
| 208 | jimmy | 28 | Mcu_Init(); |
| 127 | jimmy | 29 | Timebase_Init(); |
| 149 | jimmy | 30 | Serial_Init(); |
| 208 | jimmy | 31 | |
| 61 | jimmy | 32 | sei(); |
| 33 | |||
| 144 | jimmy | 34 | printf("\n------------------------------------------------------------\n"); |
| 35 | printf( " CAN Test: Periodic Transmission\n"); |
||
| 36 | printf( "------------------------------------------------------------\n"); |
||
| 61 | jimmy | 37 | |
| 38 | printf("CanInit..."); |
||
| 127 | jimmy | 39 | if (Can_Init() != CAN_OK) { |
| 61 | jimmy | 40 | printf("FAILED!\n"); |
| 41 | } |
||
| 42 | else { |
||
| 43 | printf("OK!\n"); |
||
| 44 | } |
||
| 45 | |||
| 46 | uint32_t timeStamp = 0; |
||
| 47 | |||
| 127 | jimmy | 48 | Can_Message_t txMsg; |
| 49 | Can_Message_t rxMsg; |
||
| 197 | andfri | 50 | //The databytes are just what happens to be in the memory. They are never set. |
| 61 | jimmy | 51 | txMsg.RemoteFlag = 0; |
| 197 | andfri | 52 | txMsg.ExtendedFlag = 1; |
| 426 | eqlazer | 53 | txMsg.Id = 1600000; |
| 197 | andfri | 54 | txMsg.DataLength = 2; |
| 61 | jimmy | 55 | |
| 56 | /* main loop */ |
||
| 57 | while (1) { |
||
| 142 | jimmy | 58 | /* service the CAN routines */ |
| 59 | Can_Service(); |
||
| 61 | jimmy | 60 | |
| 61 | /* send CAN message and check for CAN errors once every second */ |
||
| 127 | jimmy | 62 | if (Timebase_PassedTimeMillis(timeStamp) >= 1000) { |
| 142 | jimmy | 63 | timeStamp = Timebase_CurrentTime(); |
| 127 | jimmy | 64 | /* send txMsg */ |
| 61 | jimmy | 65 | txMsg.Id++; |
| 144 | jimmy | 66 | Can_Send(&txMsg); |
| 142 | jimmy | 67 | } |
| 68 | |||
| 69 | /* check if any messages have been received */ |
||
| 70 | while (Can_Receive(&rxMsg) == CAN_OK) { |
||
| 144 | jimmy | 71 | 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)); |
| 72 | printf("data={ "); |
||
| 73 | for (uint8_t i=0; i<rxMsg.DataLength; i++) { |
||
| 142 | jimmy | 74 | printf("%x ", rxMsg.Data.bytes[i]); |
| 144 | jimmy | 75 | } |
| 76 | printf("}\n"); |
||
| 61 | jimmy | 77 | } |
| 78 | } |
||
| 79 | |||
| 80 | return 0; |
||
| 81 | } |