PWM Lib. on OC0B with ATMEGA 88 or other

General discussion on mikroPascal PRO for AVR.
Post Reply
Author
Message
stefkpl
Posts: 26
Joined: 21 Jan 2009 15:57

PWM Lib. on OC0B with ATMEGA 88 or other

#1 Post by stefkpl » 11 Sep 2013 16:40

Hi,

How can I use only OC0B output with my atmega88?

The lib. work fine with 0C0A but :evil: and I don't found doc or sample...

Regards

Stéf.

User avatar
marina.petrovic
Posts: 2986
Joined: 18 Apr 2013 08:11

Re: PWM Lib. on OC0B with ATMEGA 88 or other

#2 Post by marina.petrovic » 12 Sep 2013 12:53

Hi,

Please, take a look at ATmega88 Datasheet:
http://www.atmel.com/Images/doc2545.pdf

OC0B (Timer/Counter0 output compare match B output) - PD5 pin
OC0A (Timer/Counter0 output compare match A output) - PD6 pin

Also, you can take a look at PWM example from compiler:
...\mikroPascal PRO for AVR\Examples\Internal MCU modules\ATmega16\PWM

The example is written for ATmega16, but with the help of datasheet,
you can try to adjust the example for controller that you use.

Best regards,
Marina

stefkpl
Posts: 26
Joined: 21 Jan 2009 15:57

Re: PWM Lib. on OC0B with ATMEGA 88 or other

#3 Post by stefkpl » 13 Sep 2013 09:47

Thank you marina but where do you see other port in code?

Code: Select all

program PWM_Test;

var current_duty : byte;
    current_duty1 : byte;

  begin
    DDB0_bit := 0;                   // Set PORTB pin 0 as input
    DDB1_bit := 0;                   // Set PORTB pin 1 as input

    DDC0_bit := 0;                   // Set PORTC pin 0 as input
    DDC1_bit := 0;                   // Set PORTC pin 1 as input

    current_duty     := 16;         // initial value for current_duty
    current_duty1    := 16;         // initial value for current_duty

    DDB3_bit := 1;                    // Set PORTB pin 3 as output pin for the PWM (according to datasheet)
    DDD7_bit := 1;                    // Set PORTD pin 7 as output pin for the PWM1 (according to datasheet)

    PWM1_Init (_PWM1_FAST_MODE, _PWM1_PRESCALER_8, _PWM1_NON_INVERTED, 16);
    PWM2_Init(_PWM2_FAST_MODE, _PWM2_PRESCALER_8, _PWM2_NON_INVERTED, 16);

    while TRUE do                                 // Endless loop
      begin
        if (PINB0_bit <> 0) then
          begin                                   // Detect if PORTB pin 0 is pressed
            Delay_ms(40);                         // Small delay to avoid deboucing effect
            Inc(current_duty);                    // Increment duty ratio
            PWM1_Set_Duty(current_duty);           // Set incremented duty
          end
        else
          if (PINB1_bit <> 0) then                // Detect if PORTB pin 1 is pressed
            begin
              Delay_ms(40);                       // Small delay to avoid deboucing effect
              Dec(current_duty);                  // Decrement duty ratio
              PWM1_Set_Duty(current_duty);         // Set decremented duty ratio
            end
          else
            if (PINC0_bit <> 0) then              // Detect if PORTC pin 0 is pressed
              begin
                Delay_ms(40);                     // Small delay to avoid deboucing effect
                Inc(current_duty1);               // Increment duty ratio
                PWM2_Set_Duty(current_duty1);     // Set incremented duty
              end
            else
              if (PINC1_bit <> 0) then            // Detect if PORTC pin 1 is pressed
                begin
                  Delay_ms(40);                   // Small delay to avoid deboucing effect
                  Dec(current_duty1);             // Decrement duty ratio
                  PWM2_Set_Duty(current_duty1);   // Set decremented duty ratio
                end;

      end;

  end.
On my hardware, I have an LED on PD6 and transistor PD5.

PWM must be on PD5 and not PD6 for me...

Regard

(sorry for my english)

User avatar
marina.petrovic
Posts: 2986
Joined: 18 Apr 2013 08:11

Re: PWM Lib. on OC0B with ATMEGA 88 or other

#4 Post by marina.petrovic » 16 Sep 2013 15:36

Hi,

The project/code in our compiler on which I referred you to is written for ATmega16.
On ATmega16, OC0 is on PB3 pin and OC1 is on PD7 pin:

Code: Select all

 DDB3_bit := 1;                    // Set PORTB pin 3 as output pin for the PWM (according to datasheet)
 DDD7_bit := 1;                    // Set PORTD pin 7 as output pin for the PWM1 (according to datasheet)
The example changes PWM duty ratio on pins PB3 and PD7 continually.

So you need to take a look at datasheet of your device to see which pins you can use for PWM.

Best regards,
Marina

stefkpl
Posts: 26
Joined: 21 Jan 2009 15:57

Re: PWM Lib. on OC0B with ATMEGA 88 or other

#5 Post by stefkpl » 17 Sep 2013 16:02

marina.petrovic wrote: OC0B (Timer/Counter0 output compare match B output) - PD5 pin
OC0A (Timer/Counter0 output compare match A output) - PD6 pin
For ATMEGA88 it's
OC0A (Timer/Counter0 output compare match A output) - PD5 pin
OC0B (Timer/Counter0 output compare match B output) - PD6 pin

marina.petrovic wrote: The project/code in our compiler on which I referred you to is written for ATmega16.
On ATmega16, OC0 is on PB3 pin and OC1 is on PD7 pin:
Code: Select all
DDB3_bit := 1; // Set PORTB pin 3 as output pin for the PWM (according to datasheet)
DDD7_bit := 1; // Set PORTD pin 7 as output pin for the PWM1 (according to datasheet)


The example changes PWM duty ratio on pins PB3 and PD7 continually.
It's ok for OC0A and don't work for OC0B

I try this with an easyAVR5 board and ATMEGA164PA :

Code: Select all

    current_duty     := 16;         // initial value for current_duty
    current_duty1    := 16;         // initial value for current_duty

    DDB4_bit := 1;                    // Set PORTB pin 4 as output pin for the PWM0 (according to datasheet) (OC0B)
    DDD6_bit := 1;                    // Set PORTD pin 6 as output pin for the PWM1 (according to datasheet) (OC1B)

    PWM1_Init (_PWM1_FAST_MODE, _PWM1_PRESCALER_8, _PWM1_NON_INVERTED, 16);
    PWM2_Init(_PWM2_FAST_MODE, _PWM2_PRESCALER_8, _PWM2_NON_INVERTED, 16);
My subject is : "PWM Lib. on OC0B" ... and not on OC0 or OC1 etc

Regards

User avatar
marina.petrovic
Posts: 2986
Joined: 18 Apr 2013 08:11

Re: PWM Lib. on OC0B with ATMEGA 88 or other

#6 Post by marina.petrovic » 19 Sep 2013 13:50

Hi,

Like I already mention, the project in our compiler on which I referred you to is written for ATmega16
I referred to the project as an example that might help.

Please, can you try to use PB3 pin for OC0B on your ATMEGA164PA that you mention that you have?

Best regards,
Marina

stefkpl
Posts: 26
Joined: 21 Jan 2009 15:57

Re: PWM Lib. on OC0B with ATMEGA 88 or other

#7 Post by stefkpl » 20 Sep 2013 10:57

marina.petrovic wrote:Please, can you try to use PB3 pin for OC0B on your ATMEGA164PA that you mention that you have?
PB3 is OC0A and PB4 is OC0B

I already tried with PB4.

Regard

Post Reply

Return to “mikroPascal PRO for AVR General”