I'm doing a home automation project with an UNO arduino and I have to use a light sensor, however since in assembly there is no analogRead function () I have to program it, I already read the documentation of avr and atmel about the ADC, the MUX and its selection but it is not clear to me how it is programmed or how it works, I could use an explanation of the following code that a colleague told me, which affirms that the values of a certain PIN are stored in ADCL and ADCH, My question is if someone would be so kind as to explain me the code and what is happening in the ADC MUX. It's a 328p:
.equ F_CPU = 16000000;
.DEF rmp = R16
.DEF tmp = R17
.DEF tmp2 = R18
.DEF reg1 = r20
.DEF reg2 = r21
.EQU ALFA=250
.DSEG
.ORG 0X0100
.CSEG
.ORG $0000
jmp Main
Main:
ldi rmp, HIGH(RAMEND) ; Init MSB stack
out SPH,rmp
ldi rmp, LOW(RAMEND) ; Init LSB stack
out SPL,rmp
; Init Port B
ldi rmp,0xff ; DIRECCION DEL Port B
out DDRB,rmp
rcall ad ; Call ADC Initialization
; [Add all other init routines here]
ldi rmp,1<<SE ;
out MCUCR,rmp
sei
Loop:
rcall adcRead
out PORTB,r18
rjmp loop ; go back to loop
ad:
ldi rmp, (1<<REFS0)
sts ADMUX, rmp
ldi rmp, (1<<ADEN)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0)
sts ADCSRA, rmp
ret
adcRead:
lds rmp, ADCSRA
ori rmp, (1<<ADSC)
sts ADCSRA, rmp
loopx:
lds rmp, ADCSRA
sbrc rmp,ADSC
rjmp loopx
lds r17,ADCL
lds r18,ADCH
ret