Subversion Repositories HomeAutomation

Rev

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

  1. #include <inttypes.h>
  2. #include <avr/interrupt.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <config.h> // All configuration parameters
  6. #include <bios.h>   // BIOS interface declarations, including CAN structure and ID defines.
  7. #include <drivers/timer/timer.h>
  8. #include <drivers/adc/adc.h>
  9.  
  10. #define APP_TYPE    0xf001
  11. #define APP_VERSION 0x0002
  12.  
  13.  
  14. #define R51 12000
  15. #define R50 47000
  16.  
  17. #if (5 * (R51 + R50))/(R51) > 64
  18.     #error "ADC_FACTOR must be less then 64, change R51 and R52"
  19. #else
  20.     #define ADC_FACTOR  (5 * (R51 + R50))/(R51)
  21.     #define ADC_SCALE   10
  22. #endif
  23.  
  24. // A simple message "queue", with space for one message only.
  25. // These are declared volatile to tell the compiler not to optimize away accesses.
  26. volatile Can_Message_t rxMsg; // Message storage
  27. volatile uint8_t rxMsgFull;   // Synchronization flag
  28.  
  29. // CAN message reception callback.
  30. // This function runs with interrupts disabled, keep it as short as possible.
  31. void can_receive(Can_Message_t *msg) {
  32.     if (!rxMsgFull) {
  33.         memcpy((void*)&rxMsg, msg, sizeof(rxMsg));
  34.         rxMsgFull = 1;
  35.     }
  36. }
  37.  
  38. // Timer callback function used for some timer tests
  39. void timer_callback(uint8_t timer) {
  40. }
  41.  
  42. int main(void)
  43. {
  44.     // Enable interrupts as early as possible
  45.     sei();
  46.    
  47.     Timer_Init();
  48.     ADC_Init();
  49.    
  50.     unsigned long time;
  51.    
  52.     Can_Message_t txMsg;
  53.     txMsg.Id = (CAN_NMT_APP_START << CAN_SHIFT_NMT_TYPE) | (NODE_ID << CAN_SHIFT_NMT_SID);
  54.     txMsg.DataLength = 4;
  55.     txMsg.RemoteFlag = 0;
  56.     txMsg.ExtendedFlag = 1;
  57.     txMsg.Data.words[0] = APP_TYPE;
  58.     txMsg.Data.words[1] = APP_VERSION;
  59.    
  60.     uint16_t reg5Vfeedback;
  61.     uint16_t currentfeedback;
  62.     uint8_t DUTconnected=0;
  63.     uint8_t DUTconnectcnt=0;
  64.  
  65.     // Set up callback for CAN reception, this is optional if only sending is required.
  66.     BIOS_CanCallback = &can_receive;
  67.     // Send CAN_NMT_APP_START
  68.     BIOS_CanSend(&txMsg);
  69.    
  70.     // Set up three timers (assume at least three has been defined)
  71.     // The timeout is specified in ticks, which is equal to ms if
  72.     // the tick frequency is set to 1000.
  73.     Timer_SetTimeout(0, 3000, TimerTypeFreeRunning, 0);
  74.     Timer_SetTimeout(1, 100, TimerTypeFreeRunning, 0);
  75.     DDRB |= (1<<PB7);
  76.    
  77.     PORTD &= ~(1<<PD7);//turn off output
  78.     DDRD |= (1<<PD7);
  79.     DDRC &= ~(1<<PC2);      //set EXP_N to input
  80.     PORTC |= (1<<PC2);      //set EXP_N to pullup
  81.  
  82.     while (1) {
  83.         if (Timer_Expired(1)) {
  84.             if (!(PINC & (1<<PC2))) {
  85.                 if (DUTconnectcnt < 10) {
  86.                     DUTconnectcnt++;
  87.                 }
  88.             } else {
  89.                 DUTconnected = 0;
  90.                 //DUTconnectcnt --;
  91.                 DUTconnectcnt = 0;
  92.             }
  93.             if (DUTconnectcnt == 10) {
  94.                 DUTconnected = 1;
  95.             } //else if (DUTconnectcnt == 0) {
  96.                 //DUTconnected = 0;
  97.             //}
  98.  
  99.             reg5Vfeedback = ADC_Get(ADREG5VFEEDBACK);
  100.             reg5Vfeedback = (reg5Vfeedback & 0x03ff) * ADC_FACTOR;  //get voltage in mV (typical 5000mV)
  101.             currentfeedback = ADC_Get(ADCURRENTFEEDBACK);
  102.             currentfeedback = (currentfeedback>>1);     //get current in mA (  cur [A] = (ad*Vcc /1024)/R, R=10  ) (typical 20mA)
  103.            
  104.             if (reg5Vfeedback > 6000 || currentfeedback > 40) {
  105.                 PORTD &= ~(1<<PD7);     //turn off output
  106.             }
  107.         }
  108.         if (Timer_Expired(0)) {
  109.             /*reg5Vfeedback = ADC_Get(ADREG5VFEEDBACK);
  110.             reg5Vfeedback = (reg5Vfeedback & 0x03ff) * ADC_FACTOR;  //get voltage in mV (typical 5000mV)
  111.             currentfeedback = ADC_Get(ADCURRENTFEEDBACK);
  112.             currentfeedback = (currentfeedback>>1);     //get current in mA (  cur [A] = (ad*Vcc /1024)/R, R=10  ) (typical 20mA)
  113. */
  114.             //PORTB ^= (1<<PB7);
  115.             //PORTD ^= (1<<PD7);    //toggle output
  116.            
  117. /*if (DUTconnected) {
  118.             PORTD |= (1<<PD7);  //toggle output
  119. }*/
  120.  
  121.             txMsg.Id = 0;
  122.             txMsg.DataLength = 6;
  123.             txMsg.RemoteFlag = 0;
  124.             txMsg.ExtendedFlag = 1;
  125.             txMsg.Data.bytes[0] = (reg5Vfeedback>>8)&0xff;
  126.             txMsg.Data.bytes[1] = reg5Vfeedback&0xff;
  127.             txMsg.Data.bytes[2] = (currentfeedback>>8)&0xff;
  128.             txMsg.Data.bytes[3] = currentfeedback&0xff;
  129.  
  130.             txMsg.Data.bytes[4] = DUTconnected;
  131.             txMsg.Data.bytes[5] = ((PORTD & (1<<PD7))>>PD7);
  132.    
  133.             // Send CAN_NMT_APP_START
  134.             BIOS_CanSend(&txMsg);
  135.         }
  136.        
  137.         if (rxMsgFull) {
  138.  
  139.             rxMsgFull = 0; //  
  140.         }
  141.     }
  142.    
  143.     return 0;
  144. }
  145.