PWM using the 12F683


Closed Thread
Results 1 to 7 of 7
  1. #1
    Join Date
    Apr 2016
    Location
    Plainfield, Illinois
    Posts
    73

    Default PWM using the 12F683

    I've read in these forums someone stating that any IO pin can be used for PWM on the 12F683, but in the 683 docs it states only the CCP1 pin can be used for PWM - pin 5. True?

    I've already laid out a circuit board for this first try, but of course it can be modified on next rev. I start programming tomorrow with it - any feedback would be appreciated as I do this and post my code.
    From what I've read I'll need to setup CCP1 register before I can use the PWM function, and change OSC to 8Mhz..(option?)..

    Basically I'm using an input pin (pulled up/ to ground) to "step" the brightness from dim to bright, reach a top brightness, and recycle to dimmest again. Going from 0 to 255 and a repetition rate of I dont know yet (10?).

    Continuous loop - the chip will do nothing else..

  2. #2
    Join Date
    May 2013
    Location
    australia
    Posts
    2,392


    Did you find this post helpful? Yes | No

    Default Re: PWM using the 12F683

    hpwm and pwm are different commands pwm is not continuous and can be on any output pin and is a blocking cmd.
    hpwm can only be on a ccpx pin is continuous and is non blocking [ccp1 is gp2 /pin5 on a 12f683]
    both work as per the book if your define OSC is correct, hpwm is limited to 0 - 33khz unless the ccpx regs are set manualy

  3. #3
    Join Date
    Apr 2016
    Location
    Plainfield, Illinois
    Posts
    73


    Did you find this post helpful? Yes | No

    Default Re: PWM using the 12F683

    So is this command needed If I'm using any output pin? Does setting the OSC mode to 8Mhz do anything for the PWM as far as resolution or is 4Mhz good enough? (I'm dimming LEDs).

    CCP1CON = %00001100 ' CCP1, PWM mode

    and

    OSCCON = %01110001 ' Internal 8MHz osc.
    DEFINE OSC 8
    My dad never liked you...

  4. #4
    Join Date
    May 2013
    Location
    australia
    Posts
    2,392


    Did you find this post helpful? Yes | No

    Default Re: PWM using the 12F683

    easy way

    Code:
    ;pic12f683
    #CONFIG
    cfg = _INTOSCIO
    cfg&= _WDT_ON
    cfg&= _PWRTE_OFF
    cfg&= _MCLRE_ON
    cfg&= _CP_OFF
    cfg&= _CPD_OFF
    cfg&= _BOD_ON
    cfg&= _IESO_ON
    cfg&= _FCMEN_ON
      __CONFIG cfg
    #ENDCONFIG
    
    
    DEFINE OSC  8
    OSCCON=$70
    CMCON0    = 7
    TRISIO   = %111011      ;gp2= output
    
    ANSEL    =0
    ; hpwm Channel, Dutycycle, Frequency
      
    hpwm     1,128,600   ;       50% 600 hz
     
    mainloop:
    
    ; do stuff
    
    goto mainloop
    
    
    


    to do it manually

    Code:
     TRISIO   = %111011      ;gp2= output
        t2con=5       ; set up timer/pr2  for frequency and pwm  bits   ; note this is not equivalent to above  resulting freq is ? [you work it out]
        PR2 = 255;
        CCPR1L = pulswidth     ; set pulsewidth high bits
        ccp1con=12             ; set ccp reg and or pulsewidth  low bits











































































    Last edited by richard; - 25th April 2016 at 05:57.

  5. #5
    Join Date
    May 2013
    Location
    australia
    Posts
    2,392


    Did you find this post helpful? Yes | No

    Default Re: PWM using the 12F683

    Does setting the OSC mode to 8Mhz do anything for the PWM as far as resolution or is 4Mhz good enough? (I'm dimming LEDs).
    more freq == more options == finer control == uses more power

    whatever floats your boat

  6. #6
    Join Date
    Apr 2016
    Location
    Plainfield, Illinois
    Posts
    73


    Did you find this post helpful? Yes | No

    Default Re: PWM using the 12F683

    here's my first attempt - haven't tried it yet - I will try PWM also as an experiment


    '12f638

    ' ======= config SETUP =================================


    #CONFIG
    cfg = _INTOSCIO
    cfg&= _WDT_ON
    cfg&= _PWRTE_OFF
    cfg&= _MCLRE_OFF
    cfg&= _CP_OFF
    cfg&= _CPD_OFF
    cfg&= _BOD_ON
    cfg&= _IESO_ON
    cfg&= _FCMEN_ON
    __CONFIG cfg
    #ENDCONFIG

    ' ======= Common Settings =================================

    OSCCON = %01110001 ' Internal 8MHz osc.
    DEFINE OSC 8
    CCP1CON = %00001100 ' CCP1, PWM mode - could be %00001110 for negative
    CMCON0 = 7
    TRISIO = %101011 ' GP2 and GP4 are outputs
    ANSEL = 0

    HLEDOUT VAR GPIO.2 ' HPWM output
    PLEDOUT var GPIO.4 ' PWM output
    BUTTEN var GPIO.3 ' BUTTON IN

    HERTZ VAR BYTE
    DUTY VAR BYTE

    DUTY = 1
    HERTZ = 600


    ; hpwm Channel, Dutycycle, Frequency

    ' hpwm 1,128,600 ; 50% 600 hz

    MAIN:

    HPWM 1, DUTY, HERTZ ' continuous output from GP2

    IF BUTTEN = 0 THEN ' button pushed
    pause 50 ' debounce
    DUTY = DUTY +1 ' incrememnt duty cycle
    ENDIF
    IF DUTY > 254 THEN ' MAX number
    DUTY = 1 ' reset duty
    ENDIF

    GOTO MAIN

    end
    My dad never liked you...

  7. #7
    Join Date
    Apr 2016
    Location
    Plainfield, Illinois
    Posts
    73


    Did you find this post helpful? Yes | No

    Default Re: PWM using the 12F683

    PWM code and board works... but I had to throw the pause 50 (debounce) out because that caused a blink. HPWM might work better.
    My dad never liked you...

Similar Threads

  1. 12F683 2 x PWM Outputs
    By retepsnikrep in forum mel PIC BASIC Pro
    Replies: 29
    Last Post: - 23rd July 2021, 20:20
  2. Dual PWM's using 12F683? Failure, Suggestions?
    By wdmagic in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 17th October 2015, 01:22
  3. please help with 12f683 pwm code
    By haidar in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 22nd May 2013, 21:25
  4. 12f683 comparator and pwm
    By Automan in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 23rd May 2007, 18:05
  5. PIC 12f683 AD/PWM Problems:
    By surfer0815 in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 7th December 2005, 16:48

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

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