Subversion Repositories HomeAutomation

Rev

Rev 164 | 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. #include <eqlazer_funcdefs.h>
  26.  
  27. #define BUFFER_SIZE 20
  28.  
  29. /*-----------------------------------------------------------------------------
  30.  * Main Program
  31.  *---------------------------------------------------------------------------*/
  32. int main(void) {
  33.  
  34.     char buffer[ BUFFER_SIZE ];
  35.     uint8_t subzero;
  36.  
  37.     Timebase_Init();
  38.     Serial_Init();
  39.     sei();
  40.     lcd_init( LCD_DISP_ON );
  41.  
  42.     printf("\n------------------------------------------------------------\n");
  43.     printf(  "   CAN Test: LCD\n");
  44.     printf(  "------------------------------------------------------------\n");
  45.  
  46.     lcd_clrscr();
  47.     lcd_puts("CAN LCD test\n");
  48.  
  49.     printf("CanInit...");
  50.     lcd_puts("CanInit... ");
  51.     if (Can_Init() != CAN_OK) {
  52.         printf("FAILED!\n");
  53.         lcd_puts("FAILED!\n");
  54.     }
  55.     else {
  56.         printf("OK!\n");
  57.         lcd_puts("OK!\n");
  58.     }
  59.    
  60.     uint32_t timeStamp = 0;
  61.    
  62.     Can_Message_t txMsg;
  63.     Can_Message_t rxMsg;
  64.     txMsg.Id = 0;
  65.     txMsg.RemoteFlag = 0;
  66.     txMsg.ExtendedFlag = 1; //DataLength and the databytes are just what happens to be in the memory. They are never set.
  67.  
  68.     lcd_clrscr();
  69.  
  70.     /* main loop */
  71.     while (1) {
  72.         /* service the CAN routines */
  73.         Can_Service();
  74.        
  75.         /* send CAN message and check for CAN errors once every second */
  76. //      if (Timebase_PassedTimeMillis(timeStamp) >= 1000) {
  77. //          timeStamp = Timebase_CurrentTime();
  78.             /* send txMsg */
  79. //          txMsg.Id++;
  80. //          Can_Send(&txMsg);
  81. //      }
  82.  
  83.         /* check if any messages have been received */
  84.         while (Can_Receive(&rxMsg) == CAN_OK) {
  85.             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("data={ ");
  87.  
  88.             for (uint8_t i=0; i<rxMsg.DataLength; i++) {
  89.                 printf("%x ", rxMsg.Data.bytes[i]);
  90.             }
  91.             printf("}\n");
  92.  
  93.  
  94.             if( (rxMsg.Id & 0x1E000000)>>25 == FUNCT_SENSORS){ // if sensor data received
  95.  
  96.                 /* which temperature sensor? */
  97.                 if( (rxMsg.Id & 0x01FF8000)>>15 == FUNCC_SENSORS_TEMPERATURE_INSIDE ){
  98.                     /* Below 0 degrees celsius? */
  99.                     if(rxMsg.Data.bytes[0]&0x80){
  100.                         rxMsg.Data.bytes[0]= -rxMsg.Data.bytes[0];
  101.                         subzero = 1;
  102.                     }else{
  103.                         subzero = 0;
  104.                     }
  105.                         snprintf( buffer, BUFFER_SIZE, (subzero)?"-%d,%d%cC":"%d,%d%cC", rxMsg.Data.bytes[0], rxMsg.Data.bytes[1], 0xdf );
  106.                         lcd_gotoxy( LCD_LINE0_0 );
  107.                         lcd_puts( LCD_SENSOR_TEMPERATURE_INSIDE );
  108.                         lcd_gotoxy( LCD_LINE1_0 );
  109.                         lcd_puts( buffer );
  110.                 }else if( (rxMsg.Id & 0x01FF8000)>>15 == FUNCC_SENSORS_TEMPERATURE_OUTSIDE ){
  111.                     if(rxMsg.Data.bytes[0]&0x80){
  112.                         rxMsg.Data.bytes[0]= -rxMsg.Data.bytes[0];
  113.                         subzero = 1;
  114.                     }else{
  115.                         subzero = 0;
  116.                     }
  117.                         snprintf( buffer, BUFFER_SIZE, (subzero)?"-%d,%d%cC":"%d,%d%cC", rxMsg.Data.bytes[0], rxMsg.Data.bytes[1], 0xdf );
  118.                         lcd_gotoxy( LCD_LINE2_0 );
  119.                         lcd_puts( LCD_SENSOR_TEMPERATURE_OUTSIDE );
  120.                         lcd_gotoxy( LCD_LINE3_0 );
  121.                         lcd_puts( buffer );
  122.                 }else if( (rxMsg.Id & 0x01FF8000)>>15 == FUNCC_SENSORS_TEMPERATURE_FREEZER ){
  123.                     if(rxMsg.Data.bytes[0]&0x80){
  124.                         rxMsg.Data.bytes[0]= -rxMsg.Data.bytes[0];
  125.                         subzero = 1;
  126.                     }else{
  127.                         subzero = 0;
  128.                     }
  129.                         snprintf( buffer, BUFFER_SIZE, (subzero)?"-%d,%d%cC":"%d,%d%cC", rxMsg.Data.bytes[0], rxMsg.Data.bytes[1], 0xdf );
  130.                         lcd_gotoxy( LCD_LINE2_2 );
  131.                         lcd_puts( LCD_SENSOR_TEMPERATURE_FREEZER );
  132.                         lcd_gotoxy( LCD_LINE3_2 );
  133.                         lcd_puts( buffer );
  134.                 }else{
  135.                     lcd_clrscr();
  136.                     lcd_puts("Sensors: No config.");
  137.                 }
  138. /*
  139.                 for( uint8_t i=0; i<(rxMsg.DataLength/2); i++ ){
  140.                         snprintf( buffer, BUFFER_SIZE, "Sensor %i: %d,%d%cC", i, rxMsg.Data.bytes[i*2], rxMsg.Data.bytes[i*2+1], 0xdf );
  141.                         lcd_gotoxy(0,1+i);
  142.                         lcd_puts( buffer );
  143.                 }*/
  144.             }
  145.         }
  146.     }
  147.    
  148.     return 0;
  149. }
  150.