Hot!How to configure and control PWM output as DC Voltmeter ?

Author
ndquoc
New Member
  • Total Posts : 3
  • Reward points : 0
  • Joined: 2017/01/16 21:02:19
  • Location: 0
  • Status: offline
2017/09/13 17:41:30 (permalink)
0

How to configure and control PWM output as DC Voltmeter ?

Hello all,
 
I am a PIC newuser. I am reading a code PWM of PIC18F66K80 of our company and known that they want to exchange PWM 10bits  to 0-5V in our design. I am testing this code and see that:
Call function: PWM2_LoadDutyValue(valPWM);
-When I put valPWM = 1023 --> Output : 5V
-But put valPWM = 205 (expected 1V) --> But output only 0.7V
-Put valPWM = 410(expected 2V) --> But output only 1.59V 
So I wonder how to control or configure PWM so that we can drive the input value and output is equal expected voltage as ratio:
_Put value valPWM = 205  --> Output 1V
_Put value 410  --> Output 2V
_Put value 615  --> Output 3V
_Put value 820  --> Output 4V
_Put value 1023 --> Output 5V
 
Would you please help us find a root cause of problem ?
Thanks in advance !
QUOC NGUYEN
PS: We use mplabc18 with compiler v3.47 and configure PWM2 10bits as below
 
// Set TMR2 to the options selected in the User Interface
  // T2CKPS 1:1; T2OUTPS 1:1; TMR2ON off;
  T2CON = 0x00;  
  // PR2 255;
  PR2 = 0xFF;
  // TMR2 0;
  TMR2 = 0x00;
  // Clearing IF flag before enabling the interrupt.
  PIR1bits.TMR2IF = 0;
  // Start the Timer by writing to TMRxON bit
  T2CONbits.TMR2ON = 1;


// Set the PWM to the options selected in the MPLAB(c) Code Configurator
  // CCP2M PWM; DC2B 0;
  CCP2CON = 0x0C;
  // CCPR2L 0;
  CCPR2L = 0x00;
  // CCPR2H 0;
  CCPR2H = 0x00;
  // Selecting Timer 2
  CCPTMRSbits.C2TSEL = 0x0;


//------------------------------------//
// PWM2_LoadDutyValue()          //
//------------------------------------//
void PWM2_LoadDutyValue(unsigned int dutyValue)
{
// Writing to 8 MSBs of pwm duty cycle in CCPRL register
CCPR2L = ((dutyValue & 0x03FC)>>2);

// Writing to 2 LSBs of pwm duty cycle in CCPCON register
CCP2CON = (CCP2CON & 0xCF) | ((dutyValue & 0x0003)<<4);
}
#1

9 Replies Related Threads

    qhb
    Superb Member
    • Total Posts : 5505
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: online
    Re: How to configure and control PWM output as DC Voltmeter ? 2017/09/13 19:26:32 (permalink)
    +3 (3)
    What components do you have connected to the PWM output pin?
    You need a low-pass filter to convert PWM output to a DC voltage. What do you have?
    i.e. please show a schematic of what circuit you are using.
     
    #2
    ndquoc
    New Member
    • Total Posts : 3
    • Reward points : 0
    • Joined: 2017/01/16 21:02:19
    • Location: 0
    • Status: offline
    Re: How to configure and control PWM output as DC Voltmeter ? 2017/09/13 20:13:09 (permalink)
    0
    Yes, we have it.
    But output is still unexpected !
    post edited by ndquoc - 2017/09/13 22:03:27

    Attached Image(s)

    #3
    katela
    Super Member
    • Total Posts : 454
    • Reward points : 0
    • Joined: 2013/06/11 05:25:18
    • Location: South Africa
    • Status: offline
    Re: How to configure and control PWM output as DC Voltmeter ? 2017/09/14 00:07:23 (permalink)
    +1 (1)
    The MCC function PWM2_LoadDutyValue() is expecting an integer value, for you to achieve the exact output level, this value is expected to be a decimal value that your function PWM2_LoadDutyValue() is not expected. Instead of 410 for 2V maybe a 409.2 value could work.
    You'll need a filter at the output.
     

    Free online Microcontroller Tutorials and Projects for Hobbyists and students. From beginners to advanced. Website: www.studentcompanion.co.za
    Youtube Tutorials: http://www.youtube.com/user/StudentCompanionSA/videos
    #4
    rpg7
    Super Member
    • Total Posts : 1326
    • Reward points : 0
    • Joined: 2003/11/07 12:47:35
    • Status: offline
    Re: How to configure and control PWM output as DC Voltmeter ? 2017/09/14 00:49:49 (permalink)
    0
    What is your supply voltage AND post a schematic of your PWM output
    #5
    qhb
    Superb Member
    • Total Posts : 5505
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: online
    Re: How to configure and control PWM output as DC Voltmeter ? 2017/09/14 01:04:30 (permalink)
    +2 (2)
    katela
    The MCC function PWM2_LoadDutyValue() is expecting an integer value, for you to achieve the exact output level, this value is expected to be a decimal value that your function PWM2_LoadDutyValue() is not expected. Instead of 410 for 2V maybe a 409.2 value could work.

    I think you're just confusing the issue.
    I think you're trying to say that you can only set discrete levels, so sometimes the ideal value won't be an exact integer.
    That's irrelevant to the OP's problem, which is that he's getting a figure 30% different to what he's expecting.
    I suspect he's trying to measure the PWM waveform itself with a voltmeter, which is an invalid test.
     
     
     
    #6
    katela
    Super Member
    • Total Posts : 454
    • Reward points : 0
    • Joined: 2013/06/11 05:25:18
    • Location: South Africa
    • Status: offline
    Re: How to configure and control PWM output as DC Voltmeter ? 2017/09/14 01:22:14 (permalink)
    +2 (2)
    Definitely. The PWM waveform must be filtered to get an average DC value with a simple low-pass filter 
    This thread can assist: http://www.microchip.com/forums/m254959.aspx
     
    post edited by katela - 2017/09/14 01:25:02

    Free online Microcontroller Tutorials and Projects for Hobbyists and students. From beginners to advanced. Website: www.studentcompanion.co.za
    Youtube Tutorials: http://www.youtube.com/user/StudentCompanionSA/videos
    #7
    ndquoc
    New Member
    • Total Posts : 3
    • Reward points : 0
    • Joined: 2017/01/16 21:02:19
    • Location: 0
    • Status: offline
    Re: How to configure and control PWM output as DC Voltmeter ? 2017/09/14 03:26:04 (permalink)
    +1 (1)
    Hi all,


    I found the root cause. 
    Thank you Katela and QHB so much ! 
    After reading thread : http://www.microchip.com/forums/m254959.aspx
    I try to change Capacitor down from 10uF to 1uF or 0.1uF then I have got the expected result.

    Now It works very exactly !
    _Put value valPWM = 205  --> Output 1V
    _Put value 410  --> Output 2V
    _Put value 615  --> Output 3V
    _Put value 820  --> Output 4V
    _Put value 1023 --> Output 5V
    #8
    katela
    Super Member
    • Total Posts : 454
    • Reward points : 0
    • Joined: 2013/06/11 05:25:18
    • Location: South Africa
    • Status: offline
    Re: How to configure and control PWM output as DC Voltmeter ? 2017/09/14 10:00:57 (permalink)
    +1 (1)
    Always a good idea to mark the thread as solved!

    Free online Microcontroller Tutorials and Projects for Hobbyists and students. From beginners to advanced. Website: www.studentcompanion.co.za
    Youtube Tutorials: http://www.youtube.com/user/StudentCompanionSA/videos
    #9
    qhb
    Superb Member
    • Total Posts : 5505
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: online
    Re: How to configure and control PWM output as DC Voltmeter ? 2017/09/14 13:13:04 (permalink)
    +3 (3)
    ndquoc
    After reading thread : http://www.microchip.com/forums/m254959.aspx
    I try to change Capacitor down from 10uF to 1uF or 0.1uF then I have got the expected result.

    In that case, it's not the value of the capacitance that was the problem, it will be that the larger capacitor probably had a lot more ESR (equivalent series resistance) than the smaller one.
    It was probably an aluminium electrolytic cap, and the smaller caps would have been monolithic or ceramic, which would have had a much lower ESR.
    #10
    Jump to:
    © 2017 APG vNext Commercial Version 4.5