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

Can't make pwm

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



Joined: 12 Mar 2013
Posts: 3

View user's profile Send private message

Can't make pwm
PostPosted: Tue Mar 12, 2013 1:56 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Mar 12, 2013 5:46 pm     Reply with quote

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

View user's profile Send private message AIM Address

PostPosted: Tue Mar 12, 2013 7:49 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Mar 12, 2013 11:09 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Mar 13, 2013 1:39 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Mar 13, 2013 1:59 am     Reply with quote

If you are working on a "hobby servo" project, which I think you are,
because of the 50 Hz frequency, then you should tell us in the first post.
Just say "I need help to make a program to drive a hobby servo".

Here is a demo program that shows how to use the CCS servos.c file:
http://www.ccsinfo.com/forum/viewtopic.php?t=34560&start=22

There are many examples of programs to drive servos in the forum
archives. Examples:
http://www.ccsinfo.com/forum/viewtopic.php?t=47510
http://www.ccsinfo.com/forum/viewtopic.php?t=44328
khaledki7



Joined: 12 Mar 2013
Posts: 3

View user's profile Send private message

PostPosted: Wed Mar 13, 2013 6:19 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Mar 13, 2013 7:57 am     Reply with quote

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: 9240
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Mar 13, 2013 8:08 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Mar 13, 2013 9:12 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Mar 13, 2013 2:18 pm     Reply with quote

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);
}
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