Neues Problem:
Ich möchte, dass der Sensor in der Mitte auf dem Servo sich wärend des fahrens dreht, und in 3 Stellungen eine Messung macht. Jedoch kann die MCU ja immer nur eine Sache pro Takt machen, desshalb wärden die anderen 3 Sensoren einfach ignoriert -> der Bot fährt überall rein.
Ich habe den Code mal gepastet:
http://rafb.net/p/pHGMMB69.html
Code:
#include <avr/io.h>
#include <stdlib.h>
#include <inttypes.h>
#include <avr/interrupt.h>
#include <util/delay.h>
volatile unsigned char servopos0, p=0;
#define SERVO_0_PIN 7
#define SERVOPORT PORTC
#define DDRSERVO DDRC
#ifndef F_CPU
#define F_CPU 8000000UL
#endif
void servo_init(void)
{
TIMSK|=(1<<OCIE2); // Interrupt gestatten
TCCR2 |= (1<<WGM21) | (1<<CS20); // CTC-Mode, no prescaling (= wäre sicherer)
OCR2 = F_CPU/100000; // 100000kHz
DDRSERVO |= (1<<SERVO_0_PIN);
}
ISR(TIMER2_COMP_vect)
{
char cSREG;
cSREG = SREG;
static int scount;
if(scount>servopos0)SERVOPORT&=~(1<<SERVO_0_PIN);
else SERVOPORT|=(1<<SERVO_0_PIN);
if(scount<2000)scount++;
else {scount=0; if(p) p--;}
SREG = cSREG;
}
uint16_t readADC(uint8_t channel)
{
uint16_t result;
ADCSRA = (1<<ADEN) | (1<<ADPS2) | (1<<ADPS1); // ADC einschalten, prescal /64
ADMUX = channel; // Kanal wählen
//ADMUX |= (1<<REFS1) | (1<<REFS0); // + interne 2,56V-Referenz
ADMUX |= (0<<REFS1) | (1<<REFS0); // + 5V-Referenz
ADCSRA |= (1<<ADSC); // Konvertierung starten
while(ADCSRA & (1<<ADSC)); // warten bis Wandlung abgeschlossen
result= ADC; // Ergebnis zwischspeichern
ADCSRA &= ~(1<<ADEN); // ADC ausschalten
return(result); // Ergebnis abliefern
}
/***************************Hauptprogramm***************************/
int main (void)
{
DDRD = 0xFF;
PORTA = 0b00001111;
uint16_t result0 = 0;
uint16_t result1 = 0;
uint16_t result2 = 0;
uint16_t result3 = 0;
servo_init();
sei();
while (1)
{
servopos0 = 90;
result0= readADC(4);
result1= readADC(5);
result2= readADC(6);
result3= readADC(7);
_delay_ms(200);
if(result0 >=200)
{
PORTD |= (1<<PD5);
PORTA |= ((1<<PA0) | (1<<PA1) | (1<<PA2) | (1<<PA3));
_delay_ms (100);
PORTA |= ((1<<PA0) | (1<<PA2));
PORTA &=~ ((1<<PA1) | (1<<PA3));
_delay_ms (100);
PORTA |= ((1<<PA0) | (1<<PA1));
_delay_ms(100);
}
else
{
PORTD |= (1<<PD5);
PORTA |= ((1<<PA1) | (1<<PA3));
PORTA &=~ ((1<<PA0) | (1<<PA2));
}
if(result1 >=200)
{
PORTD |= (1<<PD5);
PORTA |= ((1<<PA0) | (1<<PA1) | (1<<PA2) | (1<<PA3));
_delay_ms (100);
PORTA |= ((1<<PA0) | (1<<PA2));
PORTA &=~ ((1<<PA1) | (1<<PA3));
_delay_ms (100);
PORTA |= ((1<<PA0) | (1<<PA1));
_delay_ms(100);
}
else
{
PORTD |= (1<<PD5);
PORTA |= ((1<<PA1) | (1<<PA3));
PORTA &=~ ((1<<PA0) | (1<<PA2));
}
if(result2 >=200)
{
PORTD |= (1<<PD5);
PORTA |= ((1<<PA0) | (1<<PA1) | (1<<PA2) | (1<<PA3));
_delay_ms (100);
PORTA |= ((1<<PA0) | (1<<PA2));
PORTA &=~ ((1<<PA1) | (1<<PA3));
_delay_ms (100);
PORTA |= ((1<<PA0) | (1<<PA1));
_delay_ms(100);
}
else
{
PORTD |= (1<<PD5);
PORTA |= ((1<<PA1) | (1<<PA3));
PORTA &=~ ((1<<PA0) | (1<<PA2));
}
servopos0 = 60;
result0= readADC(4);
result1= readADC(5);
result2= readADC(6);
result3= readADC(7);
_delay_ms(200);
if(result0 >=200)
{
PORTD |= (1<<PD5);
PORTA |= ((1<<PA0) | (1<<PA1) | (1<<PA2) | (1<<PA3));
_delay_ms (100);
PORTA |= ((1<<PA0) | (1<<PA2));
PORTA &=~ ((1<<PA1) | (1<<PA3));
_delay_ms (100);
PORTA |= ((1<<PA0) | (1<<PA1));
_delay_ms(100);
}
else
{
PORTD |= (1<<PD5);
PORTA |= ((1<<PA1) | (1<<PA3));
PORTA &=~ ((1<<PA0) | (1<<PA2));
}
if(result1 >=200)
{
PORTD |= (1<<PD5);
PORTA |= ((1<<PA0) | (1<<PA1) | (1<<PA2) | (1<<PA3));
_delay_ms (100);
PORTA |= ((1<<PA0) | (1<<PA2));
PORTA &=~ ((1<<PA1) | (1<<PA3));
_delay_ms (100);
PORTA |= ((1<<PA0) | (1<<PA1));
_delay_ms(100);
}
else
{
PORTD |= (1<<PD5);
PORTA |= ((1<<PA1) | (1<<PA3));
PORTA &=~ ((1<<PA0) | (1<<PA2));
}
if(result2 >=200)
{
PORTD |= (1<<PD5);
PORTA |= ((1<<PA0) | (1<<PA1) | (1<<PA2) | (1<<PA3));
_delay_ms (100);
PORTA |= ((1<<PA0) | (1<<PA2));
PORTA &=~ ((1<<PA1) | (1<<PA3));
_delay_ms (100);
PORTA |= ((1<<PA0) | (1<<PA1));
_delay_ms(100);
}
else
{
PORTD |= (1<<PD5);
PORTA |= ((1<<PA1) | (1<<PA3));
PORTA &=~ ((1<<PA0) | (1<<PA2));
}
servopos0 = 150;
result0= readADC(4);
result1= readADC(5);
result2= readADC(6);
result3= readADC(7);
_delay_ms(200);
if(result0 >=200)
{
PORTD |= (1<<PD5);
PORTA |= ((1<<PA0) | (1<<PA1) | (1<<PA2) | (1<<PA3));
_delay_ms (100);
PORTA |= ((1<<PA0) | (1<<PA2));
PORTA &=~ ((1<<PA1) | (1<<PA3));
_delay_ms (100);
PORTA |= ((1<<PA0) | (1<<PA1));
_delay_ms(100);
}
else
{
PORTD |= (1<<PD5);
PORTA |= ((1<<PA1) | (1<<PA3));
PORTA &=~ ((1<<PA0) | (1<<PA2));
}
if(result1 >=200)
{
PORTD |= (1<<PD5);
PORTA |= ((1<<PA0) | (1<<PA1) | (1<<PA2) | (1<<PA3));
_delay_ms (100);
PORTA |= ((1<<PA0) | (1<<PA2));
PORTA &=~ ((1<<PA1) | (1<<PA3));
_delay_ms (100);
PORTA |= ((1<<PA0) | (1<<PA1));
_delay_ms(100);
}
else
{
PORTD |= (1<<PD5);
PORTA |= ((1<<PA1) | (1<<PA3));
PORTA &=~ ((1<<PA0) | (1<<PA2));
}
if(result2 >=200)
{
PORTD |= (1<<PD5);
PORTA |= ((1<<PA0) | (1<<PA1) | (1<<PA2) | (1<<PA3));
_delay_ms (100);
PORTA |= ((1<<PA0) | (1<<PA2));
PORTA &=~ ((1<<PA1) | (1<<PA3));
_delay_ms (100);
PORTA |= ((1<<PA0) | (1<<PA1));
_delay_ms(100);
}
else
{
PORTD |= (1<<PD5);
PORTA |= ((1<<PA1) | (1<<PA3));
PORTA &=~ ((1<<PA0) | (1<<PA2));
}
}
}
//////////
/*
Servopos: Mitte = 90
Links = 150
Rechts = 60
*/
//////////
Hat jmd. eine bessere Idee?
Mfg JeyBee
Lesezeichen