Hi,
ich arbeite immer noch sehr fleisig an meinen Smartinius und versuche nun schon seit einiger
Zeit an der Sensor auslesung via ADC aber das will nicht recht klappen.
Ich glaube es liegt daran das ich mit Kommerzahlen rechen.
Zur der Brechnung kurz
Die Entfernung ist proprtional zur ausgegebenen Spannung =>
D = A/(X-B)
D ist die Entfernung X ist der Ausgabewert des Sensors A ist die Steigung der Kurve A/X B ist der Offset der Kurve
Da ich bei beiden Sensoren leichte Unterschiede messe habe ich B auf links und rechts aufgeteilt.
BL und BR.
Für das ADC benutzte ich AVCC und einen Vorteiler von 64 ich hoffe
der Rest ist aus dem leicht kommentierten Code zu erkennen
mfG _Martinius_Code:/* * SMARTINIUS.c * * Created: 26.07.2011 00:17:22 * Author: Martinius */ #define F_CPU 16000000 #include <avr/io.h> #include <util/delay.h> #include <avr/interrupt.h> const ON = 1; //andere Werte const OFF = 0; const Left = 2; const Right = 1; const Enable = 1; const Disable = 0; const Reset = 2; #define A 11.925 #define BL 0.660 #define BR 0.610 #define ADCconst 00.04883 void Setup(void){ // Stellt den Controller für das nachfolgende Programm ein // muss immer als erstes aufgerufen werden. DDRC |= (1<<PC7); // Status LED DDRB |= (1<<PB0); // IR_Eable DDRA &= ~(1<<PA0); // IR_Left DDRA &= ~(1<<PA1); // IR_Right ADMUX |= (1<<REFS0);//Reverrenzspannungsquelle des ADC wird festgelegt ADCSRA = (1<<ADPS1) | (1<<ADPS2); // Frequenzvorteiler ADCSRA |= (1<<ADEN); // ADC aktivieren ADCSRA |= (1<<ADSC); // eine ADC-Wandlung while (ADCSRA & (1<<ADSC) ) {} // auf Abschluss der Konvertierung warten } void Status_LED(uint8_t Status){ if(Status == 1){ PORTC |= (1<<PC7); } if(Status == 0){ PORTC &= ~(1<<PC7); } } uint16_t get_Ir_Distance (uint8_t Dir){ // Distance in mm if(Dir==1){//Right uint16_t x; ADMUX = (ADMUX & ~(0x1F)) | (1 & 0x1F); ADCSRA |= (1<<ADSC); // eine Wandlung "single conversion" while (ADCSRA & (1<<ADSC) ) {} // auf Abschluss der Konvertierung warten x = ADCW; x = x*ADCconst; x = x-BL; x = A/x; x = x*10 return x; // ADC auslesen und zurückgeben } if(Dir==2){//Left uint16_t x; ADMUX = (ADMUX & ~(0x1F)) | (0 & 0x1F); ADCSRA |= (1<<ADSC); // eine Wandlung "single conversion" while (ADCSRA & (1<<ADSC) ) {} // auf Abschluss der Konvertierung warten x = ADCW; x = x*ADCconst; x = x-BL; x = A/x; x = x*10; return x; } } void Ir_Enable(uint8_t Status){ if (Status == 1){ PORTB |= (1<<PB0); } if(Status == 0){ PORTB &= ~(1<<PB0); } } int main(void) { Setup(); Ir_Enable(ON); while(1){ drive_ahead(); if(get_Ir_Distance(Right)<200){ Status_LED(ON); } if(get_Ir_Distance(Left)< 200){ Status_LED(ON); } } }







Zitieren

Lesezeichen