View previous topic :: View next topic |
Author |
Message |
khaledki7
Joined: 12 Mar 2013 Posts: 3
|
Can't make pwm |
Posted: Tue Mar 12, 2013 1:56 pm |
|
|
Code: |
#include <18F452.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=25000000)
#INT_TIMER1 // This function is called every time
void clock_isr() { // timer 1 overflows (65535->0), which is
output_toggle(PIN_B3);
}
void main() {
setup_timer_1(T1_INTERNAL | T1_DIV_BY_4);
set_timer1(0);
enable_interrupts(INT_TIMER1);
enable_interrupts(GLOBAL);
// output_high(PIN_B3);
while(TRUE);
} |
|
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Tue Mar 12, 2013 5:46 pm |
|
|
I don't understand the question.
Are you wanting to:-
1) Create a PWM output using timer1?
OR
2) Create a PWM output using the built in hardware version?
Mike |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Tue Mar 12, 2013 7:49 pm |
|
|
PWM=Pulse Width Modulation.
Code: | output_toggle(PIN_B3);
|
Gives an approximate square wave with fixed 50% duty,
with no way to actually MODULATE it in your program.
At best it is a jittery square wave generator and nothing more. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Mar 12, 2013 11:09 pm |
|
|
I tested your program in hardware, with CCS compiler vs. 4.141. I don't
have a 25 MHz external oscillator, so I used a 20 MHz crystal. It works.
I see a squarewave signal on Pin B3, with a frequency of about 9.5 Hz.
The signal goes from 0v to 5v. I can see it on my oscilloscope. |
|
|
khaledki7
Joined: 12 Mar 2013 Posts: 3
|
|
Posted: Wed Mar 13, 2013 1:39 am |
|
|
hi I want to make a 50HZ pulse but I can't make it
pleas help and thanks for reply |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
khaledki7
Joined: 12 Mar 2013 Posts: 3
|
|
Posted: Wed Mar 13, 2013 6:19 am |
|
|
hi thanks for your help and i make this code but the servo didn't work. Any answers?
Code: |
#include <18F452.H>
#device adc=8
#fuses HS, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#define LEFT_SERVO_PIN PIN_B0
#define RIGHT_SERVO_PIN PIN_B1
//#define LONG_TIME 0.0150 // Longest pulse width high time
#include <servos.c>
//=========================================
void main()
{
int j;
j=0;
init_servos();
while(1)
{
//for (j=0;j<=4;++j)
//{
// set_servo(LEFT, FORWARD, j); // Go forward
// delay_ms(2000);
//}
// set_servo(LEFT, BACKWARD, 3); // Go reverse
set_servo(LEFT, FORWARD, 4);
}
} |
|
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Wed Mar 13, 2013 7:57 am |
|
|
Quote: | hi thanks for your help and i make this code but the servo didn't work. | What does not work?
In no particular order:-
1) Do you know that your servo is good?
2) Are there pulses appearing from the PIC pins?
3) Have you got servos which are compatible with the CCS servo drivers?
4) Have you checked, and double checked, your wiring?
5) Have you checked for accidental S/C and O/C?
6) Will your PIC run LED flasher code at the correct speed?
Mike |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9226 Location: Greensville,Ontario
|
|
Posted: Wed Mar 13, 2013 8:08 am |
|
|
S/C = Short Circuit
O/C = Open Circuit
just in case anyone else is scratching their heads.....
jay |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Wed Mar 13, 2013 9:12 am |
|
|
When you get as many S/Cs and O/Cs as I do, it's a pain to write out in full every time.
Mike |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Mar 13, 2013 2:18 pm |
|
|
Don't call set_servo() continuously in a high-speed while() loop.
Just call it once, and it will work. See the example below:
Code: |
#include <18F452.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4M)
#define LEFT_SERVO_PIN PIN_B0
#define RIGHT_SERVO_PIN PIN_B1
#include <servos.c>
//=========================================
void main()
{
init_servos();
set_servo(LEFT, FORWARD, 4);
while(1);
} |
|
|
|
|