12 led chaser with fading effect PIC16F628A

Thread Starter

jayanath

Joined Dec 11, 2010
17
Dear all i have following code for 16f628a but all leds fade together andgetting on together no running action with fade. request corrct my code. and i also need to connect porta for 4 leds to make 12 led chaser. anybody can help me with hitech c?

Rich (BB code):
#include <pic.h>  :mad:
  #define FADE_RATE 12 
  #define INITIAL_WIDTH 128 
  void fade()
  {
    unsigned char rate = FADE_RATE;
    unsigned char pulse_width = INITIAL_WIDTH;
    unsigned char count;
  while(pulse_width)
  {                       
      PORTB= 0b11111111;  
      for(count=0; count<pulse_width; count++)
  {   
  asm("NOP");
      }
  PORTB=0b00000000;       
   for(; count; count++)
  { 
     asm("NOP");       
      }
   
      if(!rate--){        
        pulse_width--;    
        rate = FADE_RATE; 
                          
      }     

   
    } 
   
  } 
  int main(void) {
  char b;       
   TRISB  = 0b00000000;  
  while(1)
  {     
   for(b=0x02; b; b<<=1)
  {  
   PORTB = b;  
   fade(b>>1); 
      }
  for(b=0x40; b; b>>=1)
  { 
  PORTB = b;  
  fade(b<<1); 
      }
    } 
   
  }
 
Last edited by a moderator:

THE_RB

Joined Feb 11, 2008
5,438
To make a light chaser with fadeout you need to be able to individually fade each LED. So that means a system to give individual brightness control of all 12 LEDs.

Once you have that correctly set up the "chaser" part will slot in pretty easily. :)
 

THE_RB

Joined Feb 11, 2008
5,438
I don't like anything about your code, and even if it fades one LED that will be of no use to you when you need to later have individual fade ability of 12 LEDs.

You first need to decide a system for generating multiple fade PWMs. Even just decide on a block diagram would be a big help.

Here is a rough outline of one way to do it;
Rich (BB code):
(TMR0 interrupt, 20MHz PIC, prescale 2:1, int is 9765 Hz)
{
  pwm++;
  if(pwm >= 100) 
  {
    pwm = 0;
    light all LEDs.
  }
  if(pwm >= led1pwm) led1 off.
  if(pwm >= led2pwm) led2 off.
  if(pwm >= led3pwm) led3 off (etc).
}
That system of manual PWM in an interrupt will work fine, it has worked well for me in a number of projects.

After that, all you need to do is set the PWM value for each LED, like this;
led1pwm = 50
which will set LED 1 to 50% brightness.
 

Thread Starter

jayanath

Joined Dec 11, 2010
17
Dear sir im not a good brain for PIC Programming so if u have time i please request u to initiate routine c code for 12 led chaser with fading effect for 16f628a.Thank you for replying me.
 

Thread Starter

jayanath

Joined Dec 11, 2010
17
Dear i made following code but still having problem with pattern. im trying and trying but i cant made my fading led chaser. ooops

Rich (BB code):
#include <pic.h>
#define FADE_RATE 12
#define INITIAL_WIDTH 128
void fade()
{
unsigned char rate = FADE_RATE;
unsigned char pulse_width = INITIAL_WIDTH;
unsigned char count;
while(pulse_width){
PORTB= 0b10000000;
for(count=0; count<pulse_width; count++){
asm("NOP");
}
PORTB=0b00000000;
for(; count; count++){
asm("NOP");
}
PORTB= 0b01000000;
for(count=0; count<pulse_width; count++){
asm("NOP");
}
PORTB=0b00000000;
for(; count; count++){
asm("NOP");
}
PORTB= 0b00100000;
for(count=0; count<pulse_width; count++){
asm("NOP");
}
PORTB=0b00000000;
for(; count; count++){
asm("NOP");
}
PORTB= 0b00010000;
for(count=0; count<pulse_width; count++){
asm("NOP");
}
PORTB=0b00000000;
for(; count; count++){
asm("NOP");
}
PORTB= 0b00001000;
for(count=0; count<pulse_width; count++){
asm("NOP");
}
PORTB=0b00000000;
for(; count; count++){
asm("NOP");
}
PORTB= 0b00000100;
for(count=0; count<pulse_width; count++){
asm("NOP");
}
PORTB=0b00000000;
for(; count; count++){
asm("NOP");
}
PORTB= 0b0000010;
for(count=0; count<pulse_width; count++){
asm("NOP");
}
PORTB=0b00000000;
for(; count; count++){
asm("NOP");
}
PORTB= 0b00000001;
for(count=0; count<pulse_width; count++){
asm("NOP");
}
PORTB=0b00000000;
for(; count; count++){
asm("NOP");
}
PORTB= 0b00000010;
for(count=0; count<pulse_width; count++){
asm("NOP");
}
PORTB=0b00000000;
for(; count; count++){
asm("NOP");
}
PORTB= 0b00000100;
for(count=0; count<pulse_width; count++){
asm("NOP");
}
PORTB=0b00000000;
for(; count; count++){
asm("NOP");
}
PORTB= 0b00001000;
for(count=0; count<pulse_width; count++){
asm("NOP");
}
PORTB=0b00000000;
for(; count; count++){
asm("NOP");
}
PORTB= 0b00010000;
for(count=0; count<pulse_width; count++){
asm("NOP");
}
PORTB=0b00000000;
for(; count; count++){
asm("NOP");
}
PORTB= 0b00100000;
for(count=0; count<pulse_width; count++){
asm("NOP");
}
PORTB=0b00000000;
for(; count; count++){
asm("NOP");
}
PORTB= 0b01000000;
for(count=0; count<pulse_width; count++){
asm("NOP");
}
PORTB=0b00000000;
for(; count; count++){
asm("NOP");
}
PORTB= 0b10000000;
for(count=0; count<pulse_width; count++){
asm("NOP");
}
PORTB=0b00000000;
for(; count; count++){
asm("NOP");
}
if(!rate--){
pulse_width--;
rate = FADE_RATE;
}
}
}
int main(void) {
char b;
TRISB = 0b00000000;
while(1){
for(b=0x02; b; b<<=1){
PORTB = 0b00000001;
fade(b>>1);
PORTB = 0b00000010;
fade(b>>1);
PORTB = 0b00000100;
fade(b>>1);
PORTB = 0b00001000;
fade(b>>1);
PORTB = 0b00010000;
fade(b>>1);
PORTB = 0b00100000;
fade(b>>1);
PORTB = 0b01000000;
fade(b>>1);
PORTB = 0b10000000;
fade(b<<1);
PORTB = 0b01000000;
fade(b<<1);
PORTB = 0b00100000;
fade(b<<1);
PORTB = 0b00010000;
fade(b<<1);
PORTB = 0b00001000;
fade(b<<1);
PORTB = 0b00000100;
fade(b<<1);
PORTB = 0b00000010;
fade(b<<1);
PORTB = 0b00000001;
fade(b<<1);
}
for(b=0x40; b; b>>=1){
PORTB = b;
fade(b<<1);
}
}
}
 
Last edited by a moderator:

THE_RB

Joined Feb 11, 2008
5,438
Dear sir im not a good brain for PIC Programming so if u have time i please request u to initiate routine c code for 12 led chaser with fading effect for 16f628a.Thank you for replying me.
I understand that you would like it if I wrote all the code for you. :)

But sorry, I am not going to do that. I don't have the time or the desire, and you won't learn much from that! That means next time you want to do something you will be asking someone to write your code because you didn't learn much.

I do empathise for you that this is a difficult project and you want help. But you need to work with the suggestions that people have provided and do your own work too.

You need to make a simple system to individually fade 12 LEDs. I helped with that. Once you have that working, you need a sequencing system to "chase" the 12 LEDs. I'm sure people will be happy to help you with suggestions if you put in some work yourself.

If the whole thing is too difficult for you (which is OK!) then I suggest you start with a simpler project and increase your skills. Maybe just do an 8 LED chaser, with no fading?
 

Thread Starter

jayanath

Joined Dec 11, 2010
17
Thank you sir, i can do 8 led chaser without fading. but i want to make 12 led chaser with fading effect. plese go through my above code (posted as#6) and request correct it for fading pattern. im trying and trying but still not done. plese correct my above code if u dont have time to write another code for me.. thank you
 

THE_RB

Joined Feb 11, 2008
5,438
We're not angry. :)

This is the way help works;
1. We suggest things to try.
2. You try those things, and then tell us what happened.
3. (repeat until fixed).
 

spinnaker

Joined Oct 29, 2009
7,830
We're not angry. :)

This is the way help works;
1. We suggest things to try.
2. You try those things, and then tell us what happened.
3. (repeat until fixed).
I would like to add.

4. Understand the concept of what you are being suggested. If you don't understand that then trying to figure out what went wrong will be near impossible.

5. Learn to troubleshoot your own code. Microchip invested time into developing a debugger to help you figure out what went wrong. . Learn how to use it.


6. Learn how to use tools like a scope, logic analyzer and logic probe.

The RB is very talented but he just gave you an example. Plus you may have made a mistake in entering the sample.

Everyone makes mistakes (except me of course :) ) . If you can't understand or figure out where you went wrong then you are not going to get very far in this hobby.
 

takao21203

Joined Apr 28, 2012
3,702
what about to fade just one LED?

and write a seperate program for LED chaser.

some years ago I wrote fairly large programs in assembler. Sometimes I was stuck for hours. There is no way around investing some hard work.

Also breaking up into seperate programs can help.

One problem at a time.

Don't expect anyone to write code for you.
these requests are annoying.

If you want this, hire a programmer + pay hourly rates.
You can get cheap from India 5 to 10 dollars, from the US, few will do anything for less than $30. Engineers will charge $80 or $100 for each hour.

What people on the forum can do is to put you in the right direction or point out mistakes in your code.
 
Top