Created
November 22, 2014 13:44
-
-
Save BobBurns/474f044242363bc78542 to your computer and use it in GitHub Desktop.
AVR Assembly program using adc, the multiplexer, and sram for variable data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;Night Light program from p.148 | |
; | |
;example program using adc, mux, and sram variable storage | |
; | |
;compile with gavrasm serial_assembly.asm | |
;flash with avrdude -c avrisp -p m168 -P /dev/tty.usbmodem1411 -b 19200 -U flash:w:serial_assembly.hex | |
; | |
;program to utilize adc mux | |
.device atmega 168 | |
rjmp start | |
.dseg | |
l_thresh: .byte 2 | |
s_val: .byte 2 | |
.cseg | |
.org 0 | |
.def temp = r16 | |
.def temp2 = r17 | |
;--- Inits --- | |
; | |
start: lds temp,ADMUX | |
ori temp,(1 << REFS0) | |
sts ADMUX,temp | |
lds temp,ADCSRA | |
ori temp,(1 << ADPS1) | (1 << ADPS0) | (1 << ADEN) | |
sts ADCSRA,temp | |
ldi temp,0xff | |
out DDRB,temp | |
loop: lds temp,ADMUX | |
andi temp,0xf0 | |
ori temp,3 ;pot sensor channel PC3 = 3 | |
sts ADMUX,temp | |
lds temp,ADCSRA | |
ori temp,(1 << ADSC) ;start adc | |
sts ADCSRA,temp | |
wait: lds temp,ADCSRA | |
sbrc temp,ADSC | |
rjmp wait | |
lds temp,ADCL | |
ldi r26,low(l_thresh) | |
ldi r27,high(l_thresh) ;load X with address of l-thresh | |
st X+,temp | |
lds temp,ADCH | |
st X,temp | |
;read sensor value and store in s_val | |
lds temp,ADMUX | |
andi temp,0xf0 | |
ori temp,0 ;light sensor PC0 | |
sts ADMUX,temp | |
sts ADMUX,temp | |
lds temp,ADCSRA | |
ori temp,(1 << ADSC) | |
sts ADCSRA,temp | |
wait2: lds temp,ADCSRA | |
sbrc temp,ADSC | |
rjmp wait2 | |
lds temp,ADCL | |
ldi r28,low(s_val) | |
ldi r29,high(s_val) | |
st Y+,temp | |
lds temp,ADCH | |
st Y,temp | |
;compare readings | |
ld temp,X ;high byte of pot reading | |
ld temp2,Y ;high byte of sensor reading | |
cp temp,temp2 | |
brge setLed | |
ld temp,-X | |
ld temp2,-Y | |
cp temp,temp2 ;low pot compare low sensor | |
brge setLed | |
ldi temp,0x00 | |
out PORTB,temp | |
rjmp loop | |
setLed: ldi temp,0xff | |
out PORTB,temp | |
rjmp loop |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment