SHARE
TWEET

Untitled

a guest Aug 13th, 2019 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;   Write an assembly code that repeatedly cycles RGB LED
  2. ; from red to green to blue (holding each color for 0.5 seconds)
  3. ; in main code. Use the timer functionality to cycle one motor
  4. ; at a time through the following steps (0.3 seconds per step):
  5.  
  6. ;   1. Right motor forward at 100% power
  7. ;   2. Reduce power to 50%
  8. ;   3. Left motor backward at 100% power
  9. ;   4. Reduce power to 50%
  10. ;   5. Left motor forward at 100% power
  11. ;   6. Reduce power to 50%
  12. ;   7. Right motor backward at 100% power
  13. ;   8. Reduce power to 50%
  14. ;   9. Go to step 1
  15.  
  16.    
  17.     list p=18F1220
  18.     radix hex
  19.     config WDT=OFF,LVP=OFF,OSC=INTIO2
  20.  
  21. #include p18F1220.inc
  22.  
  23. #define dCount 0x80
  24. #define dCountInner 0x81
  25. #define WregShadow 0x82
  26.    
  27. #define CurrentStateMotor 0x83
  28. #define CurrentStateLED 0x84
  29.    
  30.     ; There are a few states we could be in
  31.     ; 0 Right motor -   forward     100%
  32.     ; 1 Right motor -   forward     50%
  33.    
  34.     ; 2 Left motor  -   backward    100%
  35.     ; 3 Left motor  -   backward    50%
  36.    
  37.     ; 4 Left motor  -   forward     100%
  38.     ; 5 Left motor  -   forward     50%
  39.    
  40.     ; 6 Right motor -   backward    100%
  41.     ; 7 Right motor -   backward    50%
  42.    
  43.     org 0x00
  44.     GOTO Init
  45.    
  46.     org 0x08 ; HTimer 0 interrupt (for RGB LED cycling)
  47.     GOTO RGBCycle
  48.    
  49.     org 0x18    ; Timer 1 interrupt (for motor cycling)
  50.     GOTO MotorCycle
  51.    
  52.     org 0x20
  53. Init:
  54.     ; Setting up EDbot
  55.     CLRF PORTA
  56.     CLRF PORTB
  57.     MOVLW 0x7F
  58.     MOVWF ADCON1
  59.     MOVLW 0x35
  60.     MOVWF TRISA
  61.     MOVLW 0xC3
  62.     MOVWF TRISB
  63.    
  64.     ; Set the internal system clock to 4 MHz
  65.     ; This means that each instruction cycle will take 1 microsecond
  66.     MOVLW 0x70
  67.     IORWF OSCCON
  68.    
  69.     ; Setting up timer
  70.     BSF INTCON, PEIE        ; enable peripheral interrupts
  71.     BSF INTCON, TMR0IE      ; enable TMR0 interrupt
  72.     BSF INTCON2, TMR0IP     ; set TMR0 to high priority
  73.     BSF RCON, IPEN      ; priority levels enabled
  74.     BCF INTCON, TMR0IF      ; clear TMR0 flag so it can fire
  75.    
  76.     BSF PIE1, TMR1IE        ; enable TMR1 interrupt
  77.     BCF IPR1, TMR1IP        ; set TMR1 to low priority
  78.     BCF PIR1, TMR1IF        ; clear TMR1 flag so it can fire
  79.    
  80.     ; Timer 0 should be firing every 0.5 seconds
  81.    
  82.     ; Setting data for T0CON
  83.     ; 0100 0001
  84.     ; Bit   Meaning     Value
  85.     ; ==============================================
  86.     ; 7     TMR0ON      0   (off - we don't want it firing yet)
  87.     ; 6     T08BIT      0   (16-bit mode)
  88.     ; 5     T0CS        0   (internal clock)
  89.     ; 4     T0SE        0   (rising edge)
  90.    
  91.     ; 3     PSA     0   (prescaler assigned)
  92.     ; 2     T0PS2       0
  93.     ; 1     T0PS1       1
  94.     ; 0     T0PS0       0   (1:8 prescale value)
  95.    
  96.     MOVLW 0x02
  97.     MOVWF T0CON         ; 1 timer tick takes 8 microseconds
  98.                 ; thus, it will take 8 * 65536 = 524288 microsecs
  99.                 ; = approx 0.52 seconds to interrupt (close enough)
  100.                
  101.                 ; 8x = 500000
  102.                 ;  x =  62500
  103.                 ; 65536 - 62500 = 3036 (0x0BDC)
  104.                
  105.     MOVLW 0x0B
  106.     MOVWF TMR0H
  107.     MOVLW 0xDC
  108.     MOVWF TMR0L
  109.                
  110.     CLRF TMR0H
  111.     CLRF TMR0L
  112.    
  113.    
  114.    
  115.     ; Timer 1 should fire every 0.3 seconds
  116.     ; Setting data for T1CON
  117.     ; 1111 1000
  118.     ; Bit   Meaning     Value
  119.     ; ==============================================
  120.     ; 7     RD16        1   (16-bit read mode)
  121.     ; 6     T1RUN       1   (Use TMR1 oscillator)
  122.     ; 5     T1CKPS1     1  
  123.     ; 4     T1CKPS0     1   (1:8 Prescale amount)
  124.    
  125.     ; 3     T1OSCEN     1   (TMR1 oscillator enabled)
  126.     ; 2     T1SYNC      0   (Useless)
  127.     ; 1     TMR1CS      0   (Internal clock)
  128.     ; 0     TMR1ON      0   (Not enabled for now)
  129.    
  130.     MOVLW 0xF8
  131.     MOVWF T1CON
  132.    
  133.     ; 1 timer tick takes 8 microseconds
  134.     ; thus, it will take 8 * 65536 = 524288 microsecs
  135.     ; = approx 0.52 seconds to interrupt
  136.     ; this is NOT GOOD
  137.     ; we want it to take 0.3 secs = 300000 microsecs
  138.     ; 8x = 300000
  139.     ; x  =  37500
  140.    
  141.    
  142.     ; 65536 - 37500 = 28036 (0x6D84)
  143.    
  144.     MOVLW 0x6D
  145.     MOVWF TMR1H
  146.     MOVLW 0x84
  147.     MOVWF TMR1L
  148.    
  149.    
  150.     BSF T1CON, TMR1ON       ; enable TMR1
  151.     BSF T0CON, TMR0ON       ; enable TMR0
  152.     BSF INTCON, GIE     ; enable interrupts globally
  153.    
  154. Loop:
  155.     BRA Loop
  156.  
  157.    
  158. RGBCycle:
  159.     BTG PORTB, 2 ; toggle Red LED
  160.    
  161.     MOVLW 0x0B
  162.     MOVWF TMR0H
  163.     MOVLW 0xCD
  164.     MOVWF TMR0L
  165.     BCF INTCON, TMR0IF
  166.     RETFIE
  167.    
  168. MotorCycle:
  169.     ;BTG PORTA, 3 ; toggle Green LED
  170.    
  171.     ; CurrentState
  172.     INCF CurrentStateMotor
  173.    
  174.     ; Let's make sure we're not at a state > 8
  175.     MOVLW .8
  176.     SUBWF CurrentStateMotor
  177.     BZ ResetMotorCycle
  178.    
  179. FinishMotorCycle:
  180.     MOVLW 0x6D
  181.     MOVWF TMR1H
  182.     MOVLW 0x84
  183.     MOVWF TMR1L
  184.     BCF PIR1, TMR1IF
  185.     RETFIE
  186.    
  187. ResetMotorCycle:
  188.     CLRF CurrentStateMotor
  189.     BRA FinishMotorCycle
  190.    
  191.     END
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top