herbas.blogg.se

Mplab xc8 how to change pwm frequency in code
Mplab xc8 how to change pwm frequency in code












mplab xc8 how to change pwm frequency in code
  1. Mplab xc8 how to change pwm frequency in code generator#
  2. Mplab xc8 how to change pwm frequency in code code#

PERIOD = (unsigned int)((XTAL_FOSC/(Freq*4*PRESCALE))-1) Unsigned int PERIOD=0,PRESCALE = ,i=0, ERROR=0 Parameter: Frequency in Khz e.g., 100Hz->0.1

mplab xc8 how to change pwm frequency in code

Var = PWM_Init(20) //In KHz 20=20KHz, 0.62=620Hzĭescription: Automatic initialisation of Period and timer prescale Unsigned char PWM_Init(const float Freq)

Mplab xc8 how to change pwm frequency in code generator#

PWM Generator 16F877a from 62Hz to 2.5Mhz There is separate PWM stop function for PWM1 and 2.Ĭonfiguring the two lsb bits of duty cycle on CCPxCON registers is practically not necessary, so you can remove those lines

Mplab xc8 how to change pwm frequency in code code#

This code is written in Hitech PICC compiler. If the selected frequency is out of range, the bit RD0 is driven high to indicate the error Then select the PWM frequency between min and max value for a particular XTAL frequency. Value W7 PWM Paramaters PWM Polarity activehi PWM Period 19.9EB mis PWM Frequency 50.08 Hz PWM Resolution. Change the configuration bits (0x3F71 or 0x3F72) according to the crystal frequency. To change the frequency, First configure the XTAL_FOSC to suit your crystal. You may experience a deviation from calculated value.

Configure the CCP pins as Output by writing to TRIS<2> and TRIS<1> bits.

The relation between register value and duty cycle is shown below.

Then set the PWM Duty Cycle by writing to CCPR1L and CCP1CON<5:4> registers.

The relation between PR2 value and time period is shown below. The Max and Min frequency is calculated in theory. Set the PWM Period by writing to PR2 register. So the code is modified to accept the parameter in percentage and automatically calculates the TIMER2 value. The common way to specify the duty cycle is percentage. This code is very simple and powerful when we need a single or Dual PWM pulse with variable frequency.














Mplab xc8 how to change pwm frequency in code