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

16F684 issue: performance decay with PWM

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







16F684 issue: performance decay with PWM
PostPosted: Wed Mar 18, 2009 3:36 pm     Reply with quote

Hi, I have something happening inside my 16F684, below is the code of my simple program:

_ it listen on PINs A0 A1 A2 for some button to be pressed via interrupt
_ then it increase/decrease/zero the value of PWM
_ and increments a variable (which represent how many times buttons where pressed)

The behavior is that if I:

_ comment the instructions inside the ending while(true) the system is responsive to button press (empty loop)
_ uncomment (enable) the code inside the while_loop the system becames unresponsive, slow to react and sometimes i miss button press.

I used fast_IO on port C and A; Some of you may guess, the 4 bits output is feeded to a BCD decoder attached to a 7 segment display (to keep track of button press, not yet tested).

What i'm missing?

Tnx.

P.S. PWM-PWM-PWM looking inside the forum there are too many question arising from it Smile - at first, did i setted right the PWM? (8MHz clock, 20Khz PWM signal, 400 steps - actually it is working)

Code:
#include "C:\Documents and Settings\Guest\Desktop\PIC\main.h"
#use fast_io (A)
#use fast_io (C)

long value=0;
int cont=0;

#int_RA
void  RA_isr(void)
{
   delay_ms (30); // leave out bounces
   
   // increment PWM value
   if (input_state(PIN_A0)==1) {
      output_toggle(PIN_C3);
      if (value<=395) value+=5;
      set_pwm1_duty(value);
     cont++;
      while (input_state(PIN_A0)==1);
      }
   
   // zero value
   if (input_state(PIN_A1)==1) {
      value=0;
      set_pwm1_duty(value);
     cont++;
      while (input_state(PIN_A1)==1);
      }
   
   // decrease PWM value
   if (input_state(PIN_A2)==1)
   {
      output_toggle(PIN_C2);
      if (value>=5) value-=5;
      set_pwm1_duty(value);
     cont++;
      while (input_state(PIN_A2)==1);
      }
}

void main()
{
   set_tris_a (0x3F); // all inputs
   set_tris_c (0x00); // all outputs
   setup_adc_ports(NO_ANALOGS|VSS_VDD);
   setup_adc(ADC_OFF);
   
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   
   // 20KHz PWM with a total of 400 possible steps
   setup_timer_2(T2_DIV_BY_1, 99, 1);
   setup_ccp1(CCP_PWM);
   set_pwm1_duty(value);
   
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
   
   enable_interrupts(INT_RA);
   enable_interrupts(GLOBAL);
   setup_oscillator(OSC_8MHZ);

   while(true) {
   delay_ms(1000);
   output_c(cont & 0x0F);
   }

}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Mar 18, 2009 3:58 pm     Reply with quote

Look at your compiler warnings (or enable them). You should see
this warning:
Quote:
Interrupts disabled during call to prevent re-entrancy: (@delay_ms1)

Read this thread:
http://www.ccsinfo.com/forum/viewtopic.php?t=38151


Adding the line in bold will solve your immediate problem:
Quote:
#use delay(fill in with your freq)
void main()
{
Guest








PostPosted: Fri Mar 20, 2009 4:42 pm     Reply with quote

Exactly! wow, must refine my search routines Wink

Tnx!
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