• AVR Freaks

Hot!PIC18F25K40 PWM Issue...

Author
sparkicidal
New Member
  • Total Posts : 4
  • Reward points : 0
  • Joined: 2019/07/03 12:46:42
  • Location: 0
  • Status: offline
2019/07/08 12:28:05 (permalink)
0

PIC18F25K40 PWM Issue...

Chip: PIC18F25K40
Pin in use: RC2
Function: PWM through CCP.
Code: Attached.
 
Hey all!
 
I've worked with PICs since 2002 and this is the first time I've got really, really stuck. I'm a professional EE so it's even more frustrating.
 
There's something wrong with how I'm setting up the PWM in CCP such that it just won't work and I cannot see it for the life of me. If I "bit-bash" a PWM, it works fine. I've uploaded the code and I'm looking at the code in the main() procedure, but I left the other code in the other procedure just in case that helps. I've removed all unnecessary code.
 
Any help anyone can give would be greatly appreciated.
 
Pete.
 
#1

5 Replies Related Threads

    mbrowning
    USNA79
    • Total Posts : 1565
    • Reward points : 0
    • Joined: 2005/03/16 14:32:56
    • Location: Melbourne, FL
    • Status: offline
    Re: PIC18F25K40 PWM Issue... 2019/07/09 10:59:34 (permalink)
    +1 (1)
    You didn't assign the PWM output to a pin (RC2PPS = 0x05, perhaps?).
     
    Some other comments:
    This is useless - the include file for the project selected part is included by xc.h automatically
    #include <pic18f25k40.h>

    This is useless without the requisite unlock sequence (section 17.4)
    PPSLOCK = 0x01;   //Peripheral Pin Select is Not Locked and can be changed.

    Having trouble turning on the timer?
        T2ON = 1;
        T2CONbits.ON = 1;
        T2CONbits.TMR2ON = 1;



     

    Go Navy! Beat Army!
    #2
    sparkicidal
    New Member
    • Total Posts : 4
    • Reward points : 0
    • Joined: 2019/07/03 12:46:42
    • Location: 0
    • Status: offline
    Re: PIC18F25K40 PWM Issue... 2019/07/09 11:55:29 (permalink)
    0
    MBrowning, thank you! Assigning the RC2PPS has solved it and it's working. What's reeeeeeeally annoying is that I had that line in my code, then moved it to the top of the other procedure, then didn't call the procedure. Geeeeenuis.
     
    With respect to your other points:
     
    Yeah, I understand this bit of code is useless, it's just habit. When I started doing these, it was all in assembler so I had to set everything up manually. You may have noted the binary commented out alongside the hex.
    #include <pic18f25k40.h>

     
    The PPSLOCK was the first time I'd encountered such a thing. It was put in as a backside covering process.
    PPSLOCK = 0x01;   //Peripheral Pin Select is Not Locked and can be changed.

     
    I couldn't figure out why it wasn't working, so I turned everything on! :)
       T2ON = 1;
        T2CONbits.ON = 1;
        T2CONbits.TMR2ON = 1;

     
    Thank you again for your help, it was most definitely appreciated!
    post edited by sparkicidal - 2019/07/09 11:57:14
    #3
    sparkicidal
    New Member
    • Total Posts : 4
    • Reward points : 0
    • Joined: 2019/07/03 12:46:42
    • Location: 0
    • Status: offline
    Re: PIC18F25K40 PWM Issue... 2019/07/19 08:10:46 (permalink)
    0
    Hi!
     
    If anyone sees this, could you whip your eyes over the updated code (attached) please?
     
    I've implemented the RC2PPS and all is good, now I have the challenge of it being on permanently, I can't get it to oscillate.
     
    I've adjusted T2PR and the CCPR1H / CCPR1L values, but to no avail.
     
    What am I doing wrong this time? Honestly, I've never had so much trouble with a single PIC fct.
     
    Cheer!
     
    Pete.
    #4
    mbrowning
    USNA79
    • Total Posts : 1565
    • Reward points : 0
    • Joined: 2005/03/16 14:32:56
    • Location: Melbourne, FL
    • Status: offline
    Re: PIC18F25K40 PWM Issue... 2019/07/19 09:19:14 (permalink)
    +1 (1)
    Perhaps main problem - T2PR = 0x00
    If you want T2 to be an 8bit counter you need to set T2PR=0xff (the reset value).
    I've never tried setting it to 0, but I think it won't count at all, so PWM can't work.
     
    Get rid of this stuff: The first should never be included and the rest are unused in this code.
    #include <pic18f25k40.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>

    Your config bits are incomplete. You should have all of them in here, especially RSTOSC. Don't rely on defaults because you have to go back to the datasheet to see what they are.
    OSCON2 is read only. You can't write to it. You are setting OSCON1 for a divider of 8. If your external crystal is 8MHz as in the comments, then Fosc = 8 * 4 / 8 = 4MHz. In this case set _XTAL_FREQ to 4000000 for accurate delay macros.
    Why are you setting RC2 as both the CCP2 input and the CCP1 output? (CCP2PPS = 0x11/RC2PPS=0x05)
    Why is there a while(1) loop at the end of PWMInit() ?
    Why is PWMInit() inside the main while loop?
    Why are you clearing the same bit three times? T2ON = 0; T2CONbits.ON = 0; T2CONbits.TMR2ON = 0;
       and setting the same bit four times?    T2CON = 0x80; T2ON = 1; T2CONbits.ON = 1; T2CONbits.TMR2ON = 1;

    Go Navy! Beat Army!
    #5
    sparkicidal
    New Member
    • Total Posts : 4
    • Reward points : 0
    • Joined: 2019/07/03 12:46:42
    • Location: 0
    • Status: offline
    Re: PIC18F25K40 PWM Issue... 2019/08/02 08:03:18 (permalink)
    0
    Hi!
    I'll experiment and let you know how I get on. Thanks again for your help.
     
    #6
    Jump to:
    © 2019 APG vNext Commercial Version 4.5