1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

Can CCP do this on 16F876A? :(

Discussion in 'Microcontrollers' started by bjox1, Mar 7, 2008.

  1. bjox1

    bjox1 New Member

    Joined:
    Mar 7, 2008
    Messages:
    38
    Likes:
    0
    Location:
    UK
    Hello friends,

    I am stuck with a problem and will kindly appreciate any help on this.

    I have got two TTL signals ranging from 500 Hz to 10kHz. I am trying to measure the delay of one with reference to other. As the signals can be of any frequency I am not sure how to use CCP for this task. I have searched a few topics on here but couldn't find anything appropriate.

    I am a newbie and wold really appreciate any help.

    Thank you all in advance

    bjox
     
  2. Nigel Goodwin

    Nigel Goodwin Super Moderator Most Helpful Member

    Joined:
    Nov 17, 2003
    Messages:
    39,218
    Likes:
    640
    Location:
    Derbyshire, UK
    Presumably the two frequencies are identical?, otherwise it wouldn't be possible.

    If you XOR the signals together, this will give you a pulse representing the phase difference between them.
     
  3. bjox1

    bjox1 New Member

    Joined:
    Mar 7, 2008
    Messages:
    38
    Likes:
    0
    Location:
    UK
    Hi Nigel,

    Thank you for the reply.

    I am sorry for not providing enough info. YES, the two signals at any given time will have the same frequencies.

    I am very thankful to you for giving this idea but would really appreciate if you could explain it in a bit more detail. Probably if there's a circuit to look at?? For instance, what will be the output from an XOR and what will be the input to my PIC?? And finally what method I will have to use to get the phase difference in the PIC? [I apologise if I ask too much]


    Thanks in advance Nigel.

    -I am pretty much new to the electronics :(
     
    Last edited: Mar 7, 2008
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,016
    Likes:
    317
    Location:
    Brisbane Australia

    Have a read of this thread. For some reason the OP has deleted his initial question but he basically wanted to find the phase difference between two signals.

    Mike.
     
  6. bjox1

    bjox1 New Member

    Joined:
    Mar 7, 2008
    Messages:
    38
    Likes:
    0
    Location:
    UK
    Hey Pommie,
    Thanks for the thread. I had a look at it and it seems a gr8 technique. The only concern is whether I could measure the phase at different freq??

    I mean will it work if the signals are at 9.9kHz or in the second case if the signals are at 990Hz?? Please enlighten me!

    Thanks.
     
  7. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,016
    Likes:
    317
    Location:
    Brisbane Australia
    If you use a 20MHz crystal then you will be able to measure 2μS periods. It really depends how accurate you want it.

    Mike.
     
    Last edited: Mar 8, 2008
  8. Nigel Goodwin

    Nigel Goodwin Super Moderator Most Helpful Member

    Joined:
    Nov 17, 2003
    Messages:
    39,218
    Likes:
    640
    Location:
    Derbyshire, UK
    Draw yourself some little diagrams and work it out, it will help you understand it.

    All you need to know is that an XOR gate sets the output HIGH, when the two inputs are DIFFERENT, if the two inputs are the same, them the output is LOW.

    So get a sheet of graph paper, draw your two input waves on it above each other, then follow the rules above to draw the output waveform below them. Do this for various phase differences, this should make it all clear.
     
  9. bjox1

    bjox1 New Member

    Joined:
    Mar 7, 2008
    Messages:
    38
    Likes:
    0
    Location:
    UK
    Hi Pommie and Nigel,

    I was looking for something with a very high accuracy. I mean I calculated [ and I saw some of ur replies to the same OP] and :
    For 1kHz signals- 10us*100 is 1ms i.e. 100 is the max value therefore 3.6 deg for every counter.
    & for example for 10kHz signals- 10us*10 will be 100us i.e. 36 deg for every counter.
    So I was wondering if I could eliminate this error or probably a technique which is independent of the signal frequency?

    Hi Nigel,

    I am sorry for asking this again but I am kind of struggling in analysing the XOR technique [probably because Im in my first year :D]. If you see the diagram I have attached, if the second signal is say 50 deg lag, and if I feed these two signals to XOR gate, then one signal will be high and the other will be low and the output from XOR will be high all the time. I don't know how can I find the phase difference from there? :confused:

    I apologise if I ask a bit too much basics.

    Thanks again.
     

    Attached Files:

    Last edited: Mar 8, 2008
  10. Nigel Goodwin

    Nigel Goodwin Super Moderator Most Helpful Member

    Joined:
    Nov 17, 2003
    Messages:
    39,218
    Likes:
    640
    Location:
    Derbyshire, UK
    Your diagram clearly shows a time when one is HIGH and one is LOW, when both are HIGH, and when both are LOW, bear in mind to have a phase difference you can measure both waves must be exactly the same frequency.
     
  11. bjox1

    bjox1 New Member

    Joined:
    Mar 7, 2008
    Messages:
    38
    Likes:
    0
    Location:
    UK
    Mate I am so sorry but I don't understand what to do with the output.:( :( :eek: :eek:

    I am so so sorry but please enlighten me. I think my brain hates me!

    Thanks Nigel.
     
  12. Nigel Goodwin

    Nigel Goodwin Super Moderator Most Helpful Member

    Joined:
    Nov 17, 2003
    Messages:
    39,218
    Likes:
    640
    Location:
    Derbyshire, UK
    Like I said, draw a diagram - it's easy - heres an example, top two are the two input waveforms, bottom one the output from an XOR. The bottom waveform is a direct representation of the phase difference.
     

    Attached Files:

  13. bjox1

    bjox1 New Member

    Joined:
    Mar 7, 2008
    Messages:
    38
    Likes:
    0
    Location:
    UK
    Dear Nigel,
    I am so so thankful to you for explaining this to me in such a detail.

    As u have suggested me to draw a few cases, I have attached a diagram where I have assumed two cases of delayed signal [named as signal2]. As far as I could imagine [I might be wrong] that if we take a reference point on signal [signal1] on the rising edge of the pulse, the delay signal will possibly have the two cases:
    1) if signal2 arrives as low = that means 0-180 deg delay &
    2) if signal2 arrives as high = that means the delay signal [signal2]has
    crossed the 0-180 deg of delay range and now
    it's in 180-360 deg range.

    In the attached diagram, I have taken two such cases, one for 10 deg delay and second one is for 350 deg delay and I have found out that the output pulse from XOR gate has the same pulse width.
    Now that implies that for any delay value [in deg] between 0-180 [say 'x'] will have the same value for the signal with a delay of 360-x deg[ In my case,10 deg will be same as 360-10 =350 deg]. I am all confused about how to calculate the phase on these cases?? :(

    I apologise Nigel if I have over thought it but I would really appreciate any further help on this matter.

    Thank you soo soo much .

    Ps: I apologise if the diagrams are not upto the scale.
     

    Attached Files:

    Last edited: Mar 8, 2008
  14. Nigel Goodwin

    Nigel Goodwin Super Moderator Most Helpful Member

    Joined:
    Nov 17, 2003
    Messages:
    39,218
    Likes:
    640
    Location:
    Derbyshire, UK
    You are correct, 10 degrees and 350 degrees will read the same, because they are the same - are you needing to know which leads or lags as well?.

    Perhaps you might try telling us exactly what you are trying to do?.
     
  15. bjox1

    bjox1 New Member

    Joined:
    Mar 7, 2008
    Messages:
    38
    Likes:
    0
    Location:
    UK
    Hi Nigel,
    Sorry about the late reply.

    Well I am at Uni [first year] and this task is a part of our module. There are a few circuits given by technician which introduce delay [0-360 deg].

    So we have same signals [same freq at a given time] but one is ref and the other signal goes through these circuits which introduce resp delay. As I mentioned earlier, I have to measure the phase diff but the freq can range from 500Hz to 10kHz.:eek:
    Well, I have to measure the delay of signal2 with repect to signal1. Hence my reference point is signal1. I hope this might give you a rough idea! I apologise again for being so stupid.

    Is it still possible to achieve 0-360 phase measurement using XOR technique??

    Thanks once again Nigel.
     
    Last edited: Mar 8, 2008
  16. Nigel Goodwin

    Nigel Goodwin Super Moderator Most Helpful Member

    Joined:
    Nov 17, 2003
    Messages:
    39,218
    Likes:
    640
    Location:
    Derbyshire, UK
    As the signal always lags, detect the start of the reference pulse, then start counting while you wait for the start of the following pulse.
     
  17. bjox1

    bjox1 New Member

    Joined:
    Mar 7, 2008
    Messages:
    38
    Likes:
    0
    Location:
    UK
    Hi Nigel,
    Thanks for the reply.
    Will I still need an XOR gate for this?? I mean I can just wait for signal1 to get high, start counting till I get next rising edge on signal2 [exactly what Pommie did in the previous post]. :confused:

    Thanks again Nigel.

    <EDIT>
    Nigel,
    I have found this diagram from a website [attached]. The only other way to find the phase diff of 180+ is that if you look at the signal2 or RX signal waveform and XOR output waveform together [in the diagram], for the phase diff between 0-180, when the signal2 [delay signal] rises, the XOR output is low. For example, for the case of 45 deg, as the signal2 rises, the XOR output is low and for 315 deg, as the signal2 rises, the XOR is high.

    Am I right in assumin that if I use a bit test for this case, I can fix the problem??
    Lastly,if we feed the XOR output to PIC, will I be using counter to find the width of the signal?? If so then wouldn't the counter value be varied? I mean say for 45 deg of phase diff [for 1000Hz signals] the counter will give 'x' value and for the same phase diff [now for 10 kHz signal] the counter will give less than 'x' value?? So if I read the counter value on the output, I will not b getting the same 45 deg value!

    I am so so sorry I sound a bit annoying but I need your kind help on this matter.

    Thanks again for your time Nigel.
     

    Attached Files:

    • XOR.jpg
      XOR.jpg
      File size:
      40.9 KB
      Views:
      190
    Last edited: Mar 9, 2008
  18. Nigel Goodwin

    Nigel Goodwin Super Moderator Most Helpful Member

    Joined:
    Nov 17, 2003
    Messages:
    39,218
    Likes:
    640
    Location:
    Derbyshire, UK
    If you're using different frquencies you need to measure both the HIGH period and the LOW period, the phase difference is the ratio between them.
     
  19. bjox1

    bjox1 New Member

    Joined:
    Mar 7, 2008
    Messages:
    38
    Likes:
    0
    Location:
    UK
    Sorry Nigel, I am finding very hard to analyse ur solution :(. Please enlighten me.

    I may have explained it wrongly. That’s the whole task:
    1) User will choose any frequency from 500 Hz to 10kHz.
    2) Then same frequency signal will be tap down to TWO signals. One will be the reference and the other will go through this unknown delay circuit.
    3) My aim will be to find the value of the phase difference hence determining the unknown circuit [phase value].

    I am sorry for not explaining it clearly.

    Awaiting ur reply Nigel.
    Thanks again.
     
  20. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,016
    Likes:
    317
    Location:
    Brisbane Australia
    Do you need to calculate time difference or angular difference? I.E. 2mS or 2°.

    Mike.
     
  21. bjox1

    bjox1 New Member

    Joined:
    Mar 7, 2008
    Messages:
    38
    Likes:
    0
    Location:
    UK
    Hey Mikey,
    That's a very nice question. :confused:

    Well I am expected to show the value at the output pin of my PIC. may be using LED [8 bits]. I think output as the angular difference will help me in this.

    <edit>
    Pommie, Can u suggest me any technique for measuring the pulse width using PIC?? [In my case, the output of XOR gate i.e. the ON time of the pulse]

    Thanks again.
     
    Last edited: Mar 9, 2008

Share This Page