Subversion Repositories HomeAutomation

Rev

Blame | Last modification | View Log | SVN | RSS feed

  1. /**
  2.  * CAN Fancontroller. This application is used to control fans via PWM.
  3.  * Might also add some sort of display if the pincount is high enough
  4.  * Or temperature sensors. who knows :o
  5.  *
  6.  * @date    2008-01-10
  7.  * @author  Martin Nordén
  8.  *
  9.  * TODO: Everything.
  10.  */
  11.  
  12.  
  13. /*-----------------------------------------------------------------------------
  14.  * Includes
  15.  *---------------------------------------------------------------------------*/
  16. /* system files */
  17. #include <avr/interrupt.h>
  18. #include <inttypes.h>
  19. #include <stdio.h>
  20. #include <avr/pgmspace.h> //To allow read/write from flash
  21.  
  22. #include <math.h>
  23.  
  24. #include <drivers/timer/timebase.h>
  25.  
  26. #include <config.h> // All configuration parameters
  27. #include <bios.h>   // BIOS interface declarations, including CAN structure and ID defines.
  28.  
  29. #include <string.h> //for memcpy
  30. /* lib files */
  31. #include <bios.h>
  32. //#include <funcdefs/funcdefs.h>
  33.  
  34. //#include <settings.h>
  35.  
  36. /*----------------------------------------------------------------------------
  37.  * Defines
  38.  * -------------------------------------------------------------------------*/
  39.  
  40. //Working on implementing all of these...
  41. #define FANCONT_CMD_STATUS          0x00    /* Get status */
  42. #define FANCONT_CMD_SETSPEED        0x01    /* Set Fan Speed */
  43. #define FANCONT_CMD_SETFREQENCY     0x02    /* Get PWM Frequency */
  44.  
  45.  
  46.  
  47. #define APP_TYPE    CAN_APPTYPES_FANCONTROLLER
  48. #define APP_VERSION 0x0001
  49.  
  50. #define TRUE 1
  51. #define FALSE !TRUE
  52.  
  53.  
  54. /*-----------------------------------------------------------------------------
  55.  * Global variables
  56.  *---------------------------------------------------------------------------*/
  57. uint8_t DutyCycle;  //Duty cycle   
  58. uint32_t timeStamp;
  59.  
  60. Can_Message_t txMsg;    //Transmit message storage
  61.  
  62. volatile Can_Message_t rxMsg; // Message storage
  63. volatile uint8_t rxMsgFull;   // Synchronization flag
  64.  
  65. /*----------------------------------------------------------------------------
  66.  * Functions
  67.  * -------------------------------------------------------------------------*/
  68.  
  69. /* Takes care of incoming messages and parses them if this node is adressed */
  70. void can_receive(Can_Message_t *msg)
  71. { // CAN callback function
  72.     if (!rxMsgFull) {
  73.         memcpy((void*)&rxMsg, msg, sizeof(rxMsg));
  74.         rxMsgFull = 1;
  75.     }
  76. }
  77.  
  78.  
  79. /*-----------------------------------------------------------------------------
  80.  * Main Program
  81.  *---------------------------------------------------------------------------*/
  82. int main(void) {
  83.  
  84. //Setting up OC1A, 16 bit counter used as 8 bit.
  85.     TCCR1A |= (1<<COM1A1) | (1<<WGM11); /* Set OC1A at TOP, mode 14 */
  86.     TCCR1B |= (1<<WGM13) | (1<<WGM12) | (1<<CS12) | (0<<CS11)| (1<<CS10); /* Timer uses main system clock with ? prescale */
  87.     ICR1 = 256; //8 bit precision to match the other two counters.
  88.     OCR1A = 0;  //0% as standard
  89.     DDRB |= (1<<PB1); /* Enable pin as output */
  90. //Setting up OC0A
  91.     TCCR0A |= (1<<COM0A1)|(1<<WGM01)|(1<<WGM00); /* Set OC0A at TOP, Mode 7 */
  92.     TCCR0B |= (1<<CS01)|  (1<<CS00); /* Prescaler updating now */
  93.     OCR0A = 0;  //0% standard
  94.     DDRD |= (1<<PD6);
  95. //Setting up OC0B
  96.     TCCR0A |= (1<<COM0B1);
  97.     OCR0B = 0;
  98.     DDRD |= (1<<PD5);
  99.  
  100. //Initialize variables
  101.     timeStamp = Timebase_CurrentTime();
  102.     sei();
  103.  
  104.     BIOS_CanCallback = &can_receive;
  105.  
  106.  
  107. //Send a message that we are alive!
  108.     txMsg.Id = (CAN_NMT_APP_START << CAN_SHIFT_NMT_TYPE) | (NODE_ID << CAN_SHIFT_NMT_SID);
  109.     txMsg.DataLength = 4;
  110.     txMsg.RemoteFlag = 0;
  111.     txMsg.ExtendedFlag = 1;
  112.     txMsg.Data.words[0] = APP_TYPE;
  113.     txMsg.Data.words[1] = APP_VERSION;
  114.     BIOS_CanSend(&txMsg);
  115.  
  116. //Lets get timebase up and running
  117.     Timebase_Init();
  118.  
  119.  
  120.     /* main loop */
  121.     while (1) {
  122.        
  123.         /* Time to fade color? */
  124.         if ((Timebase_CurrentTime() - timeStamp) >= 100) {
  125.             //OCR1A++;
  126.             //if (OCR1A>150) OCR1A = 50;
  127.             timeStamp = Timebase_CurrentTime();
  128.         }
  129.  
  130.  
  131.         /* Check if any messages has been recieved */
  132.         if (rxMsgFull) {
  133.             uint16_t acttype;
  134.             uint8_t fancontid;         
  135.        
  136.             if ( ((rxMsg.Id & CAN_MASK_CLASS)>>CAN_SHIFT_CLASS) == CAN_ACT){
  137.                 // Actuator package
  138.                 acttype =(uint16_t)((rxMsg.Id & CAN_MASK_ACT_TYPE) >> CAN_SHIFT_ACT_TYPE);
  139.                 fancontid = (uint8_t)((rxMsg.Id & CAN_MASK_ACT_ID) >> CAN_SHIFT_ACT_ID);
  140.                
  141.                 if ((acttype == ACT_TYPE_FAN) && (fancontid == 1)) {                                                           
  142.                     // This is a message for this fancontroller
  143.                        
  144.                    
  145.                     if( rxMsg.Data.bytes[0] == FANCONT_CMD_SETSPEED ){
  146.                         /* Set duty cycle */
  147.                         OCR1A =  rxMsg.Data.bytes[1];
  148.                     }
  149.                    
  150.                    
  151.                 }
  152.             }
  153.             rxMsgFull = 0; //  
  154.             rxMsgFull = 0; //  
  155.  
  156.         }
  157.         /* Done checking incoming packages */
  158.  
  159.     }
  160.    
  161.     return 0;
  162. }
  163.