Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Copyright 2011 Dustin L. Westaby
- ----------------------------------------------------------------------
- Timed Relay Control for ATtiny2313
- ----------------------------------------------------------------------
- Title: relay_timer.c
- Author: Dustin Westaby
- Date Created: 03/06/11
- Last Modified: 03/15/11
- Purpose: Control program for relays. See included sch file for
- circuit info. Timing is input on DIP switches with 30ms
- resolution. From 0 to 420ms.
- Note: Relays are connected for NC operation. Program only engages
- relays for a brief period of time.
- Compiled with AVR-GCC WinAVR
- Revisions List:
- 03/06/11 Initial program
- 03/07/11 Added input to start program
- 03/10/11 Added timing select with 4pos DIP switch
- 03/15/11 Small modifications for final circuit
- ----------------------------------------------------------------------
- */
- //--------------------------------------
- // Global Variables |
- //--------------------------------------
- #define F_CPU 8000000UL /* 8 MHz Internal Oscillator */
- enum { ON, OFF };
- //--------------------------------------
- // Includes |
- //--------------------------------------
- #include <avr/io.h>
- #include <util/delay.h>
- #include <inttypes.h>
- //--------------------------------------
- // Delay Subroutines |
- //--------------------------------------
- void delay_ms(uint16_t ms) {
- while ( ms )
- {
- _delay_ms(1);
- ms--;
- }
- }
- void delay_us(uint16_t us) {
- while ( us )
- {
- _delay_us(1);
- us--;
- }
- }
- //--------------------------------------
- // Relay Subroutines |
- //--------------------------------------
- void relay1(int command)
- {
- if (command==ON)
- PORTB = (1<<PB0);
- else
- PORTB = (0<<PB0);
- }
- void relay2(int command)
- {
- //PD3 is LED indicator
- if (command==ON)
- PORTD = (1<<PD5)|(1<<PD3);
- else
- PORTD = (0<<PD5)|(0<<PD3);
- }
- int button_is_pressed()
- {
- // the button is pressed when bit is clear
- if (bit_is_clear(PIND, PD4))
- {
- delay_us(800);
- if (bit_is_clear(PIND, PD4)) return 1;
- }
- return 0;
- }
- int get_timer_selection()
- {
- //resolution of 4 bit DIP selection is 30ms
- //30ms resolution was chosen due to the accuracy limitations of the AVR's RC oscilator clock
- int resolution;
- int timer_number;
- resolution = 30;
- timer_number = 0;
- //0000 = 0
- //0001 = 30*
- //0010 = 60*
- //0011 = 90
- //0100 = 120*
- //0101 = 150
- //0110 = 180
- //0111 = 210
- //1000 = 240*
- //1001 = 270
- //1010 = 300
- //1011 = 330
- //1100 = 360
- //1101 = 390
- //1110 = 420
- //1111 = 450
- // the switch is ON when bit is clear (grounded)
- if (bit_is_clear(PINB, PB1))
- timer_number+=resolution;
- if (bit_is_clear(PINB, PB2))
- timer_number+=resolution*2;
- if (bit_is_clear(PINB, PB3))
- timer_number+=resolution*4;
- if (bit_is_clear(PINB, PB4))
- timer_number+=resolution*8;
- return timer_number;
- }
- void ioinit (void)
- {
- //PD4 is an input and requires a pull up resistor
- //PB1, PB2, PB3, PB4 are inputs and require pull-up resistors
- //All others outputs
- DDRB = 0b11100001;
- PORTB = 0b00011110;
- DDRD = 0b11101111;
- PORTD = 0b00010000;
- DDRA = 0b11111111;
- PORTA = 0b00000000;
- }
- //--------------------------------------
- // Main |
- //--------------------------------------
- int main (void)
- {
- ioinit ();
- //start with relays off
- //relays are connected for NC operation, relays off = power on
- relay1(OFF);
- relay2(OFF);
- while(1)
- {
- //wait for button press to start
- while (!button_is_pressed());
- //run instant power program
- relay1(ON);
- relay2(ON);
- delay_ms(get_timer_selection());
- relay1(OFF);
- relay2(OFF);
- //don't let program run again for 5 seconds
- delay_ms(5000);
- }
- while(1); // Ending infinite loop
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement