12 led chaser with fading effect PIC16F628A

Discussion in 'Programmer's Corner' started by jayanath, Dec 29, 2013.

  1. jayanath

    Thread Starter New Member

    Dec 11, 2010
    17
    0
    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?

    Code ( (Unknown Language)):
    1.  
    2. #include <pic.h>  :mad:
    3.   #define FADE_RATE 12
    4.   #define INITIAL_WIDTH 128
    5.   void fade()
    6.   {
    7.     unsigned char rate = FADE_RATE;
    8.     unsigned char pulse_width = INITIAL_WIDTH;
    9.     unsigned char count;
    10.   while(pulse_width)
    11.   {                      
    12.       PORTB= 0b11111111;  
    13.       for(count=0; count<pulse_width; count++)
    14.   {  
    15.   asm("NOP");
    16.       }
    17.   PORTB=0b00000000;      
    18.    for(; count; count++)
    19.   {
    20.      asm("NOP");      
    21.       }
    22.    
    23.       if(!rate--){        
    24.         pulse_width--;    
    25.         rate = FADE_RATE;
    26.                          
    27.       }    
    28.  
    29.    
    30.     }
    31.    
    32.   }
    33.   int main(void) {
    34.   char b;      
    35.    TRISB  = 0b00000000;  
    36.   while(1)
    37.   {    
    38.    for(b=0x02; b; b<<=1)
    39.   {  
    40.    PORTB = b;  
    41.    fade(b>>1);
    42.       }
    43.   for(b=0x40; b; b>>=1)
    44.   {
    45.   PORTB = b;  
    46.   fade(b<<1);
    47.       }
    48.     }
    49.    
    50.   }
    51.  
     
    Last edited by a moderator: Jan 1, 2014
  2. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    5,435
    1,305
    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. :)
     
  3. jayanath

    Thread Starter New Member

    Dec 11, 2010
    17
    0
    Dear THE RB If u have time please I request u to make first led to fade (correct my c code example).
     
  4. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    5,435
    1,305
    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;
    Code ( (Unknown Language)):
    1.  
    2. (TMR0 interrupt, 20MHz PIC, prescale 2:1, int is 9765 Hz)
    3. {
    4.   pwm++;
    5.   if(pwm >= 100)
    6.   {
    7.     pwm = 0;
    8.     light all LEDs.
    9.   }
    10.   if(pwm >= led1pwm) led1 off.
    11.   if(pwm >= led2pwm) led2 off.
    12.   if(pwm >= led3pwm) led3 off (etc).
    13. }
    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.
     
  5. jayanath

    Thread Starter New Member

    Dec 11, 2010
    17
    0
    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.
     
  6. R!f@@

    AAC Fanatic!

    Apr 2, 2009
    8,754
    760
    RB did what u are asking again I believe.

    I think you are asking to complete coding
     
  7. jayanath

    Thread Starter New Member

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

    Code ( (Unknown Language)):
    1.  
    2. #include <pic.h>
    3. #define FADE_RATE 12
    4. #define INITIAL_WIDTH 128
    5. void fade()
    6. {
    7. unsigned char rate = FADE_RATE;
    8. unsigned char pulse_width = INITIAL_WIDTH;
    9. unsigned char count;
    10. while(pulse_width){
    11. PORTB= 0b10000000;
    12. for(count=0; count<pulse_width; count++){
    13. asm("NOP");
    14. }
    15. PORTB=0b00000000;
    16. for(; count; count++){
    17. asm("NOP");
    18. }
    19. PORTB= 0b01000000;
    20. for(count=0; count<pulse_width; count++){
    21. asm("NOP");
    22. }
    23. PORTB=0b00000000;
    24. for(; count; count++){
    25. asm("NOP");
    26. }
    27. PORTB= 0b00100000;
    28. for(count=0; count<pulse_width; count++){
    29. asm("NOP");
    30. }
    31. PORTB=0b00000000;
    32. for(; count; count++){
    33. asm("NOP");
    34. }
    35. PORTB= 0b00010000;
    36. for(count=0; count<pulse_width; count++){
    37. asm("NOP");
    38. }
    39. PORTB=0b00000000;
    40. for(; count; count++){
    41. asm("NOP");
    42. }
    43. PORTB= 0b00001000;
    44. for(count=0; count<pulse_width; count++){
    45. asm("NOP");
    46. }
    47. PORTB=0b00000000;
    48. for(; count; count++){
    49. asm("NOP");
    50. }
    51. PORTB= 0b00000100;
    52. for(count=0; count<pulse_width; count++){
    53. asm("NOP");
    54. }
    55. PORTB=0b00000000;
    56. for(; count; count++){
    57. asm("NOP");
    58. }
    59. PORTB= 0b0000010;
    60. for(count=0; count<pulse_width; count++){
    61. asm("NOP");
    62. }
    63. PORTB=0b00000000;
    64. for(; count; count++){
    65. asm("NOP");
    66. }
    67. PORTB= 0b00000001;
    68. for(count=0; count<pulse_width; count++){
    69. asm("NOP");
    70. }
    71. PORTB=0b00000000;
    72. for(; count; count++){
    73. asm("NOP");
    74. }
    75. PORTB= 0b00000010;
    76. for(count=0; count<pulse_width; count++){
    77. asm("NOP");
    78. }
    79. PORTB=0b00000000;
    80. for(; count; count++){
    81. asm("NOP");
    82. }
    83. PORTB= 0b00000100;
    84. for(count=0; count<pulse_width; count++){
    85. asm("NOP");
    86. }
    87. PORTB=0b00000000;
    88. for(; count; count++){
    89. asm("NOP");
    90. }
    91. PORTB= 0b00001000;
    92. for(count=0; count<pulse_width; count++){
    93. asm("NOP");
    94. }
    95. PORTB=0b00000000;
    96. for(; count; count++){
    97. asm("NOP");
    98. }
    99. PORTB= 0b00010000;
    100. for(count=0; count<pulse_width; count++){
    101. asm("NOP");
    102. }
    103. PORTB=0b00000000;
    104. for(; count; count++){
    105. asm("NOP");
    106. }
    107. PORTB= 0b00100000;
    108. for(count=0; count<pulse_width; count++){
    109. asm("NOP");
    110. }
    111. PORTB=0b00000000;
    112. for(; count; count++){
    113. asm("NOP");
    114. }
    115. PORTB= 0b01000000;
    116. for(count=0; count<pulse_width; count++){
    117. asm("NOP");
    118. }
    119. PORTB=0b00000000;
    120. for(; count; count++){
    121. asm("NOP");
    122. }
    123. PORTB= 0b10000000;
    124. for(count=0; count<pulse_width; count++){
    125. asm("NOP");
    126. }
    127. PORTB=0b00000000;
    128. for(; count; count++){
    129. asm("NOP");
    130. }
    131. if(!rate--){
    132. pulse_width--;
    133. rate = FADE_RATE;
    134. }
    135. }
    136. }
    137. int main(void) {
    138. char b;
    139. TRISB = 0b00000000;
    140. while(1){
    141. for(b=0x02; b; b<<=1){
    142. PORTB = 0b00000001;
    143. fade(b>>1);
    144. PORTB = 0b00000010;
    145. fade(b>>1);
    146. PORTB = 0b00000100;
    147. fade(b>>1);
    148. PORTB = 0b00001000;
    149. fade(b>>1);
    150. PORTB = 0b00010000;
    151. fade(b>>1);
    152. PORTB = 0b00100000;
    153. fade(b>>1);
    154. PORTB = 0b01000000;
    155. fade(b>>1);
    156. PORTB = 0b10000000;
    157. fade(b<<1);
    158. PORTB = 0b01000000;
    159. fade(b<<1);
    160. PORTB = 0b00100000;
    161. fade(b<<1);
    162. PORTB = 0b00010000;
    163. fade(b<<1);
    164. PORTB = 0b00001000;
    165. fade(b<<1);
    166. PORTB = 0b00000100;
    167. fade(b<<1);
    168. PORTB = 0b00000010;
    169. fade(b<<1);
    170. PORTB = 0b00000001;
    171. fade(b<<1);
    172. }
    173. for(b=0x40; b; b>>=1){
    174. PORTB = b;
    175. fade(b<<1);
    176. }
    177. }
    178. }
    179.  
     
    Last edited by a moderator: Jan 1, 2014
  8. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    5,435
    1,305
    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?
     
  9. jayanath

    Thread Starter New Member

    Dec 11, 2010
    17
    0
    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
     
  10. blueroomelectronics

    AAC Fanatic!

    Jul 22, 2007
    1,758
    98
    No thanks.
     
  11. jayanath

    Thread Starter New Member

    Dec 11, 2010
    17
    0
    its ok..................
     
  12. jayanath

    Thread Starter New Member

    Dec 11, 2010
    17
    0
    are u angry with me? im still fail
     
  13. R!f@@

    AAC Fanatic!

    Apr 2, 2009
    8,754
    760
    No body is angry.

    Why don't u try a sample code tht can fade just one LED first.
     
  14. jayanath

    Thread Starter New Member

    Dec 11, 2010
    17
    0
    are u angry with me?
     
  15. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    5,435
    1,305
    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).
     
  16. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,012
    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.
     
  17. takao21203

    Distinguished Member

    Apr 28, 2012
    3,577
    463
    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.
     
Loading...