how to generate two signals from two ports at the same time by using p16f84


Closed Thread
Results 1 to 27 of 27

Hybrid View

  1. #1
    Join Date
    Aug 2010
    Location
    Maryland, USA
    Posts
    869


    Did you find this post helpful? Yes | No

    Default Re: how to generate two signals from two ports at the same time by using p16f84

    You are close to what I mean. TMR0 Should be setup as a time base, you had mentioned 8uSec in another post. So compute the reload value for TMR0 such that you get an interrupt flag at 8uSec. (hint: use PicMultiCalc to help get the values)

    Next the freq values are computed by YOU and hardcoded. so if TMR0 is 8uSec, and you need a period of 800 uSec, freq will be 800/8 or freq = 100.

    Your while loop just needs to check for TMR0 interrupt flag bit. You do not want to enable interrupts, but the flag will be set anyway. So as soon as the flag is set you need to do this:
    Code:
    reload the TMR
    clear the flag
    decrement freq1 &freq2
    check each for zero (IF !freq1 then) this will check if freq1 = 0
    I will let you work on how to actually code the above. You almost have the Freq checks correct. Put comments in your code. This will do 2 things:
    1 tells others and yourself what things are doing. If you code more beyond this class, you will be VERY happy you started doing this.
    2 When you comment what you think things are doing, It will make it easier to see what is wrong. Here is a for instance:
    Code:
    while   tmr0>=250  ' this line checks if TMR0 is greater then 250
        tmr0= 0            'this line sets TMR0 to equal 0 if it is greater then 250
        wend               ' this is where the program jumps to when TMR0 is less then 250
    This will not work ok, the ugly is all your program will run while waiting for the timer to reach 250. Then when tmr does reach 250, you just set it back to zero. Effectively tmr0 is doing nothing for you here. I will leave it to you to try to figure out why.

    BTW, I am in no way a very good teacher, so please bear with me. My goal is to give you clues and help you to understand what is going on as opposed to telling you how to do each step. Is there a deadline when you must be done?
    Last edited by cncmachineguy; - 29th July 2011 at 14:53.
    -Bert

    The glass is not half full or half empty, Its twice as big as needed for the job!

    http://foamcasualty.com/ - Warbird R/C scratch building with foam!

  2. #2
    Join Date
    Jul 2011
    Posts
    17


    Did you find this post helpful? Yes | No

    Default Re: how to generate two signals from two ports at the same time by using p16f84

    sory i am writing fast and i forget something. there is no deadline but i must submit as soon as possible.

    i am going to review the program, and i will send the results , i must be careful. thanks a lot for your advices

    best regards

  3. #3
    Join Date
    Jul 2011
    Posts
    17


    Did you find this post helpful? Yes | No

    Default Re: how to generate two signals from two ports at the same time by using p16f84

    hi cncmachineguy

    i think there are two choices which depend on whether the codes ( decrement freq1 &freq2, IF !freq1&&freq2 then) are in interrupt loop or main loop.

    for the first one, they are in the interrupt loop, these codes takes min 10 mic. sec. ( with adjusting option_reg, and loading tmr0 for min. time ) , therefore i can not obtain a period increment below 10 mic. seconds.

    for the second one, i have tried to put those codes into main loop, but then i have problem to control tmr0 to obtain exact values.

    after so much efforts i have decided to build this system is impossible by using pic16f84 with 10 mhz and i think we need at least two timer interrupts to obtain two different precise frequencies.

    thank you a lots for advices.

  4. #4
    Join Date
    Aug 2010
    Location
    Maryland, USA
    Posts
    869


    Did you find this post helpful? Yes | No

    Default Re: how to generate two signals from two ports at the same time by using p16f84

    I understand your frustration, but don't give up yet. Your teacher has it working so it is not impossible.

    New question, you say you need 2uSec percision, but then you talk about using an 8uSec time base. this will not be able to provide 2uSec percision. So is the 8uSec ok?

    lets look at some math for a minute. you have a 10Mhz clock, so thats 2.5MIPS. each instruction will take .4uSec, so for 8uSec time base, you have 20 inscrutions worth of time to decide what to do.

    If you don't want to give up, lets keep working on this.
    -Bert

    The glass is not half full or half empty, Its twice as big as needed for the job!

    http://foamcasualty.com/ - Warbird R/C scratch building with foam!

  5. #5
    Join Date
    Jul 2011
    Posts
    17


    Did you find this post helpful? Yes | No

    Default Re: how to generate two signals from two ports at the same time by using p16f84

    yes 8u sec. is also ok. let's look at it closely again ( by the way i hope i am a good student ) maybe this time we can do it. i think there is no problem about timing by using tmr0 and prescaler, it works well. the main problem is that we need a loop including two variables depending on tmr0 and to determine intended two periods time.
    i think putting this loop into interrupt is better since if we have had to use a keyboard to select frequencies, we would use it in the main loop, so no waste time for our periods.
    but on the other hand when we use below codes in the interrupt loop, it needs 22-25usec.. i am writing codes below how i calculate these times. i get 22-25 usec. by measuring pulse width of porta.1 via oscilloscope.

    int_loop:
    high porta.1 ; start of the pulse i measure

    fre1=fre1+1 ; increment for period 1 depending on tmr0 interrupt time
    fre2=fre2+1 ; increment for period 2 depending on tmr0 interrupt time

    if fre1>=5 then ; 5 random number if i suceed it will be fraction of x8
    toggle porta.0
    fre1=0
    else
    endif

    if fre2>=3 then ;3 random number if i suceed it will be fraction of x8
    toggle porta.0 ; this must be porta.1 but now not important
    fre2=0
    else
    endif
    INTCON.2=0 ; setting up timer
    TMR0 =254 ; reload tmr0

    low porta.1 ; end of pulse

    pauseus 100
    goto int_loop


    and this is the shotest loop i know for our aim, and it requires at least 20usec bigger than 8usec. (

    thanks a lot
    Last edited by bikxsici; - 1st August 2011 at 10:56.

  6. #6
    Join Date
    Aug 2010
    Location
    Maryland, USA
    Posts
    869


    Did you find this post helpful? Yes | No

    Default Re: how to generate two signals from two ports at the same time by using p16f84

    OK, Good start. First I think this will have to be your main and grabbing input will have to be your interrupt. I say this because even if you used pure ASM to get in the ISR, it will take at least 8 ASM instructions for the context save and restore. This leaves you almost no room to do anything. On the other hand, if the signalk generation is the main, with an interrupt call for the keypress, maybe it will be ok. you will get a glitch or 2, but only when you change freq.

    No, lets see how to speed this up a bit.
    First remove the else statements. you are not using them so you can get rid of them. I don't think this will speed it up, but lets see.

    After you try that, change the counters to count down instead of up. Checking if freq = 0 should be much faster then if freq >= something. The reason for this is in the compiled code, it should compile to be something like this:
    Code:
    DECF freq    'decrenment freq
    BTFSS freq  'test if freq is zero, if not skip the next step
    JMP notzero
    MOVLW 5
    MOVF freq
    notzero:
    reutrn
    In order to test for equal or greater then any number other then zero, first the number must be subtracted from the counter, then status.zero must be tested. If not zero, then borrow or carry must be tested and the program will branch accordingly. Does this make sense?

    After you try these 2 things, report back with the results. There is at least 1 more thing we can do, but I want you to try each thing 1 at a time and understand why it worked or didn't work.
    -Bert

    The glass is not half full or half empty, Its twice as big as needed for the job!

    http://foamcasualty.com/ - Warbird R/C scratch building with foam!

  7. #7
    Join Date
    Aug 2010
    Location
    Maryland, USA
    Posts
    869


    Did you find this post helpful? Yes | No

    Default Re: how to generate two signals from two ports at the same time by using p16f84

    Forgot to mention, No need for a prescaler, 8uSec is WAY shorter then TMR0 just running. In fact, we may find the loop can be tuned to take EXACTLY 8uSec, so no need for the TMR at all!

    But first we must get through timing tests as you are doing now.

    Yes, you are a fine student, just don't give up.

    1 more thing, does the language used matter? As a last resort we will try this with ASM. What type of class is this for?
    -Bert

    The glass is not half full or half empty, Its twice as big as needed for the job!

    http://foamcasualty.com/ - Warbird R/C scratch building with foam!

Members who have read this thread : 0

You do not have permission to view the list of names.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts