Yesterday 21:46
Forum: Proton Plus Compiler v3
Starter: joesaliba
Views: 0
Replies: 9
Go to last post By: joesaliba
Yesterday 19:57
Forum: Proton Plus Compiler v3
Starter: johngb
Views: 0
Replies: 16
+ Reply to Thread
Results 1 to 5 of 5

Thread: PIC12F1571 10bit PWM8 days old

  1. #1
    Inactive member AndrewB's Avatar
    Join Date
    Nov 2017
    Posts
    1
    Thumbs Up
    Received: 0
    Given: 0
    Total Downloaded
    2.4 KB

    0 Not allowed!

    Default PIC12F1571 10bit PWM

    Have the new 16bit PWM files that Les has made but I would like to convert the 10bit Adc to a 10bit PWM output.
    Can this be done on the PIC12F1571?

    Thanks

  2. #2
    Developer Les's Avatar
    Join Date
    Feb 2002
    Posts
    3,232
    Thumbs Up
    Received: 229
    Given: 83
    Total Downloaded
    1.50 GB

    0 Not allowed!

    Default Re: PIC12F1571 10bit PWM

    Take a look at the "Scale.inc" libraries within the compiler's "includes" directory.

    You can then scale the 10-bit ADC value into 16-bits and use this for the PWM.

    Take a look at the unsigned 16-bit scale routine:

    wScale(pValueIn, pInMin, pInMax, pOutMin, pOutMax, pResult)
    Last edited by Les; 14th November 2017 at 17:23.
    For more example programs for Proton and Proton24 or updates, please visit: Proton WIKI or Proton Files

  3. #3
    Developer Les's Avatar
    Join Date
    Feb 2002
    Posts
    3,232
    Thumbs Up
    Received: 229
    Given: 83
    Total Downloaded
    1.50 GB

    0 Not allowed!

    Default Re: PIC12F1571 10bit PWM

    If you want to just add the 16-bit scale routine into your program, here it is.

    It will convert one set of values into another set, quite efficiently. For example, the values 0 to 1023 (10-bit) can be converted to values 0 to 65535 (16-bit).

    Code:
        Device = 12F1571
        Declare Xtal = 32
       
        GoTo Main       ' Jump over the routines to the main program
    '
    ' Create variables for Scale
    '
        Dim Scale_dValueIn As Dword
        Dim Scale_dInMin As Dword
        Dim Scale_dInMax As Dword
        Dim Scale_dOutMin As Dword
        Dim Scale_dOutMax As Dword
        Dim Scale_dResult As Dword
    
        Dim Scale_wValueIn As Scale_dValueIn.Word
        Dim Scale_wInMin As Scale_dInMin.Word
        Dim Scale_wInMax As Scale_dInMax.Word
        Dim Scale_wOutMin As Scale_dOutMin.Word
        Dim Scale_wOutMax As Scale_dOutMax.Word
    '
    ' Create variables for the demo
    '
        Dim MyResult  As Word
        Dim MyADCValue As Word
    
    '-------------------------------------------------------------------------------------------------------------
    ' Scale one 16-bit unsigned integer value range to another 16-bit unsigned integer value range
    ' Input     : pValueIn holds the value to scale
    '           : pInMin is the lower bound of the value's input range
    '           : pInMax is the upper bound of the value's input range
    '           : pOutMin is the lower bound of the value's target range
    '           : pOutMax is the upper bound of the value's target range
    ' Output    : Returns the scaled result
    ' Notes     : Result = (((pValueIn - pInMin) * (pOutMax - pOutMin)) / (pInMax - pInMin)) + pOutMin
    '
    $define wScale(pValueIn, pInMin, pInMax, pOutMin, pOutMax, pResult) '
        Scale_wValueIn = pValueIn                                       '
        Scale_wInMin = pInMin                                           '
        Scale_wInMax = pInMax                                           '
        Scale_wOutMin = pOutMin                                         '
        Scale_wOutMax = pOutMax                                         '
        _wScale                                                         '
        pResult = Scale_dResult.Word
    
    _wScale Macro-
        GoSub __wScale
    Endm
    
    __wScale:
        Scale_dResult = Scale_wValueIn - Scale_wInMin
        Scale_dResult = Scale_dResult * (Scale_wOutMax - Scale_wOutMin)
        Scale_dResult.Word = Scale_dResult.Word / (Scale_wInMax - Scale_wInMin)
        Scale_dResult.Word = Scale_dResult.Word + Scale_wOutMin
        Return       
        
    Main:
    ' Convert the 10-bit values within MyADCValue into 16-bit values within MyResult 
    
        wScale(MyADCValue, 0, 1023, 0, 65535, MyResult)
    The actual scaling expression has been expanded into a few lines, so that it is a lot more efficient, as it does not need to use math stack variables as much, and do maths for "worse case" scenarios.
    Last edited by Les; 14th November 2017 at 18:39.
    For more example programs for Proton and Proton24 or updates, please visit: Proton WIKI or Proton Files

  4. #4
    Developer Les's Avatar
    Join Date
    Feb 2002
    Posts
    3,232
    Thumbs Up
    Received: 229
    Given: 83
    Total Downloaded
    1.50 GB

    0 Not allowed!

    Default Re: PIC12F1571 10bit PWM

    Alternatively, you can adjust the PWM SFRs to give a 10-bit resolution for them.

    The PWM peripheral is based upon a timer, so higher frequencies give less resolution to the PWM. Take a look in the datasheet for the calculation to see what resolution is being given for the frequency. The 16-bit resolution is simply the maximum, not the mandatory. That's the same with all the PWM peripherals on all the devices.
    For more example programs for Proton and Proton24 or updates, please visit: Proton WIKI or Proton Files

  5. #5
    Prolific Poster joesaliba's Avatar
    Join Date
    Sep 2004
    Posts
    2,629
    Thumbs Up
    Received: 59
    Given: 18
    Total Downloaded
    2.27 GB

    0 Not allowed!

    Default Re: PIC12F1571 10bit PWM

    Les,

    This scale routine is very useful. However I had to adjust it a bit for my needs. I needed the following scale: -

    Code:
    wScale(Var1, 0, 2000, 0, 100, S_Result) ' Scale Var1 0% - 100%
    I could not get the desired results so I changed: -

    Code:
    Scale_dResult.Word = Scale_dResult.Word / (Scale_wInMax - Scale_wInMin)
    Scale_dResult.Word = Scale_dResult.Word + Scale_wOutMin
    to: -

    Code:
    Scale_dResult = Scale_dResult / (Scale_wInMax - Scale_wInMin)
    Scale_dResult = Scale_dResult + Scale_wOutMin
    Maths when calculated where resulting greater than a Word so returned wrong values.
    Last edited by joesaliba; 20th November 2017 at 16:13.
    Regards

    Joseph

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. Replies: 0
    Last Post: 1st June 2017, 18:39
  2. ADC 10bit to 8Bit resolution 45K22<->877A
    By Henk57 in forum Proton Plus Compiler v3
    Replies: 9
    Last Post: 30th October 2015, 14:38
  3. [SOLVED !] Pic12f1571
    By RayEllam in forum Proton Plus Compiler v3
    Replies: 2
    Last Post: 26th September 2015, 11:37
  4. 16bit ADin result with a 10bit AD converter? 18F4320
    By kuhrig in forum Proton Plus Compiler v3
    Replies: 2
    Last Post: 17th August 2005, 15:04

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts