; Low frequency cut off detector
; Started 24/03/16
; Version 01
;
; 25/03/16
; Version 02
; Changing to use timer1 interrupting
; Using internal oscillator 4.0 MHZ Fosc/4 = 1 MHZ (Period = 1 uS)
list p=12f629
errorlevel +302
include "p12f629.inc"
;*******************************************************************************
; Configuration Bits
;*******************************************************************************
__CONFIG _CPD_OFF & _CP_OFF & _BODEN_ON & _MCLRE_OFF & _PWRTE_ON & _WDT_OFF & _INTRC_OSC_NOCLKOUT ;Internal oscillator
; __CONFIG _CPD_OFF & _CP_OFF & _BODEN_ON & _MCLRE_OFF & _PWRTE_ON & _WDT_OFF & _HS_OSC & _EXTRC_OSC_NOCLKOUT ; Crystal
;*******************************************************************************
; Constants
;*******************************************************************************
RAM_START equ 0x20
RAM_SIZE equ 0x40
TMR1_Counts equ D'1000'
Max_Retrig_Count equ D'50' ;Max number of milliseconds allowed between input pulses
Delay_On_Count equ D'20' ;Number of input pulses before Enable is set.
SDELAY_18_8uS equ D'16'
SDELAY_81_6uS equ D'68'
;I/O ports
; GPIO 0 Pin 7 Enable output
; GPIO 1 Pin 6 Input pulse
; GPIO 2 Pin 5 (Int) (Use for testing as one shot output.)
; GPIO 3 Pin 4 (Can only be used as an input.)
; GPIO 4 Pin 3
; GPIO 5 Pin 2
;
;*******************************************************************************
; Pin Assignments
;*******************************************************************************
#DEFINE Pulse_in GPIO, 1
;*******************************************************************************
; File Register Variables
;*******************************************************************************
cblock RAM_START
;param1 ; parameter 1 for function calls (Only seems to be used in delay routine)
;param2 ; parameter 2 for function calls (Only seems to be used in delay routine)
;temp1 ; temporary scratch variable (Used in dealy routine
Count_L ;Holds value to be loaded into TMR1 Low byte
Count_H ;Holds value to be loaded into TMR1 High byte
Retrig_Count ;Counts number of milliseconds since it was reset by input pulse
;So for minimum input frequency of 20 HZ then if this count reaches 50 then set One_shot_out low
Delay_Count
Flags ;(Other bits not used)
; Bit 0 represents "One_shot_out"
; Bit 1
endc
;*******************************************************************************
; Reset Vector
;*******************************************************************************
org 0x000 ; processor reset vector
goto main ; jump to main function
;*******************************************************************************
; Interrupt Vector
;*******************************************************************************
; For 1 mS interrupts we need to preset the timer register to (65536 - 1000) = 64536 = 0xFC18
org 0x004 ; processor reset vector
btfss PIR1,TMR1IF ;Is it a TMR1 interrupt ?
GOTO T1_02 ;No - return from interrupt
bcf PIR1,TMR1IF
bcf STATUS,RP0 ; bank 0
bcf T1CON,TMR1ON ; stop timer 1 (1 uS) (1 cycle)
;Load TMR1 for next delay.
movlw LOW TMR1_Counts ;
movwf Count_L ;
movlw HIGH TMR1_Counts ;
movwf Count_H
COMF Count_L, W ; (1 uS) (1 cycle)
MOVWF TMR1L ; timer 1 (1 uS) (1 cycle)
COMF Count_H, W
MOVWF TMR1H ; timer 1 (1 uS) (1 cycle)
BSF T1CON,TMR1ON ; Start timer 1 (1 uS) (1 cycle) (5 cycles from start of interrupt)
MOVLW Max_Retrig_Count
SUBWF Retrig_Count, W
BTFSC STATUS, Z
GOTO T1_01
INCF Retrig_Count
GOTO T1_02
T1_01:
BCF Flags,0
BCF GPIO, 2 ;
T1_02:
RETFIE ; (2 uS) (2 cycles)
;*******************************************************************************
; Function: init
; Description: Intialize the PIC registers
; Parameters: None
; Returns: None
;*******************************************************************************
init:
bsf STATUS,RP0 ; bank 1
MOVLW B'00000010' ;GPIO 1 as intput. All others outputs
movwf TRISIO ; 1 - input, 0 - output
; movlw B'00000111' ; prescaler (128) internal clock and /256, 1.0 uS * 256 = 128 uS (Overflow every 51.2 * 256 = 13.1072 mS)
; movwf OPTION_REG ; pullups enabled
bcf STATUS,RP0 ; bank 0
movlw 0x7 ; turn off the comparator module
movwf CMCON ; 111 - comparator off
bsf T1CON,TMR1ON ; enable timer1 All other bits 0 1:1 Prescaler ,Internal clock Fosc/4
bsf STATUS,RP0 ; bank 1
BSF PIE1, TMR1IE ;Enable Timer 1 interrupt
bcf STATUS,RP0 ; bank 0
BSF INTCON, PEIE ;Enable peripheral Interrupts
BSF INTCON, GIE ;Emable global interrupts
return
;*******************************************************************************
; Main Function
;*******************************************************************************
main:
BCF INTCON, GIE ;Disable global interrupts
;
call init ; initialize PIC registers
;
Wait_High:
WH_loop:
BTFSS Pulse_in ;Wait for pulse to be low
GOTO WH01
GOTO WH_loop
WH01:
BTFSS Pulse_in ;Wait for pulse to be high
GOTO WH01
WH02:
CLRF Retrig_Count
BTFSS Flags,0
CLRF Delay_Count
MOVLW Delay_On_Count
SUBWF Delay_Count, W
BTFSC STATUS, Z
GOTO WH03
INCF Delay_Count
BCF GPIO, 0 ;Enable out
GOTO WH04
WH03:
BSF GPIO, 0 ;Enable out
WH04:
BSF GPIO, 2
BSF Flags,0
GOTO WH_loop
end