PWM LED Flasher

Discussion in 'Embedded Systems and Microcontrollers' started by lloydi12345, Dec 23, 2010.

  1. lloydi12345

    Thread Starter Member

    Aug 15, 2010
    103
    0
    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?

    Code ( (Unknown Language)):
    1. void InitMain() {
    2.   Adcon1 = 6;
    3.   cmcon = 7;
    4.  
    5.   TRISD = 0;                          // designate PORTD pins as output
    6.   PWM1_Init(1500);                    // Initialize PWM1 module at 5KHz
    7. }
    8.  
    9. void main() {
    10.   InitMain();
    11.  
    12.   PWM1_Start();                       // start PWM1
    13.  
    14.   while (1) {                         // endless loop
    15.  
    16.       PORTD = 0xFF;
    17.       Delay_ms(1000);
    18.       PWM1_Set_Duty(85);    //duty cycle = 85%
    19.  
    20.       PORTD = 0xFF;
    21.       Delay_ms(1000);
    22.       PWM1_Set_Duty(40);    //duty cycle = 40%
    23.  
    24.  
    25.       PORTD = 0xFF;
    26.       Delay_ms(1000);
    27.       PWM1_Set_Duty(5);     //duty cycle = 5%
    28.      }
    29.  
    30. }
    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. :(
     
  2. lloydi12345

    Thread Starter Member

    Aug 15, 2010
    103
    0
    help please?
     
  3. tyblu

    Member

    Nov 29, 2010
    199
    16
    Let's see "PWM1_Set_Duty" and "PWM1_Init".
     
  4. lloydi12345

    Thread Starter Member

    Aug 15, 2010
    103
    0
    Thanks tyblu for the reply, What do you mean?
     
  5. EsDorlion

    New Member

    Dec 24, 2010
    8
    0
    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.
     
  6. tyblu

    Member

    Nov 29, 2010
    199
    16
    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
     
  7. Tahmid

    Active Member

    Jul 2, 2008
    344
    25
    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
    Code ( (Unknown Language)):
    1. 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
    Code ( (Unknown Language)):
    1. PWM1_Init(5000);
    sets PWM for 5kHz, you set it for 1.5kHz.

    Hope this helps.
    Tahmid.
     
  8. lloydi12345

    Thread Starter Member

    Aug 15, 2010
    103
    0
    Thank you for your replies guys. I thought PWM on mikroC was the easiest. Now I know. :(
     
  9. Tahmid

    Active Member

    Jul 2, 2008
    344
    25
    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.
     
Loading...