Using PWM built in Module 16F628


Closed Thread
Results 1 to 9 of 9
  1. #1
    Join Date
    Jan 2007
    Location
    Houston, TX
    Posts
    96

    Cool Using PWM built in Module 16F628

    Hey there,
    I would like to use the internal PWM module in my PIC16F628 chip instead of the PWM software statement. I have heard this will allow me to use a seperate clock speed (rc=3?) so I can run a motor speed control while monitoring several other switches with my Xtal. Where I am really stuck is... How to set up the loops such that I am really doing 2 things at once.... , checking conditions of switches and fetching POT value for PWM on a seperate clock speed. I would appreciate any help.
    thanks!

    'general idea, no coding as I do not want to be mocked just yet
    example....

    loop 1: 'internal RC constant or something
    ' fetch POT value...store as B0
    ' use value for PWM output to motor



    loop2: 'external 4Mhz crystal
    'if then else statements looking at 5 switch values
    'branching to subroutines
    Padawan-78

  2. #2
    Join Date
    Oct 2003
    Location
    holland
    Posts
    251


    Did you find this post helpful? Yes | No

    Default

    Yust use the hpwm instruction and put in the define part:
    DEFINE CCP1_REG PORTB
    DEFINE CCP1_BIT 3

    nothing else more !!

  3. #3
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by mat janssen View Post
    Yust use the hpwm instruction and put in the define part:
    DEFINE CCP1_REG PORTB
    DEFINE CCP1_BIT 3

    nothing else more !!
    For PICs that have onboard PWM modules, like 628, CCP DEFINEs are not required.


    Reading POT value and using it to drive a motor, try the following demo code.


    Code:
    @ DEVICE PIC16F628, INTRC_OSC_NOCLKOUT
    @ DEVICE PIC16F628, MCLR_OFF
    @ DEVICE PIC16F628, WDT_OFF
    @ DEVICE PIC16F628, PROTECT_OFF
    @ DEVICE PIC16F628, BOD_OFF
    @ DEVICE PIC16F628, CPD_OFF
    
    TRISA = 255     'All pins are input.
    TRISB = 0       'All pins are output.
    
    '=====Timer2 settings  - 8Bit ====================
    T2CON = %00000000      ' Required for accurate timing for RPM.
    INTCON.7 = 0           ' Disable interrupts.
    
    '===================PWM Part===============
    CMCON     = 7          ' PortA = digital I/O
    VRCON     = 0          ' Disable A/D Voltage reference.
    PR2       = 255        ' PWM frequency.
    CCP1CON   = %00001100  ' PWM Mode selected.
    T2CON     = %00000100  ' Timer2 ON + 1:1 prescale
    CCPR1L    = 0          ' Set PWM Duty-Cycle
    
    PotPin VAR PORTB.0
    
    Begin:
    
        POT PotPin , 255, CCPR1L         'Read Pot value and put on PWM Module. From 0 to 255. From zero to full speed.
        '... have your switch readings here.
        '......
        '........
        
        
        
        
        Goto Begin
    
    
    END
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  4. #4
    Join Date
    Jan 2007
    Location
    Houston, TX
    Posts
    96


    Did you find this post helpful? Yes | No

    Smile thanks!

    thanks for the help
    Padawan-78

  5. #5
    Join Date
    May 2007
    Posts
    65


    Did you find this post helpful? Yes | No

    Red face

    Quote Originally Posted by sayzer View Post
    For PICs that have onboard PWM modules, like 628, CCP DEFINEs are not required.


    Reading POT value and using it to drive a motor, try the following demo code.


    Code:
    @ DEVICE PIC16F628, INTRC_OSC_NOCLKOUT
    @ DEVICE PIC16F628, MCLR_OFF
    @ DEVICE PIC16F628, WDT_OFF
    @ DEVICE PIC16F628, PROTECT_OFF
    @ DEVICE PIC16F628, BOD_OFF
    @ DEVICE PIC16F628, CPD_OFF
    
    TRISA = 255     'All pins are input.
    TRISB = 0       'All pins are output.
    
    '=====Timer2 settings  - 8Bit ====================
    T2CON = %00000000      ' Required for accurate timing for RPM.
    INTCON.7 = 0           ' Disable interrupts.
    
    '===================PWM Part===============
    CMCON     = 7          ' PortA = digital I/O
    VRCON     = 0          ' Disable A/D Voltage reference.
    PR2       = 255        ' PWM frequency.
    CCP1CON   = %00001100  ' PWM Mode selected.
    T2CON     = %00000100  ' Timer2 ON + 1:1 prescale
    CCPR1L    = 0          ' Set PWM Duty-Cycle
    
    PotPin VAR PORTB.0
    
    Begin:
    
        POT PotPin , 255, CCPR1L         'Read Pot value and put on PWM Module. From 0 to 255. From zero to full speed.
        '... have your switch readings here.
        '......
        '........
        
        
        
        
        Goto Begin
    
    
    END
    hi Sayzer;
    looking at your really cool sample code i think instead of
    "PotPin VAR PORTB.0"

    should be
    "PotPin VAR PORTA.0"
    because as PIC's datasheet says, analog inputs are only in PORTA, and also you configured PORTA as inputs and PORTB as outputs.


    anyway i found that your POT & CCPR1L line is amazing !

  6. #6
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    yeah but CMCON=7 disable all analog feature, there's no analog-to-digital converter on this one, and POT don't even need any analog stuff to work, it just measure the RC constant or whatever else useless/inacurate method.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  7. #7
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by RodSTAR View Post
    ...

    anyway i found that your POT & CCPR1L line is amazing !
    Thanks.

    If you like to use a PIC with on board ADC and PWM modules, like 16F877 or 12F683, then you can also use a single line as below.

    Code:
    ADCIN 0, CCPR1L
    
    'This will read Analog channel 0 
    'and put on PWM register right away.

    If you like to use F628 with POT command, then make sure that the pot is not connected to "input only" pins. Pin must be a bi-directional pin.
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  8. #8
    Join Date
    May 2007
    Posts
    65


    Did you find this post helpful? Yes | No

    Wink

    good morning sayzer, mister_e,

    About the PWM thing, I'm clear and OK,

    but... reading your last posts I don't know if understood correctly:

    -> what I understood is that I can measure the value of a potentiometer into any I/O pin without any necessary A/D conversion feature? In other words, in a 16F628A: I could use 16 potentiometers?? wow!! that would be crazyly cool!

    (sorry my english)

  9. #9


    Did you find this post helpful? Yes | No

    Talking almost!

    Quote Originally Posted by RodSTAR View Post
    -> what I understood is that I can measure the value of a potentiometer into any I/O pin without any necessary A/D conversion feature? In other words, in a 16F628A: I could use 16 potentiometers?? wow!! that would be crazyly cool!
    From what I've read, that's sort of correct. it can use any bidirectional digital pin.

    It can not be used on an open collector output pin, or an input only pin.

Similar Threads

  1. Half-bridge PWM with a 16F684 ?
    By Byte_Butcher in forum General
    Replies: 7
    Last Post: - 17th January 2010, 22:18
  2. Variable PWM PIC18F2431
    By trr1985 in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 12th June 2009, 06:03
  3. PWM Module 18Fxx31
    By iphillipsca in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 11th April 2008, 08:06
  4. Pwm 16f628
    By mladenovic in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 21st September 2005, 21:16
  5. pic 18Fxx31 PWM module
    By dirace in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 15th May 2005, 19:51

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