Advertisement
Electgpl

PIC - Fade LED RGB

Jun 19th, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.20 KB | None | 0 0
  1. #include <16F628A.h>
  2. #FUSES NOWDT, HS, NOMCLR
  3. #use delay(clock=4000000)
  4. #int_timer0
  5. void pwm_duty(int pwm0, int pwm1, int pwm2){
  6.    int cont;
  7.    if(cont>15)
  8.       cont=0;
  9.    else
  10.       cont++;
  11.    if(cont>0&&cont<pwm0)
  12.       output_high(PIN_B0);
  13.    else
  14.       output_low(PIN_B0);
  15.    if(cont>0&&cont<pwm1)
  16.       output_high(PIN_B1);
  17.    else
  18.       output_low(PIN_B1);
  19.    if(cont>0&&cont<pwm2)
  20.       output_high(PIN_B2);
  21.    else
  22.       output_low(PIN_B2);
  23.    set_timer0(0);      
  24. }
  25. void main(){
  26.    setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
  27.    set_timer0(0);
  28.    enable_interrupts(INT_TIMER0);
  29.    enable_interrupts(GLOBAL);
  30.    while(true){
  31.       int r, g, b;
  32.       for(b=0;b<16;b++){
  33.          pwm_duty(16,0,b);
  34.          delay_ms(50);
  35.       }
  36.       for(r=16;r>1;r--){
  37.          pwm_duty(r,0,16);
  38.          delay_ms(50);
  39.       }
  40.       for(g=0;g<16;g++){
  41.          pwm_duty(0,g,16);
  42.          delay_ms(50);
  43.       }
  44.       for(b=16;b>1;b--){
  45.          pwm_duty(0,16,b);
  46.          delay_ms(50);
  47.       }      
  48.       for(r=0;r<16;r++){
  49.          pwm_duty(r,16,0);
  50.          delay_ms(50);
  51.       }
  52.       for(g=16;g>1;g--){
  53.          pwm_duty(16,g,0);
  54.          delay_ms(50);
  55.       }
  56.    }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement