Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;*-------------------------------------------------------------------
- ;* Name: lab_4_program.s
- ;* Purpose: A sample style for lab-4
- ;* Term: Winter 2013
- ;*-------------------------------------------------------------------
- THUMB ; Declare THUMB instruction set
- AREA My_code, CODE, READONLY ;
- EXPORT __MAIN ; Label __MAIN is used externally
- ENTRY
- __MAIN
- LDR R10, = ISER0
- LDR R1, [R10]
- ORR R1, R1, #0x200000 ; setting bit 21 of ISER0
- STR R1, [R10] ; store the value into ISER0 to enable EINT3
- MOV R6, #0x1 ; R6 is getting set a as a flag, 1 when not pressed, 0 when pressed
- LDR R10, = IO2IntEnf
- LDR R1, [R10]
- ORR R1, R1, #0x400
- STR R1, [R10]
- ; falling edge because that's when the button is pressed, raising edge doesn't record this
- ; The following lines are similar to previous labs.
- ; They just turn off all LEDs
- LDR R10, =LED_BASE_ADR ; R10 is a pointer to the base address for the LEDs
- MOV R3, #0xB0000000 ; Turn off three LEDs on port 1
- STR R3, [r10, #0x20]
- MOV R3, #0x0000007C
- STR R3, [R10, #0x40] ; Turn off five LEDs on port 2
- ; This line is very important in your main program
- ; Initializes R11 to a 16-bit non-zero value and NOTHING else can write to R11 !!
- MOV R11, #0xABCD ; Init the random number generator with a non-zero number
- BL RNG
- ; going to use R12 and so on
- MOV R2, R11
- AND R2, R2, #0x7 ; Take last 3 bits of R11 (#0x7 = 111 in binary)
- ADD R2, R2, #2 ; AND to store last 3 bits of R11 in R5
- AND R3, R11, #0x10 ; Take 4th bit of R11
- LSR R3, R3, #4 ; Shift it to make the bit LSB for adding
- ADD R2, R2, R3 ; After adding, R5 has random number between 2-10
- MOV R3, #10000
- MUL R2, R2, R3 ; Multiply R5 by 10000
- ADD R0, R2, #0 ; Store into R0 for delay decrementer
- MOV R9, #0x007D
- MOVT R9, #0x0 ; Takes 0.1 ms (approx) to subtract to 0
- MUL R0, R9, R0 ; R0 now has a number that takes between 2-10 seconds to subtract to 0
- BL LED_ALT
- ;
- ; Your main program can appear here
- ;
- ; 4C4B4 for 0.25 second
- ; try not to use R6
- ; 4FFFFF83 for turning all of them on
- ; need some work
- LED_ALT STMFD R13!, {R1-R12, R14}
- MOV R4, #0xC4B4
- MOVT R4, #0x0004
- MOV R5, R0 ; copy over the value of R0
- MOV R0, R4 ; calls the .25 delay
- BL DELAY ; call DELAY
- SUBS R5, R5, R4
- ADD R0, R5, #0
- BL LED_OFF
- ; for lighting the second four (left side)
- MOV R3, #0xFFFB
- MOVT R3, #0x4FFF
- STR R3, [R10, #0x20]
- STR R3, [R10, #0x40]
- MOV R4, #0xC4B4
- MOVT R4, #0x0004
- MOV R5, R0 ; copy over the value of R0
- MOV R0, R4 ; calls the .25 delay
- BL DELAY ; call DELAY
- SUBS R5, R5, R4
- ADD R0, R5, #0
- BL LED_OFF
- ; for lighting the first four (right side)
- MOV R3, #0x00000004
- STR R3, [R10, #0x40]
- BLT LED_ALT
- LDMFD R13!, {R1-R12, R15}
- LED_OFF STMFD R13!, {R3, R14}
- MOV R3, #0xB0000000 ; Turn off three LEDs on port 1
- STR R3, [R10, #0x20] ; Write the necessary bits to turn off the LED on port 1
- MOV R3, #0x0000007C ; Move the necessary number for turning off port 2
- STR R3, [R10, #0x40] ; Turn off five LEDs on port 2
- LDMFD R13!, {R3, R15}
- ;*-------------------------------------------------------------------
- ; Subroutine RNG ... Generates a pseudo-Random Number in R11
- ;*-------------------------------------------------------------------
- ; R11 holds a random number as per the Linear feedback shift register (Fibonacci) on WikiPedia
- ; R11 MUST be initialized to a non-zero 16-bit value at the start of the program
- ; R11 can be read anywhere in the code but must only be written to by this subroutine
- RNG STMFD R13!,{R1-R3, R14} ; Random Number Generator
- AND R1, R11, #0x8000
- AND R2, R11, #0x2000
- LSL R2, #2
- EOR R3, R1, R2
- AND R1, R11, #0x1000
- LSL R1, #3
- EOR R3, R3, R1
- AND R1, R11, #0x0400
- LSL R1, #5
- EOR R3, R3, R1 ; The new bit to go into the LSB is present
- LSR R3, #15
- LSL R11, #1
- ORR R11, R11, R3
- LDMFD R13!,{R1-R3, R15}
- ;*-------------------------------------------------------------------
- ; Subroutine DELAY ... Causes a delay of 1ms * R0 times
- ;*-------------------------------------------------------------------
- DELAY STMFD R13!,{R0, R14}
- ; MUL R9, R9, R0 ; code to generate a delay of 0.1mS * R0 times
- loopDelay SUBS R0, R0, #1 ; Keep subtracting until we have reached 0
- BNE loopDelay
- ;
- exitDelay LDMFD R13!,{R0, R15}
- ; The Interrupt Service Routine MUST be in the startup file for simulation
- ; to work correctly. Add it where there is the label "EINT3_IRQHandler
- ;
- ;*-------------------------------------------------------------------
- ; Interrupt Service Routine (ISR) for EINT3_IRQHandler
- ;*-------------------------------------------------------------------
- ; This ISR handles the interrupt triggered when the INT0 push-button is pressed
- ; with the assumption that the interrupt activation is done in the main program
- EINT3_IRQHandler
- ;STMFD ... ; Use this command if you need it
- ;
- ; Code that handles the interrupt
- ;
- ;LDMFD ... ; Use this command if you used STMFD (otherwise use BX LR)
- MOV R6, #0x00 ; clearing the interrupt
- LDR R10, =IO2IntEnf
- LDR R1, [R10]
- EOR R1, R1, #0X400
- STR R1, [R10]
- BX LR
- ;*-------------------------------------------------------------------
- ; Below is a list of useful registers with their respective memory addresses.
- ;*-------------------------------------------------------------------
- LED_BASE_ADR EQU 0x2009c000 ; Base address of the memory that controls the LEDs
- PINSEL3 EQU 0x4002C00C ; Pin Select Register 3 for P1[31:16]
- PINSEL4 EQU 0x4002C010 ; Pin Select Register 4 for P2[15:0]
- FIO1DIR EQU 0x2009C020 ; Fast Input Output Direction Register for Port 1
- FIO2DIR EQU 0x2009C040 ; Fast Input Output Direction Register for Port 2
- FIO1SET EQU 0x2009C038 ; Fast Input Output Set Register for Port 1
- FIO2SET EQU 0x2009C058 ; Fast Input Output Set Register for Port 2
- FIO1CLR EQU 0x2009C03C ; Fast Input Output Clear Register for Port 1
- FIO2CLR EQU 0x2009C05C ; Fast Input Output Clear Register for Port 2
- IO2IntEnf EQU 0x400280B4 ; GPIO Interrupt Enable for port 2 Falling Edge
- ISER0 EQU 0xE000E100 ; Interrupt Set-Enable Register 0
- ALIGN
- END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement