Advertisement
Slyfoxx724

LAB6 EXP1

Mar 22nd, 2017
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.44 KB | None | 0 0
  1. list p=18F1220 ; processor type
  2. radix hex ; default radix for data
  3. ; Disable Watchdog timer, Low V. Prog, and RA6 as a clock
  4. config WDT=OFF, LVP=OFF, OSC = INTIO2
  5.  
  6. #include p18f1220.inc
  7. #define lastL 0x80 ; Last L Sensor Value
  8. #define loopCount 0x81 ; Timer Loop Count
  9. #define countL 0x82 ; Count the cycles we have had echoL on
  10. #define countOD 0x83 ; Count for outer delay loop
  11. #define countID 0x84 ; Count for inner delay loop
  12.  
  13. ;these are shortcuts, string replacements
  14. #define _TrigL PORTA,RA1
  15. #define _TrigR PORTA,RA4
  16. #define _EchoL PORTA,RA0
  17.  
  18. org 0x000 ; Executes after reset, equivalent to org
  19.  
  20. GOTO StartL
  21. org 0x008 ; Executes after high priority interrupt
  22.  
  23. GOTO HPRIO
  24. org 0x020 ; Start of the code
  25.  
  26. HPRIO:
  27. BTFSC PIR1, TMR2IF ; high priority loop
  28. BRA iLoop
  29. RETFIE ; return from interrupt
  30.  
  31. iLoop:
  32. INCF loopCount
  33. MOVLW .120
  34. CPFSLT loopCount
  35. BRA doTrigger ; trigger every 30,000 uSec.
  36.  
  37. MOVLW .1
  38. CPFSGT loopCount
  39. BRA stopTrigger
  40. ; we didn't trigger so update
  41. BRA updateSensor
  42.  
  43. doTrigger:
  44. CLRF loopCount
  45. BRA doTriggerL
  46.  
  47. doTriggerL:
  48. MOVFF countL,lastL
  49. ; we should check to see if echo is high and kill trigger if that's the case.
  50. BTFSC _EchoL
  51. BRA killL
  52.  
  53. continueL:
  54. BSF _TrigL ; Set Left trigger on
  55. CLRF countL ; clear count of eccho
  56. BRA loopDone
  57.  
  58. killL: ; Sensors is known to hang whne when no object is found within its
  59. ; Measurement range - Noise is known to reset the sensor.
  60. ; So here, we are using the left sensor to reset right sensor.
  61. ; Sensors work best with 4.5-5.5 v supply voltage.
  62. BSF _TrigR ; start trigger or right sensor
  63. MOVLW .1 ; 1 millisecond
  64. CALL Delay
  65. BCF _TrigR ; Clear right trigger on
  66. MOVLW .1 ; 1 millisecond
  67. CALL Delay
  68. ; If Echo is not cleared then try to reset it again
  69. BTFSS _EchoL
  70. BRA continueL
  71. BRA killL
  72.  
  73. stopTrigger:
  74. BCF _TrigL ; Set Left trigger off
  75. BRA loopDone
  76.  
  77. updateSensor:
  78. ;increment count for each cycle echo is on
  79. btfsc _EchoL
  80. incf countL
  81. bra loopDone
  82.  
  83. loopDone:
  84. bcf PIR1, TMR2IF ; Clear Timer 2 interrupt Flag
  85. bra HPRIO ; Go to start and service any pending Interrupt
  86.  
  87. StartL:
  88. ; Initialize all I/O ports per EDbot Specifications
  89. MOVLW 0x7F
  90. MOVWF ADCON1 ; Set all Port A Pins as digital
  91. CLRF PORTA ; Initialize PORTA
  92. CLRF PORTB ; Initialize PORTB
  93. MOVLW 0x0D
  94. MOVWF TRISA ; Set Port A direction
  95. MOVLW 0xC7
  96. MOVWF TRISB ; Set Port B direction
  97. MOVLW 0x60
  98. IORWF OSCCON ; Set to 4mhz
  99. ; Clear Sensor related counter
  100. CLRF lastL
  101. CLRF loopCount
  102.  
  103. BSF INTCON, PEIE ; enable peripheral interrupts
  104. ; Enable Timer2 Interrupat as high priority
  105. BSF PIE1, TMR2IE
  106. BSF IPR1, TMR2IP
  107. CLRF TMR2
  108. CLRF T2CON ; Timer 2 is set to 8-bit with no scaling
  109. MOVLW 0xFA ; Timer 2 is set to interrupt in 250 uSec.
  110. MOVWF PR2
  111. BSF T2CON,TMR2ON ; enable TMR2
  112. BSF INTCON, GIE ; enable interrupts globally
  113. BSF PORTB, 3 ;enable right motor
  114. BSF PORTB, 4 ;enable left motor
  115.  
  116. Mloop:
  117. BCF PORTB,RB5 ; turn off LED
  118. MOVLW .6 ; 10 inch stopping distance
  119. CPFSGT lastL ; skip if LastL > wreg
  120. CALL Motors
  121. BRA Mloop
  122. ;Function to delay for Wreg miliseconds
  123.  
  124. Motors:
  125. BSF PORTB,5
  126. BCF PORTA,6
  127. BCF PORTA,7
  128. MOVLW .10
  129. CALL Delay
  130. BSF PORTA,6
  131. CALL Delay
  132. BSF PORTA,7
  133. RETURN
  134.  
  135. Delay:
  136. MOVWF countOD
  137.  
  138. DelayOL: ; delay Outer loop
  139. CLRF countID
  140.  
  141. DelayIL: ; Delay Inner Loop
  142. NOP
  143. INCF countID
  144. BNZ DelayIL
  145. DECF countOD
  146. BNZ DelayOL
  147. RETURN ; end delay function
  148.  
  149. end ; end of code
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement