Hello,
I would like to use pulsin to record the length of a positive pulse, thus I would use
pulsin trigger,1,pulse_length
if trigger is a pin name and pulse_length is a word variable
As I don't know when the trigger will happen, can I jump to the pulsin when I detect a low to high change on a pin (using a polling loop or an interrupt) or is PULSIN expecting to see the rising edge.
As I understand it from the manual, it will record the pulse even if already on and will stop recording once the pin goes low, it it right?
I don't know if my question is clear enough?
Code:
'*******************************************************************************
'preset the pic specs and config words at chip programming time
@ device pic16F684, intrc_osc_noclkout, BOD_OFF, PWRT_OFF, wdt_off, mclr_off, protect_off
'*******************************************************************************
'*******************************************************************************
DEFINE OSC 8 'set OSC value to 8MHz for instructions PAUSE, PAUSEus and SOUND
'*******************************************************************************
'*******************************************************************************
'VARIABLES declaration
'IF VARIABLES USED FOUR COUNTER GO OVER 256, USE WORD INSTEAD
pulse_length var word
'*******************************************************************************
CLEAR 'clears all data ram (128 bytes) and initialize variables (this should however be done in software)
INTCON = 0 'disable interrupts and reset int flags
IOCA = %000000
'*******************************************************************************
'PWM (single output mode) related registers for porta.5.
'PWM is not used. We use normal output on C5.
T2CON = %00000000 'value:4, 1:1 postscale (unused), Timer2 is OFF, Timer 2 Clock prescaler = 1
PR2 = %00110001 'value: 49, PWM Period: 40.0KHz
CCP1CON = %00000000 'capture/compare/PWM module OFF, no pwm signal on portc.5 at this time
CCPR1L = %00001111 'value: 15, PWM Duty Cycle 8 MSB of 10 bits (2 LSB in bits 5 and 4 of CCP1CON), total: 60d
'*******************************************************************************
'*******************************************************************************
'Setting registers
OSCCON = %01110001
CMCON0 = %00000111 'comparators OFF, val = 7
ANSEL = %00000000 'choose digital i/o, val = 0
OPTION_REG = %01111111 'enable porta weak pullups (no weak pull-up available for porta.3), p12
'*******************************************************************************
'*******************************************************************************
'Setting ports states, direction and internal weak pull-ups
'USE 10K WPU on RA3 if used as input !!!
TRISA = %000001 'set porta pins directions (1=input, high impedance),portA.3 is always input pin (datasheet p5)
TRISC = %000000
'use 10K WPU on A3
WPUA = %000000
PORTA = %000000 'set port pins logic, A0 will be a default LOW with 100K WPD
PORTC = %000000 'set port pins logic
'*******************************************************************************
'*******************************************************************************
'aliasing pins
Symbol trigger = PORTA.0
SYMBOL led = PORTC.3
'*******************************************************************************
PAUSE 3000 'let the power level stabilize if not already done
'flashes at 5Hz
for i = 0 to 4
High led
PAUSE 100
LOW led
PAUSE 100
NEXT i
PAUSE 2000
start:
While (trigger == 0) : WEND 'while pin is low (no pulse on pin)
'a pulse has been detected (at this step the rising edge is over, the pin is high)
PULSIN trigger,1,pulse_length '16 bits
WRITE 0,pulse_length.lowbyte
WRITE 1,pulse_length.highbyte
'flash when recording occured and finished (max 327ms with 8MHz 5 us resolution 65536 increments)
HIGH led
PAUSE 2000
LOW led
goto start
END 'end of program
Bookmarks