PWM Freq PIC32MZ2048EFG144

General discussion on mikroBasic PRO for PIC32.
Post Reply
Author
Message
ahmed.hasib
Posts: 71
Joined: 11 Jul 2015 17:59

PWM Freq PIC32MZ2048EFG144

#1 Post by ahmed.hasib » 21 Apr 2020 21:28

Hi,
i use this example to use PWM, every thing is ok but when i use low freq (1 to 20 Hz) the o/p is not correct.
any help to generate low freq PWM?

Code: Select all

program Pwm
dim current_duty1, old_duty1,pwm_period1 as word

sub procedure InitMain()
  ANSELB = 0
  ANSELD = 0
  TRISB = 0xFFFF                             	' configure PORTB pins as input
  PORTD = 0                                  	' set PORTD to 0
  TRISD = 0                                  	' designate PORTD pins as output
end sub

main:
  JTAGEN_bit = 0
  InitMain()
  current_duty1 = 100     	                   ' initial value for current_duty
  PPS_Mapping(_RPD0, _OUTPUT, _OC1)
  pwm_period1 = PWM_Init(20000 , 1, 1, 2)
  PWM_Start(1)
  PWM_Set_Duty(current_duty1,  1)            ' set current duty for PWM1

  while (TRUE)   	          		     ' endless loop
    if RB12_bit = 0 then                      	     ' button on RB0 pressed
      Delay_ms(1)
      current_duty1 = current_duty1 + 5        ' increment current_duty
      if (current_duty1 > pwm_period1) then  ' if we increase current_duty greater then possible pwm_period1 value
        current_duty1 = 0                    	       ' reset current_duty value to zero
      end if
      PWM_Set_Duty(current_duty1,  1)        ' set newly acquired duty ratio
    end if
  wend
end.

User avatar
stefan.filipovic
mikroElektronika team
Posts: 1135
Joined: 18 Dec 2018 10:30

Re: PWM Freq PIC32MZ2048EFG144

#2 Post by stefan.filipovic » 29 Apr 2020 13:41

Hi Ahmed,

Have you checked if it's possible to set such a low frequency of PWM for your MCU clock frequency?
Here you can find the formula for calculating PWM period:
http://ww1.microchip.com/downloads/en/D ... 61111E.pdf

Kind regards,
Stefan Filipović

Post Reply

Return to “mikroBasic PRO for PIC32 General”