Subversion Repositories HomeAutomation

Rev

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