Subversion Repositories HomeAutomation

Rev

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

  1. /**
  2.  * CAN Relay.
  3.  *
  4.  *
  5.  * @date    2006-12-17
  6.  * @author  Erik Larsson
  7.  *
  8.  *   Observera! Applikationen är testad på labplatta med m88, pot och lysdiod
  9.  *
  10.  *   ADC=Vin*1024/Vref
  11.  *   Vout=( 10 mV/C )( Temperature C ) + 500 mV
  12.  *
  13.  *  Transmitts: DATA: <temperature high byte> <temperature low byte> <relay status>
  14.  *
  15.  *  TODO fixa så status/temp ligger i en funktion
  16.  *  temperaturomvandlingen fungerar fint, men sparas i Data på ett fuckat sätt
  17.  *  fixa så adcn inte pollas hela tiden, det tar för mycket tid
  18.  *
  19.  */
  20.  
  21. /*
  22.  * For testing with ATmega88 PDIP enable this fetaure
  23.  * Since PDIP doesn't have ADC7 it will use the ADC0 instead
  24.  */
  25. #define PDIP    1
  26. #define DEBUG   0 /* Prints debug messages to uart */
  27.  
  28. /*-----------------------------------------------------------------------------
  29.  * Includes
  30.  *---------------------------------------------------------------------------*/
  31. /* system files */
  32. #include <avr/io.h>
  33. #include <avr/interrupt.h>
  34. #include <avr/wdt.h>
  35. #include <stdio.h>
  36. /* lib files */
  37. #include <can.h>
  38. #include <serial.h>
  39. #include <timebase.h>
  40. /* defines */
  41. #define OFF     0x01
  42. #define ON      0x02
  43. #define Vref    5
  44.  
  45. /*-----------------------------------------------------------------------------
  46.  * Main Program
  47.  *---------------------------------------------------------------------------*/
  48. int main(void) {
  49.  
  50.     uint8_t relayStatus;
  51.     uint32_t boardTemperature;
  52.  
  53.     Timebase_Init();
  54.     Serial_Init();
  55.     sei();
  56.  
  57.     /* Turn relay off */
  58.     PORTC &= ~(1<<PC1);
  59.     DDRC |= (1<<DDC1);
  60.  
  61.     relayStatus = OFF;
  62.  
  63.     /* Initiate ADC for reading temperature sensor */
  64.  
  65.     /* Wake up ADC */
  66.     PRR &= ~(1<<PRADC);
  67. #if PDIP
  68.     /* Enable AREF and ADC0 (For PDIP package) */
  69.     ADMUX = 0x00; // TODO 0x00 för aref, 0xc0 för internal 1,1
  70. #else
  71.     /* Enable AREF and ADC7 (For TQFP package) */
  72.     ADMUX = 0x07;
  73. #endif
  74.     ADCSRA |= (1<<ADEN);
  75.  
  76. #if DEBUG
  77.     printf("\n------------------------------------------------------------\n");
  78.     printf(  "   CAN: Relay\n");
  79.     printf(  "------------------------------------------------------------\n");
  80.  
  81.  
  82.     printf("CanInit...");
  83.     if (Can_Init() != CAN_OK) {
  84.         printf("FAILED!\n");
  85.     }else{
  86.         printf("OK!\n");
  87.     }
  88. #endif
  89.     uint32_t timeStamp = 0;
  90.  
  91.     Can_Message_t txMsg;
  92.     Can_Message_t rxMsg;
  93.     txMsg.Id = 0;
  94.     txMsg.RemoteFlag = 0;
  95.     txMsg.ExtendedFlag = 1;
  96.  
  97.     /* main loop */
  98.     while (1) {
  99.         /* service the CAN routines */
  100.         Can_Service();
  101.  
  102.         /* check if any messages have been received */
  103.         while (Can_Receive(&rxMsg) == CAN_OK) {
  104.             /* This relay is adressed*/
  105.             if( rxMsg.Id == 0x1200 ){
  106.  
  107.                 if( rxMsg.Data.bytes[0] == 0x01 ){
  108.                     /* Turn relay on */
  109.  
  110.                     /* Set as input and enable pull-up */
  111.                     DDRC &= ~(1<<DDC1);
  112.                     PORTC |= (1<<PC1);
  113.  
  114.                     relayStatus = ON;
  115. #if DEBUG
  116.                     printf("turn on\n");
  117. #endif
  118.  
  119.                 }else if(rxMsg.Data.bytes[0] == 0x02){
  120.                     /* Turn relay off */
  121.  
  122.                     PORTC &= ~(1<<PC1);
  123.                     DDRC |= (1<<DDC1);
  124.  
  125.                     relayStatus = OFF;
  126. #if DEBUG
  127.                     printf("turn off\n");
  128. #endif
  129.  
  130.                 }else if(rxMsg.Data.bytes[0] == 0x03){
  131.                     /* Toggle relay */
  132.  
  133.                     if(relayStatus == ON){
  134.                         PORTC &= ~(1<<PC1);
  135.                         DDRC |= (1<<DDC1);
  136. #if DEBUG
  137.                         printf("toggle: turn off\n");
  138. #endif
  139.  
  140.                         relayStatus = OFF;
  141.  
  142.                     }else{
  143.                         DDRC &= ~(1<<DDC1);
  144.                         PORTC |= (1<<PC1);
  145. #if DEBUG
  146.                         printf("toggle: turn on\n");
  147. #endif
  148.  
  149.                         relayStatus = ON;
  150.                     }
  151.  
  152.                 }else if(rxMsg.Data.bytes[0] == 0x04){
  153.                     /* Get relay status (ON/OFF?, temperature) */
  154.                     ADCSRA |= (1<<ADSC);
  155.  
  156.                     /* Wait for conversion to complete */
  157.                     while( ADCSRA & (1<<ADSC) ){}
  158.  
  159.                     txMsg.Id = 0x1201; // temporary ID
  160.  
  161.                     txMsg.Data.bytes[3] = relayStatus;
  162.  
  163.                     /* Convert voltage to temperature */
  164.  
  165.                     boardTemperature = ADCW;
  166.  
  167.                     boardTemperature = boardTemperature*Vref;
  168.                     boardTemperature = (boardTemperature*100/1024)-50;
  169.  
  170.                     /* Save and send */
  171.                     txMsg.Data.bytes[0]= (uint32_t)boardTemperature&0x00FF;
  172.                     txMsg.Data.bytes[1]= ((uint32_t)boardTemperature&0xFF00)>>8;
  173.  
  174.                     txMsg.DataLength = 3;
  175.  
  176.                     Can_Send( &txMsg );
  177.                 }
  178.             }
  179.         }
  180.  
  181.         // TODO
  182.         // ska status på relä och temperatur skickas periodiskt?
  183.         // tätare intervall om temperaturen kommer över en viss gräns?
  184.  
  185.         if( Timebase_PassedTimeMillis(timeStamp) >=10000){
  186.             timeStamp = Timebase_CurrentTime();
  187.             /* Get relay status (ON/OFF?, temperature) and send */
  188.  
  189.             ADCSRA |= (1<<ADSC);
  190.  
  191.             /* Wait for conversion to complete */
  192.             while( ADCSRA & (1<<ADSC) ){}
  193.  
  194.             txMsg.Id = 0x1201; // temporary ID
  195.  
  196.             txMsg.Data.bytes[3] = relayStatus;
  197.  
  198.             /* Convert voltage to temperature */
  199.  
  200.             boardTemperature = ADCW;
  201.  
  202.             boardTemperature = boardTemperature*Vref;
  203.             boardTemperature = (boardTemperature*100/1024)-50;
  204.  
  205.             /* Save and send */
  206.             txMsg.Data.bytes[0]= (uint32_t)boardTemperature&0x00FF;
  207.             txMsg.Data.bytes[1]= ((uint32_t)boardTemperature&0xFF00)>>8;
  208.  
  209.             txMsg.DataLength = 3;
  210.             Can_Send( &txMsg );
  211. #if DEBUG
  212.             printf("Message sent...\n");
  213.             printf("Temperature: %d\nRelay status: %x, %u\n", boardTemperature, relayStatus, relayStatus);
  214. #endif
  215.  
  216.         }
  217.     }
  218.     return 0;
  219. }
  220.  
  221.