Bài-5 (1)

Embed Size (px)

DESCRIPTION

java

Citation preview

Bi 5. Kim sot n cu thang1. Yu cu bi ton:Cho mt h thng n cu thang, gm n led v 2 cng tc tt/bt K1, K2 nh trn hnh. Nguyn tc hot ng nh sau: Khi K1 bm, c 8 n led cu thang s chuyn trng thi t bt --> tt, hoc ngc ti, tt --> bt Khi K2 bm, tng t S dng thm 1 External Timer m s xung nhp, t xc nh thi im K1, K2 c bm.Hy vit chng trnh hp ng :a. Thc hin chc nng m t v K1, K2, Ledb. Trn giao din console, c menu hin th tng s ln tt n, tng s ln bt n.c. Trn giao din console, c menu hin th thi im bm K1, K2 gn nht, tnh theo s xung nhp m External Timer ghi li (tickcount).

2. Thut tona. X l ngt S dng cc lnhli t2,0x22li t3,0x11li t4,0x44 bt s kin bm cng tc K1,K2 v External Timer Hm tang_xung: S dng thanh ghi s6 lu thi im xung hin ti Hm bam_ct_k1: Bm cng tc K1 th t n sng sang tt v ngc li Hm bam_ct_k2: Tng t hm bam_ct_k1 Hm on: Bt n Dng lnh: li s1,0xffla t0, LEDSsb s1,0(t0)addi s2,s2,1(thanh ghi s2 lu s ln bt n) Hm off:Tt n(Tng t hm off, dng thanh ghi s3 lu s ln tt n)b. Vit menu In ra menu bng lnh:la a0,_menujal printf(menu l xu c khi to) S dng lnh getchar,putchar yu cu ngi dng nhp s la chn ca menu c gii hn yu cu lc nhp bng cch chuyn t dng k t v s nguyn sau so snh vi cc la chn ca menujal getcharadd a0,v0,zeroaddi t7,v0,-48jal putchar3. In ra Console v I/O:a. Menu:

b. La chn 1:Sau khi 4 ln bt n v 3 ln tt n

c. La chn 2:Thi im xung th 9 lu gi ln bm K1 gn nhtThi im xung th 7 lu gi ln bm K2 gn nht

4. Ton b code bi ton

#include #include #include #define PIO_SETUP2 0xffffea2a#define SWITCHES 0xbf900000#define LEDS 0xbf900000#define BUTTONS 0xbfa00000 .dataslbat: .asciiz "\n-----------------------------------------------\n\nSo lan bat den: %d"enter: .asciiz "\n"sltat: .asciiz "\nSo lan tat den: %d"tdbk1: .asciiz "\nThoi diem bam K1 gan nhat: %d"tdbk2: .asciiz "\nThoi diem bam K2 gan nhat: %d"_menu: .asciiz "\n\n\n-----------------------MENU-------------------------\n\n1. Hien thi tong so lan dat tat den\n2. Hien thi thoi gian bat tat den gan nhat\n3. Thoat\n\nLua chon: "_chon: .space 3thanks: .asciiz "\n\nTHANKS YOU !!!"nhap_lai: .asciiz "\nNhap sai ! Nhap lai mot gia tri tu ( 1 - 3): " .text#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~# GENERAL INTERRUPT SERVED ROUTINE for all interrupts#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~.globl introutine.ent introutine.set reorder.set noat# Not warning if the AT register is used introutine:#--------------------------------------------------------# SAVE the current REG FILE to stack#-------------------------------------------------------- subu sp, sp, 22*4 # Allocate space, 18 regs, 4 args sw AT, 4*4(sp) # Save the registers on the stack sw v0, 5*4(sp) sw v1, 6*4(sp) sw a0, 7*4(sp) sw a1, 8*4(sp) sw a2, 9*4(sp) sw a3, 10*4(sp) sw t0, 11*4(sp) sw t1, 12*4(sp) sw t2, 13*4(sp) sw t3, 14*4(sp) sw t4, 15*4(sp) sw t5, 16*4(sp) sw t6, 17*4(sp) sw t7, 18*4(sp) sw t8, 19*4(sp) sw t9, 20*4(sp) sw ra, 21*4(sp) # Note that 1*4(sp), 2*4(sp), and 3*4(sp) are # reserved for printf arguments .set reorder #-------------------------------------------------------- # Detect the CAUSE of Interrupt, maybe K1, K2, Timer and # the instruction address in the main program when it happens (to return later). #-------------------------------------------------------- mfc0 k0, C0_CAUSE # Retrieve the cause register -- (do ngat cung) mfc0 k1, C0_EPC # Retrieve the EPC#-------------------------------------------------------- # Get the I/O port address # Used to detect K1, K2, timer were pressed #-------------------------------------------------------- lui s0, 0xbfa0 # Place interrupt I/O port address in s0 #-------------------------------------------------------- # The main function of GENERAL INTERRUPT SERVED ROUTINE # Print CAUSE, EPC, I/O Port to console # K1: a3 = 22 # K2: a3 = 11 # EXT: a3 = 44 #-------------------------------------------------------- lbu a3, 0x0(s0)li t2,0x22li t3,0x11li t4,0x44beqa3,t4,tang_xungand t5,a3,t2beq t5,t2,bam_ct_k1and t5,a3,t3beq t5,t3,bam_ct_k2j donetang_xung:addi s6,s6,1 // tang xung len 1, s6 bien dem xung (chu ky ngat dinh thoi external timer)j donebam_ct_k1:add s4,s6,zero // s4 = thoi diem bam k1 gan nhatbeq s1,0xff,off // neu trang thai den dang la 0xff tuc dang bat --> nhay den off de tat denbeq s1,0x00,on//bam_ct_k2:add s5,s6,zero// s5 = thoi diem bam k2 gan nhatbeq s1,0xff,offbeq s1,0x00,onon:li s1,0xffla t0, LEDSsb s1,0(t0)addi s2,s2,1 // s2 = so lan bat denj doneoff:li s1,0x00la t0, LEDSsb s1,0(t0)addi s3,s3,1 // s3 = so lan tat den done: #-------------------------------------------------------- # Reset the I/O port address to zero after serving interrupt #-------------------------------------------------------- sb zero,0x0(s0) # Acknowledge interrupt, (resets latch) #--------------------------------------------------------# RESTORE the REG FILE from STACK#-------------------------------------------------------- .set noreorder lw ra, 21*4(sp) # Restore the registers from the stack lw t9, 20*4(sp) lw t8, 18*4(sp) lw t7, 18*4(sp) lw t6, 17*4(sp) lw t5, 16*4(sp) lw t4, 15*4(sp) lw t3, 14*4(sp) lw t2, 13*4(sp) lw t1, 12*4(sp) lw t0, 11*4(sp) lw a3, 10*4(sp) lw a2, 9*4(sp) lw a1, 8*4(sp) lw a0, 7*4(sp) lw v1, 6*4(sp) lw v0, 5*4(sp) lw AT, 4*4(sp) addu sp, sp, 22*4 # Return activation record #-------------------------------------------------------- # noreorder must be used here to force the # rfe-instruction to the branch-delay slot jr k1 # Jump to EPC rfe # Return from exception # Restores the status register .set reorder.end introutine

# The only purpose of the stub routine below is to call # the real interrupt routine. It is used because it is # of fixed size and easy to copy to the interrupt start # address location..ent intstub.set noreorderintstub: j introutine nop.set reorder.end intstub

.globl start # Start of the main program.ent startstart:la t0, LEDS// load dia chi cua den vao t0lb s1, 0(t0) // lay trang thai ban dau cua den va la bien luu trang thai cua denli s2, 0 // luu so lan bat denli s3, 0 // luu so lan tat denli s4, 0 // thoi diem bam k1 gan nhatli s5, 0 // thoi diem bam k2 gan nhatli s6, 0// dem xung #---------------------------------------------------------- # Enable IO PIN of K1, K2 buttons #---------------------------------------------------------- lh a0, PIO_SETUP2 # Enable button port interrupts andi a0, 0xbfff sh a0, PIO_SETUP2 #---------------------------------------------------------- # Reset I/O port address before enable interrupts #---------------------------------------------------------- lui t0, 0xbfa0 # Place interrupt I/O port address in t0, t0 = 0xbfa0.0000 sb zero,0x0(t0) # Acknowledge interrupt, (resets latch) #---------------------------------------------------------- # Register the GENERAL INTERRUPT SERVED ROUTINE #---------------------------------------------------------- la t0, intstub # These instructions copy the stub la t1, 0x80000080 # routine to address 0x80000080 lw t2, 0(t0) # Read the first instruction in stub lw t3, 4(t0) # Read the second instruction sw t2, 0(t1) # Store the first instruction sw t3, 4(t1) # Store the second instruction #---------------------------------------------------------- # Set the status register to ENABLE EXPECTED INTERRUPTS # such as K1, K2, timer and ENABLE THE GENERAL INTERRUPT #---------------------------------------------------------- mfc0 v0, C0_SR # Retrieve the status register li v1, ~SR_BEV # Set the BEV bit of the status and v0, v0, v1 # register to 0 (first exception vector) ori v0, v0, 1 # Enable user defined interrupts ori v0, v0,EXT_INT3 # Enable interrupt 3 (K1, K2, timer) mtc0 v0, C0_SR # Update the status register #---------------------------------------------------------- # No-end loop, main program, to demo the effective of interrupt #----------------------------------------------------------Loop:la a0,_menu// in ra menujal printfLUA_CHON:loop1:jal getchar# Nhap su lua chon menuadd a0,v0,zero#addi t7,v0,-48#jal putchar#la a0,enter#jal printf#lit4,1lit5,2lit6,3beqt4,t7,case1beqt5,t7,case2beqt6,t7,case3rechoice:la a0,nhap_laijal printfjloop1Switch:case1://So lan tat/batla a0,slbatadd a1,s2,zerojal printfla a0,sltatadd a1,s3,zerojal printfj break_switch // break

case2:// Thoi diem su dung cac cong tac K1,K2la a0,tdbk1add a1,s4,zerojal printflaa0,tdbk2add a1,s5,zerojal printfj break_switchcase3://--------------------------------------------------// in ra xau cam on va thoat chuong trinh//--------------------------------------------------la a0,thanksjal printfj THOATbreak_switch:j Loop

THOAT:j THOAT.end start