CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

pic16f628a/pwm

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
alex
Guest







pic16f628a/pwm
PostPosted: Tue Jan 29, 2008 8:01 pm     Reply with quote

Hi again.
I need to create one pulse with frequency of 46 hz(21.5 ms). 1.5 ms in high and 20 ms in low, for use with servos.
I am using a pic16f628a , CCS compiler 3.20 and proteus 6.2 SP5. I can´t use clock 4.0 mhz for setup_timer_2 and use clock of 500000 for configurate the setup_timer_2. But idon´t know if it is right. I am a new guy in CCS Very Happy .

Ah! I see in proteus a pulse of 3 ms....

The code:

Quote:
#include<16f628a.h>

#fuses NOWDT,NOPROTECT,PUT,NOLVP,MCLR,INTRC_IO

#use delay(clock=500000)
#use fast_io(b)
#use fast_io(a)


void main()
{
set_tris_b(0xF7);



setup_ccp1(CCP_PWM); // Configure CCP1 as a PWM

setup_timer_2(T2_DIV_BY_16, 169, 1);
set_pwm1_duty(84);

}


any help is good....
Thanks
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Jan 29, 2008 10:50 pm     Reply with quote

CCS has a servo driver file in this directory:
Quote:
c:\Program Files\picc\Drivers\Servos.c


This post has a test program which uses the servos.c driver.
http://www.ccsinfo.com/forum/viewtopic.php?t=31149&start=11
alex
Guest







PostPosted: Wed Jan 30, 2008 7:24 am     Reply with quote

Thanks , PCM programmer.
I will try use the servos.c. But i don't understand the clock. The clock of pic is set with 4 MHz and insides is 1 MHz , so if use clock=500000, below of 1 MHz, I can´t change the clock for 500.000?

PS. Is there a sample of set_servo in the net?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Jan 30, 2008 7:58 am     Reply with quote

I tested it just now with an oscillator frequency of 500 KHz and it works
fine. Here is the test program:
Code:

#include <18F4550.h>
#fuses INTRC_IO, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 500000)

#include <servos.c>

//=========================================
void main()
{

init_servos(); 

while(1)
  {
   set_servo(LEFT, FORWARD, 4);
   delay_ms(500);

   set_servo(LEFT, FORWARD, 1);
   delay_ms(500);

   set_servo(LEFT, BACKWARD, 4);
   delay_ms(500);
  }

}


Quote:
PS. Is there a sample of set_servo in the net?

I'm not sure what you mean by this. Search the forum for set_servo.
Search the net for it. There are many examples.
Guest








PostPosted: Thu Jan 31, 2008 5:36 pm     Reply with quote

I can´t create a period of 20ms.

What´s wrong?

Quote:
#include<16f628a.h>

#fuses NOWDT,NOPROTECT,PUT,NOLVP,MCLR,INTRC_IO

#use delay(clock=500000)
#use fast_io(b)
#use fast_io(a)


void main()
{
int value;
value = 12;
set_tris_b(0xF7);



setup_ccp1(CCP_PWM); // Configure CCP1 as a PWM

setup_timer_2(T2_DIV_BY_16,155, 1); //
set_pwm1_duty(value); // high time= value * 4 * (1/clock) * prescale //


}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Jan 31, 2008 6:58 pm     Reply with quote

Quote:
I can't create a period of 20ms.

The internal oscillator for the 16F628A can't run at 500 KHz. It only has
two speeds: 4 MHz and 48 KHz. This is stated in the data sheet.

It's defaulting to 4 MHz operation, so it's running 8x faster than you think
it is. That's why you're getting a period of 2.5 ms instead of 20 ms.
alex
Guest







PostPosted: Thu Jan 31, 2008 7:14 pm     Reply with quote

Thanks, PCM programmer.

I'm learning much about the Pics here and I am really sorry for my errors.
alex
Guest







PostPosted: Thu Jan 31, 2008 9:16 pm     Reply with quote

It has now a period of 20 ms.
And high time = 5ms , so the wave has 5ms -high and 15 ms-low.
But I calculate high for 1.5 ms.

#include<16f628a.h>

#fuses NOWDT,NOPROTECT,PUT,NOLVP,MCLR,INTRC_IO

#use delay(clock=48000)
#use fast_io(b)
#use fast_io(a)


void main()
{
long int value;
value = 72;
set_tris_b(0xF7);



setup_ccp1(CCP_PWM); // Configure CCP1 as a PWM

setup_timer_2(T2_DIV_BY_1,239, 1); //
set_pwm1_duty(value); // high time= value * (1/clock) * prescale //


}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Jan 31, 2008 10:11 pm     Reply with quote

See this post. Notice how the values for 25% and 50% duty cycle are set.
http://www.ccsinfo.com/forum/viewtopic.php?t=17729&start=1

You want a 7.5% duty cycle.

(1.5 ms / 20 ms) * 100 = 7.5%
alex
Guest







PostPosted: Mon Feb 04, 2008 1:25 pm     Reply with quote

I can´t shrink the duty pulse of 5 ms to 1.5 ms.

I made
1.5/20= 0.075

Xduty=239 x 0.0725 = 17.95
What´s wrong?
Thanks

Code:

#include<16f628a.h>

#fuses NOWDT,NOPROTECT,PUT,NOLVP,MCLR,INTRC_IO

#use delay(clock=48000)
#use fast_io(b)
#use fast_io(a)


void main()
{
set_tris_b(0xF7);
output_low(PIN_b3);


setup_ccp1(CCP_PWM);  // Configure CCP1 as a PWM

setup_timer_2(T2_DIV_BY_1,239, 1);
set_pwm1_duty(17);


}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Feb 04, 2008 1:44 pm     Reply with quote

Add the two lines shown in bold below:
Quote:

#include<16f628a.h>
#fuses NOWDT,NOPROTECT,PUT,NOLVP,MCLR,INTRC_IO
#use delay(clock=48000)

#use fast_io(b)
#use fast_io(a)

void main()
{
setup_oscillator(OSC_48KHZ);

set_tris_b(0xF7);
output_low(PIN_b3);

setup_ccp1(CCP_PWM); // Configure CCP1 as a PWM

setup_timer_2(T2_DIV_BY_1,239, 1);
set_pwm1_duty(17);

while(1);
}
alex
Guest







PostPosted: Tue Feb 05, 2008 4:14 pm     Reply with quote

Error(compiler CCS):

undefined identifier setup_oscillator

I change only the two lines like your suggests.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Feb 05, 2008 4:26 pm     Reply with quote

Your version may be too old. It may not have that function.
Add the #byte, #bit, and #define statements shown below.
This code does the same thing as the modern setup_oscillator() function.
Code:

#define OSC_48KHZ   0
#define OSC_4MHZ    8

#byte PCON = 0x8E
#bit OSCF = PCON.3

#define setup_oscillator(x)  (x & 8) ? (OSCF=1) : (OSCF=0)

void main()
{
setup_oscillator(OSC_48KHZ);

// Put  your code here.


while(1);
}
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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 vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group