View previous topic :: View next topic |
Author |
Message |
artohautala
Joined: 17 Nov 2011 Posts: 187
|
how to PWM control for PIC16F88 ? |
Posted: Thu Nov 08, 2018 7:58 am |
|
|
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
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19546
|
|
Posted: Thu Nov 08, 2018 8:27 am |
|
|
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
|
|
Posted: Thu Nov 08, 2018 8:42 am |
|
|
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)
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19546
|
|
Posted: Thu Nov 08, 2018 9:01 am |
|
|
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
|
|
Posted: Thu Nov 08, 2018 9:27 am |
|
|
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 |
|
|
|