PWM program from mikroC PRO for PIC Help

Post your requests and ideas for the mikroElektronika website & forums.
Author
Message
Ideacharge
Posts: 16
Joined: 26 Dec 2010 16:47

PWM program from mikroC PRO for PIC Help

#1 Post by Ideacharge » 20 Feb 2011 01:10

Hello, i am trying to use Your program example of PWM usage:

Code: Select all

unsigned short current_duty, old_duty, current_duty1, old_duty1;

void InitMain() {
  ANSEL  = 0;                         // Configure AN pins as digital
  ANSELH = 0;
  C1ON_bit = 0;                       // Disable comparators
  C2ON_bit = 0;
  
  PORTA = 255;
  TRISA = 255;                        // configure PORTA pins as input
  PORTB = 0;                          // set PORTB to 0
  TRISB = 0;                          // designate PORTB pins as output
  PORTC = 0;                          // set PORTC to 0
  TRISC = 0;                          // designate PORTC pins as output
  PWM1_Init(5000);                    // Initialize PWM1 module at 5KHz
  PWM2_Init(5000);                    // Initialize PWM2 module at 5KHz
}

void main() {
  InitMain();
  current_duty  = 16;                 // initial value for current_duty
  current_duty1 = 16;                 // initial value for current_duty1

  PWM1_Start();                       // start PWM1
  PWM2_Start();                       // start PWM2
  PWM1_Set_Duty(current_duty);        // Set current duty for PWM1
  PWM2_Set_Duty(current_duty1);       // Set current duty for PWM2

  while (1) {                         // endless loop
    if (RA0_bit) {                    // button on RA0 pressed
      Delay_ms(40);
      current_duty++;                 // increment current_duty
      PWM1_Set_Duty(current_duty);
     }

    if (RA1_bit) {                    // button on RA1 pressed
      Delay_ms(40);
      current_duty--;                 // decrement current_duty
      PWM1_Set_Duty(current_duty);
     }

    if (RA2_bit) {                    // button on RA2 pressed
      Delay_ms(40);
      current_duty1++;                // increment current_duty1
      PWM2_Set_Duty(current_duty1);
     }

    if (RA3_bit) {                    // button on RA3 pressed
      Delay_ms(40);
      current_duty1--;                // decrement current_duty1
      PWM2_Set_Duty(current_duty1);
     }

    Delay_ms(5);                      // slow down change pace a little
  }
}

I am sorry, but here is another mistake in this program... There is stated:
The example changes PWM duty ratio on RC1 and RC2 pins continually. If LED is connected to these pins, you can observe the gradual change of emitted light.
But as I think, there is nothing about PWM signal, which should be sent to RC1 and RC2 pins, so this program does not work (the LED's, connected to RC1 and RC2 pins does not light). Please, could you tell, how to correct this program? Thank you.

User avatar
filip
mikroElektronika team
Posts: 11874
Joined: 25 Jan 2008 09:56

Re: PWM program from mikroC PRO for PIC Help

#2 Post by filip » 22 Feb 2011 09:55

Hi,

Which MCU are you using ?
Did you pull-down the buttons RA0-RA3, used for PWM control ?

Regards,
Filip.

Ideacharge
Posts: 16
Joined: 26 Dec 2010 16:47

Re: PWM program from mikroC PRO for PIC Help

#3 Post by Ideacharge » 24 Feb 2011 13:16

Hello, I am using PIC16F887 and this schematic:
Attachments
untitled.PNG
untitled.PNG (45.02 KiB) Viewed 23547 times

Radu86
Posts: 47
Joined: 11 Jan 2010 17:10
Location: Romania, Iasi

Re: PWM program from mikroC PRO for PIC Help

#4 Post by Radu86 » 24 Feb 2011 17:05

The example works. I have played with it by lighting a led and by controlling a DC 3V motor RPM.
Are you sure 10K resistor is ok? Maybe the pin isn't pulled down. Try using a smaller resistor.

Ideacharge
Posts: 16
Joined: 26 Dec 2010 16:47

Re: PWM program from mikroC PRO for PIC Help

#5 Post by Ideacharge » 24 Feb 2011 18:00

I tried many other circuits with 10k resistor, and everything worked..

What means if (RA0_bit) ?
What logic state should go to PIC, when button is pressed?

There is no voltage on output pins at all... :|

Radu86
Posts: 47
Joined: 11 Jan 2010 17:10
Location: Romania, Iasi

Re: PWM program from mikroC PRO for PIC Help

#6 Post by Radu86 » 24 Feb 2011 18:52

if (ra_bit) = If(ra_bit==1)
When you press the button 5V must get to the pin of the PIC.

If I may, what software do you use to draw the schematics like the one you've posted? Thank you.

Ideacharge
Posts: 16
Joined: 26 Dec 2010 16:47

Re: PWM program from mikroC PRO for PIC Help

#7 Post by Ideacharge » 24 Feb 2011 18:55

What should do RA0-RA3 buttons? What should they change?

The program is Proteus 7 Professional, and it let to simulate a whole schematic, as it would be in reality :)

Radu86
Posts: 47
Joined: 11 Jan 2010 17:10
Location: Romania, Iasi

Re: PWM program from mikroC PRO for PIC Help

#8 Post by Radu86 » 24 Feb 2011 20:19

Ra0-Ra3 are used to increase or decrease the length of the pulses. They modify how long does the PIC keep the pin in High state and how long in keeps it in LOW state. It acts like a chopper.
Thanks for the Proteus. :)

Ideacharge
Posts: 16
Joined: 26 Dec 2010 16:47

Re: PWM program from mikroC PRO for PIC Help

#9 Post by Ideacharge » 24 Feb 2011 20:54

So why the program isn't working? :|

Radu86
Posts: 47
Joined: 11 Jan 2010 17:10
Location: Romania, Iasi

Re: PWM program from mikroC PRO for PIC Help

#10 Post by Radu86 » 24 Feb 2011 21:00

I really don't know. I have tried this same example and it worked for me. I used PIC18F452 and easyPIC 6. All I have changed was the configuration for PIC's I/O pins.
I have checked it with an oscilloscope to see how the signals change and everything is ok.

Ideacharge
Posts: 16
Joined: 26 Dec 2010 16:47

Re: PWM program from mikroC PRO for PIC Help

#11 Post by Ideacharge » 24 Feb 2011 21:06

How the program worked for you? What has been changing, when you each time pressed those buttons? Did the brightness increased very slow, or you need to press a button and wait for some time to increase brightness...?

Radu86
Posts: 47
Joined: 11 Jan 2010 17:10
Location: Romania, Iasi

Re: PWM program from mikroC PRO for PIC Help

#12 Post by Radu86 » 24 Feb 2011 21:33

You have to press repeatedly or keep a button pressed for a little while to notice the change. After I download the hex into the PIC, the RC1 and RC2 leds are lighting very dim. When I press a button it either gets dimmer or stronger. As I said, it is not instant. You have to press repeatedly or keep the button pressed to see the difference.
However, the results were much easier to see with the DC motor as it's rpm's increased or decreased immediately. If you want I can record a short movie to see how it works for me.

Ideacharge
Posts: 16
Joined: 26 Dec 2010 16:47

Re: PWM program from mikroC PRO for PIC Help

#13 Post by Ideacharge » 24 Feb 2011 21:41

OK, that would be great.

Radu86
Posts: 47
Joined: 11 Jan 2010 17:10
Location: Romania, Iasi

Re: PWM program from mikroC PRO for PIC Help

#14 Post by Radu86 » 24 Feb 2011 22:46

I have sent you a PM with a link towards the video.

Ideacharge
Posts: 16
Joined: 26 Dec 2010 16:47

Re: PWM program from mikroC PRO for PIC Help

#15 Post by Ideacharge » 24 Feb 2011 23:01

Shouldn't first go?

TRISA = 255;
and then
PORTA = 255;

Instead of:
PORTA = 255;
TRISA = 255;

Heyy, maybe it is PWM1_Init ? Maybe it should be changed? I am using 4MHz external quartz crystal oscillator (XT).

void PWM1_Init(const long freq);
Initializes the PWM module with duty ratio 0. Parameter freq is a desired PWM frequency in Hz (refer to device data sheet for correct values in respect with Fosc).

Post Reply

Return to “Website & Forums Wishlist”