Gebruik van een servo sturing voor het aansturen van 2 wissels die steeds samen omgelegd moeten worden.
Er wordt gebruikt gemaakt van een atmel2313 chip voor de sturing.
Programmatie met AVR studio4.
/* servo_swith
atiny2313
1 - 20 VCC
2 - 19
3 - 18
4 - 17
5 - 16 PB4 - SERVO
SWITCH BUTTON 6 - 15
7 - 14
8 - 13 PB1 - LED
9 - 12 PB0 - LED
GND 10 - 11
*/
#define F_CPU 8000000UL /* 8MHz crystal oscillator */
#define BUTTON_DDR DDRD
#define BUTTON_PORT PORTD /* PORTx - register for button output */
#define BUTTON_PIN PIND /* PINx - register for button input */
#define BUTTON_BIT PD2 /* bit for button input/output */
#define LED_DDR DDRB /* LED data direction register */
#define LED_PORT PORTB /* PORTx - register for LED output */
#define LED_BIT PB0 /* bit for button input/output */
#define LED1_BIT PB1
#define DEBOUNCE_TIME 20 /* time to wait while "de-bouncing" button */
#define LOCK_INPUT_TIME 150 /* time to wait after a button press */
/* include files */
#include <avr/io.h>
#include <inttypes.h>
#include <util/delay.h>
#define output_low(port,pin) port &= ~(1<<pin)
#define output_high(port,pin) port |= (1<<pin)
#define set_input(portdir,pin) portdir &= ~(1<<pin)
#define set_output(portdir,pin) portdir |= (1<<pin)
/* function prototypes */
void delay_ms(uint16_t ms);
void init_io();
int button_is_pressed();
void set_led_r();
void set_led_a();
void toggle_servo_a();
void toggle_servo_r();
/* variables */
int bp;
int richting;;
//int prev_richting = 1;
uint16_t servoTiming; // servo timing
/* main */
int
main (void)
{
init_io();
// default values
richting = 1; // set to rechtdoor
set_led_r();
toggle_servo_r();
while (1)
{
bp = button_is_pressed();
if ( bp == 1 ) // so button is pressed
{
if ( richting == 1 ) // was rechtdoor, moet nu afslaan
{
richting = 2; // set to afslaan
set_led_a();
toggle_servo_a();
}
else
if ( richting == 2 ) // was afslaan moet nu rechtdoor
{
richting = 1; // set to rechtdoor
set_led_r();
toggle_servo_r();
}
} // end-if-bp==0
delay_ms(LOCK_INPUT_TIME);
} // end-while
return 0;
} // end-main
/* Initialize I/O */
void
init_io()
{
DDRB |= (1 << LED_BIT); // PB0
DDRB |= (1 << LED1_BIT); // PB1
DDRB |= (1 << PB4); // PB4/OCR1B as output
/* turn on internal pull-up resistor for the switch */
BUTTON_PORT |= _BV(BUTTON_BIT);
// timer1: fast pwm, clear OC1B on match
TCCR1A = (1 << COM1B1) | (1 << WGM11) | (1 << WGM10);
// timer1: fast pwm, ctc top=OCR1A, clk/1
TCCR1B = (1 << WGM13) | (1 << WGM12) | (1 << CS10);
OCR1A = 20000; // 20ms
OCR1B = 1500; // 1.5ms
}
/* delay_ms */
void
delay_ms(uint16_t ms)
{
while ( ms )
{
_delay_ms(1);
ms--;
}
}
int
button_is_pressed()
{
/* the button is pressed when BUTTON_BIT is clear */
if (bit_is_clear(BUTTON_PIN, BUTTON_BIT))
{
delay_ms(DEBOUNCE_TIME);
if (bit_is_clear(BUTTON_PIN, BUTTON_BIT)) return 1;
}
return 0;
}
void
toggle_servo_a()
{
for(servoTiming = 1450; servoTiming < 1610; servoTiming++)
{
OCR1B = servoTiming;
_delay_ms(1);
}
}
void
toggle_servo_r()
{
// slowly turns the servo back
for(servoTiming = 1610; servoTiming > 1450; servoTiming--)
{
OCR1B = servoTiming;
_delay_ms(1);
}
}
void
set_led_a()
{
output_high(LED_PORT,LED1_BIT);
output_low(LED_PORT,LED_BIT);
}
;
void
set_led_r()
{
output_high(LED_PORT,LED_BIT);
output_low(LED_PORT,LED1_BIT);
}
Print gebouwd op gaatjesprint.
last update 16/02/2012