CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

how to PWM control for PIC16F88 ?

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
artohautala



Joined: 17 Nov 2011
Posts: 187

View user's profile Send private message Send e-mail

how to PWM control for PIC16F88 ?
PostPosted: Thu Nov 08, 2018 7:58 am     Reply with quote

Hello everybody and good Thursday evening from Finland (timezone +2 GMT)

I'm making case for my 3D printer because I want to blow out to open air those toxid ABS filament gases...
and I want to install 21 super bright leds to case for light and I want to dim them with potentiometer connected to some AD-pin of PIC16F88...

so I can't get PWM control for LED bars to work ...

flashing LED bar works fine but not dimming ..

I know PIC16F88 is rather old but I found one from one of my junk boxes and
I have not lot of money so I think it will work ...
Here's my code

Code:


void main(){
 
   
         //PWM (10 bits)   pin B3 use timer 2
         
         //led BAR IS CONNECTED TO PIN b3
         
         set_tris_b(0b11100000);
         
         setup_timer_2(T2_DIV_BY_4, 127, 1);
         enable_interrupts(INT_TIMER2);
         Enable_interrupts(GLOBAL);         
   
   //delay_ms(2000);
 
  // setup_oscillator( OSC_INTRC ); 
 
  //set_tris_b(0B11000000);
 
 
    //output_high(SERVO);         //servo running when HIGH
    //delay_ms(5000);
   
    output_low(SERVO);
 
    setup_ccp1(CCP_PWM);   // Configure CCP1 as PWM
     
    set_pwm1_duty(800);     
     
     WAIT:
     
     
     //THIS WORKS FINE
     
     /*
     output_high(pin_B3);
     delay_ms(1000);
     output_low(pin_B3);
     delay_ms(1000);     
     
     */
     
     
     ;
     
     
     
     
     
     GOTO WAIT;
     


I'll be very happy if somebody want to help me

Smile
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Thu Nov 08, 2018 8:27 am     Reply with quote

Why are you enabling the timer2 interrupt.? This will be being called every 512 machine instructions, and unless you need an interrupt this fast, could cause problems...

The problem with your PWM, is the duty cycle.

The PWM duty cycle, can vary from 0 to (PR2+1)*4. You are loading PR2 with 124, so the maximum duty cycle, is 500. Loading with 800 it'll be full on...

You only have a 9bit PWM, since you are setting PR2 to half it's maximum value. So for 80% on, you'd need to set the duty to 400.
artohautala



Joined: 17 Nov 2011
Posts: 187

View user's profile Send private message Send e-mail

PostPosted: Thu Nov 08, 2018 8:42 am     Reply with quote

Ttelmah wrote:
Why are you enabling the timer2 interrupt.? This will be being called every 512 machine instructions, and unless you need an interrupt this fast, could cause problems...

The problem with your PWM, is the duty cycle.

The PWM duty cycle, can vary from 0 to (PR2+1)*4. You are loading PR2 with 124, so the maximum duty cycle, is 500. Loading with 800 it'll be full on...
You only have a 9bit PWM, since you are setting PR2 to half it's maximum value. So for 80% on, you'd need to set the duty to 400.


Thank you for your good advice and fast answer.

I tested again and comment off those interrupts and I changed duty cycle to
400 but still it not work ... ??

Is it OK to use pin B3 for PWM control ??


... so how I can fix the problem ?
my clock frequency is: #use delay(clock = 11059200)
Razz
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Thu Nov 08, 2018 9:01 am     Reply with quote

Depends on how you have the fuses set.

Fuse CCPB3 or CCPB0 determines which pin it goes to.
artohautala



Joined: 17 Nov 2011
Posts: 187

View user's profile Send private message Send e-mail

PostPosted: Thu Nov 08, 2018 9:27 am     Reply with quote

Ttelmah wrote:
Depends on how you have the fuses set.

Fuse CCPB3 or CCPB0 determines which pin it goes to.

Thank you ! That fuse setting CCPB3 helped to make things run in pin B3.

Lot of good life for you and have fun Smile Smile
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group