hi, im having a pretty stupid problem. using a 18f2431
i currently have my pic capturing a signal, it measures the period of the signal. the signal im inputting is at 19.45/64 khz so it has a period of about 3280 microseconds. i then use this for a duty for the output (the period will affect my pulsewidth in this project). it gives me an output, and the pulse width is about 1/10 of the period. my output signal which is at about 20khz.
my question is.. when i measure the period is it measuring in microseconds? or how is it measured because im quite confused and the data sheet doesnt seem to enlighten me...
this is my code:
Code:
define osc 20
T1 VAR word
Capture VAR PIR3.1
False CON 0
True CON 1
' auto time base reset, frequency mode, capture on every rising edge
Frequency CON %01000101
ANSEL0=0 ' All digital
TRISA.2 = 1 ' this is your (CAP1) input pin measuring the frequency
INTCON = 0 ' Interrupts off
TMR5H = 0 ' Clear high byte of TMR5 counter
TMR5L = 0 ' Clear low byte
T5CON = %00000001 ' prescale=1:1, int clock, TMR5=on
CAP1CON = Frequency ' we're measuring a frequency
Duty Var word
'byte being the one that is answer of algorithm when thats created
PORTB = 0 ' clear port latch
TRISB = %11000000 ' PWM0,1,2,3,4,5 outputs
TRISC = 2 ' RC1 = FLTA input (ground RC1 to halt PWM)
' RC1 should be pulled high for normal PWM operation
' when fault A is enabled.
' PCPWM init
DTCON = %00000101 ' ~500nS dead-time (for complementary outputs only)
PTCON0 = %00000000 ' 1:1 postscale, Fosc/4 1:1 prescale, free running mode
' PTCON0 = %00000100 would give 19.45kHz/4
PTPERL = 0 '
PTPERH = 1 ' PTPER = $0100 or 256d for ~19.45kHz
' PWM4,5 independent, PWM0,1,2,3 complementary
PWMCON0 = %01010100 ' PWM[5:0] outputs enabled
PWMCON1 = 1 ' updates enabled, overrides sync w/timebase
PTCON1 = %10000000 ' PWM time base is ON, counts up
FLTCONFIG = %00000011 ' enable fault A, cycle-by-cycle mode
Main:
Capture = False ' Reset capture flag
GOSUB Freq ' get frequency
Duty = T1 '
PDC2L = Duty.LowByte ' maintain a fixed 50% duty cycle on PWM4,5
PDC2H = Duty.HighByte ' independent PWM outputs.
GOTO Main
Freq:
' Frequency measurement mode, capture Timer5 count every rising edge
' with auto reset of Timer5 on each event
WHILE Capture = False
WEND
T1.HighByte = CAP1BUFH
T1.LowByte = CAP1BUFL
RETURN
END
Bookmarks