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

Stuck in a loop

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



Joined: 18 Jan 2010
Posts: 17

View user's profile Send private message

Stuck in a loop
PostPosted: Fri Dec 30, 2011 7:47 am     Reply with quote

Hi

I'm trying to pwm on some leds and then pwm them off. However, the problem I'm having is that the loop runs once and then won't run again.

I toggled an led to see where I was in the loop and it seems to hang at the end of the second loop

What am I missing here.

Code:

#include <16F628A.h>

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES INTRC_IO                 //Internal RC Osc, no CLKOUT
#FUSES NOPUT                      //Power Up Timer
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NOBROWNOUT                 //Reset when brownout detected
#FUSES MCLR                   //Master Clear pin used for I/O
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD                    //No EE protection

#use delay(clock=4000000)

int16 PWM_PIN[]=
{
PIN_B0,
PIN_B1,
PIN_B2,
};

#define LOOPCNT 39

int8 width;
int8 x;

void main()
{
   width = 0;
   x=0;

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

   while(1)
   {
         
      for (x=0;x<=2;x++)
      {
         for(width = 0; width <= LOOPCNT; width++)
         {
         delay_ms(100);
         }
      }
      output_toggle(PIN_B3);
      for (x=2;x>=0;x--)                       
      {
         for(width = 39;width <= LOOPCNT; width--)
         {
            delay_ms(100);
         }                                       
      }
      output_toggle(PIN_B3);                    // this line does not run
   }
   
}

#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[x]);
       pulse--;
    }
    else
    {
        output_low(PWM_PIN[x]);
    }
}
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Fri Dec 30, 2011 8:37 am     Reply with quote

int8 x defaults to an unsigned variable so x>=0 must always be true. If you want x to be signed you must declare to as such.
_________________
The search for better is endless. Instead simply find very good and get the job done.
danen



Joined: 18 Jan 2010
Posts: 17

View user's profile Send private message

PostPosted: Fri Dec 30, 2011 10:49 am     Reply with quote

Thank you for your help I've been racking my brain for three days now.
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