Advertisement
dlwestab

relay_timer

Mar 15th, 2011
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.87 KB | None | 0 0
  1.  
  2. /*
  3. Copyright 2011 Dustin L. Westaby
  4.  
  5. ----------------------------------------------------------------------
  6.         Timed Relay Control for ATtiny2313        
  7. ----------------------------------------------------------------------
  8. Title:      relay_timer.c
  9. Author:     Dustin Westaby
  10. Date Created:   03/06/11
  11. Last Modified:  03/15/11
  12. Purpose:  Control program for relays. See included sch file for
  13.           circuit info. Timing is input on DIP switches with 30ms
  14.           resolution. From 0 to 420ms.
  15. Note: Relays are connected for NC operation.  Program only engages
  16.       relays for a brief period of time.
  17.  
  18. Compiled with AVR-GCC WinAVR
  19.  
  20. Revisions List:
  21. 03/06/11  Initial program
  22. 03/07/11  Added input to start program
  23. 03/10/11  Added timing select with 4pos DIP switch
  24. 03/15/11  Small modifications for final circuit
  25.  
  26. ----------------------------------------------------------------------
  27. */
  28.  
  29. //--------------------------------------
  30. //          Global Variables           |
  31. //--------------------------------------
  32. #define F_CPU 8000000UL  /* 8 MHz Internal Oscillator */
  33.  
  34. enum { ON, OFF };
  35.  
  36. //--------------------------------------
  37. //              Includes               |
  38. //--------------------------------------
  39. #include <avr/io.h>
  40. #include <util/delay.h>
  41. #include <inttypes.h>
  42.  
  43. //--------------------------------------
  44. //          Delay Subroutines          |
  45. //--------------------------------------
  46. void delay_ms(uint16_t ms) {
  47.     while ( ms )
  48.     {
  49.         _delay_ms(1);
  50.         ms--;
  51.     }
  52. }
  53.  
  54. void delay_us(uint16_t us) {
  55.     while ( us )
  56.     {
  57.         _delay_us(1);
  58.         us--;
  59.     }
  60. }
  61.  
  62. //--------------------------------------
  63. //          Relay Subroutines          |
  64. //--------------------------------------
  65. void relay1(int command)
  66. {
  67.     if (command==ON)
  68.         PORTB = (1<<PB0);
  69.     else
  70.         PORTB = (0<<PB0);
  71.  
  72. }
  73.  
  74. void relay2(int command)
  75. {
  76.     //PD3 is LED indicator
  77.     if (command==ON)
  78.         PORTD = (1<<PD5)|(1<<PD3);
  79.     else
  80.         PORTD = (0<<PD5)|(0<<PD3);
  81.  
  82. }
  83.  
  84. int button_is_pressed()
  85. {
  86.  
  87.     // the button is pressed when bit is clear
  88.     if (bit_is_clear(PIND, PD4))
  89.     {
  90.         delay_us(800);
  91.         if (bit_is_clear(PIND, PD4)) return 1;
  92.     }
  93.  
  94.     return 0;
  95. }
  96.  
  97. int get_timer_selection()
  98. {
  99.     //resolution of 4 bit DIP selection is 30ms
  100.     //30ms resolution was chosen due to the accuracy limitations of the AVR's RC oscilator clock
  101.     int resolution;
  102.     int timer_number;
  103.     resolution = 30;
  104.     timer_number = 0;
  105.  
  106.     //0000 = 0
  107.     //0001 = 30*
  108.     //0010 = 60*
  109.     //0011 = 90
  110.     //0100 = 120*
  111.     //0101 = 150
  112.     //0110 = 180
  113.     //0111 = 210
  114.     //1000 = 240*
  115.     //1001 = 270
  116.     //1010 = 300
  117.     //1011 = 330
  118.     //1100 = 360
  119.     //1101 = 390
  120.     //1110 = 420
  121.     //1111 = 450
  122.  
  123.     // the switch is ON when bit is clear (grounded)
  124.     if (bit_is_clear(PINB, PB1))
  125.         timer_number+=resolution;
  126.  
  127.     if (bit_is_clear(PINB, PB2))
  128.         timer_number+=resolution*2;
  129.  
  130.     if (bit_is_clear(PINB, PB3))
  131.         timer_number+=resolution*4;
  132.  
  133.     if (bit_is_clear(PINB, PB4))
  134.         timer_number+=resolution*8;
  135.  
  136.     return timer_number;
  137. }
  138.  
  139. void ioinit (void)
  140. {
  141.  
  142.     //PD4 is an input and requires a pull up resistor
  143.     //PB1, PB2, PB3, PB4 are inputs and require pull-up resistors
  144.     //All others outputs
  145.     DDRB =  0b11100001;
  146.     PORTB = 0b00011110;
  147.     DDRD =  0b11101111;
  148.     PORTD = 0b00010000;
  149.     DDRA =  0b11111111;
  150.     PORTA = 0b00000000;
  151.  
  152. }
  153.  
  154. //--------------------------------------
  155. //               Main                  |
  156. //--------------------------------------
  157. int main (void)
  158. {
  159.    
  160.     ioinit ();
  161.  
  162.  
  163.     //start with relays off
  164.     //relays are connected for NC operation, relays off = power on
  165.     relay1(OFF);
  166.     relay2(OFF);
  167.    
  168.     while(1)
  169.     {
  170.  
  171.         //wait for button press to start
  172.         while (!button_is_pressed());
  173.        
  174.         //run instant power program
  175.         relay1(ON);
  176.         relay2(ON);
  177.         delay_ms(get_timer_selection());
  178.         relay1(OFF);
  179.         relay2(OFF);
  180.        
  181.         //don't let program run again for 5 seconds
  182.         delay_ms(5000);
  183.  
  184.     }
  185.  
  186.  
  187.     while(1);                       // Ending infinite loop
  188.  
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement