Subversion Repositories HomeAutomation

Rev

Rev 389 | Rev 427 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  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.  *
  5.  * @date    2006-11-21
  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>
  20. #include <serial.h>
  21. #include <timebase.h>
  22.  
  23.  
  24. /*-----------------------------------------------------------------------------
  25.  * Main Program
  26.  *---------------------------------------------------------------------------*/
  27. int main(void) {
  28.     Mcu_Init();
  29.     Timebase_Init();
  30.     Serial_Init();
  31.    
  32.     sei();
  33.    
  34.     printf("\n------------------------------------------------------------\n");
  35.     printf(  "   CAN Test: Periodic Transmission\n");
  36.     printf(  "------------------------------------------------------------\n");
  37.    
  38.     printf("CanInit...");
  39.     if (Can_Init() != CAN_OK) {
  40.         printf("FAILED!\n");
  41.     }
  42.     else {
  43.         printf("OK!\n");
  44.     }
  45.    
  46.     uint32_t timeStamp = 0;
  47.    
  48.     Can_Message_t txMsg;
  49.     Can_Message_t rxMsg;
  50.     //The databytes are just what happens to be in the memory. They are never set.
  51.     txMsg.RemoteFlag = 0;
  52.     txMsg.ExtendedFlag = 1;
  53.     txMsg.Id = 1600000;
  54.     txMsg.DataLength = 2;
  55.    
  56.     /* main loop */
  57.     while (1) {
  58.         /* service the CAN routines */
  59.         Can_Service();
  60.        
  61.         /* send CAN message and check for CAN errors once every second */
  62.         if (Timebase_PassedTimeMillis(timeStamp) >= 1000) {
  63.             timeStamp = Timebase_CurrentTime();
  64.             /* send txMsg */
  65.             txMsg.Id++;
  66.             Can_Send(&txMsg);
  67.         }
  68.        
  69.         /* check if any messages have been received */
  70.         while (Can_Receive(&rxMsg) == CAN_OK) {
  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++) {
  74.                 printf("%x ", rxMsg.Data.bytes[i]);
  75.             }
  76.             printf("}\n");
  77.         }
  78.     }
  79.    
  80.     return 0;
  81. }
  82.