Skip to content

Instantly share code, notes, and snippets.

@caitlynrw
Last active May 13, 2023 12:57
Show Gist options
  • Save caitlynrw/dbf23c7ab4a68643ffcdc435e7d5e9e3 to your computer and use it in GitHub Desktop.
Save caitlynrw/dbf23c7ab4a68643ffcdc435e7d5e9e3 to your computer and use it in GitHub Desktop.
ENGR2721 Practical 5
interrupt_init:
movlw b'11100000' ; Enable global, enable periferal, enable t0
movwf INTCON
movlw b'00000000' ; Set INT1 to falling edge
movwf INTCON2
movlw b'00001000' ; Enable INT1
movwf INTCON3
movlw b'00000001' ; t1 overflow
movwf PIR1
;; dont need to configure PIR2
movlw b'00000001' ; Enable t1
movwf PIE1
;; dont need to configure PIE2
;; dont need to configure PIR, PIR2 (priority is disabled)
bcf RCON, IPEN ; disable interrupt priority
return
INT_handler:
btfsc INTCON3, INT1IF ; skip if INT1 flag bit clear
call INT1_handler
btfsc INTCON, TMR0IF ; skip if TMR0 flag bit clear
call TMR0_handler
btfsc PIR1, TMR1IF ; skip if TMR1 flag bit clear
call TMR1_handler
retfie FAST
;;;---------------------------------------------
;;; ENGR2721 Microprocessors
;;; Prac 5, Pulse Generator
;;; Caitlyn Pascoe, 12/05/2023
;;;
;;; S3 = DAC, LCD
;;; S4 = AN0, AN1, PB0, PB1
;;;---------------------------------------------
#include <p18f452.inc>
config OSC = HS
config BOR = OFF, WDT = OFF, LVP = OFF
org 0x0000
goto start
org 0x0008
goto INT_handler
org 0x0020
constant delay = d'125000'
constant length = d'250000'
cblock
pulse_delay
pulse_length
endc
#include "../include/analog.inc"
#include "interrupts.inc"
#include "../include/LCD.inc"
#include "../include/ops.inc"
#include "../include/table.inc"
#include "timer.inc"
start:
call analog_init
call timer_init
call interrupt_init
call LCD_init
bsf TRISB, 0 ; Set PB0 to input
bsf TRISB, 1 ; Set PB1 to output
loop:
call ADC_0
movwf pulse_delay
foreach TEXT_delay, LCD_ASCII
call LCD_BCD
call ADC_1
movwf pulse_length
foreach TEXT_length, LCD_ASCII
call LCD_BCD
goto loop
INT1_handler:
bcf INTCON3, INT1IF ; clear INT flag bit
call timer_reset
movlw h'FF' ; 5V for DAC values
call DAC_A ; set DAC_A high to begin trigger
; Set delay timer
movf pulse_delay, w
negf WREG
movwf TMR0H
clrf TMR0L
bsf T0CON, TMR0ON ; start timer 0 to time delay
return
TMR0_handler:
movlw h'FF' ; 5V for DAC values
call DAC_B ; set DAC_B high to begin pulse
; Set length timer
movf pulse_length, w
negf WREG
movwf TMR1H
clrf TMR1L
bsf T1CON, TMR1ON ; start timer 1 to time length
return
TMR1_handler:
clrf WREG
call DAC_A
call DAC_B ; set DAC_A and DAC_B low to end cycle
TEXT_delay:
dw 'Delay: '
TEXT_length:
dw 'Length: '
end
;;;=============================================
;;; PIC Trainer timer support
;;;=============================================
timer_init:
movlw b'00000010' ; 16-bit, internal clock, prescaler, 1:8
movwf T0CON
movlw b'10110000' ; 16-bit, 1:8, internal clock
movwf T1CON
return
timer_reset:
bcf T0CON, TMR0ON ; Stop timer0
clrf TMR0L ; Clear timer0 value
clrf TMR0H
bcf T1CON, TMR1ON ; Stop timer1
clrf TMR1L ; Clear timer1 value
clrf TMR1H
return
timer0_load macro value
movlw high(-value)
movwf TMR0H
movlw low(-value)
movwf TMR0L
endm
timer1_load macro value
movlw high(-value)
movwf TMR1H
movlw low(-value)
movwf TMR1L
endm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment