• AVR Freaks

PIC24FJ1024GB610 no PWM on RP1

Author
DanielPLongo
New Member
  • Total Posts : 8
  • Reward points : 0
  • Joined: 2019/11/26 12:12:42
  • Location: 0
  • Status: offline
2020/01/13 08:24:52 (permalink)
0

PIC24FJ1024GB610 no PWM on RP1

Not sure what I'm missing. I even explicitly made RP1 (RB1) an output although the reference manual says OC1 controls it. Here is my code:
 
#include <cstdlib>
#include <xc.h>
using namespace std;
/*
*
*/
int main(int argc, char** argv) {

RCONbits.SWDTEN = 0; // Disable Watch Dog Timer

LATBbits.LATB1 =0; //clear RB1 (RP1)
TRISBbits.TRISB1 = 0; //sets RB1 (RP1)as output
Nop();

T2CON = 0; //Stop Timer2 and reset control reg
//defaults clock source FOSC/2 (4MHz) = 250ns period
//continues timer in Idle mode
//prescale 1:1
TMR2 = 0; //Clear contents of the timer register
OC1CON1 = 0; //Clear OC1CON1 register
//defaults OCTSEL to Timer2 clock
//default to continue in Idle mode
//OCM = 0b000 (OFF)
PR2 = 1333; //Load the Period register with decimal value 1333
//4MHz/1333 = 3kHz or 1333*250ns = 333.3us = 3kHz
OC1RS = 1226; //sets the pulse low after 1226 counts
//duty cycle to 92%; D = OC1RS/(PR2+1)= 1333/(1226+1)= 0.92
OC1R = 1226; //initializes OC1R which after the first pulse is read only,
//and will read in the value of OC1RS which can be written to
//to adjust the pulse width.
T2CONbits.TON = 1; //turns on Timer2
OC1CON1bits.OCM = 0b110; //turns on OC1
//Edge-Aligned PWM mode
RPOR0bits.RP1R = 13; //Output Function Number 13 from table 11-4 selects
//OC1 as the output function of pin 24 (RP1)


while(1); //infinite loop


return 0;
}
 
Thanks in advance for your help!


while(1); //infinite loop


return 0;
}
#1

2 Replies Related Threads

    DanielPLongo
    New Member
    • Total Posts : 8
    • Reward points : 0
    • Joined: 2019/11/26 12:12:42
    • Location: 0
    • Status: offline
    Re: PIC24FJ1024GB610 no PWM on RP1 2020/01/13 09:31:47 (permalink)
    0
    Solved.
    I put an LED on in the while loop to make sure the program had at least one known working component. I saw it flash and then not work. Something was up. I copied the PWM code over to a new project and compiled. Only then did MPLAB complain about "cstlib" and "namespace". I deleted both of them (since they were not required), rebuilt and the program works. Not sure what happened there, but the code works. Hope this helps someone.
    #2
    ric
    Super Member
    • Total Posts : 25101
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: PIC24FJ1024GB610 no PWM on RP1 2020/01/13 12:44:32 (permalink)
    0
    Also, not a "show stopper", but in XC16 your main() function definition should be:
    int main(void)

    There's no operating system supplying any command line parameters...
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #3
    Jump to:
    © 2020 APG vNext Commercial Version 4.5