Subversion Repositories HomeAutomation

Rev

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

  1.  
  2. #include "act_RGBdriver.h"
  3. #define ALGO_FREQ       5
  4. #define TARGET_CURRENT  250UL
  5. #define TARGET_ADC      TARGET_CURRENT*4UL*1024UL/5000UL
  6.  
  7. #define TARGET_CURRENT2 50UL
  8. #define TARGET_ADC2     TARGET_CURRENT2*4UL*3UL*1024UL/5000UL
  9.  
  10. #define TARGET_CURRENT3 50UL
  11. #define TARGET_ADC3     TARGET_CURRENT3*4UL*3UL*1024UL/5000UL
  12.  
  13. #define TARGET_CURRENT4 50UL
  14. #define TARGET_ADC4     TARGET_CURRENT4*4UL*3UL*1024UL/5000UL
  15.  
  16. /*
  17.  
  18. 50mA*0.1R = 5mV
  19. 5mV*40ggr = 200mV
  20. 200mV*1024/5V = 41
  21.  
  22. 50mA*0.1R = 5mV
  23. 5mV*40ggr = 200mV
  24. 50mA*0.1*40*1024/5 = 41
  25. 50mA*4*1024/5 = 41
  26. */
  27.  
  28. void calculatePWM(uint8_t timer) {
  29.     uint16_t adcValue;
  30.     adcValue = ADC_Get(5);
  31.  
  32.     if (adcValue > TARGET_ADC+5)
  33.         OCR1B -=1;
  34.     else if (adcValue < TARGET_ADC)
  35.         OCR1B++;
  36.     else if (adcValue > TARGET_ADC+10)
  37.         OCR1B=0;
  38.  
  39.  
  40.     adcValue = ADC_Get(4);
  41.  
  42.     if (adcValue > TARGET_ADC2+5)
  43.         OCR1A -=1;
  44.     else if (adcValue < TARGET_ADC2)
  45.         OCR1A++;
  46.     else if (adcValue > TARGET_ADC2+10)
  47.         OCR1A=0;
  48.  
  49.  
  50.     adcValue = ADC_Get(2);
  51.  
  52.     if (adcValue > TARGET_ADC3+5)
  53.         OCR0A -=1;
  54.     else if (adcValue < TARGET_ADC3)
  55.         OCR0A++;
  56.     else if (adcValue > TARGET_ADC3+10)
  57.         OCR0A=0;
  58.  
  59.  
  60.     adcValue = ADC_Get(0);
  61.  
  62.     if (adcValue > TARGET_ADC4+5)
  63.         OCR0B -=1;
  64.     else if (adcValue < TARGET_ADC4)
  65.         OCR0B++;
  66.     else if (adcValue > TARGET_ADC4+10)
  67.         OCR0B=0;
  68.  
  69. }
  70.  
  71. void sendADCvalue(uint8_t timer) {
  72.     StdCan_Msg_t txMsg;
  73.     StdCan_Set_class(txMsg.Header, CAN_CLASS_MODULE_ACT);
  74.     StdCan_Set_direction(txMsg.Header, DIR_FROM_OWNER);
  75.     txMsg.Header.ModuleType = CAN_TYPE_MODULE_act_RGBdriver;
  76.     txMsg.Header.ModuleId = act_RGBdriver_ID;
  77.     txMsg.Header.Command = CAN_CMD_MODULE_NMT_LIST;
  78.     txMsg.Length = 6;
  79.  
  80.     //txMsg.Data[0] = (uint8_t)(ADC_Get(5)>>2);
  81.     txMsg.Data[0] = (uint8_t)(ADC_Get(5)&0xff);
  82.     txMsg.Data[1] = 0;
  83.     txMsg.Data[2] = OCR1B;
  84.     txMsg.Data[3] = 0;
  85.    
  86.     txMsg.Data[4] = TARGET_CURRENT;
  87.     txMsg.Data[5] = TARGET_ADC;
  88.    
  89.     StdCan_Put(&txMsg);
  90. }
  91.  
  92. void act_RGBdriver_Init(void)
  93. {
  94.     ADC_Init();
  95.    
  96.     //DDRB &= ~(1<<PB1);
  97.     //DDRB &= ~(1<<PB2);    //never set PB2 as input
  98.     TCCR1A = 0;
  99.     TCCR1B = 0;
  100.     TCCR1A |= (1 << WGM10) | (0 << WGM11);  //8-bit pwm with top=0xff
  101.     TCCR1B |= (1 << WGM12);     //fast pwm
  102.     cli();
  103.     OCR1A = 0;  // set on time (tp)
  104.     OCR1B = 0;  // set on time (tp)
  105.     sei();
  106.     DDRB |= _BV(PB2);           //set port to output
  107.     TCCR1A |= (1<<COM1A1);      //enable output compare
  108.     DDRB |= _BV(PB1);           //set port to output
  109.     TCCR1A |= (1<<COM1B1);      //enable output compare
  110.  
  111.     //cli();
  112.     //OCR1A = 10;  // set on time (tp)
  113.     //OCR1B = 40;  // set on time (tp)
  114.     //sei();
  115.  
  116.     TCCR1B |= (1 << CS10);      //enable timer, prescaler=1
  117.  
  118.  
  119.     // Contrast
  120.     TCCR0A |= (1<<WGM01)|(1<<WGM00);
  121.     TCCR0B |= (1<<CS00);
  122.     cli();
  123.     OCR0A = 0;  // set on time (tp)
  124.     OCR0B = 0;  // set on time (tp)
  125.     sei();
  126.     DDRD |= (1<<DDD5);
  127.     DDRD |= (1<<DDD6);
  128.     TCCR0A |= (1<<COM0B1)|(1<<COM0A1);
  129.  
  130.     Timer_SetTimeout(act_RGBdriver_ADC_SEND_TIMER, 2000, TimerTypeFreeRunning, sendADCvalue);
  131.     Timer_SetTimeout(act_RGBdriver_REGULATOR_TIMER, ALGO_FREQ, TimerTypeFreeRunning, calculatePWM);
  132. }
  133.  
  134. void act_RGBdriver_Process(void)
  135. {
  136.     ///TODO: Stuff that needs doing is done here
  137. }
  138.  
  139. void act_RGBdriver_HandleMessage(StdCan_Msg_t *rxMsg)
  140. {
  141.     if (    StdCan_Ret_class(rxMsg->Header) == CAN_CLASS_MODULE_ACT &&
  142.         StdCan_Ret_direction(rxMsg->Header) == DIR_TO_OWNER &&
  143.         rxMsg->Header.ModuleType == CAN_TYPE_MODULE_act_RGBdriver &&
  144.         rxMsg->Header.ModuleId == act_RGBdriver_ID)
  145.     {
  146.         switch (rxMsg->Header.Command)
  147.         {
  148.         case 0:
  149.         ///TODO: Do something dummy
  150.         break;
  151.         }
  152.     }
  153. }
  154.  
  155. void act_RGBdriver_List(uint8_t ModuleSequenceNumber)
  156. {
  157.     StdCan_Msg_t txMsg;
  158.    
  159.     StdCan_Set_class(txMsg.Header, CAN_CLASS_MODULE_ACT);
  160.     StdCan_Set_direction(txMsg.Header, DIR_FROM_OWNER);
  161.     txMsg.Header.ModuleType = CAN_TYPE_MODULE_act_RGBdriver;
  162.     txMsg.Header.ModuleId = act_RGBdriver_ID;
  163.     txMsg.Header.Command = CAN_CMD_MODULE_NMT_LIST;
  164.     txMsg.Length = 6;
  165.  
  166.     txMsg.Data[0] = NODE_HW_ID_BYTE0;
  167.     txMsg.Data[1] = NODE_HW_ID_BYTE1;
  168.     txMsg.Data[2] = NODE_HW_ID_BYTE2;
  169.     txMsg.Data[3] = NODE_HW_ID_BYTE3;
  170.    
  171.     txMsg.Data[4] = NUMBER_OF_MODULES;
  172.     txMsg.Data[5] = ModuleSequenceNumber;
  173.    
  174.     StdCan_Put(&txMsg);
  175. }
  176.