kalinga
Newbie level 5
- Joined
- May 19, 2009
- Messages
- 8
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1,281
- Activity points
- 1,355
Hi,
I am newbie to PIC. I want to build a PWM LED sequencer with tail effect like below.
start LED1 > small delay > start LED2 > small delay > start LED3 > ...............
I wrote this by PIC C for 16F628A.
// LED connections
sbit LED1 at RB0_bit;
sbit LED2 at RB1_bit;
sbit LED3 at RB2_bit;
sbit LED4 at RB3_bit;
sbit LED5 at RB4_bit;
sbit LED6 at RB5_bit;
sbit LED7 at RB6_bit;
sbit LED8 at RB7_bit;
sbit LED9 at RA0_bit;
sbit LED10 at RA1_bit;
sbit LED11 at RA2_bit;
sbit LED12 at RA3_bit;
sbit LED1_Direction at TRISB0_bit;
sbit LED2_Direction at TRISB1_bit;
sbit LED3_Direction at TRISB2_bit;
sbit LED4_Direction at TRISB3_bit;
sbit LED5_Direction at TRISB4_bit;
sbit LED6_Direction at TRISB5_bit;
sbit LED7_Direction at TRISB6_bit;
sbit LED8_Direction at TRISB7_bit;
sbit LED9_Direction at TRISA0_bit;
sbit LED10_Direction at TRISA1_bit;
sbit LED11_Direction at TRISA2_bit;
sbit LED12_Direction at TRISA3_bit;
// End LED connections
int i = 0 ,PWN_COUNTER = 256;
void PWM_FADE_1_TO_0(void);
void Delay(int num);
void main(void)
{
CMCON |= 7; // Comparator is Off
TRISA = 0;
PORTA = 0;
TRISB = 0;
PORTB = 0;
while(1){
PWM_FADE_1_TO_0();
}
}
void PWM_FADE_1_TO_0(void) // Duty cycle - 100% to 0%
{
for (i = 0; i < PWN_COUNTER; i++)
{
LED1 = 1;
Delay(PWN_COUNTER - i);
LED1 = 0;
Delay(i);
LED2 = 1;
Delay (PWN_COUNTER - i);
LED2 = 0;
Delay(i);
LED3 = 1;
Delay (PWN_COUNTER - i);
LED3 = 0;
Delay(i);
LED4 = 1;
Delay (PWN_COUNTER - i);
LED4 = 0;
Delay(i);
LED5 = 1;
Delay (PWN_COUNTER - i);
LED5 = 0;
Delay (i);
LED6 = 1;
Delay (PWN_COUNTER - i);
LED6 = 0;
Delay (i);
LED7 = 1;
Delay (PWN_Counter - i);
LED7 = 0;
Delay(i);
LED8 = 1;
Delay (PWN_Counter - i);
LED8 = 0;
Delay(i);
LED9 = 1;
Delay (PWN_Counter - i);
LED9 = 0;
Delay(i);
LED10 = 1;
Delay (PWN_Counter - i);
LED10 = 0;
Delay(i);
LED11 = 1;
Delay (PWN_Counter - i);
LED11 = 0;
Delay(i);
LED12 = 1;
Delay (PWN_Counter - i);
LED12 = 0;
Delay(i);
}
}
void Delay(int num)
{
while(num>0)
num--;
}
But LEDs not work correctly. Please anyone can help me. actually I want to build a LED waterfall for my daughter's school exhibition.
Kalinga
(Sorry for my bad English)
I am newbie to PIC. I want to build a PWM LED sequencer with tail effect like below.
start LED1 > small delay > start LED2 > small delay > start LED3 > ...............
I wrote this by PIC C for 16F628A.
// LED connections
sbit LED1 at RB0_bit;
sbit LED2 at RB1_bit;
sbit LED3 at RB2_bit;
sbit LED4 at RB3_bit;
sbit LED5 at RB4_bit;
sbit LED6 at RB5_bit;
sbit LED7 at RB6_bit;
sbit LED8 at RB7_bit;
sbit LED9 at RA0_bit;
sbit LED10 at RA1_bit;
sbit LED11 at RA2_bit;
sbit LED12 at RA3_bit;
sbit LED1_Direction at TRISB0_bit;
sbit LED2_Direction at TRISB1_bit;
sbit LED3_Direction at TRISB2_bit;
sbit LED4_Direction at TRISB3_bit;
sbit LED5_Direction at TRISB4_bit;
sbit LED6_Direction at TRISB5_bit;
sbit LED7_Direction at TRISB6_bit;
sbit LED8_Direction at TRISB7_bit;
sbit LED9_Direction at TRISA0_bit;
sbit LED10_Direction at TRISA1_bit;
sbit LED11_Direction at TRISA2_bit;
sbit LED12_Direction at TRISA3_bit;
// End LED connections
int i = 0 ,PWN_COUNTER = 256;
void PWM_FADE_1_TO_0(void);
void Delay(int num);
void main(void)
{
CMCON |= 7; // Comparator is Off
TRISA = 0;
PORTA = 0;
TRISB = 0;
PORTB = 0;
while(1){
PWM_FADE_1_TO_0();
}
}
void PWM_FADE_1_TO_0(void) // Duty cycle - 100% to 0%
{
for (i = 0; i < PWN_COUNTER; i++)
{
LED1 = 1;
Delay(PWN_COUNTER - i);
LED1 = 0;
Delay(i);
LED2 = 1;
Delay (PWN_COUNTER - i);
LED2 = 0;
Delay(i);
LED3 = 1;
Delay (PWN_COUNTER - i);
LED3 = 0;
Delay(i);
LED4 = 1;
Delay (PWN_COUNTER - i);
LED4 = 0;
Delay(i);
LED5 = 1;
Delay (PWN_COUNTER - i);
LED5 = 0;
Delay (i);
LED6 = 1;
Delay (PWN_COUNTER - i);
LED6 = 0;
Delay (i);
LED7 = 1;
Delay (PWN_Counter - i);
LED7 = 0;
Delay(i);
LED8 = 1;
Delay (PWN_Counter - i);
LED8 = 0;
Delay(i);
LED9 = 1;
Delay (PWN_Counter - i);
LED9 = 0;
Delay(i);
LED10 = 1;
Delay (PWN_Counter - i);
LED10 = 0;
Delay(i);
LED11 = 1;
Delay (PWN_Counter - i);
LED11 = 0;
Delay(i);
LED12 = 1;
Delay (PWN_Counter - i);
LED12 = 0;
Delay(i);
}
}
void Delay(int num)
{
while(num>0)
num--;
}
But LEDs not work correctly. Please anyone can help me. actually I want to build a LED waterfall for my daughter's school exhibition.
Kalinga
(Sorry for my bad English)