Hi,
Iam using mikroC PRO , easypic7 and PIC16F887. when i intend to set the PWM to 50 Hz (for servo motor) the software indicates error. The error message is "argument out of range" please help.
cannot set 50Hz PWM frequency
-
- Posts: 715
- Joined: 27 Dec 2006 14:33
- Location: Le Tréport , FRANCE
Re: cannot set 50Hz PWM frequency
Hi, Saharul
just open your Pic Datasheet and see what's the minimum HPWM freq for your selected XTAL ...
no other solution ( using that Pic HPWM modules ... )
Alain
just open your Pic Datasheet and see what's the minimum HPWM freq for your selected XTAL ...
no other solution ( using that Pic HPWM modules ... )
Alain
Re: cannot set 50Hz PWM frequency
Thanks Acetronics,
i found that if i set the MCu Clock to 500KHz the error message dissappear. But would PIC16F887 works with 500KHz crystal...
i found that if i set the MCu Clock to 500KHz the error message dissappear. But would PIC16F887 works with 500KHz crystal...
Re: cannot set 50Hz PWM frequency
Using HWPM on an 8-bit PIC is not the way to control R/C servos. Even if you can achieve 50Hz (using a reduced main clock frequency), the resolution is going to be very poor. At best, you will get <6-bit resolution (only about 50 steps for a 1mS range of control).
Why pay for overpriced toys when you can have
professional grade tools for FREE!!!
professional grade tools for FREE!!!
Re: cannot set 50Hz PWM frequency
Thanks aCKo
i tried your program with easyPIC6 PIC16F887 with crystal 8Mhz and 20Mhz but not working..PORTA0 continuously high...no PWM.
i tried your program with easyPIC6 PIC16F887 with crystal 8Mhz and 20Mhz but not working..PORTA0 continuously high...no PWM.
Re: cannot set 50Hz PWM frequency
Code from the above link is for P16F628A. For P16F887 you need to add:Use 8 MHz crystal.
Regards
Code: Select all
ANSEL = 0;
Regards
Re: cannot set 50Hz PWM frequency
Thanks aCko,
Yeah , it works..
could you please explain this? (= 1000 * __FOSC__ / 4000;) and (ch * 10 * Get_Fosc_kHz() / 4000;)
i have never been working with comparator and it surprising me when comparator can generate PWM. how does it work?
Yeah , it works..
could you please explain this? (= 1000 * __FOSC__ / 4000;) and (ch * 10 * Get_Fosc_kHz() / 4000;)
i have never been working with comparator and it surprising me when comparator can generate PWM. how does it work?
Re: cannot set 50Hz PWM frequency
The principle is very simple.
CCP1CON = 0x0B sets the CCP module in compare mode (trigger special event). Whenever TMR1 (TMR1H:TMR1L) matches CCPR1 (CCPR1H:CCPR1L), CCP1IF bit is set and TMR1 is cleared. The "ON" and "OFF" times are manipulated through CCPR1.
If the PWM frequency is 50 Hz (T = 20 ms) and you want 25% duty cycle, then the ON and OFF times are: Ton = 5 ms, Toff = T - Ton = 15 ms.
Now you need to convert the time values to register values (ticks or counts). If the MCU frequency is 8 MHz, the timer is incremented every 4 clocks (instruction cycle): T = 4 * 1 / 8 = 0.5 us.
The more general formula would be T = 4 / f[MHz] or in kHz: T = 4000 / __FOSC__ ( __FOSC__ is a project level predefined constant. See Help for details).
Q: If T is the time of 1 tick then how many ticks represent variable time t?
A: 1 : T = N : t => N = t * 1 / T = t * __FOSC__ / 4000
The final formula: N = t * __FOSC__ / 4000 is used to translate time value to register value (t is given in microseconds).
In this particular example, 50 Hz PWM switching is performed like this:
1. TMR1 starts counting; SOUT = 1; CCPR1 is set to match 5ms ON time
2. TMR1 reaches CCPR1 and the interrupt occurs; TMR1 automatically resets; SOUT = 0; CCPR1 is set to match 15 ms OFF time
3. TMR1 reaches CCPR1 after 15 ms and the process repeats again from step 1
I hope the explanation was clear enough.
Regards
CCP1CON = 0x0B sets the CCP module in compare mode (trigger special event). Whenever TMR1 (TMR1H:TMR1L) matches CCPR1 (CCPR1H:CCPR1L), CCP1IF bit is set and TMR1 is cleared. The "ON" and "OFF" times are manipulated through CCPR1.
If the PWM frequency is 50 Hz (T = 20 ms) and you want 25% duty cycle, then the ON and OFF times are: Ton = 5 ms, Toff = T - Ton = 15 ms.
Now you need to convert the time values to register values (ticks or counts). If the MCU frequency is 8 MHz, the timer is incremented every 4 clocks (instruction cycle): T = 4 * 1 / 8 = 0.5 us.
The more general formula would be T = 4 / f[MHz] or in kHz: T = 4000 / __FOSC__ ( __FOSC__ is a project level predefined constant. See Help for details).
Q: If T is the time of 1 tick then how many ticks represent variable time t?
A: 1 : T = N : t => N = t * 1 / T = t * __FOSC__ / 4000
The final formula: N = t * __FOSC__ / 4000 is used to translate time value to register value (t is given in microseconds).
In this particular example, 50 Hz PWM switching is performed like this:
1. TMR1 starts counting; SOUT = 1; CCPR1 is set to match 5ms ON time
2. TMR1 reaches CCPR1 and the interrupt occurs; TMR1 automatically resets; SOUT = 0; CCPR1 is set to match 15 ms OFF time
3. TMR1 reaches CCPR1 after 15 ms and the process repeats again from step 1
I hope the explanation was clear enough.
Regards
Re: cannot set 50Hz PWM frequency
Thanks aCko.
well explained.
t.q
well explained.
t.q
Re: cannot set 50Hz PWM frequency
I was trying to generate the 50Hz PWM signal. But it dosen't work.
Here is my code:
Can you tell me what wrong I did here?
Here is my code:
Code: Select all
void interrupt()
{
if(CCP1IF_bit)
{
CCP1IF_bit = 0;// clear flag
Clock1 = 0;
}
if(TMR2IF_bit)
{
Clock1 = 0;
TMR2IF_bit = 0;//clear flag
}
} // ISR...
void PORT_Init(void)
{
TRISA = 0xFF; // PORTA input
TRISB = 0b00000001; // PORTB output except RB0 & RB1
PORTB = 0x00; // all zero
TRISC = 0b00001000; // PORTC output
PORTC = 0x00; // all zero
}
void main()
{
PORT_Init(void);
// Timer2 settings for 7ms interrupt
T2CON = 0x36;
PR2 = 223; // 10 ms time
TMR2IE_bit = 1;
INTCON = 0xC0;
// compare module settings...
CCP1CON = 0b00001011;// trigger special event
CCP1IE_bit = 1;
CCPR1L = 0x1B;
CCPR1H = 0xCF;
//timer 1 settings...
T1CON = 0x01;
TMR1IF_bit = 0;
TMR1H = 0xCF;//10ms timer
TMR1L = 0x2B;
while(1)
{
} // while
}// void main
Re: cannot set 50Hz PWM frequency
hi MKdas,
sorry i have no idea, the best person to ask is peter,acko or any ME team.
sorry i have no idea, the best person to ask is peter,acko or any ME team.
Re: cannot set 50Hz PWM frequency
Hi
I have also the PWM frequency 50 Hz ( T = 20000 micro second) for BLDC motor ESC input. (I want to use it without receiver signal) I need to regulate my PWM width via my ADC module between 900 micro sec and 1800 micro sec. Also between % 4.5 and % 9 Pulse width . Please help me about the algorithm .(16F877A) Thanks in advance
I have also the PWM frequency 50 Hz ( T = 20000 micro second) for BLDC motor ESC input. (I want to use it without receiver signal) I need to regulate my PWM width via my ADC module between 900 micro sec and 1800 micro sec. Also between % 4.5 and % 9 Pulse width . Please help me about the algorithm .(16F877A) Thanks in advance