PIC16F1509 - PWM and LED fade

General discussion on mikroC.
Post Reply
Author
Message
.:Mik:.
Posts: 5
Joined: 17 Jun 2013 10:28

PIC16F1509 - PWM and LED fade

#1 Post by .:Mik:. » 15 Oct 2014 00:15

Hi everybody!
I'm trying making a led pulse on and off smoothly with PWM output of a PIC16F1509.
I write this code in mikroC:

Code: Select all

unsigned int d;

void InitMain() {
  ANSELA = 0;                        
  ANSELB = 0;
  ANSELC = 0;

  C1ON_bit = 0;                     
  C2ON_bit = 0;

  PORTB = 0;                          
  TRISA = 0x00;
  TRISB = 0b00100000;                
  PORTC = 0;                          
  TRISC = 0;                          
  PWM1_Init(500);                    
  
  Delay_ms(100);
}

void main() {
  InitMain();

  PWM1_Start();


  while (1) {

  for(d=0;d<1024;d++){
     PWM1_Set_Duty(d);
     Delay_ms(10);
  }
  
  for(d=1024;d>0;d--){
     PWM1_Set_Duty(d);
     Delay_ms(10);
  }

  }

}
  • I noticed that when the d index is equal to 0 or 1024, the PWM signal is kinda mirrored vertically for a while...why does it happen?
  • Anyway when I set PWM duty cycle to 0, the led never turn off at all and I cannot understand the reason of it. Any ideas?
  • How to understand exactly what value insert in PWM1_Init() function (the PIC is working @0.5MHz)?
Thanks in advance.

Muphy
Posts: 318
Joined: 24 Feb 2008 14:05
Location: Stonehaven Scotland

Re: PIC16F1509 - PWM and LED fade

#2 Post by Muphy » 15 Oct 2014 12:32

In both of your for loops, d never get's to be 1024 or 0 because of the test you are employing. Perhaps you should be doing something like this if you want 0 and 1024 to be values that are applied to your PWM:

Code: Select all

for(d=0;d<=1024;d++){
and

Code: Select all

for(d=1024;d>=0;d--){
Does that make sense??

Good luck

M

.:Mik:.
Posts: 5
Joined: 17 Jun 2013 10:28

Re: PIC16F1509 - PWM and LED fade

#3 Post by .:Mik:. » 15 Oct 2014 18:59

Actually, writing

Code: Select all

for(d=0;d<1024;d++){
and

Code: Select all

for(d=1024;d>0;d--){
you set 0 and 1024 values to d in the first loop in the two for.

Anyway I solved it by myself: now I'm setting directly the PWM registers (and the PR2) without using PWM mikroC's library and it works perfectly!
Maybe there is a bug for the 10-bit PWM management in mikroC PWM library or maybe it is no supported at all.

Thanks anyway

Muphy
Posts: 318
Joined: 24 Feb 2008 14:05
Location: Stonehaven Scotland

Re: PIC16F1509 - PWM and LED fade

#4 Post by Muphy » 15 Oct 2014 21:16

Not sure you are correct but if it works for you with what you have done then good stuff.

Another happy camper :-)

M

Post Reply

Return to “mikroC General”