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

Seek low Freq PWM help for PIC16F877A @ 20MHz
Goto page Previous  1, 2
 
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

PostPosted: Thu Oct 05, 2006 7:38 am     Reply with quote

Yes, you are correct and hopefully in the near future (when I understand how to implement this code into my working firmware) it will control the RC servo, but for now I just monitor the signal on my scope. As for the servo it is a DS300BB and the signal wire is amplified via a 2N2222. I built a 555timer circuit and used a 20K pot to sweep the duty cycle from 1 - 2ms and measure the current throught the control signal of 230uA because I could find a spec sheet to reference for you. I don't plan to connect the servo to the PIC external circuit until I can see the wave form execute the code to sweep or have the ability to change the duty cycle while performing its the other tasks. Thank you all for your insightful input, I believe we are real close to solving my dilemma.
fushion3



Joined: 28 Sep 2006
Posts: 24

View user's profile Send private message

PostPosted: Thu Oct 05, 2006 12:18 pm     Reply with quote

Here is a follow-up question regarding the code & compiler. Below is the code that I have atempted to make the "width" change, but it is set to 10 and stays at that value, does any one know how to apply this?

Code:
#include <16F877A>
#fuses HS, NOWDT, NOPROTECT, BROWNOUT
#use delay(clock = 20000000)

#define PWM_PIN  PIN_C2

#define LOOPCNT 39

int8 width = 10;
int8 up = 1;

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

//====================================
main()
{
setup_counters(RTCC_INTERNAL, RTCC_DIV_1);
enable_interrupts(INT_RTCC);
enable_interrupts(GLOBAL);

if(up == 1)
{
   width += 1;
}
else
{
   width -= 1;
}
if (width 10 >= 50)
{
   up = 0;
}    
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);
  }
}


Last edited by fushion3 on Fri Oct 06, 2006 12:21 pm; edited 1 time in total
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Oct 05, 2006 1:19 pm     Reply with quote

You just want a simple interface to the PC, to allow you to test the PWM
routine. I've posted one below. Notice what I've done:
Make a while() loop. Call getc() to wait for a keypress in the terminal
window. Convert it to upper case. That's because the user might have
their keyboard set for lower case or ALL CAPS. You want your program
to work, no matter what case the key is typed in.

Check if it's a 'U' or a 'D' with two if() statements. Within each statement,
do a range check. Don't let the user keep pressing up or down, and
exceed a boundary. (In other words, validate the entry). Then update
the global width variable with the new value. Also use printf() to report
the new width value to the user, so they know what's happening.
Code:

#include <16F877A.h>
#fuses HS, NOWDT, NOPROTECT, PUT, BROWNOUT, NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

#define PWM_PIN PIN_C2

#define LOOPCNT 39
#define MAX_WIDTH 39

int8 width = 10;


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

//====================================
main()
{
char c;

setup_counters(RTCC_INTERNAL, RTCC_DIV_1);
enable_interrupts(INT_RTCC);
enable_interrupts(GLOBAL);

while(1)
  {
   c = toupper(getc());
 
   if(c == 'U')   // Up ?
     {
      if(width < MAX_WIDTH)
         width++;

      printf("width = %u\n\r", width);
     }
 

   if(c == 'D')   // Down ?
     {
      if(width > 0)
         width--;

      printf("width = %u\n\r", width);
     }
 
  }


}

//====================================
#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);
  }
}
fushion3



Joined: 28 Sep 2006
Posts: 24

View user's profile Send private message

PostPosted: Fri Oct 06, 2006 10:06 am     Reply with quote

Thanks PCM,

I made minor changes to your code so I could use my bluetooth and it works beautifully! Could you explain why it didn't work incrementally with each pass of the while loop and why it does with characters, is it a timing issue?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Oct 06, 2006 10:55 am     Reply with quote

The problem is when you posted your code, you didn't select the
checkbox to "Disable HTML in this post". So several lines of your
code in main() are missing. Also you didn't enclose it in a Code block,
so the indentation wasn't preserved. So for those reasons, I really
didn't even look at it. I just made something that worked and posted it.
fushion3



Joined: 28 Sep 2006
Posts: 24

View user's profile Send private message

PostPosted: Fri Oct 06, 2006 11:07 am     Reply with quote

For future reference how does one place the code in the code box, I tried and obviously it didn't work and didn't find how to do so using forum help or FAQ.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Oct 06, 2006 11:29 am     Reply with quote

There a couple ways to do it. The method I use is to select and
highlight the code section with the mouse. Then click on the Code
button. This will place the little bracketed code block indicators
at the start and end of the highlighted section.
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 Previous  1, 2
Page 2 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