It is currently 25 Jun 2019 08:31

All times are UTC + 1 hour




Post new topic Reply to topic  [ 13 posts ] 
Author Message
PostPosted: 12 Sep 2013 08:40 
Offline

Joined: 08 Jul 2010 08:11
Posts: 482
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.


Top
 Profile  
 
PostPosted: 12 Sep 2013 10:35 
Offline

Joined: 27 Dec 2006 14:33
Posts: 709
Location: Le Tréport , FRANCE
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


Top
 Profile  
 
PostPosted: 12 Sep 2013 13:31 
Offline

Joined: 08 Jul 2010 08:11
Posts: 482
Thanks Acetronics,

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


Top
 Profile  
 
PostPosted: 12 Sep 2013 13:50 
Offline

Joined: 19 Oct 2006 17:46
Posts: 1330
Location: Colorado, USA
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


Top
 Profile  
 
PostPosted: 12 Sep 2013 14:33 
Offline

Joined: 14 Feb 2011 04:07
Posts: 1119
Location: Bar, Montenegro
http://www.mikroe.com/forum/viewtopic.php?f=97&t=55051#p216648

Regards


Top
 Profile  
 
PostPosted: 13 Sep 2013 11:04 
Offline

Joined: 08 Jul 2010 08:11
Posts: 482
Thanks aCKo

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


Top
 Profile  
 
PostPosted: 13 Sep 2013 15:08 
Offline

Joined: 14 Feb 2011 04:07
Posts: 1119
Location: Bar, Montenegro
Code from the above link is for P16F628A. For P16F887 you need to add:
ANSEL = 0;
Use 8 MHz crystal.

Regards


Top
 Profile  
 
PostPosted: 17 Sep 2013 14:52 
Offline

Joined: 08 Jul 2010 08:11
Posts: 482
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?


Top
 Profile  
 
PostPosted: 19 Sep 2013 00:27 
Offline

Joined: 14 Feb 2011 04:07
Posts: 1119
Location: Bar, Montenegro
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


Top
 Profile  
 
PostPosted: 19 Sep 2013 07:37 
Offline

Joined: 08 Jul 2010 08:11
Posts: 482
Thanks aCko.


well explained. :D

t.q


Top
 Profile  
 
PostPosted: 24 Feb 2014 19:23 
Offline

Joined: 25 Sep 2012 09:39
Posts: 4
I was trying to generate the 50Hz PWM signal. But it dosen't work.

Here is my code:
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?


Top
 Profile  
 
PostPosted: 18 Mar 2014 10:35 
Offline

Joined: 08 Jul 2010 08:11
Posts: 482
hi MKdas,

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


Top
 Profile  
 
PostPosted: 02 Aug 2015 21:36 
Offline

Joined: 21 Apr 2015 10:34
Posts: 5
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


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 13 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: Google [Bot] and 10 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: