HerculesDev
HerculesDev

Reputation: 21

Code for ADC input not working

I am trying to read in a value from a potentiometer using the ADC and then use it to set the length of my delay which will in turn light different lights for a certain interval (dependant on the input from the ADC). I might be making a simple error. Here is the code so far:`

` initialize:

 LDI R16, HIGH(RAMEND)  ;Initializing the r16 register

 OUT SPH, R16
 LDI R16, LOW(RAMEND)
 OUT SPL, R16   

LDI R16, 0xFF   
OUT DDRB, R16   ;Initializes port B as output
LDI R16, 0x00
OUT DDRC, R16   ;Initializes port C as an input

LDI R16, 0b00000000; Initialize ADC port and reference voltage
STS ADMUX, R16 

startADC:
LDI R16, 0b11000111
STS ADCSRA, R16 ;Begins converstion 
KeepPolling:
LDI R16, ADCSRA
SBRS R16, 4     ;Checks if bit is set
RJMP KeepPolling    ;If bit is not set, conversion continues
LDS R18, ADCL       ;Loads ADCL value onto R16  
LDS R16, ADCH       ;Readvalue so resistor doesnt lock down
RCALL   Green


Green:

MOV R16, R18            ;Load value of ADC to R24
SBI PORTB, 0b00000001       ;Output High to portb
RCALL loop
CBI PORTB, 0b00000001
RCALL Yellow        ;Go to yellow if R24 is 0



Yellow: 

MOV R16, R18            ;Load value of ADC to R24
LSR R16             ;Divide R24 by 2
SBI PORTB, 0b00000010       ;Output high to port B 
CALL loop
CBI PORTB, 0b00000010
RCALL Red

Red:

MOV R16, R18
SBI PORTB, 0b00000011
RCALL loop
CBI PORTB, 0b00000011
RCALL startADC


loop: 

MOV R20, R16 

outer_loop: 

LDI R24, 0

Add_Dec:    

ADIW R24, 1
BRNE Add_Dec
DEC R20
BRNE outer_loop 
RET

Any help will be appreciated.

Upvotes: 0

Views: 456

Answers (1)

Mike
Mike

Reputation: 11

The first line of the KeepPolling loop does not read the ADC status register:

LDI R16, ADCSRA

Maybe you want a LDS instruction there. Depending on the AVR, you might be able to use the SBI instruction.

Also, startADC() calls Green() which calls Yellow() which calls Red() which calls startADC(). I think you'll run out of stack space pretty fast.

Also, once you break that infinite loop, Yellow() calls Red(), and then continues directly into Red(). Same with Green() falling into Yellow().

Upvotes: 1

Related Questions