PWM PIC16887 Cant stop at max duty

Thread Starter

LauraB

Joined Dec 15, 2011
43
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
void main()
{
short duty1 = 0;

TRISD = 0xFF;
TRISC = 0x00;

PWM1_Init(5000);
PWM1_Start();
PWM1_set_duty(duty1);

while (1)

{
if (PORTD.F0==0)
{Delay_ms(1);
duty1++;
if (duty1>200) duty1=200; // here I set the max duty
PWM1_set_duty(duty1);
}
if (PORTD.F1==0)
{
Delay_ms(1);
duty1--;
PWM1_set_duty(duty1);
}
Delay_ms(10);
}
}
Thank you
 

tshuck

Joined Oct 18, 2012
3,534
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:

Thread Starter

LauraB

Joined Dec 15, 2011
43
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.
 

tshuck

Joined Oct 18, 2012
3,534
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.
The Pickit only understands the hex file generated from the compiler - regardless of where it came from.

Also I would like to set min duty = 0, so even if I press the button it won't jump from 0 to 255.
Easy - check the value of duty1 when PORTD.F1==0, if it is 0, don't decrement.
 

tshuck

Joined Oct 18, 2012
3,534
Rich (BB code):
PWM1_Init(5000);
PWM1_Start();
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:

JohnInTX

Joined Jun 26, 2012
4,787
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:
Top