You are on page 1of 9

;************************************************************** ;* This program loads four constants and stores them in RAM ;* Title: Exercise 10 * ;* Names: Matthew Leung,

Scott Button * ;* Date: November 7, 2012 * ;* Class: 0306-250 * ;* Section: Section 1 Lab Section 4, Wednesdays 6-8 pm ;************************************************************** ; export symbols XDEF Entry, main ; Allows "Entry" to be referenced either ; in the linker .prm file or from C/C++ XREF __SEG_END_SSTACK ; symbol defined by linker for end of stack ; include derivative specific macros INCLUDE 'mc9s12dt256.inc'

;Initialization of the ECT to generate a channel seven output compare (OC7) inte rrupt every ;0.01 s from a timer clock of 2 MHz. ;ECT Interrupt Handler ECT_2MHZ_RST_OC7: ECT_OC7: ECT_10ms: ECT_C7I: ECT_ENABLE: EQU EQU EQU EQU EQU %00001000 %10000000 20000 %10000000 %10000000

;Baud Rate 9600 (same as for polling) SCI_9600H: EQU $00 SCI_9600: EQU $0D ;Protocol (8N1-same as for polling) SCI_8N1: EQU %00000000 SCI_RI_T: EQU %00101100 ; M=0 and PE=0

;Enable transmit interrupts ;want Rx IRQs, Tx IRQs, Rx, and Tx enabled SCI_RI_TI: EQU %10101100 ;Other EQUates SCI_TDRE: EQU SCI_RDRF: EQU ;Queue EQUates TX_BUF_SZ: EQU TXQ_REC_SZ: EQU 10 RX_BUF_SZ: EQU RXQ_REC_SZ: EQU 10 Q_BUF_SZ: EQU 25 %10000000 %00100000 80 80

Q_REC_SZ:

EQU

10

;Queue Record EQUates IN_PTR: OUT_PTR: BUF_STRT: BUF_PAST: BUF_SIZE: NUM_ENQD: EQU EQU EQU EQU EQU EQU 0 2 4 6 8 9 79

MAX_STRING: EQU MyCode: main: Entry: SECTION LDS CLI

#__SEG_END_SSTACK ; initialize the stack pointer ; enable interrupts movb movb movw movb movb #ECT_2MHZ_RST_OC7,TSCR2 #ECT_OC7,TIOS #ECT_10ms,TC7 #ECT_C7I,TIE #ECT_ENABLE,TSCR1

;Disable transmit interrupts ;want to leave Rx interrupts, Rx, and Tx enabled ;Initialization of SCI Registers movb #SCI_9600H,SCI0BDH movb #SCI_9600,SCI0BDL movb ;Disable transmit interrupts movb ;Record Initialization ldx movw movw movw movw movb clr ;TxRecord Initialization ldx movw movw movw movw movb clr ;RxRecord Initialization ldx movw movw movw #RxRecord #RXQBuffer,IN_PTR,x #RXQBuffer,OUT_PTR,x #RXQBuffer,BUF_STRT,x #TxRecord #TXQBuffer,IN_PTR,x #TXQBuffer,OUT_PTR,x #TXQBuffer,BUF_STRT,x #(TXQBuffer+TX_BUF_SZ),BUF_PAST,x #TX_BUF_SZ,BUF_SIZE,x NUM_ENQD,x #QRecord #QBuffer,IN_PTR,x #QBuffer,OUT_PTR,x #QBuffer,BUF_STRT,x #(QBuffer+Q_BUF_SZ),BUF_PAST,x #Q_BUF_SZ,BUF_SIZE,x NUM_ENQD,x #SCI_8N1,SCI0CR1 #SCI_RI_T,SCI0CR2 ; SCI0BD[12:8] ; SCI0BD[7:0]

movw movb clr cli movw movb movw TimingMain: RandomNumGenerator: movw ldd ldx idiv stab ; if 1 that led is on ; if 0 that led is off

#(RXQBuffer+RX_BUF_SZ),BUF_PAST,x #RX_BUF_SZ,BUF_SIZE,x NUM_ENQD,x TCNT,randnum #0,RunStopWatch #0,Count

TCNT,randnum randnum #256 numled

;to check the number of lit led's ;loop through the number using a rotate right. ;check the carry bit in the ccr ;if it is 1 increment the led count ;after 8 passes of the loop, the led count will show the number of lit led's ;score ;if correct the score for that round is equal to the round number * 10 ;add up the total score

;Transmit Interrupt Implementation from Lab 8 GetChar: pshx GetCharLoop: ldx art of receive queue sei ;mask interrupts jsr character to B cli ;unmask interrupts bcs pulx rts ;Recieve Interrupt Implementation from Lab 8 PutChar: pshx PutCharLoop: ldx art of transmit queue sei ;mask interrupts #TxRecord ;load st GetCharLoop Dequeue ;dequeue #RxRecord ;load st

jsr contents of B cli ;unmask interrupts bcs movb pts until next failed dequeue pulx rts

Enqueue

;enqueue

PutCharLoop #SCI_RI_TI,SCI0CR2 ;enable transmit interru

;GetString ; Reads a string from the terminal keyboard and stores it in memory starting ; at the address where X points (i.e. characters typed until "Enter" is pressed) ; Parameters ; Input: ; X: Pointer to destination string ; Uses: ; GetChar ; Leaves all registers unchanged on return besides CCR GetString: pshx pshd clra While: jsr cmpb beq LoopBody: cmpa beq inca jsr jsr bcs bra NQFail: inc bra EndLoop: ldx jsr puld pulx RTS ;PutString ; Displays a null-terminated string, from memory starting at the address where #NewLne PutString NQFailCntr While #(MAX_STRING-1) While PutChar Enqueue NQFail While GetChar #$0D EndLoop

; X points, to ; Parameters ; Input: ; ; Uses: ; ; Leaves PutString:

the terminal screen X: Pointer to destination string PutChar all registers unchanged on return besides CCR pshx pshy pshd

PSLoop: ldab beq jsr bra StopPS: puld puly pulx RTS ;*************************************************************** ;************ The following code is for Exercise 5. ************ ;*************************************************************** ;Enqueue ; If the queue (whose queue record structure's address is in X) is ; not full, enqueues the character from register B to the queue and ; and reports success by returning with CCR bit C cleared (i.e., 0) ; other wise only reports failure by returning with CCR bit C set ; (i.e., 1) ; Parameters ; Input: ; B: Character to enqueue ; X: Address if qyeye record structure ; Output: ; CCR bit C: Success(0) or Failure(1) ; Modifies: ; CCR ; Leaves all registers unchanged on return besides CCR Enqueue: psha pshb pshx pshy ldaa cmpa blt sec FinalNQ: puly pulx NUM_ENQD,x BUF_SIZE,x QueueNotFull 1,x+ StopPS PutChar PSLoop

pulb pula RTS QueueNotFull: ldy stab inc cpy blo ldy InPtrLsTnBufPst: sty clc bra FinalNQ IN_PTR,x IN_PTR,x 1,y+ NUM_ENQD,x BUF_PAST,x InPtrLsTnBufPst BUF_STRT,x

;Dequeue ; If the queue (whose queue record structure's address is in X) is ; not empty, dequeues a character from the queue to register B and ; and reports success by returning with CCR bit C cleared (i.e., 0) ; other wise only reports failure by returning with CCR bit C set ; (i.e., 1) ; Parameters ; Input: ; X: Address if qyeye record structure ; B: Character dequeued ; Output: ; CCR bit C: Success(0) or Failure(1) ; Modifies: ; B; CCR ; Leaves all registers unchanged on return besides CCR Dequeue: psha pshx pshy ldaa beq NumQGrZero: ldy ldab dec cpy blo ldy OutPtrLsTnBufPst: sty OUT_PTR,x OUT_PTR,x 1,y+ NUM_ENQD,x BUF_PAST,x OutPtrLsTnBufPst BUF_STRT,x NUM_ENQD,x DQFailed

clc bra DQFailed: sec DQDone: puly pulx pula RTS PutNumW: pshd pshx pshy Loop: ldx idiv pshd iny tfr cpd bne InPutNumW: puld orab jsr dey beq jmp EndLoopforPutNum: puly pulx puld RTS SCI0_ISR: ;go to Tx part if transmission, stay in Rx part otherwis e brclr ldab x register ldx it jsr bra Enqueue S0ISR_END ;return #RxRecord ;enqueue SCI0SR1,#SCI_RDRF,S0ISR_Tx SCI0DRL ;get data from R EndLoopforPutNum InPutNumW #'0' PutChar x,d #0 Loop #10 DQDone

S0ISR_Tx: brclr brclr ldx next char to send jsr bcs f dequeue failed stab turn if false bra S0ISR_FAIL: movb upts until next enqueue S0ISR_END: rti ;Enhanced Capture Timer Output Channel 7 Compare Interrupt Interrupt Service Rou tine ; On an ECT interrupt, if the (byte) variable 'RunStopWatch' is not zero , increment the ; (word) variable 'Count'; otherwise it leaves 'Count' unchanged. ECT_OC7_ISR: ldaa RunStopWatch beq ISR_ECT_END ldd Count addd #1 std Count ISR_ECT_END: movb rti MyConst: SECTION Start: DC.B NewLne: DC.B MyData: SECTION String: DS.B RString: DS.B RunStopWatch: DS.B Count: DS.W QBuffer: QRecord: DS.B DS.B #ECT_C7I,TFLG1 #SCI_RI_T,SCI0CR2 ;disable transmit interr S0ISR_END SCI0DRL ;transmit and re Dequeue S0ISR_FAIL ;check i SCI0SR1,#SCI_TDRE,S0ISR_END SCI0CR2,#SCI_TDRE,S0ISR_END #TxRecord ;dequeue

'',$0A,$0D,'>',0 $0A,$0D,0 MAX_STRING MAX_STRING 1 1 Q_BUF_SZ Q_REC_SZ TX_BUF_SZ TXQ_REC_SZ RX_BUF_SZ RXQ_REC_SZ

TXQBuffer: DS.B TxRecord: DS.B RXQBuffer: DS.B RxRecord: DS.B NQFailCntr:

DS.B

1 ORG $FFD6 ;set add

ress of Serial I/O ISR

DC.W

SCI0_ISR ;set add

ORG $FFE0 ress of Enhanced Capture Timer DC.W ECT_OC7_ISR