PDA

View Full Version : capturing signal and outputing it



robertpeach
- 2nd September 2009, 10:37
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:


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

robertpeach
- 2nd September 2009, 11:15
just managed to get it to work with 3phases and to average them and output them... but im still unsure on the actual values. when a period is measured what does it get measured in? and how do i relate that to pwm?

Acetronics2
- 2nd September 2009, 12:35
Hi,

A closer look at your PIC Datasheet will show you HOW the CCP modules work ...

and from that ... What's the counting unit ...

Alain

HenrikOlsson
- 2nd September 2009, 15:25
Well the timer measuring the period is being clocked by something, it apperars you're using the main oscillator as the source for that. Then there's a prescaler that can divide the main clock but it appears you have that set to 1:1. So if the PIC runs at 20MHz one timer "tick" is 200nS.

If the timer value is captured (and then reset) on the rising edge and you get a timer value of 1234 then the persiod is 1234*200nS = 246.8uS.

/Henrik.

robertpeach
- 2nd September 2009, 15:56
hey thanks!

i had just worked that all out before i got the reply! but its just confirmed my calculations which is good. 0.2us is one timer tick so i know how to scale it all now.

i am having another problem if anyone can shed some light on it? im getting the right pulsewidth and the right frequency output (inrespect to my input) however its glitching every now and then and isnt giving me a perfectly smooth pwm output.

its glitching is not coinciding with tiny fluctuations in the input signal either...

could it be due to over measuring?

thanks!

duncan303
- 2nd September 2009, 16:55
Have you considered using the HWPM facilty?

Duncan


......

robertpeach
- 3rd September 2009, 08:55
duncan303 Have you considered using the HWPM facilty?

Duncan


......

i tried implementing the hpwm facility but it didnt seem to work for me very well... i cant remember what the problem was now but there was a reason i didnt use it. my pwm code i know works fine... ive got it working beautifully smooth from another pic... but when i add the code to capture and then output a signal that has nothing to do with the input signal it has random flickerings on the output...

robertpeach
- 3rd September 2009, 16:12
update:

i have got it working pretty nicely now, although i have added a 5 second pause to slow down updates.

there is very rarely a flickering now, which leads me to believe the highly frequent flickering before was caused by capturing and updating to quickly... why would this be tho?