View previous topic :: View next topic |
Author |
Message |
danen
Joined: 18 Jan 2010 Posts: 17
|
Stuck in a loop |
Posted: Fri Dec 30, 2011 7:47 am |
|
|
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
|
|
Posted: Fri Dec 30, 2011 8:37 am |
|
|
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
|
|
Posted: Fri Dec 30, 2011 10:49 am |
|
|
Thank you for your help I've been racking my brain for three days now. |
|
|
|