In main, you need to initialize a before you enter the loop and you don't need b, also you need a direction, and when you reach the end you need to reverse the direction and when you reach the first one you need to change the direction again.
This should do what you want, I kept it simple but you can use a += dir;
Also you can use if (!a) for comparing to 0.
also dir *= -1 will reverse direction
and other things you can try, but that main will do what you want.
void main()
{
TRISB=0x00;
TRISD=0x00;
// initialize led
char a = 0;
// define direction
char dir = 1;
while(1)
{
led_display(a);
a = a + dir; // increment or decrement depending on direction
delay_sec(1);
if(a==4)
{
dir = -1;
// Edit: adjust index to be 3 so that the last LED will light up again.
// change to a = 2 if you don't want the last LED to stay on for double
// the amount of time as the rest.
a--;
}
if(a==-1)
{
dir = 1;
// Edit: adjust index to 0 so the first LED will light up again.
// change to a = 1 if you don't want the first LED to stay on for double
// the amount of time as the rest.
a++;
}
}
}
But if you were doing PWMs and on a different chip (PSoC 4 in my case) and using interrupts instead of delays etc, this will do like a ping pong effect across 8 leds by bouncing an dark LED from one end to the other. Kind of like the Knight Rider TV show KITT but in reverse when only one dark spot moves around instead of a bright one. But because of the PWM it transitions in between LEDs so it looks continuous to your eye.
#include <project.h>
#define BRIGHTNESS_DECREASE (1024u)
uint16 curPos;
static int direction = 1;
CY_ISR(InterruptHandler)
{
// Clear TC Interrupt
PWM_INTERRUPT_REQ_REG = PWM_INTR_MASK_TC;
if (direction > 0)
{
PWM_COMP_CAP_REG = ((PWM_COMP_CAP_REG + BRIGHTNESS_DECREASE) & PWM_16BIT_MASK);
}
else
{
PWM_COMP_CAP_REG = ((PWM_COMP_CAP_REG - BRIGHTNESS_DECREASE) & PWM_16BIT_MASK);
}
}
CY_ISR(InterruptHandler_1)
{
// Clear TC Interrupt
PWM_1_INTERRUPT_REQ_REG = PWM_1_INTR_MASK_TC;
if (direction > 0)
{
PWM_1_COMP_CAP_REG = ((PWM_1_COMP_CAP_REG + BRIGHTNESS_DECREASE) & PWM_1_16BIT_MASK);
}
else
{
PWM_1_COMP_CAP_REG = ((PWM_1_COMP_CAP_REG - BRIGHTNESS_DECREASE) & PWM_1_16BIT_MASK);
}
}
CY_ISR(InterruptHandler_2)
{
// Clear TC Interrupt
PWM_2_INTERRUPT_REQ_REG = PWM_2_INTR_MASK_TC;
if (direction > 0)
{
PWM_2_COMP_CAP_REG = ((PWM_2_COMP_CAP_REG + BRIGHTNESS_DECREASE) & PWM_2_16BIT_MASK);
}
else
{
PWM_2_COMP_CAP_REG = ((PWM_2_COMP_CAP_REG - BRIGHTNESS_DECREASE) & PWM_2_16BIT_MASK);
}
}
CY_ISR(InterruptHandler_3)
{
// Clear TC Interrupt
PWM_3_INTERRUPT_REQ_REG = PWM_3_INTR_MASK_TC;
if (direction > 0)
{
PWM_3_COMP_CAP_REG = ((PWM_3_COMP_CAP_REG + BRIGHTNESS_DECREASE) & PWM_3_16BIT_MASK);
}
else
{
PWM_3_COMP_CAP_REG = ((PWM_3_COMP_CAP_REG - BRIGHTNESS_DECREASE) & PWM_3_16BIT_MASK);
}
}
CY_ISR(InterruptHandler_4)
{
uint16 input1, input2;
// Clear TC Interrupt
PWM_4_ReadStatusRegister();
input1 = PWM_4_ReadCompare1();
input2 = PWM_4_ReadCompare2();
if (direction > 0)
{
PWM_4_WriteCompare1(input1 + BRIGHTNESS_DECREASE);
}
else
{
PWM_4_WriteCompare1(input1 - BRIGHTNESS_DECREASE);
}
if (direction > 0)
{
PWM_4_WriteCompare2(input2 + BRIGHTNESS_DECREASE);
}
else
{
PWM_4_WriteCompare2(input2 - BRIGHTNESS_DECREASE);
}
}
CY_ISR(InterruptHandler_5)
{
uint16 input1, input2;
// Clear TC Interrupt
PWM_5_ReadStatusRegister();
input1 = PWM_5_ReadCompare1();
input2 = PWM_5_ReadCompare2();
if (direction > 0)
{
PWM_5_WriteCompare1(input1 + BRIGHTNESS_DECREASE);
}
else
{
PWM_5_WriteCompare1(input1 - BRIGHTNESS_DECREASE);
}
if (direction > 0)
{
if (((input2 + BRIGHTNESS_DECREASE)&0xFFFFu) < input2)
direction = -1;
else
PWM_5_WriteCompare2(input2 + BRIGHTNESS_DECREASE);
}
else
{
if(((input2-BRIGHTNESS_DECREASE)&0xFFFFu) > input2)
direction = 1;
else
PWM_5_WriteCompare2(input2 - BRIGHTNESS_DECREASE);
}
}
int main()
{
CyDelay(50u);
// Enable the global interrupt
CyGlobalIntEnable;
// Enable the Interrupt component connected to interrupt
TC_ISR_StartEx(InterruptHandler);
TC_ISR_1_StartEx(InterruptHandler_1);
TC_ISR_2_StartEx(InterruptHandler_2);
TC_ISR_3_StartEx(InterruptHandler_3);
TC_ISR_4_StartEx(InterruptHandler_4);
TC_ISR_5_StartEx(InterruptHandler_5);
// Start the components
PWM_Start();
PWM_1_Start();
PWM_2_Start();
PWM_3_Start();
PWM_4_Start();
PWM_5_Start();
// Set periods
PWM_WritePeriod(65535u);
PWM_1_WritePeriod(65535u);
PWM_2_WritePeriod(65535u);
PWM_3_WritePeriod(65535u);
PWM_4_WritePeriod(65535u);
PWM_5_WritePeriod(65535u);
// Init dual PWMs
PWM_WriteCompare(BRIGHTNESS_DECREASE-1u);
PWM_1_WriteCompare(8192u+(BRIGHTNESS_DECREASE-1u));
PWM_2_WriteCompare(2*8192u+(BRIGHTNESS_DECREASE-1u));
PWM_3_WriteCompare(3*8192u+(BRIGHTNESS_DECREASE-1u));
PWM_4_WriteCompare1(4*8192u+(BRIGHTNESS_DECREASE-1u));
PWM_4_WriteCompare2(5*8192u+(BRIGHTNESS_DECREASE-1u));
PWM_5_WriteCompare1(6*8192u+(BRIGHTNESS_DECREASE-1u));
PWM_5_WriteCompare2(7*8192u+(BRIGHTNESS_DECREASE-1u));
for(;;)
{
}
}