Announcement

Collapse
No announcement yet.

10 bit PWM on PIC12F1571

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • 10 bit PWM on PIC12F1571

    Does anybody know how to program the PIC12F1571 for 10bit PWM? Is it 8 bit or 16bit only?

    I have modified some code but it's not working.

    OSCCON= %01101000 'Internal 4MHz Oscillator
    DEFINE OSC 16 'Internal RC Oscillator 8MHz
    DEFINE HPWM_OSC 16000000 ' PWM clock at 16MHz
    ' Connect PWM2 to RA0 (Pin 7)
    ' Connect PWM1 to RA1 (Pin 6) **
    ' Connect ADC2 VIN1 to RA2 (Pin 5) **
    ' Connect NC to RA3 (Pin 4)
    ' Connect VIN2 to RA4 (Pin 3)
    ' Connect NC to RC5 (Pin 2)


    @ ERRORLEVEL -306 ' Turn off "Crossing page boundary" error message


    DEFINE ADC_BITS 10 'Set number of bits in result (8, 10 or 12)
    DEFINE ADC_CLOCK 3 'Set clock source (rc = 3)
    DEFINE ADC_SAMPLEUS 50 'Set sampling time in microseconds

    TRISA = %00111100 'Set PORTA.0, 1, 3, 4 & 5 as digital inputs
    ANSELA = %00010100 'Set PORTA.0 & 2 as analog inputs

    ADCON0 = %00001001 'Enable ADC
    ADCON1 = %10010000 'Right Justify
    'Fosc/16
    'Vref connected to Vdd
    FVRCON.7 = 0 'Disable Fixed Voltage Reference
    CM1CON0.7 = 0 'Disable Comparitor 1
    'CM2CON0.7 = 0 'Disable Comparitor 2
    INTCON.7 = 0 'Disable Interrupts

    T2CON = %00000110 'Enable TMR2 and set PreScale to 16

    ' Set up PWM Registers
    PWM1CON = %11000000
    PR2 = %11111111

    VIN1 VAR WORD
    VIN1 = 255
    HPWM 1,VIN1,1000
    ADCON0.1 = 1


    Start:

    while ADCON0.1 = 1
    pause 1
    wend
    PWM1DCL = ADRESL
    PWM1DCH = ADRESH
    ADCON0.1 = 1
    'HPWM 1,VIN1,1000

    GOTO Start

    End

  • #2
    First, take the result from A/D and make it one WORD, ie. RESULT = ADRESH << 8 + ADRESL.
    next, shift it to the left 5 places, ie. PWM = RESULT << 5.
    Now you have an equivalent 10 bit pwm value from 0 to full scale.
    Dave Purola,
    N8NTA
    EN82fn

    Comment


    • #3
      Hi DavidP, Thanks.
      Problem is HPWM command is only 8bit .

      Looks like you set hpwm then set PWM1DCL & PWM1DCH after.

      Problem is the scale of adc is not the full range of pwm.
      Last edited by AndrewB; 1 week ago.

      Comment


      • #4
        In the PWM section of ECCP in the Data Sheet there is a sequence to set the PortPin to input, fire up TMR2, turn on the PWM until TMR2IF sets, clear the flag, clear the PortPin for Output, then your PWM1DCL_H will work. Here is similar code for PIC16F1828:

        PIR1.1 = 0 ;TMR2IF
        PIE1.1 = 1
        T2CON = %01111111 ;Turns TMR2 ON
        do
        loop while PIR1.1 = 0 ;TMR2IF
        PIR1.1 = 0 ;TMR2IF
        TRISC.1 = 0
        CCPR1L = 0 ;PWM DC = 0%

        Comment


        • #5
          I will read up the datasheet a bit more.
          Thanks for your input and advice.

          Comment

          Working...
          X