Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

PIC16F628 PWM LED Sequencer error

Status
Not open for further replies.

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)
 

try removing delay inbetween two leds

LED1 = 0; Delay(i); LED2 = 1; make it as LED1 = 0;LED2 = 1;

so that it looks like falling leds.

In the function "PWM_FADE_1_TO_0" you are increasing the delay after each LED gets off and switchs on next LED.


If the error is something else please post whats not working ?
 
Last edited:

Thank you for your reply.

I changed the code according to your instructions. But LEDs display very unusual pattern.

That is all LEDs lit random order without any order or delay.

Can you clarify what is the error?
 

make your function as so

Code:
void PWM_FADE_1_TO_0(void) // Duty cycle - 100% to 0%
{
    for (i = 0; i < PWN_COUNTER; i++) {
     LED1 = ~LED1;
     Delay(10);
     LED2 = ~LED2;
     Delay(10);
     LED3 = ~LED3;
     Delay (10);
     LED4 = ~LED4;
     Delay(10);
     ....
     ....
     LED12 = ~LED12;
     Delay(10);
}


Even if its still randomly going on, check out your schematic, probably you might have led connections differently.
 
I changed the code according to your reply. But now all LEDs are on simultaneously. No any effect. This is changed code

Code:
/*
'*******************************************************************************
'  Configurations
'    Oscillator:                 INTOSC:I/O
'    Watchdog Timer:             OFF
'    Power up Timer:             Disabled
'    Master Clear Enable:        Enabled
'    Browun Out Detect:          Enabled
'    Low Voltage Program:        Disabled
'    Data EE Read Protect:       Disabled
'    Code Protect:               OFF
'*******************************************************************************
*/
// 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 = ~LED1;
     Delay(10);
     LED2 = ~LED2;
     Delay(10);
     LED3 = ~LED3;
     Delay (10);
     LED4 = ~LED4;
     Delay(10);
     LED5 = ~LED5;
     Delay(10);
     LED6 = ~LED6;
     Delay(10);
     LED7 = ~LED7;
     Delay(10);
     LED8 = ~LED8;
     Delay(10);
     LED9 = ~LED9;
     Delay(10);
     LED10 = ~LED10;
     Delay(10);
     LED11 = ~LED11;
     Delay(10);
     LED12 = ~LED12;
     Delay(10);
}
}

void Delay(int num)
{
 while(num>0)
 num--;
}
 

Code:
void Delay(int num)
{
 int i;
 while(num>0){
       for(i=0;i<1275;i++)
            Nop();
       num--;
}

change your delay as such
 

I changed the code as below

Code:
/*
'*******************************************************************************
'    Configurations
'    Oscillator:                 INTOSC:I/O
'    Watchdog Timer:             OFF
'    Power up Timer:             Disabled
'    Master Clear Enable:        Enabled
'    Browun Out Detect:          Enabled
'    Low Voltage Program:        Disabled
'    Data EE Read Protect:       Disabled
'    Code Protect:               OFF
'*******************************************************************************
*/
// 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 = ~LED1;
     Delay(10);
     LED2 = ~LED2;
     Delay(10);
     LED3 = ~LED3;
     Delay(10);
     LED4 = ~LED4;
     Delay(10);
     LED5 = ~LED5;
     Delay(10);
     LED6 = ~LED6;
     Delay(10);
     LED7 = ~LED7;
     Delay(10);
     LED8 = ~LED8;
     Delay(10);
     LED9 = ~LED9;
     Delay(10);
     LED10 = ~LED10;
     Delay(10);
     LED11 = ~LED11;
     Delay(10);
     LED12 = ~LED12;
     Delay(10);
  }
}

     void Delay(int num)
  {
     int i;
     while(num>0)
     for(i=0;i<1275;i++)
     Nop();
     num--;
  }

Now LED1 (PORTB 0) continuously ON and others OFF. No any change. I attached my schematic with here. LED Tester.jpg
 

copy this code

Code:
#include<P16F628.h>
// 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;
// End LED connections

   void PWM_FADE_1_TO_0(void);
   void Delay(int num);

   void main(void){
       	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%
{
     LED1 = ~LED1;
     Delay(10);
     LED2 = ~LED2;
     Delay(10);
     LED3 = ~LED3;
     Delay(10);
     LED4 = ~LED4;
     Delay(10);
     LED5 = ~LED5;
     Delay(10);
     LED6 = ~LED6;
     Delay(10);
     LED7 = ~LED7;
     Delay(10);
     LED8 = ~LED8;
     Delay(10);
     LED9 = ~LED9;
     Delay(10);
     LED10 = ~LED10;
     Delay(10);
     LED11 = ~LED11;
     Delay(10);
     LED12 = ~LED12;
     Delay(10);
}

void Delay(int num){
     int i;
     while(num>0){
     	for(i=0;i<1275;i++)
    	 Nop();
     num--;
     }
  }
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top