Menu

12F683 Help - resolved

Anobium
2023-11-30
2023-12-01
  • Anobium

    Anobium - 2023-11-30

    Anyone able to answer this for me. I am stumped.

    A user has asked for 10Bit PWM/CCP1 on a 12F683. A reason request.

    The datasheet explains 10bit operation, section 11.3 for the datasheet.

    So, how do this work? I know it has something to do with CCPR1L and CCP1CON register two LSbs DC1B<1:0> .

    But, I am stuck. How to operate in 10bit mode?

    Below is the code for a 1Hz signal at 50% duty.

    How to make this into a 10Bit duty cycle ?
    What are the operating limits for a 10Bit duty cycle, in terms of chip freq and PWM freq ?

    Thanks

    'Chip Settings
    #chip 12F683, 8
    #option Explicit 
    
    'Set the direction of the CCP1/PWM port
    DIR GP2 Out
    
    // Set for 1Hz
    PR2         = 125
    CCPR1L      = 63
    CCP1CON     = 0
    CCP1M3      = 1
    CCP1M2      = 1
    CCP1M1      = 0
    CCP1M0      = 0
    DC1B1       = 0
    DC1B0       = 0
    
    // Setup the timer
    T2CKPS0     = 0
    T2CKPS1     = 1
    TMR2ON      = 1
    
    
    do
    loop
    
     
    • Awais

      Awais - 2023-11-30
       
      • Anobium

        Anobium - 2023-12-01

        Thanks.

        The first URL is discussing PWM options and not PWM/CCP in 10 bit mode.
        The second URL URL is discussing PWM options across the range of PICs.
        The PDF discusses 10Bit PWM on the 12F683 then disregards the 10bit option to use the 8bit PWM/CPP ( as 10 bit is not required) signal therefore it is not a 10bit implementation using CCPR1L and CCP1CON register two LSbs DC1B<1:0> . So, not a reference implementation.


        However, I remember tools/software in https://github.com/GreatCowBASIC/Demonstration_Sources/tree/main/Interrupt_and_Timer_Solutions/Interrupt_Timed_examples There is a ZIP. In the ZIP is Mister E PIC Multi-Calc tool. The PWM option shows how to obtain a 10bit PWM/CPP signal.

        As initially thought. Using 10bit PWM/CPP signals is very specific frequencies for specific MCU clock frequencies.

        This information is not constrained by GCBASIC but the implementation of PWM/CCP in the MCU.

        The specifics of PWM/CCP10bit range can be determined in the following table, derived from the Mister E PIC Multi-Calc tool ( which is an implemention of the datasheet).

        MCU Freq 10bit PMW/CPP Freq Range
        8 0 .489Khz > < 15.60Khz
        4 0.245Khz> < 7.8Khz
        2 0.123Khz> < 3.9Khz
        1 0.062Khz> < 1.95Khz

        So, theory resolved. There is a software tool to assist in the modelling!!

        Back to a real implementation in GCBASIC.

        Fixed Model PWM is using the two constants to control PWM frequency and Duty.
        HPWM CCP is using HPWM command.

        Examples Code
        Fixed Mode PWM Min: #chip 12f683,8
        #define PWM_Freq 0.489 'Frequency of PWM in KHz
        #define PWM_Duty 50 'Duty cycle of PWM (%)

        Max: #chip 12f683,8
        #define PWM_Freq 15.6 'Frequency of PWM in KHz
        #define PWM_Duty 50 'Duty cycle of PWM (%)

        Min: #chip 12f683,4
        #define PWM_Freq 0.245 'Frequency of PWM in KHz
        #define PWM_Duty 50 'Duty cycle of PWM (%)

        Max: #chip 12f683,4
        #define PWM_Freq 7.8 'Frequency of PWM in KHz
        #define PWM_Duty 50 'Duty cycle of PWM (%)

        Min:#chip 12f683,2
        #define PWM_Freq 0.123 'Frequency of PWM in KHz
        #define PWM_Duty 50 'Duty cycle of PWM (%)

        Max: #chip 12f683,2
        #define PWM_Freq 3.9 'Frequency of PWM in KHz
        #define PWM_Duty 50 'Duty cycle of PWM (%)

        Min: #chip 12f683,1
        #define PWM_Freq 0.062 'Frequency of PWM in KHz
        #define PWM_Duty 50 'Duty cycle of PWM (%)

        Max: #chip 12f683,1
        #define PWM_Freq 1.95 'Frequency of PWM in KHz
        #define PWM_Duty 50 'Duty cycle of PWM (%)
        HPWM CCP Mode Duty set to 127 equates to 50% Duty

        Min: #chip 12f683,8
        HPPWM 1, 1, 127

        Max: #chip 12f683,8
        HPPWM 1, 15, 127

        Min: #chip 12f683,4
        HPPWM 1, 1, 127

        Max: #chip 12f683,4
        HPPWM 1, 7, 127

        Min: #chip 12f683,2
        HPPWM 1, 1, 127

        Max: #chip 12f683,2
        HPPWM 1, 3, 127

        Min: #chip 12f683,1
        HPPWM 1, 1, 127

        Max: #chip 12f683,1
        HPPWM 1, 1, 127

        The duty for Fixed Model PWM is 0% to 100% therefore the is a 7-bit calculation. There is nothing stopping a user from calculating the CCPR1L and CCP1CON register two LSbs DC1B<1:0> values for 10bit and setting using this code segment. See example below.

        The range of HPWM CCP Mode is limited to 1KHz as GCBASIC uses an integer for parameter, hence, the constraints shown in the table above.

        Now I know the ranges and the constraints I can test on a real chip.


        A real 10bit implementation using OSC at 8 Mhz, PWM at 0.5 using a loop to control the 10bit control. It is nearly 10bit... as this would be 1016 increments where PR2 = 253

            'Chip Settings
            #chip 12F683, 8
            #option Explicit 
        
            'Set the direction of the CCP1/PWM port
            DIR GP2 Out
        
            #define PWM_Freq 0.5     'Frequency of PWM in KHz
            #define PWM_Duty 50      'Duty cycle of PWM (%)
        
            // Turn on Fixed Mode PWM/CPP1
            PWMON
        
            // The maximum duty is four times the PR2. So, we can calc that and assign.
            Dim maxduty as Word
            maxduty = [WORD]( PR2 ) * 4
        
        do
                    Dim dutyvalue, dutycalc as Word
                    Dim dutytransformed as Word
        
                    // Need to cast the calc to ensure the WORD is the result of the BYTE calculation
                    for dutyvalue = 0 to maxduty
                        // Set the 10bit value using CCPR1L and CCP1CON register two LSbs DC1B<1:0>
                        DC1B1  = dutyvalue.1
                        DC1B0  = dutyvalue.0
                        CCPR1L = dutyvalue / 4  // shift the 10 bit value right
                        wait 1 ms
                    next
                    for dutyvalue = maxduty to 0 Step -1
                        // Set the 10bit value using CCPR1L and CCP1CON register two LSbs DC1B<1:0>
                        DC1B1  = dutyvalue.1
                        DC1B0  = dutyvalue.0
                        CCPR1L = dutyvalue / 4  // shift the 10 bit value right
                        wait 1 ms
                    next
        
            loop
        

        Enjoy

         

        Last edit: Anobium 2023-12-01
  • Anobium

    Anobium - 2023-12-01

    I think I should make it clear. These old PWM/CPP chips are not 10bit PWM at all frequencies. They are capable of 10bit only at specific mcu frequencies and specfic PWM frequencies. An example is mcu at 4Mhz and PWM at 0.245Khz - these will give 1020 steps ( 10 bit value). Anything above mcu at 4Mhz and PWM at 0.487Khz is a 9-bit step.

    For real 10bit PWM across all frequencies use a mcu with a true 10bit PWM ( not PWM/CPP).

    :-)

     
  • Fabrice Engel

    Fabrice Engel - 2023-12-01

    Yes Evan, I confirm, I needed also to add more calculation in my code PWM2Laser to be able to manage different resolutions according frequencies (see code examples in attached picture). Needed time to understand, debug and fix, and the solution is mostly a multiplicator :)

     
    • Anobium

      Anobium - 2023-12-01

      You may have to manual update registers. The basic PWM are there for the novice users.

      It would be good to have your analysis also.

       

Log in to post a comment.