Mam wrażenie, że ilość stanów rzeczywistych w ADC 10bit jest niższa od ilości możliwych (czyli 10 bit). ADC skonfigurowany jest na AREF = internal.
masa -----/\/\/ 1MOHM /\/\/------|/tuADC/|---------|fototranz|------ AREF
Skonstruowałem prosty czujnik optyczny na fototranzystorze połączonym w szereg z 1Mohm'owym rezystorem. Napięcie na 'dzielniku' waha się od
0 do AREF V, tak, że po oświetleniu fototranzystora, napięcie na dzielniku rośnie(fototranzystor przewodzi). Do "dzielnika" wpiąłem wejście ADC.Wyniki, jakie osiągam powoli zakrywając 'oczko' fototranzystora zmieniają się dość skokowo, czasem znacznie. Powinno być dość powoli. Podobnie jest, jak podepnę wejście ADC do regulowanego rezystora, co w tym wypadku łatwiej wytłumaczyć kiepskimi ścieżkami węglowymi.
KOD : -------------------------------------------
#include <avr/io.h> // dostęp do rejestrów #include <avr/interrupt.h> // funkcje sei(), cli() #include <util/delay.h>
#include <dodane/mojLCD.h>
#include <stdlib.h>
void lcd_print(char *str) { unsigned char i=0;
while(str[i]!=0) { lcdData(str[i]); i++; }
}void adc_init(void) // Function to initialise the ADC feature { ADCSRA=0X00; // Clear previous ADC results and status flags ADMUX=0X40 ; // 0x40 for 10 bits (0x60) ADCSRA=0X87; // We have set the ADSC bit to start a conversion, and the // ADPS bits are set so that the prescaler is 128 ADCSRA=0X80; // ADEN is set, to enable the ADC }
unsigned int adc_start(unsigned char channel) // Function to perform an ADC conversion, Takes 0-8 as input // to select which input to convert { unsigned char i; ADCH=0x00; // Clear the previous result ADCL=0x00; // Clear the previous result
i=channel&0x07; // Decide which line to perform ADC conversion on ADMUX=i|0x60; // Enter which line to perform in the ADC control register ADCSRA|=1<<ADSC;
while(ADCSRA & (1<<ADSC)); // wait for conv. to complete unsigned int temp=ADC; //unsigned int temp=ADC; for 10 bits return temp; }
/* ---------------- Sample ADC Call -----------------------*/ int main(void) { unsigned int result=0; char str[10]; _delay_ms(500); lcd_init(); adc_init(); while(1) { result=adc_start(0); // This performs ADC conversion on Pin A0 and stores the result lcdCommand(HD44780_CLEAR); _delay_ms(2); lcd_print("ADC : "); itoa(result,str,10); lcd_print(str); _delay_ms(50); } return 0; }
-----------------------------------------------------