title: How to code an ADC for an atmega328p in assembly
body: <p>I have done a huge amount of research and have found next to nothing in assembly.
  My code does not work and I do not understand why, it is supposed to alter the speed at which the lights change colour using the delay loop, I'm very new to assembly and would appreciate any help.</p>
  
  <pre><code>    ;
  ; Labs.asm
  ;
  ; Created: 8/14/2016 7:34:43 AM
  ; Author : Dario
  ;
  
  
  ; Replace with your application code
      .include "M328PDEF.INC"
      .org 0x000
  start:
      ;ddrx controls the if a pin is in/out, if the pins corresponding bit in the ddr is 1 it's an out, if it's 0 its an in
      ldi r16, 0x00 ; set r16 = 0000 0000
      out ddrc, r16 ;set all c pins as input
      ldi r16, 0xFF ; set r16 = 1111 1111
      out ddrd, r16 ;set all d pins as output
      out ddrb, r16 ;set all b pins as output
  
      ldi r16, HIGH(RAMEND)
      out SPH, r16
      ldi r16, LOW(RAMEND)
      out SPL, r16
  
  
  
  prog:
      ;----------------initialise adc
  
      sbi PORTD,5
  setADC:
  
      lds r16,0x00
      STS ADCSRA,R16
  
      ldi r16,0xC3
      sts ADCSRA,r16
      ldi r16,0x23    ;the 2 sets the bits to be left justified
      sts admux,r16
  
  keepPolling:
      lds R16,ADCSRA
      sbrs R16,4 ;wait for conversion to complete, when conversion is complete the 4th bit in the ADCSRA is set to true therefor skipping the r jump because of sbrs
      jmp keepPolling
  
      lds r17,ADCL    
      lds r16,ADCH
  
      sbi PORTD,2
      ldi r18,2       ;r18 is used by delay- setting it to 2 doubles the delay, as opposed to if it was set to 1
      call delay
      cbi PORTD,2
      ldi r18,2
      call delay
  
      rjmp prog
  
  
  
  delay:
      loop1:
          ldi r21,150
      loop2:
          ldi r22,100
      loop3:
          mov r23,r16
      loop4:
          nop
          nop
          dec r23
          brne loop4
          dec r22
          brne loop3
          dec r21
          brne loop2
      ret
  </code></pre>
  
  <p>Any help would be appreciated</p>