Skip to content

Instantly share code, notes, and snippets.

@BobBurns
Created November 21, 2014 14:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BobBurns/90dab6b22f45788f1dc4 to your computer and use it in GitHub Desktop.
Save BobBurns/90dab6b22f45788f1dc4 to your computer and use it in GitHub Desktop.
light metering program in avr assembly
;Light Meter assembly version
;based on E.Williams AVR Programming lightSensor.c p.135
;compile with gavrasm lightMeter.asm
;
.device atmega 168
.cseg
.org 0
.def temp = r16
;--- Inits ---
ldi temp,0xff
out DDRB,temp ;output on led ports
lds temp,ADMUX
ori temp,(1 << REFS0) | (1 << ADLAR) ;reference voltage on AVCC. Use Left Justified data
sts ADMUX,temp
lds temp,ADCSRA
ori temp,(1 << ADPS1) | (1 << ADPS0) | (1 << ADEN)
;Enables ADC and set clock prescaler to 8
sts ADCSRA,temp
;--- event loop ---
lp:
lds temp,ADCSRA
ori temp,(1 << ADSC)
sts ADCSRA,temp ;start ADC conversion
wait: lds temp,ADCSRA
sbrc temp,ADSC ;wait for ADSC bit to clear
rjmp wait
.def adH = r17
.def s_adc = r18
; get converted data and right shift four
ldi temp,1
out PORTB,temp ;got through first wait * debugging
lds s_adc,ADCH
lsr s_adc
lsr s_adc
lsr s_adc
lsr s_adc
.undef adH
.def count = r17
ldi temp,2
out PORTB,temp ;got through second stage * debugging
mov count,s_adc
ldi temp,1
l_lp: out PORTB,temp
sec
rol temp
dec count
brne l_lp
;delay
.equ d_cnt = 50000
ldi r25,high(d_cnt)
ldi r24,low(d_cnt)
dlp: sbiw r25:r24,1
brne dlp
rjmp lp
@BobBurns
Copy link
Author

The hard part of this program was figuring out ADCH bytes aren't updated until ADCHL is read. So in order to do the bit shift I had to left justify the ADC value (ADLAR = 1).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment