cannot set 50Hz PWM frequency

General discussion on mikroC PRO for PIC.
Post Reply
Author
Message
saharul
Posts: 489
Joined: 08 Jul 2010 08:11

cannot set 50Hz PWM frequency

#1 Post by saharul » 12 Sep 2013 08:40

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.

Acetronics
Posts: 715
Joined: 27 Dec 2006 14:33
Location: Le Tréport , FRANCE

Re: cannot set 50Hz PWM frequency

#2 Post by Acetronics » 12 Sep 2013 10:35

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

saharul
Posts: 489
Joined: 08 Jul 2010 08:11

Re: cannot set 50Hz PWM frequency

#3 Post by saharul » 12 Sep 2013 13:31

Thanks Acetronics,

i found that if i set the MCu Clock to 500KHz the error message dissappear. But would PIC16F887 works with 500KHz crystal...

rmteo
Posts: 1330
Joined: 19 Oct 2006 17:46
Location: Colorado, USA

Re: cannot set 50Hz PWM frequency

#4 Post by rmteo » 12 Sep 2013 13:50

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!!! :D :D :D

aCkO
Posts: 1119
Joined: 14 Feb 2011 04:07
Location: Bar, Montenegro

Re: cannot set 50Hz PWM frequency

#5 Post by aCkO » 12 Sep 2013 14:33


saharul
Posts: 489
Joined: 08 Jul 2010 08:11

Re: cannot set 50Hz PWM frequency

#6 Post by saharul » 13 Sep 2013 11:04

Thanks aCKo

i tried your program with easyPIC6 PIC16F887 with crystal 8Mhz and 20Mhz but not working..PORTA0 continuously high...no PWM.

aCkO
Posts: 1119
Joined: 14 Feb 2011 04:07
Location: Bar, Montenegro

Re: cannot set 50Hz PWM frequency

#7 Post by aCkO » 13 Sep 2013 15:08

Code from the above link is for P16F628A. For P16F887 you need to add:

Code: Select all

ANSEL = 0;
Use 8 MHz crystal.

Regards

saharul
Posts: 489
Joined: 08 Jul 2010 08:11

Re: cannot set 50Hz PWM frequency

#8 Post by saharul » 17 Sep 2013 14:52

Thanks aCko,

Yeah , it works.. :D

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?

aCkO
Posts: 1119
Joined: 14 Feb 2011 04:07
Location: Bar, Montenegro

Re: cannot set 50Hz PWM frequency

#9 Post by aCkO » 19 Sep 2013 00:27

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

saharul
Posts: 489
Joined: 08 Jul 2010 08:11

Re: cannot set 50Hz PWM frequency

#10 Post by saharul » 19 Sep 2013 07:37

Thanks aCko.


well explained. :D

t.q

MKDas
Posts: 4
Joined: 25 Sep 2012 09:39

Re: cannot set 50Hz PWM frequency

#11 Post by MKDas » 24 Feb 2014 19:23

I was trying to generate the 50Hz PWM signal. But it dosen't work.

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
Can you tell me what wrong I did here?

saharul
Posts: 489
Joined: 08 Jul 2010 08:11

Re: cannot set 50Hz PWM frequency

#12 Post by saharul » 18 Mar 2014 10:35

hi MKdas,

sorry i have no idea, the best person to ask is peter,acko or any ME team.

selnet
Posts: 5
Joined: 21 Apr 2015 10:34

Re: cannot set 50Hz PWM frequency

#13 Post by selnet » 02 Aug 2015 21:36

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

Post Reply

Return to “mikroC PRO for PIC General”