PWM PIC16887 Cant stop at max duty

Discussion in 'Embedded Systems and Microcontrollers' started by LauraB, Jan 2, 2014.

  1. LauraB

    Thread Starter Member

    Dec 15, 2011
    43
    0
    Hello,

    I'm trying to stop increasing duty even if I press the button.
    But it works only when I set max ~120 in the code. If I set more, then it just goes to max and jump to min again. I totally dont understand why I'm not able to do that.
    Here is my code
    Thank you
     
  2. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    Is this the High-Tech compiler?

    You haven't specified what oscillator this is running with, nor your configuration. If I remember correctly, running the PWM module too fast will result in less resolution...

    Edit: Yup, see page 131
     
    Last edited: Jan 2, 2014
  3. LauraB

    Thread Starter Member

    Dec 15, 2011
    43
    0
    Actually I used microC this time. But I have to program it using MPlab, cuz microC doesn't work with Pic Kit 3. I tried with 8Mhz, but still doesnt work.
    Also I would like to set min duty = 0, so even if I press the button it won't jump from 0 to 255.
    Any idea?

    Thank you for the reply.
     
  4. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    The Pickit only understands the hex file generated from the compiler - regardless of where it came from.

    Easy - check the value of duty1 when PORTD.F1==0, if it is 0, don't decrement.
     
  5. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    Code ( (Unknown Language)):
    1. PWM1_Init(5000);
    2. PWM1_Start();
    3. PWM1_set_duty(duty1)
    This its part of the problem with using compiler-specific function calls, it isn't clear what values are being set to what...

    The help documentation at least shows that the number in your initialization call is the PWM frequency...
     
    Last edited: Jan 2, 2014
    LauraB likes this.
  6. JohnInTX

    Moderator

    Jun 26, 2012
    2,339
    1,022
    You might check to see if 'short' is a signed char in MicroC with your memory model. If so, its range is -128 -> 127 which would agree with it working with a number in the 120 range and failing when bigger. Try unsigned short or punt and make it an unsigned char.

    EDIT: looking at Chapter 6 of the manual pp149, short IS signed with the range noted above; +127 is as big as it will hold.
     
    Last edited: Jan 2, 2014
    LauraB likes this.
  7. LauraB

    Thread Starter Member

    Dec 15, 2011
    43
    0
    Yes. I'm rewriting the code without these function. I'm going to use Hi-tech.

    edit.
    Ok I will try.
     
  8. LauraB

    Thread Starter Member

    Dec 15, 2011
    43
    0
    Done! It worked with unsigned short.

    Thank you very much guys.
     
Loading...