Sunteți pe pagina 1din 5

UNIVERSITY OF KWAZULU-NATAL

School of Electrical, Electronic & Computer Engineering

Course : DIGITAL SYSTEMS


Code : ENEL3DSH1
Practical No. : EL3DS2
Title : CSEG and DSEG access (Lab 2 of 4)

Introduction
This practical looks at CSEG and DSEG memory types of the ATMega32 and how data is stored and
read from them. Fixed tables of data are programmed into CSEG (using .db directives) and read using
the LPM command. Dynamic tables of data are stored into DSEG using ST and read back using LD. In
both cases data are displayed on LEDs connected to Port D. You may have to change this port choice
to match the actual hardware connections in your practical workstation.

NB: that any attempts to pass off the work of others as your own will be viewed in a very serious
light. If a group is unable to explain the operation of any programs which they claim to have
written, the entire group will be severely penalized.

Task 1 (30 minutes)


Complete the code given in APPENDIX A. It is a simple program that reads in button presses and
stores them in SRAM (using st). It then reads the stored data (using ld) and displays the sequence on
the LEDs with a brief delay to ensure that the sequence can be seen. Complete the code. Simulate to
ensure correct functioning. Then demonstrate the code working on the target hardware. Note the
difference in the functioning of the code between simulation on software and implementation on
hardware.

Task 2A (20 Minutes)


Complete the code given in APPENDIX B. It is a simple program that reads stored data from a table in
CSEG (using LPM); and then displays the stored sequence on the LEDs. A brief delay is implemented
to ensure that the sequence can be seen. Complete the code. Simulate to ensure correct
functioning. Then demonstrate the code working on the target hardware.

Bonus:
Modify the table to show your personalised LED routine. This must not simply be random sequences
but should have a clear pattern to it.

Task 2B (10 minutes)


Draw a memory map of the CSEG showing where the code and table are stored.

Appendix A
;********************************************************************************
; Title: Reading Switch Inputs and Storing to SRAM for Playback
; Hardware:
; Input: Switches Connected to PORTC
; Output: LEDs connected to PORTD
; Function:
; This programs recieves button presses from the switches connected to
; PORTC and stores them in SRAM. It then retrieves each recorded entry
; individually and displays it on the LEDs. A short delay is used
; to ensure visibility to the human user and to debounce switch inputs.

;********************************************************************************
;HEADER FILES AND DEFINITIONS SECTION
;********************************************************************************
.include "m32def.inc"

.def temp = R18


.def Counter = R19
.def Stop = R20
.def oldvalue = R21
.def newvalue = R22

;********Interrupt vector table********


.org 0x0000
rjmp RESET

;*************Setup phase**************
.org 0x0020
RESET:
;set port D as output
ldi temp,$FF
out DDRD,temp
;Set Port C as Input
ldi temp, $00
out ddrc,temp
;setup X register
clr XH ; Clear X high byte
ldi XL,$90 ; Set X low byte to $60 (in DSEG)
;setup the stack
ldi temp,low(RAMEND)
out SPL,temp
ldi temp,high(RAMEND)
out SPH,temp
out PORTD, temp ;Clear port D

;************RECORDING PHASE***************
;***DO While LOOP***
;C Code: counter = 8;
; do RecordNewData while(Counter != 0);
;
;Assemply code:
in oldvalue,PINC ;initialise oldvalue
ldi Counter,8 ;initialise counter
ldi Stop,0 ;initialise stop condition
DoWhileStart:
rcall RecordNewData ;call the action
cpse Counter,Stop ;decision diamond
rjmp DoWhileStart
DoWhileEnd:

;**************PLAYBACK PHASE****************
InitialisePlayback:
;rewind X register to start of recording
clr XH ; Clear X high byte
ldi XL,$90 ; Set X low byte to $90
;***FOR LOOP***
;C code: for(Counter=8; Counter>0; Counter--) Action
;Assembly code:
ldi Counter,8 ;initialise Counter and Stop condition
ldi Stop,0
rjmp StartFor1 ;Goto Decision Diamond
Playback:
ld Temp,X+ ;read a recorded value
out PORTD,Temp ;display the recorded value
rcall DelaySub ;wastes time
dec Counter ;Action over - update counter
StartFor1:
cpse Counter,Stop ;Decision Diamond Counter==Stop?
rjmp Playback ;No so do the action
rjmp InitialisePlayback ;Yes - Rewind then start playback again

;***********************SUBROUTINES*************************
;RecordNewData: contains the following logic
;It reads pinc, displays, if the input is different from the oldvalue then it ;stores
it and debounces.
;Steps involved:
; 1. read newdata from PINC
; 2. display newdata to PORTD
; 3. if newdata is different from oldvalue
; 3.1 store newdata to RAM
; 3.2 decrement counter
; 3.3 oldvalue=newdata (update old value)
; 3.4 debounce delay (you may call DelaySub)
; End of Action

RecordNewData:
; ******************MISSING CODE GOES HERE******************

;***DelaySub: Wastes some time***


;This delay is used to aid debouncing and visualisation of rapidly changing LEDs
DelaySub:
ldi r16, $4e ;$4e
ldi r17, $ff ;$FF
Delayloop:
dec r17
brne Delayloop
dec r16
brne Delayloop
ret
;END OF CODE

Appendix B
;******************************************************************************
; Title: Read data from CSEG and display to Port D LEDs
; Hardware:
; Input: none required
; Output: LEDs connected to PORTD
; Function:
; This programs retrieves data from a table in CSEG. It individually
; displays each table entry on the LEDs. A short delay is used
; to ensure visibility to the human user.

;********************************************************************************
;HEADER FILES AND DEFINITIONS SECTION
;********************************************************************************
.include "m32def.inc"
.list
.def temp = R18
.def Counter = R19
.def Stop = R20

;********Interrupt vector table********


.org 0x0000
rjmp RESET

;****Table of data - running light from left to right****


.org 0x0010
Table: .db 0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01

;*************Setup phase**************
.org 0x0020
RESET:
;set port D as output
ldi temp,$FF
out DDRD,temp
;setup the stack
ldi temp,low(RAMEND)
out SPL,temp
ldi temp,high(RAMEND)
out SPH,temp

;************PLAYBACK FOREVER**************
InitialisePlayback:
;setup/rewind Z register to point to start of Table
ldi ZH, high(Table<<1) ;Configure Z high byte
ldi ZL, low(Table<<1) ;Configure Z low byte

;***FOR LOOP***
;C code: for(Counter=8; Counter>0; Counter--) Action
;
;Assembly code:
ldi Counter,8 ;initialise Counter and Stop condition
ldi Stop,0
rjmp StartFor ;Goto Decision Diamond

Playback:
;******************MISSING CODE GOES HERE******************

StartFor:
cpse Counter,Stop ;Decision Diamond Counter==Stop?
rjmp Playback ;No so do the action
rjmp InitialisePlayback ;Yes - Rewind then start playback again

;***********************SUBROUTINES*************************
;DelaySub: Wastes some time
;This delay is used to aid debouncing and visualisation of rapidly changing LEDs
DelaySub:
ldi r16, $4e ;$4e
ldi r17, $ff ;$FF
Delayloop:
dec r17
brne Delayloop
dec r16
brne Delayloop
ret
;END OF CODE

S-ar putea să vă placă și