Measuring pulse width using ccp
HELLO ALL
I FOUND PICBASIC PRO CODE MADE BY BRUCE(ON 20th July 2006, 07:45) FOR 18F242 TO MEASURE PW USING CCP HARDWARE. I FOUND THAT IS VERY SHORT AND,NEAT AND STRAIGHT FORWARD. I HAVE 16F877A AND I WANT TO MAKE A CAP METER. I HAVE WRITTEN CODE BASED ON BRUCE'S CODE BUT USING 16F877A. I TRIED TO SIMULATE THIS ON PROTEUS . IT NEVER WORKED. BUT BRUCE'S CODE WORK FINE FOR 18F242. I DID EVERY THING POSSIBLE TO GET IT WORKING BUT NO LUCK. I AM POSTING THIS COD FOR SOME BODY OR BRUCE TO HELP ME PLEASE ON THIS.IT LOOKED TO ME OBSCURE NO MORE I CAN DO.COD:
'PIC16F877A
DEFINE OSC 20
Define LCD_DREG PORTD
Define LCD_DBIT 4
Define LCD_RSREG PORTE
Define LCD_RSBIT 0
Define LCD_EREG PORTE
Define LCD_EBIT 1
Low PORTE.2
PIE1.2=1
Symbol Capture = PIR1.2 ' CCP1 capture flag
T1 VAR WORD ' 1st capture value
PW VAR WORD ' 2nd capture value & ultimately final pulse width
TRISC.2 = 1 ' CCP1 input pin (Capture input)
INTCON = 0 ' Interrupts off
ReLoad:
CCP1CON = %00000101 ' Capture mode, 4capture on rising edge
T1CON = 0 ' TMR1 prescale=1, clock=Fosc/4, TMR1=off (200nS per count @20MHz)
TMR1H = 0 ' Clear high byte of TMR1 counter
TMR1L = 0 ' Clear low byte
T1CON.0 = 1 ' Turn TMR1 on here
Capture = 0 ' Clear capture int flag bit
While !Capture ' Wait here until capture on rising edge
Wend
' Rising edge detected / stuff Timer1 value in T1
T1.HighByte = CCPR1H
T1.LowByte = CCPR1L
CCP1CON = %00000100 ' Configure capture for falling edge now
Capture = 0 ' Clear capture interrupt flag bit
While !Capture ' While here until capture on falling edge
Wend
' Falling edge detected / stuff Timer1 value in PW
PW.HighByte = CCPR1H
PW.LowByte = CCPR1L
PW = PW-T1 ' High pulse width = PW-T1
' Convert to uS for 20MHz osc with 200nS Timer1 ticks
PW = (PW * 2)/10
Pause 150 ' Pause to let LCD power up
LCDOut $fe,1, "PULSE WIDTH" ' Display if C1OUT = 1
LCDOut $fe,$C0, #PW
GOTO ReLoad
END
OBAID
Re: Measuring pulse width using ccp
It is sad that we are posting in picbasic pro forum ,you can see many people viwed your post, they just pass by, they do'nt offer any help.i am certain that among them some who know the answer
Re: Measuring pulse width using ccp
The one big thing you need to watch out for when moving code from one PIC to another is how the registers are configured. You need to go line by line and look up registers. For example, maybe timer1 one is associated with CCP2 on one chip and CCP1 with another. That is just purely an example but things like that are common. It might be the smallest difference from one PIC to another that will prevent code from working on one and not the other.
Re: Measuring pulse width using ccp
Quote:
Originally Posted by
abofar
It is sad that we are posting in picbasic pro forum ,you can see many people viwed your post, they just pass by, they do'nt offer any help.i am certain that among them some who know the answer
You have to realize that there are a LOT of novice members here. Just because they don't post does not mean they are ignoring you. Many read threads because they have similar questions, or like me, just want to see if they can learn something new.
I've used PICs since 2004 and have never touched PWM in a serious way.
Robert
EDIT: you can start by making sure your PIC is configured properly, this is just an example from 16F886:
Code:
#CONFIG
__CONFIG _CONFIG1, _LVP_OFF & _FCMEN_OFF & _IESO_OFF & _BOR_OFF & _CPD_OFF & _CP_OFF & _MCLRE_OFF & _PWRTE_OFF & _WDT_OFF &
_INTRC_OSC_NOCLKOUT
__CONFIG _CONFIG2, _WRT_OFF & _BOR21V
#ENDCONFIG
http://www.picbasic.co.uk/forum/show...132#post110132
Have you done much searching for CCP and PWM?
Re: Measuring pulse width using ccp
Re: Measuring pulse width using ccp
Hi,
I don't know if you eventually solved this problem but I think you may be using the wrong interrupt commands. For CCP you need to use CCP1IF interrupt flag and CCP1IE to enable/disable the interrupt. Do not use timer interrupts. The Microchip data sheet "Tips 'n Tricks" DS41214A page 8 explains how to measure pulse width. I have tried it and it does work!
Re: Measuring pulse width using ccp
Hi
Thank you for asking, problem is solved.Problem was that simulator is too slow.
Thanks again
Obaid