View previous topic :: View next topic |
Author |
Message |
alex Guest
|
pic16f628a/pwm |
Posted: Tue Jan 29, 2008 8:01 pm |
|
|
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 .
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
|
|
|
alex Guest
|
|
Posted: Wed Jan 30, 2008 7:24 am |
|
|
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
|
|
Posted: Wed Jan 30, 2008 7:58 am |
|
|
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
|
|
Posted: Thu Jan 31, 2008 5:36 pm |
|
|
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
|
|
Posted: Thu Jan 31, 2008 6:58 pm |
|
|
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
|
|
Posted: Thu Jan 31, 2008 7:14 pm |
|
|
Thanks, PCM programmer.
I'm learning much about the Pics here and I am really sorry for my errors. |
|
|
alex Guest
|
|
Posted: Thu Jan 31, 2008 9:16 pm |
|
|
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
|
|
|
alex Guest
|
|
Posted: Mon Feb 04, 2008 1:25 pm |
|
|
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
|
|
Posted: Mon Feb 04, 2008 1:44 pm |
|
|
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
|
|
Posted: Tue Feb 05, 2008 4:14 pm |
|
|
Error(compiler CCS):
undefined identifier setup_oscillator
I change only the two lines like your suggests. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Feb 05, 2008 4:26 pm |
|
|
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);
} |
|
|
|
|