Atmega328p ADC assembly doesn't convert

633 views Asked by At

Here is my code. I need to put a pull up resistor on the board so INT0 flag will send me to the conversion and then print it on PORTB. I'm using ADC0 on PC0 but it wont work. There is only one value printed on PORTB. Can you help me?

.include "m328pdef.inc"
.org 0x0000 jmp startProgram
.org 0x0002 jmp int0_int
.org 0x002A jmp adcComplete

startProgram:
    .org 0x0033

// Configuracion de Stack Pointer y Timer0
ldi R16, High(RAMEND)
out SPH, R16
ldi R16, Low(RAMEND)
out SPL, R16

// Configuracion INT0
ldi r16,    0x02
sts EICRA,  R16
ldi r16,    0x01
out EIMSK,  R16
sts EIFR,   R16

// Configuracion del ADC
ldi R16,    0x60
sts ADMUX,  R16
ldi r16,    0xFF
sts ADCSRA, R16
ldi r16,    0x02
sts ADCSRB, R16
ldi r16,    0x01
sts DIDR0,  R16
SEI

// Inicializando Puerto B
ldi R16,    0xFF
out DDRB,   R16
ldi R16,    0x04
out DDRD,   R16

// Rutina de espera del ADC
loop:
    rjmp loop

int0_int:
    reti

adcComplete:
    lds  R16, ADCH
    cpi  R16, 63
    brlo low_adc
    cpi  R16, 127
    brlo med_adc
    cpi  R16, 191
    brlo high_adc
    cpi  R16, 191
    brsh full_adc
    reti

low_adc:
    ldi R17,    0xFF
    out PORTB,  R17
    reti

med_adc:
    ldi R17,    0x3F
    out PORTB,  R17
    reti

high_adc:
    ldi R17,    0x0F
    out PORTB,  R17
    reti

full_adc:
    ldi R17,    0x03
    out PORTB,  R17
    reti
1

There are 1 answers

0
JimmyB On

I think sts EIFR, R16 should actually be out EIFR, R16.

Then, you're configuring PORTD.2 (INT0) as an output, which causes it to become 0 and stay there. No edge, no INT0, no ADC trigger.

If you have an external pull-up use

ldi R16,    0x00
out DDRD,   R16
out PIND,   R16

To use the internal pull-up use

ldi R16,    0x00
out DDRD,   R16
ldi R16,    (1<<2)
out PIND,   R16