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 support@ccsinfo.com

Seek low Freq PWM help for PIC16F877A @ 20MHz
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
fushion3



Joined: 28 Sep 2006
Posts: 24

View user's profile Send private message

Seek low Freq PWM help for PIC16F877A @ 20MHz
PostPosted: Fri Sep 29, 2006 11:40 am     Reply with quote

I have read some of the posts for low freq PWM using a 4MHz clock, is that program still valid on a 20MHz? I would also like to insert this snippet of code into a larger project using structures, can anyone help me with this solution?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Sep 29, 2006 1:21 pm     Reply with quote

Fushion,
No one knows what code snippet you're referring to. You need to
provide people with a link to the code.
fushion3



Joined: 28 Sep 2006
Posts: 24

View user's profile Send private message

PostPosted: Mon Oct 02, 2006 9:00 am     Reply with quote

My apologies, I am new to this format. Here is the code and the version of CCS PCM I have is 3.212
Code:

#include <16F877>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 4000000)

#define PWM_PIN  PIN_B1

#define LOOPCNT 39

int8 width;

//-------------------------------
#INT_RTCC
void tick_interrupt(void);

//====================================
main()
{
width = 10;

setup_counters(RTCC_INTERNAL, RTCC_DIV_1);
enable_interrupts(INT_RTCC);
enable_interrupts(GLOBAL);
 
while(1);
}

//====================================
#INT_RTCC
void tick_interrupt(void)
{
static int8 loop = LOOPCNT;
static int8 pulse;

if(--loop == 0)
  {
   loop = LOOPCNT;
   pulse = width;
  }

if(pulse)
  {
   output_high(PWM_PIN);
   pulse--;
  }
else
  {
   output_low(PWM_PIN);
  }


P.S. I can get this to compile and generate a HEX file and load it on my PIC, but I can't find a wave form on my oscope even by changing the output pin assignment location, any suggestions on this situation?
Ttelmah
Guest







PostPosted: Mon Oct 02, 2006 9:41 am     Reply with quote

To run at 20MHz, there would be one critical change necessary to make the chip work at all. 'HS', needs to be subsituted for 'XT' in the fuses line. Otherwise the code will run (the delay statement should also be changed, but will have no effect, since no 'delays' are being used).
The XT/HS change is vital, since otherwise the oscillator won't have enough gain to run the faster crystal.

Best Wishes
fushion3



Joined: 28 Sep 2006
Posts: 24

View user's profile Send private message

PostPosted: Mon Oct 02, 2006 11:47 am     Reply with quote

Ttelmah'

Thanks for your input, this did help a little because there appears to be a wave form now although its not a square wave, but it is a wave form in two pieces. The first section spikes high then gradually declines, then the second part looks like a capacitve charge signal.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Oct 02, 2006 4:54 pm     Reply with quote

Quote:

There appears to be a wave form now although its not a square wave,
but it is a wave form in two pieces. The first section spikes high then
gradually declines, then the second part looks like a capacitve charge
signal.

Describe the external circuit that the PIC is driving, including component
values.
Guest








PostPosted: Tue Oct 03, 2006 7:03 am     Reply with quote

The PIC will control a hobby (RC) servo positioning with this software generated PWM. This has to be done in a manner that the PIC can be doing other tasks simultaneously. The control operates like any other servo, the duty cycle of 1ms rotates it fully one direction (say 0 degrees) while a duty cycle of 1.5ms moves it to midpoint(say 90 degrees) and a duty of 2ms rotates it fully the opposite direction (say 180 degrees). The PIC's output pins rated @ 5 volts & 20mA are plenty powerful enough for this control.
Ttelmah
Guest







PostPosted: Tue Oct 03, 2006 9:38 am     Reply with quote

First of all, it won't be a square wave, since you are specifying 25% duty cycle.
The point being made about the connections, was not 'what do you intend to connect to the pins', but 'what is connected to the pins'. The behaviour you describe, is what you would expect, if there was perhaps a poor connection to your scope. Test this. Try a 'seven liner':
Code:

//with existing header here:
main {
   while(TRUE) {
       output_high(PWM_PIN);
       delay_ms(3);
       output_low(PWM_PIN);
       delay_ms(7);
   }
}

If your waveform is still the same, then you need to look at all your connections.

Best Wishes
fushion3



Joined: 28 Sep 2006
Posts: 24

View user's profile Send private message

PostPosted: Wed Oct 04, 2006 8:15 am     Reply with quote

The definition of square wave that was meant was one that is either HI or LOW having nothing in between. Yes, I have used that style of code you just posted and it does output a wave form, but when its running in the loop how would yoou pass different parameters to it or stop it on command for that matter? I even tried placing a variable in the delay_(i) statement and have it increment on each loop, it even compiled, but there was nothing happening on the output pin.
Ttelmah
Guest







PostPosted: Wed Oct 04, 2006 8:33 am     Reply with quote

I differ with your definition, but have 'met this' before. It is a difference in the teaching between the US, and England. Here a 'square wave', is the specific example, of a rectangular wave, with a 50:50 mark space ratio. In the US, it is common to use the term 'square wave' for the rectangular waves...
If you search the forum, you will find several posted code snippets to give variable time delays. The inbuilt 'delay' function only accepts constants. However I still have to go back through the 'circle', that as posted, the interrupt driven code should work, if the test code works.
I would clear your mind of what you have tried in the past, and run the test code. It is always possible that in experimenting, some connection has broken which then explains your scope results.

Best Wishes
fushion3



Joined: 28 Sep 2006
Posts: 24

View user's profile Send private message

PostPosted: Wed Oct 04, 2006 9:36 am     Reply with quote

There is not a connection problem if I can change the code to output on the same pin location using two different snippets of code and one outputs your diffenition of a perfect rectangle wave while the other puts out a capacitive dicharge/charge signal. I would like to use your version of code because of its flexibilty and accessibilty. I tried to copy a screen shot of the wave form, but this format wont alloy me to paste it.
treitmey



Joined: 23 Jan 2004
Posts: 1094
Location: Appleton,WI USA

View user's profile Send private message Visit poster's website

PostPosted: Wed Oct 04, 2006 9:53 am     Reply with quote

To show an image, first save it at a location like immagshack
http://imageshack.us/
then copy/past the "forum" code they provide.


Last edited by treitmey on Wed Oct 04, 2006 1:50 pm; edited 1 time in total
fushion3



Joined: 28 Sep 2006
Posts: 24

View user's profile Send private message

PostPosted: Wed Oct 04, 2006 11:54 am     Reply with quote

Here is the output of Ttelmah code...



fushion3



Joined: 28 Sep 2006
Posts: 24

View user's profile Send private message

PostPosted: Wed Oct 04, 2006 1:41 pm     Reply with quote

Here is the ouput of his code with a change in the RTCC_DIV_4 setup

PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Oct 04, 2006 2:22 pm     Reply with quote

Quote:
The PIC will control a hobby (RC) servo positioning with this software generated PWM.


You said "will control", which implies "in the future". What is your
test circuit driving at this time ? Is it the servo, or something else ?
Please describe what it's driving.

Also, is the PIC pin directly driving the servo (or whatever circuit is
emulating the servo), or is there a transistor or FET between the
PIC pin and the servo ?

My suspicion is that the PIC pin is being asked to drive something
beyond its ability. But if you tell us what the circuit is, then we'll
know for sure. If it's a servo, post a the part number and a link
to the data sheet or web page.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
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