PWM LED Flasher

Thread Starter

lloydi12345

Joined Aug 15, 2010
103
Hi I'm doing PWM on my little project this Christmas eve and I'm hoping to finish it earlier. My hardware is done and same as with the software but I would like to add some PWM effects for it to look good. Can you help me out?

I'm trying to make LEDs fade and thought of using mikroC PRO for PIC's PWM. I used it but it is not that visible. I'm using PIC16f877a and a crystal OSC of 20Mhz. Can you check the problem on my source code?

Rich (BB code):
void InitMain() {
  Adcon1 = 6;
  cmcon = 7;

  TRISD = 0;                          // designate PORTD pins as output
  PWM1_Init(1500);                    // Initialize PWM1 module at 5KHz
}

void main() {
  InitMain();

  PWM1_Start();                       // start PWM1

  while (1) {                         // endless loop
  
      PORTD = 0xFF;
      Delay_ms(1000);
      PWM1_Set_Duty(85);    //duty cycle = 85%

      PORTD = 0xFF;
      Delay_ms(1000);
      PWM1_Set_Duty(40);    //duty cycle = 40%


      PORTD = 0xFF;
      Delay_ms(1000);
      PWM1_Set_Duty(5);     //duty cycle = 5%
     }

}
On my schematic my PORTD was the output.
The problem is that even PortC.F2 was not set above as an output, it blinks (Is it because it is CCP1?) and it even shows a fade effect which was supposedly to be seen on PORTD because I used on them PWM1_Set_duty. LEDs on PORTD are just steady logic 1. They are not showing any fade effect. :(
 

EsDorlion

Joined Dec 24, 2010
8
genarally i use assembly language but when i check your codes; in my opinion it is not a pwm. and it is normal that "LEDs on PORTD are just steady logic 1". you have an endsless loop in pwm but i think you must change your duty cycle value with an algorithm.
 

tyblu

Joined Nov 29, 2010
199
Thanks tyblu for the reply, What do you mean?
Those functions must be implemented elsewhere in order to do something, like InitMain(). Would like to take a look at them so we know what is being called. If they're not in the main window (function declaration above main(), definition below) , then they're probably in a library -- do you have something similar to "#include "pwm.h" or "#include <pwm>"? If so, look inside of the linked library ("pwm.h", or whatever) to find those functions.
PWM1_Init
PWM1_Start
PWM1_Set_Duty
 

Tahmid

Joined Jul 2, 2008
343
tyblu,
Those are not necessary as I believe this is in mikroC, where these are functions and you don't need to include header files in mikroC.

loydi12345,
The code is fine. It doesn't work as you've shown. PWM happens only on CCP1 pin. When you initialize PWM1 through
Rich (BB code):
PWM1_Init(1500);
CCP1 is automatically set as output and whenever you set the duty cycle for fading effect it happens on CCP1 pin, not PORTD. When you specify PORTD, nothing really is happening, but just turning every single bit of PORTD high. You can only have PWM on the CCP pins. So if you need to have fading effect on PORTD, use the compare module and set PWM yourself or use software PWM.

I hope I could make myself clear.
PS
Rich (BB code):
PWM1_Init(5000);
sets PWM for 5kHz, you set it for 1.5kHz.

Hope this helps.
Tahmid.
 

Tahmid

Joined Jul 2, 2008
343
It is the easiest but you can't have it your way. mikroC uses existing peripherals and makes it easy for you to use them. So, you can't expect it to do something not provided by the microcontroller. There are ways to work around this as I've stated above.

Tahmid.
 
Top