Subversion Repositories HomeAutomation

Rev

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

  1.  
  2. #include "act_dimmer230.h"
  3. uint8_t state = ACT_DIMMMER230_STATE_IDLE;
  4.  
  5. ISR (TIMER1_COMPA_vect)
  6. {
  7.  
  8. }
  9. ISR (act_dimmer230_ZC_PCINT_vect)
  10. {
  11.     if (state==ACT_DIMMMER230_STATE_IDLE && (act_dimmer230_ZC_PIN&(1<<act_dimmer230_ZC_BIT)))
  12.     {
  13.        
  14.         //enable timer and timer interrupt
  15.         TCNT1 = 0;              //reset timer1 count register
  16.         // here timer oc1a register should be set to the correct time (zero-cross time + dimmer value)
  17.         TIFR1=(1<<OCF1A);       //clear interrupt flag before enabling interrupt
  18.         TIMSK1|=(1<<OCIE1A);
  19.  
  20.         state = ACT_DIMMMER230_STATE_TIMER_ON;
  21.     }
  22. }
  23.  
  24. void act_dimmer230_Init(void)
  25. {
  26.     ///TODO: Initialize hardware etc here
  27.  
  28.     // set dimmer channel1 to output 0
  29.     act_dimmer230_CHAN1_PORT &= ~_BV(act_dimmer230_CHAN1_BIT);
  30.     act_dimmer230_CHAN1_DDR |= _BV(act_dimmer230_CHAN1_BIT);
  31.     // set zero cross detection to input, no pullup
  32.     act_dimmer230_ZC_PORT &= ~_BV(act_dimmer230_ZC_BIT);
  33.     act_dimmer230_ZC_DDR &= ~_BV(act_dimmer230_ZC_BIT);
  34.    
  35.    
  36.     TIMSK1=0;                           //disable timer interrupt
  37.     TCCR1A=0;
  38.     TCCR1B=(1<<CS11);                   //enable timer, set to prescaler 8, must be changed if clk is changed
  39.    
  40.     //wait for high (ac leaving zero cross)
  41.     while (!(act_dimmer230_ZC_PIN&(1<<act_dimmer230_ZC_BIT))) { }
  42.     //wait for low (ac approaching zero cross)
  43.     while (act_dimmer230_ZC_PIN&(1<<act_dimmer230_ZC_BIT)) { }
  44.     uint16_t timeAtFall = TCNT1;    //Timer_GetTicks();
  45.     //wait for high (ac leaving zero cross)
  46.     while (!(act_dimmer230_ZC_PIN&(1<<act_dimmer230_ZC_BIT))) { }
  47.     uint16_t timeAtRise = TCNT1;    //Timer_GetTicks();
  48.  
  49.     //setup interrupt on zerocross, pcint
  50.     PCMSK0=(1<<act_dimmer230_ZC_PCINT_BIT);
  51.     PCIFR=(1<<act_dimmer230_ZC_PCIF);   //clear any pending interrupt before enabling interrupts
  52.     PCICR=(1<<act_dimmer230_ZC_PCIE);   //enable interrupt for PCINT
  53. }
  54.  
  55. void act_dimmer230_Process(void)
  56. {
  57.     ///TODO: Stuff that needs doing is done here
  58. }
  59.  
  60. void act_dimmer230_HandleMessage(StdCan_Msg_t *rxMsg)
  61. {
  62.     if (    StdCan_Ret_class(rxMsg->Header) == CAN_CLASS_MODULE_DEF && ///TODO: Change this to the actual class type
  63.         StdCan_Ret_direction(rxMsg->Header) == DIR_TO_OWNER &&
  64.         rxMsg->Header.ModuleType == CAN_TYPE_MODULE_def_default && ///TODO: Change this to the actual module type
  65.         rxMsg->Header.ModuleId == act_dimmer230_ID)
  66.     {
  67.         switch (rxMsg->Header.Command)
  68.         {
  69.         case 0:
  70.         ///TODO: Do something dummy
  71.         break;
  72.         }
  73.     }
  74. }
  75.  
  76. void act_dimmer230_List(uint8_t ModuleSequenceNumber)
  77. {
  78.     StdCan_Msg_t txMsg;
  79.    
  80.     StdCan_Set_class(txMsg.Header, CAN_CLASS_MODULE_DEF); ///TODO: Change this to the actual class type
  81.     StdCan_Set_direction(txMsg.Header, DIR_FROM_OWNER);
  82.     txMsg.Header.ModuleType = CAN_TYPE_MODULE_def_default; ///TODO: Change this to the actual module type
  83.     txMsg.Header.ModuleId = act_dimmer230_ID;
  84.     txMsg.Header.Command = CAN_CMD_MODULE_NMT_LIST;
  85.     txMsg.Length = 6;
  86.  
  87.     txMsg.Data[0] = NODE_HW_ID_BYTE0;
  88.     txMsg.Data[1] = NODE_HW_ID_BYTE1;
  89.     txMsg.Data[2] = NODE_HW_ID_BYTE2;
  90.     txMsg.Data[3] = NODE_HW_ID_BYTE3;
  91.    
  92.     txMsg.Data[4] = NUMBER_OF_MODULES;
  93.     txMsg.Data[5] = ModuleSequenceNumber;
  94.    
  95.     StdCan_Put(&txMsg);
  96. }
  97.