108 lines
2.3 KiB
C
108 lines
2.3 KiB
C
/*
|
|
* Steuerung.c
|
|
*
|
|
* Created: 24.03.2013 14:27:03
|
|
* Author: netz
|
|
*/
|
|
#define F_CPU 9600000
|
|
|
|
#define LEFT 600
|
|
#define RIGHT 202
|
|
#define STEP 8
|
|
|
|
#define ADC_VREF_TYPE ((0<<REFS0) | (1<<ADLAR))
|
|
|
|
#include <avr/io.h>
|
|
#include <util/delay.h>
|
|
#include <avr/interrupt.h>
|
|
|
|
volatile int servopos = 0;
|
|
|
|
void led(int on) {
|
|
switch(on) {
|
|
case 1:
|
|
PORTB |= (1<<PB3);
|
|
break;
|
|
default:
|
|
PORTB &= ~(1<<PB3);
|
|
}
|
|
}
|
|
|
|
unsigned char read_adc(unsigned char adc_input)
|
|
{
|
|
ADMUX=adc_input | ADC_VREF_TYPE;
|
|
// Delay needed for the stabilization of the ADC input voltage
|
|
_delay_us(10);
|
|
// Start the AD conversion
|
|
ADCSRA|=(1<<ADSC);
|
|
// Wait for the AD conversion to complete
|
|
while ((ADCSRA & (1<<ADIF))==0);
|
|
ADCSRA|=(1<<ADIF);
|
|
return ADCH;
|
|
}
|
|
|
|
void init_timer() {
|
|
// Timer/Counter 0 initialization
|
|
// Clock source: System Clock
|
|
// Clock value: 9,375 kHz
|
|
// Mode: Normal top=0xFF
|
|
// OC0A output: Disconnected
|
|
// OC0B output: Disconnected
|
|
// Timer Period: 21,76 ms
|
|
TCCR0A=(0<<COM0A1) | (0<<COM0A0) | (0<<COM0B1) | (0<<COM0B0) | (0<<WGM01) | (0<<WGM00);
|
|
TCCR0B=(0<<WGM02) | (1<<CS02) | (0<<CS01) | (1<<CS00);
|
|
TCNT0=0x34;
|
|
OCR0A=0x00;
|
|
OCR0B=0x00;
|
|
|
|
// Timer/Counter 0 Interrupt(s) initialization
|
|
TIMSK0=(0<<OCIE0B) | (0<<OCIE0A) | (1<<TOIE0);
|
|
}
|
|
|
|
void init_adc() {
|
|
// ADC initialization
|
|
// ADC Clock frequency: 600,000 kHz
|
|
// ADC Bandgap Voltage Reference: On
|
|
// ADC Auto Trigger Source: ADC Stopped
|
|
// Only the 8 most significant bits of
|
|
// the AD conversion result are used
|
|
// Digital input buffers on ADC0: On, ADC1: On, ADC2: On, ADC3: On
|
|
DIDR0=(0<<ADC0D) | (0<<ADC2D) | (0<<ADC3D) | (0<<ADC1D);
|
|
ADMUX=ADC_VREF_TYPE;
|
|
ADCSRA=(1<<ADEN) | (0<<ADSC) | (0<<ADATE) | (0<<ADIF) | (0<<ADIE) | (1<<ADPS2) | (0<<ADPS1) | (0<<ADPS0);
|
|
ADCSRB=(0<<ADTS2) | (0<<ADTS1) | (0<<ADTS0);
|
|
}
|
|
|
|
// Timer 0 overflow interrupt service routine
|
|
ISR(TIM0_OVF_vect)
|
|
{
|
|
// Reinitialize Timer 0 value
|
|
TCNT0=0x34;
|
|
|
|
PORTB |= (1<<PB1);
|
|
_delay_us(LEFT);
|
|
for(int i=0;i<servopos;i++) {
|
|
_delay_us(STEP);
|
|
}
|
|
PORTB &= ~(1<<PB1);
|
|
}
|
|
|
|
int main(void)
|
|
{
|
|
DDRB |= (1<<PB3) | (1<<PB1);
|
|
init_timer();
|
|
init_adc();
|
|
sei();
|
|
while(1)
|
|
{
|
|
led(1);
|
|
int pos = read_adc((1<<MUX1));
|
|
pos = pos;
|
|
if(pos > RIGHT) {
|
|
pos = RIGHT;
|
|
}
|
|
led(0);
|
|
servopos = pos;
|
|
//_delay_ms(500);
|
|
}
|
|
} |