Subversion Repositories HomeAutomation

Rev

Rev 171 | Rev 176 | 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.  * Added LCD output
  5.  *
  6.  * @date    2006-11-21, LCD addition 2006-12-11
  7.  * @author  Jimmy Myhrman, Erik Larsson
  8.  *  
  9.  *   Observera! Applikationen är helt otestad
  10.  *   ADC=Vin*1024/Vref
  11.  *
  12.  */
  13.  
  14. /*-----------------------------------------------------------------------------
  15.  * Includes
  16.  *---------------------------------------------------------------------------*/
  17. /* system files */
  18. #include <avr/io.h>
  19. #include <avr/interrupt.h>
  20. #include <avr/wdt.h>
  21. #include <stdio.h>
  22. /* lib files */
  23. #include <can.h>
  24. #include <serial.h>
  25. #include <timebase.h>
  26.  
  27. //#define RELAY_PORT  PORTC
  28. //#define RELAY_PIN   PC1
  29. #define OFF     0x00
  30. #define ON      0x01
  31.  
  32. /*------------------------------------------------------------------------------
  33.  * Read relay state (OPEN/CLOSED) and temperature
  34.  * ---------------------------------------------------------------------------*/
  35. void readRelayStatus( Can_Message_t* msg, uint8_t state )
  36. {
  37.             /* Start measureing */
  38.             ADCSRA |= (1<<ADSC);
  39.  
  40.             /* Wait for conversion to complete */
  41.             while( ADCSRA & (1<<ADSC) ){}
  42.  
  43.             msg->Id = 0x1201; // temporary ID
  44.  
  45.             msg->Data.bytes[0] = state;
  46.             msg->Data.bytes[1]= ADCL;
  47.             msg->Data.bytes[2]= ADCH;
  48.             msg->DataLength = 3;
  49.  
  50.             Can_Send( msg );
  51. }
  52.  
  53. /*-----------------------------------------------------------------------------
  54.  * Main Program
  55.  *---------------------------------------------------------------------------*/
  56. int main(void) {
  57.  
  58.     uint8_t relayStatus;
  59.  
  60.     Timebase_Init();
  61.     Serial_Init();
  62.     sei();
  63.  
  64.     /* Enable RELAY_PIN as output */
  65.     DDRC |= (1<<DDC1);
  66.  
  67.     /* Turn relay off */
  68.     PORTC &= ~(1<<PC1);
  69.     relayStatus = OFF;
  70.  
  71.     /* Initiate ADC for reading temperature sensor */
  72.  
  73.     /* Wake up ADC */
  74.     PRR &= ~(1<<PRADC);
  75.     /* Enable AREF and ADC7 */
  76.     ADMUX = 0x07;
  77.     ADCSRA |= (1<<ADEN);
  78.  
  79.     printf("\n------------------------------------------------------------\n");
  80.     printf(  "   CAN: Relay\n");
  81.     printf(  "------------------------------------------------------------\n");
  82.  
  83.  
  84.     printf("CanInit...");
  85.     if (Can_Init() != CAN_OK) {
  86.     printf("FAILED!\n");
  87.     }else{
  88.         printf("OK!\n");
  89.     }
  90.  
  91.     uint32_t timeStamp = 0;
  92.  
  93.     Can_Message_t txMsg;
  94.     Can_Message_t rxMsg;
  95.     txMsg.Id = 0;
  96.     txMsg.RemoteFlag = 0;
  97.     txMsg.ExtendedFlag = 1;
  98.  
  99.  
  100.     /* main loop */
  101.     while (1) {
  102.         /* service the CAN routines */
  103.         Can_Service();
  104.  
  105.         /* check if any messages have been received */
  106.         while (Can_Receive(&rxMsg) == CAN_OK) {
  107.             /* This relay is adressed*/
  108.             if( rxMsg.Id == 0x1200 ){
  109.  
  110.                 if( rxMsg.Data.bytes[0] == 0x01 ){
  111.                     /* Turn relay on */
  112.  
  113.                     /* Set as input and enable pull-up */
  114.                     DDRC &= ~(1<<DDC1);
  115.                     PORTC |= (1<<PC1);
  116.  
  117.                     relayStatus = ON;
  118.  
  119.                 }else if(rxMsg.Data.bytes[0] == 0x02){
  120.                     /* Turn relay off */
  121.  
  122.                     DDRC |= (1<<DDC1);
  123.                     PORTC &= ~(1<<PC1);
  124.  
  125.                     relayStatus = OFF;
  126.  
  127.                 }else if(rxMsg.Data.bytes[0] == 0x03){
  128.                     /* Toggle relay */
  129.  
  130.                     if(relayStatus == ON){
  131.                        
  132.                         DDRC |= (1<<DDC1);
  133.                         PORTC &= ~(1<<PC1);
  134.                         relayStatus = OFF;
  135.  
  136.                     }else{
  137.  
  138.                         DDRC &= ~(1<<DDC1);
  139.                         PORTC |= (1<<PC1);
  140.                         relayStatus = ON;
  141.                     }
  142.  
  143.                 }else if(rxMsg.Data.bytes[0] == 0x04){
  144.                     /* Get relay status (ON/OFF?, temperature) */
  145.                     readRelayStatus( &txMsg, relayStatus );
  146.                 }
  147.             }
  148.         }
  149.  
  150.         // TODO
  151.         // ska status på relä och temperatur skickas periodiskt?
  152.         // tätare intervall om temperaturen kommer över en viss gräns?
  153.  
  154.         if( Timebase_PassedTimeMillis(timeStamp) >=5000){
  155.             timeStamp = Timebase_CurrentTime();
  156.             /* Get relay status (ON/OFF?, temperature) */
  157.  
  158.              readRelayStatus( &txMsg, relayStatus );
  159.         }
  160.     }
  161.     return 0;
  162. }
  163.  
  164.