Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ; Write an assembly code that repeatedly cycles RGB LED
- ; from red to green to blue (holding each color for 0.5 seconds)
- ; in main code. Use the timer functionality to cycle one motor
- ; at a time through the following steps (0.3 seconds per step):
- ; 1. Right motor forward at 100% power
- ; 2. Reduce power to 50%
- ; 3. Left motor backward at 100% power
- ; 4. Reduce power to 50%
- ; 5. Left motor forward at 100% power
- ; 6. Reduce power to 50%
- ; 7. Right motor backward at 100% power
- ; 8. Reduce power to 50%
- ; 9. Go to step 1
- list p=18F1220
- radix hex
- config WDT=OFF,LVP=OFF,OSC=INTIO2
- #include p18F1220.inc
- #define dCount 0x80
- #define dCountInner 0x81
- #define WregShadow 0x82
- #define CurrentStateMotor 0x83
- #define CurrentStateLED 0x84
- ; There are a few states we could be in
- ; 0 Right motor - forward 100%
- ; 1 Right motor - forward 50%
- ; 2 Left motor - backward 100%
- ; 3 Left motor - backward 50%
- ; 4 Left motor - forward 100%
- ; 5 Left motor - forward 50%
- ; 6 Right motor - backward 100%
- ; 7 Right motor - backward 50%
- org 0x00
- GOTO Init
- org 0x08 ; HTimer 0 interrupt (for RGB LED cycling)
- GOTO RGBCycle
- org 0x18 ; Timer 1 interrupt (for motor cycling)
- GOTO MotorCycle
- org 0x20
- Init:
- ; Setting up EDbot
- CLRF PORTA
- CLRF PORTB
- MOVLW 0x7F
- MOVWF ADCON1
- MOVLW 0x35
- MOVWF TRISA
- MOVLW 0xC3
- MOVWF TRISB
- ; Set the internal system clock to 4 MHz
- ; This means that each instruction cycle will take 1 microsecond
- MOVLW 0x70
- IORWF OSCCON
- ; Setting up timer
- BSF INTCON, PEIE ; enable peripheral interrupts
- BSF INTCON, TMR0IE ; enable TMR0 interrupt
- BSF INTCON2, TMR0IP ; set TMR0 to high priority
- BSF RCON, IPEN ; priority levels enabled
- BCF INTCON, TMR0IF ; clear TMR0 flag so it can fire
- BSF PIE1, TMR1IE ; enable TMR1 interrupt
- BCF IPR1, TMR1IP ; set TMR1 to low priority
- BCF PIR1, TMR1IF ; clear TMR1 flag so it can fire
- ; Timer 0 should be firing every 0.5 seconds
- ; Setting data for T0CON
- ; 0100 0001
- ; Bit Meaning Value
- ; ==============================================
- ; 7 TMR0ON 0 (off - we don't want it firing yet)
- ; 6 T08BIT 0 (16-bit mode)
- ; 5 T0CS 0 (internal clock)
- ; 4 T0SE 0 (rising edge)
- ; 3 PSA 0 (prescaler assigned)
- ; 2 T0PS2 0
- ; 1 T0PS1 1
- ; 0 T0PS0 0 (1:8 prescale value)
- MOVLW 0x02
- MOVWF T0CON ; 1 timer tick takes 8 microseconds
- ; thus, it will take 8 * 65536 = 524288 microsecs
- ; = approx 0.52 seconds to interrupt (close enough)
- ; 8x = 500000
- ; x = 62500
- ; 65536 - 62500 = 3036 (0x0BDC)
- MOVLW 0x0B
- MOVWF TMR0H
- MOVLW 0xDC
- MOVWF TMR0L
- CLRF TMR0H
- CLRF TMR0L
- ; Timer 1 should fire every 0.3 seconds
- ; Setting data for T1CON
- ; 1111 1000
- ; Bit Meaning Value
- ; ==============================================
- ; 7 RD16 1 (16-bit read mode)
- ; 6 T1RUN 1 (Use TMR1 oscillator)
- ; 5 T1CKPS1 1
- ; 4 T1CKPS0 1 (1:8 Prescale amount)
- ; 3 T1OSCEN 1 (TMR1 oscillator enabled)
- ; 2 T1SYNC 0 (Useless)
- ; 1 TMR1CS 0 (Internal clock)
- ; 0 TMR1ON 0 (Not enabled for now)
- MOVLW 0xF8
- MOVWF T1CON
- ; 1 timer tick takes 8 microseconds
- ; thus, it will take 8 * 65536 = 524288 microsecs
- ; = approx 0.52 seconds to interrupt
- ; this is NOT GOOD
- ; we want it to take 0.3 secs = 300000 microsecs
- ; 8x = 300000
- ; x = 37500
- ; 65536 - 37500 = 28036 (0x6D84)
- MOVLW 0x6D
- MOVWF TMR1H
- MOVLW 0x84
- MOVWF TMR1L
- BSF T1CON, TMR1ON ; enable TMR1
- BSF T0CON, TMR0ON ; enable TMR0
- BSF INTCON, GIE ; enable interrupts globally
- Loop:
- BRA Loop
- RGBCycle:
- BTG PORTB, 2 ; toggle Red LED
- MOVLW 0x0B
- MOVWF TMR0H
- MOVLW 0xCD
- MOVWF TMR0L
- BCF INTCON, TMR0IF
- RETFIE
- MotorCycle:
- ;BTG PORTA, 3 ; toggle Green LED
- ; CurrentState
- INCF CurrentStateMotor
- ; Let's make sure we're not at a state > 8
- MOVLW .8
- SUBWF CurrentStateMotor
- BZ ResetMotorCycle
- FinishMotorCycle:
- MOVLW 0x6D
- MOVWF TMR1H
- MOVLW 0x84
- MOVWF TMR1L
- BCF PIR1, TMR1IF
- RETFIE
- ResetMotorCycle:
- CLRF CurrentStateMotor
- BRA FinishMotorCycle
- END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement