You are on page 1of 3

;********************************************************************

; File: ELET_7404_Lab_2_HDK.asm
; Date: January 23, 2014 2:17:38 PM
;
; Target: ATmega328P
; Assembler: Atmel AvrAssembler2 (AVR Studio 6)
; Author: Hai D. Kieu
;
; Hardwar: 8-LED Module connected to port D, Combination I/O Module connected to
port B
;
;
; Summary: 8-LED Module will display in a certain pattern according to the input
from the Combination Module
;
;
; History: v01 - original program
;
;
; ****************************************************************************
; constants
;.equ PIND = 0x09 ; Port D Input Pins Register
;.equ PortD0 = 0 ; bit 0 of Port D Data Register
;.equ DDRD = 0x0A ; address of Data Direction Regi
ster for PortD
;.equ PORTD = 0x0B ; address of PortD
;,equ PINB = 0x03 ;Address of data direction regis
ter for PortB
;.equ SREG = 0x3f ;address of status register

;.equ ADMUX = 0x7c ; ADC Multiplexer Selection Regi
ster
;.equ ADCSRB = 0x7b ; ADC Control and Status Registe
r B
;.equ ADCSRA = 0x7a ; ADC Control and Status Registe
r A
;.equ ADCH = 0x79 ; ADC Data Register High Byte
;.equ ADCL = 0x78 ; ADC Data Register Low Byte
; Definitions
.def temp = R16 ; temporary storage (in R16)
.def temp2 = R17 ; temporary storage (in R17)
.def threshold = R18 ; temporary storage (in R18)
.org 0x0000 ; program origin
jmp start ;Jump over interrupt
;***********************************************************************
;org 0x002a
.org ADCCaddr ; Timer Counter0 Compare Match A
jmp ISR_ADC ;Jump to interrupt service routine
;org 0x0034
.org INT_VECTORS_SIZE ; start interrupt service routine past a
ll intterupt vectors
ISR_ADC:
push temp ;save register
in temp, SREG ; save status register
push temp
;lds temp, ADCH
;out PortD, temp
lds temp, ADCH
mov temp2, temp
;**********************Test for Channel*******************************
lds temp, ADMUX
andi temp, 0b00000111 ; mask out all but channel number
cpi temp, 0b00000000 ; is it channel 0?
breq a0
cpi temp, 0b00000001 ; is it channel 1?
breq a1
jmp New_Conversion
;**************************End Test*************************************
;*******************************************************************
a0: ; left motor (P
ort D bit 7)
cp temp2, threshold ; ignore if value < threshold
brlo no_action
sbi PortD, 7 ; else, turn motor on
jmp done
no_action:
cbi PortD, 7
ldi temp, 0b01100001 ; channel was a0, now is a1
sts ADMUX, temp
jmp New_Conversion
;***********************************************
a1: ; left motor (P
ort D bit 6)
cp temp2, threshold ; ignore if value > threshold
brlo no_action1
sbi PortD, 0 ; else, turn motor on
jmp done
no_action1:
cbi PortD, 0
ldi temp, 0b01100000 ; channel was a1, now is a0
sts ADMUX, temp
jmp New_Conversion
;*******************************************************************
New_Conversion:
lds temp, ADCSRA
ori temp, 0b11001111 ; ADEN, ADSC, ADIE, 128
sts ADCSRA, temp
done:
pop temp ; restore status registe
r
out SREG, temp
pop temp ;restore register
reti ;End interrupt routine
;***************************************************************************
start:
ldi threshold, 128 ;Configure threshold to OFF stat
e
ldi temp,low(RAMEND) ; initialize the stack pointer
out SPL,temp
ldi temp,high(RAMEND) ; initialize the stack end
out SPH,temp
ldi temp, 0b11111111 ; configure all bits of Port D for outpu
t
out DDRD, temp
ldi temp, 0b01110000 ;configure ADMUX register6
sts ADMUX,temp
ldi temp, 0b00000000 ;configure ADSCRB register
sts ADCSRB,temp
ldi temp, 0b11101111 ;configure ADSCRA register
sts ADCSRA,temp
sei
loop:
jmp loop