Subversion Repositories HomeAutomation

Rev

Rev 163 | Blame | 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.  * Added LCD output
  5.  *
  6.  * @date    2006-11-21, LCD addition 2006-12-11
  7.  * @author  Jimmy Myhrman, Erik Larsson
  8.  *  
  9.  */
  10.  
  11. /*-----------------------------------------------------------------------------
  12.  * Includes
  13.  *---------------------------------------------------------------------------*/
  14. /* system files */
  15. #include <avr/io.h>
  16. #include <avr/interrupt.h>
  17. #include <avr/wdt.h>
  18. #include <stdio.h>
  19. /* lib files */
  20. #include <can.h>
  21. #include <serial.h>
  22. #include <timebase.h>
  23. #include <lcd_HD44780.h>
  24.  
  25. #define BUFFER_SIZE 20
  26.  
  27. /*-----------------------------------------------------------------------------
  28.  * Main Program
  29.  *---------------------------------------------------------------------------*/
  30. int main(void) {
  31.  
  32.     char buffer[ BUFFER_SIZE ];
  33.     int num=1234;
  34.  
  35.     Timebase_Init();
  36.     Serial_Init();
  37.     sei();
  38.     lcd_init( LCD_DISP_ON );
  39.  
  40.     printf("\n------------------------------------------------------------\n");
  41.     printf(  "   CAN Test: Periodic Transmission\n");
  42.     printf(  "------------------------------------------------------------\n");
  43.  
  44.     lcd_clrscr();
  45.     lcd_puts("CAN LCD test\n");
  46.  
  47.     printf("CanInit...");
  48.     lcd_puts("CanInit... ");
  49.     if (Can_Init() != CAN_OK) {
  50.         printf("FAILED!\n");
  51.         lcd_puts("FAILED!\n");
  52.     }
  53.     else {
  54.         printf("OK!\n");
  55.         lcd_puts("OK!\n");
  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);
  62.  
  63.     uint32_t timeStamp = 0;
  64.    
  65.     Can_Message_t txMsg;
  66.     Can_Message_t rxMsg;
  67.     txMsg.Id = 0;
  68.     txMsg.RemoteFlag = 0;
  69.     txMsg.ExtendedFlag = 1; //DataLength and the databytes are just what happens to be in the memory. They are never set.
  70.    
  71.     /* main loop */
  72.     while (1) {
  73.         /* service the CAN routines */
  74.         Can_Service();
  75.        
  76.         /* send CAN message and check for CAN errors once every second */
  77.         if (Timebase_PassedTimeMillis(timeStamp) >= 1000) {
  78.             timeStamp = Timebase_CurrentTime();
  79.             /* send txMsg */
  80.             txMsg.Id++;
  81.             Can_Send(&txMsg);
  82.         }
  83.        
  84.         /* check if any messages have been received */
  85.         while (Can_Receive(&rxMsg) == CAN_OK) {
  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));
  87.             printf("data={ ");
  88.  
  89.             for (uint8_t i=0; i<rxMsg.DataLength; i++) {
  90.                 printf("%x ", rxMsg.Data.bytes[i]);
  91.             }
  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.             }
  106.         }
  107.     }
  108.    
  109.     return 0;
  110. }
  111.