Menu

Problem with PWM in 16F1938

Help
2016-12-20
2016-12-20
  • Edward LaBudde

    Edward LaBudde - 2016-12-20

    ~~~Hi all can not get this to work on channel 5 (Straight PWM) What am I doing wrong?
    'ChristmasTree
    'By E.V. LaBudde copyright 2016

    chip 16F1938, 32 'Chip model

    'Set Configuration

    config INTOSC 'Internal Osc

    TUN = b'011111' 'Set to max (32MmHz) Verifed correct clock freq
    IRCF = b'1110' 'Set to 8 or 32 mHz Verifed correct clock freq
    PWM = On
    'Set inital parameters
    InitSer 1, r9600, 1, 8, 1, none, Normal 'LCD interface Positive going Standby bit 9600 baud
    'Set Directions
    Dir PortA.0 In 'Spare I/O 1 pin 2
    Dir PortA.3 Out 'Spare I/O 2 pin 5
    Dir PortB.0 Out 'Pin 21 White
    Dir PortB.1 Out 'Pin 22 Red
    Dir PortB.2 Out 'Pin 23 Green
    Dir PortB.3 Out 'Pin 24 Blue
    Dir PortB.5 Out 'Pin 24 Test
    Dir PortC.3 Out 'LCD Pin 14

    'Set Defines

    Define White PortB.0

    define Red PortB.1

    Define Green PortB.2

    Define Blue PortB.3

    Define LCD PortC.3

    Main:
    'Main code from help file
    do
    'Turn up brightness over 2.5 seconds
    For Bright = 1 to 255
    HPWM 5, 10, Bright
    wait 10 ms
    next
    'Turn down brightness over 2.5 seconds
    For Bright = 255 to 1
    HPWM 5, 10, Bright
    wait 10 ms
    next
    loop
    Goto Main

    ~~~

     
  • Anobium

    Anobium - 2016-12-20

    Which port do you expect the PWM5 on? Is it CCP5 ... so... this need to be an output.

    There are a few demos for PWM using.CCP.

     
  • Peter

    Peter - 2016-12-20

    I did something similar recently using PWM5, there was something I had to to get it working. I'll go and look for the code...

     

    Last edit: Peter 2016-12-20
    • Peter

      Peter - 2016-12-20

      I've attached my full code for reference, however it might not be much use:
      - I used a 16F18313 which has a PWM5/PWM6 module seperate from the CCP modules
      - The 16F18313 has PPS which needs to be set, the 16F1938 does not have PPS

      The 16F1938 does not have a PWM5 module, but it does have a CCP5 module. On the 16F1938, CCP5 is connected to PORTA.4 so you need to connect an LED to PORTA.4

      In my code, I needed the following for CCP1:

      For Bright = 1 to 200
              HPWM 1, 64, Bright
              wait PulseSpeed 10us
       Next
      

      But for PWM5 I needed this (note the extra parameter for HPWM):

      For Bright = 1 to 255
              HPWM 5, 64, Bright, 2
              wait PulseSpeed 10us
      Next
      

      But I was using the PWM5 module in the chip, not the CCP5 module.

      A very poor video of what the full code does:

       

      Last edit: Peter 2016-12-20
  • William Roth

    William Roth - 2016-12-20

    The code does not work becasue PORTA.4 (CCP5) is not set as an output Add.

    DIR PORTA.4 OUT
    

    On another note.... It is totally unnecesary to write tun and IRCF bits. This is done automatically for you by GCB. Even if you wanted to do write these bits the code is incorrect and will not work.

    If you want to do that (again unnecessary) you must either write to the actual register or set the bits individually

    The "tun" bits are in the OSCTUNE register and should not be messed with as they only fine tune the OSC. They do not set the chip frequency. Messing with these bits will take the chip out of factory calibration.

    To write the IRCF bits you must write to to OSCCON Register. The best way to do this is by seting/clearing each bit individually or else you can inadvertently change the PLL setting and clock source setting. LIke this:

     SET IRCF3  ON
     SET IRCF2  ON
     SET IRCF1  ON
     SET IRCF0  OFF
    

    Again no need to mess about with these bits. GCB does it for you.

     

    Last edit: William Roth 2016-12-20

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.