Read analog value

Read analog value
Çeşme:
http://extremeelectronics.co.in/avr-tutorials/using-adc-of-avr-microcontroller/
  1. Arduino UNO
int sensorPin = A0; // select the input pin for the potentiometer int sensorValue = 0; // variable to store the value coming from the sensor void setup() { Serial.begin(9600); } void loop() { sensorValue = analogRead(sensorPin); Serial.print("val: "); Serial.println(sensorValue); delay(2); }
#include <avr/io.h> #include <Arduino.h> #include <util/delay.h> void setupADC() { ADMUX = (1<<REFS0); // For Aref=AVcc; ADCSRA = (1<<ADEN) | (1<<ADPS2) | (1<<ADPS1) | (1<<ADPS0); //Rrescalar div factor =128 } uint16_t readADCValue(uint8_t ch){ //Select ADC Channel ch must be 0-7 ADMUX |= ch & 0b00000111; //Start Single conversion ADCSRA |= (1<<ADSC); //Wait for conversion to complete while(!(ADCSRA & (1<<ADIF))); //Clear ADIF by writing one to it //Note you may be wondering why we have write one to clear it //This is standard way of clearing bits in io as said in datasheets. //The code writes '1' but it result in setting bit to '0' !!! ADCSRA|=(1<<ADIF); return(ADC); } int main(void) { Serial.begin(9600); uint16_t value; //Initialize ADC setupADC(); while(1) { value = readADCValue(PC0); // Read Analog value from channel-0 Serial.println(value); _delay_ms(2); } return 0; }
.nolist .include "./m328Pdef.inc" .list .cseg .macro send mov data, @0 ori data, 0x30 rcall USART_Transmit .endmacro ;***** Speed setting ***** .equ F_CPU = 16000000 .equ BITRATE = 9600 .equ speed = F_CPU / 16 / BITRATE - 1 .def temp = r16 .def data = r17 .def v10k = r19 .def v1k = r20 .def v100 = r21 .def v10 = r22 .def v1 = r23 .org 0x0000 rjmp start .cseg start: ldi temp, high(RAMEND) out sph, temp ldi temp, low(RAMEND) out spl, temp rcall USART_Init lds temp,ADMUX ori temp,(1 << REFS0) sts ADMUX,temp lds temp,ADCSRA ori temp, (1<<ADPS1)|(1<<ADPS0)|(1<<ADEN) sts ADCSRA,temp loop: lds temp, ADMUX andi temp,0xf0 ori temp,5 ; PC5 sts ADMUX,temp lds temp,ADCSRA ori temp,(1 << ADSC) ;start adc sts ADCSRA,temp wait: lds temp,ADCSRA sbrc temp,ADSC rjmp wait lds zl, ADCL lds zh, ADCH rcall bin2bcd send v10k send v1k send v100 send v10 send v1 ldi data, $0D rcall USART_Transmit ldi data, $0A rcall USART_Transmit rjmp loop USART_Init: ; these values are for 9600 Baud with a 16MHz clock ; Set baud rate clr temp sts UBRR0H, temp ldi temp, speed sts UBRR0L, temp ; Enable transmitter ldi temp, (1<<TXEN0) sts UCSR0B, temp ; Set frame format: Async, no parity, 8 data bits, 1 stop bit ldi temp, (1<<UCSZ01) | (1<<UCSZ00) sts UCSR0C, temp ret USART_Transmit: ; wait for empty transmit buffer lds temp, UCSR0A sbrs temp, UDRE0 rjmp USART_Transmit ; Put data (r19) into buffer, sends the data sts UDR0, data ret .macro yhl ldi @0, low(@1) mov yl, @0 ldi @0, high(@1) mov yh, @0 .endmacro bin2bcd: yhl temp, 0x2710 ; 10k rcall m10k ret .macro bin2bcdmfn clr temp bin2bcdmfn2: sub zl,yl sbc zh,yh brcs PC+3 inc temp rjmp bin2bcdmfn2 mov @0, temp add zl, yl adc zh, yh .endmacro m10k: bin2bcdmfn v10k yhl temp, 0x3e8 ; 1k rcall m1k ret m1k: bin2bcdmfn v1k yhl temp, 0x64 ; 100 rcall m100 ret m100: bin2bcdmfn v100 yhl temp, 0x0a ; 10 rcall m10 ret m10: bin2bcdmfn v10 mov v1, zl ret
#chip mega328p, 16 #define USART_BAUD_RATE 9600 Dim adVal as Integer do Forever: adVal = ReadAD10(AN0) HSerPrint "Sensor: " HSerPrint adVal HSerSend(13) HSerSend(10) wait 100 ms loop