Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

PIC24FJ1024GB610 PWM

Status
Not open for further replies.

RF Can

New Member
Hi guys!

I recently bought a "Clicker 2 mikro bus" from Mikroelektronika. This comes with a 16-bit PIC24FJ1024GB610 microcontroller:
I'm able to communicate and load .HEX files in it. I loaded it up with a "Led Blinking" program on PORT B and worked too.

My problem is when I try to get a PWM signal out of it. For the moment: their PWM_Start() function seems to do nothing.
Then, I tried to program it "old school" fashion. Yet, nothing comes out of PIN # RF2. See below:

void PWMStep (void){
int PWMperiod=799;

TRISFbits.TRISF2 = 0; //Set RF2 as output
RPOR15bits.RP30R = 18; //OC1 Output to Pin PWM
LATFbits.LATF2 = 0; //Initialize RF2 as low
OC1CON1 = 0; // Clear registers
OC1CON2 = 0;
OC1CON2bits.SYNCSEL = 0b11111; // synchronized by itself
OC1CON1bits.OCTSEL = 0b111; // system clock is the source for output Compare
OC1CON1bits.OCM = 0b111;
OC1CON1bits.OCSIDL=1;
OC1RS = (int)(PWMperiod); // PWM period
OC1R = (int)((PWMperiod/2)); //50% initial duty cycle
}

void main(){

while(1){

PWMStep();

}

}

I tried this simple program just to get a 50% duty cycle PWM signal. For the moment, RF2 keeps a LOW state. No PWM at all.

Any help is appreciated. Many thanks,

RF Can
 
TRISFbits.TRISF2 = 0; //Set RF2 as output
RPOR15bits.RP30R = 18; //OC1 Output to Pin PWM
RPOR15bits.RP30R = 18; //OC1 Output to Pin PWM

Your PPS code the"18" is for CCP6 as the output ?
 
Last edited:
RPOR15bits.RP30R = 18; //OC1 Output to Pin PWM

Your PPS code the"18" is for CCP6 as the output ?
Nice catch. I just saw on table 11-4 (p. 161 datasheet), OSC1 code is 13 (and not 18). I re-programmed it with the right value, but still nothing on PIN # RF2.
Do you have an ideal?
 
I seem to remember i did this bug chasing some time ago on that PIC ( just as a PWM exercise ) I will see if i can find the code , The CCP1CON1L[3-0] register comes to mind Just a guess !
 
Last edited:
I seem to remember i did this bug chasing some time ago on that PIC ( just as a PWM exercise ) I will see if i can find the code , The CCP1CON1L[3-0] register comes to mind Just a guess !
OK thanks in advance if you find anything.
 
Not sure if this is any help. I was using timer 2 as clock . and OC3 , It was for a solar charge controller .
C:
    void OC_init() {
    TRISAbits.TRISA10 = 0; // OC3 OUTPUT
    Nop();
    OC3R = 0x8FFF;
    OC3RS = 0x08FC;
    OC3CON2bits.SYNCSEL = 0;
    OC3CON2bits.OCMPINV = 0;
    OC3CON1bits.OCTSEL = 0; //T2 clock
    OC3CON1bits.OCM = 0; // CONTINIOUS pwm mode = 5, 0=off
    PWM_rate=0;
   
}
void PWM_charge(int rate){
   OC3CON1bits.OCM = 0; // STOP pwm
   LATAbits.LATA10=0; // mosfet driver
   LCD_print(3,0," REG @ ");
   switch(rate){
    case 5:
       OC3R = 0xFF00;
       OC3CON1bits.OCM = 5;
       LCD_print(3,7,"  5% PWM ");
       break;
    case 25:
       OC3R = 0xCFFF;
       OC3CON1bits.OCM = 5;
        LCD_print(3,7," 25% PWM ");
       break;
    case 50:
       OC3R = 0x8FFF;
       OC3CON1bits.OCM = 5;
        LCD_print(3,7," 50% PWM ");
       break;
    case 75:
       OC3R = 0x4FFF;
       OC3CON1bits.OCM = 5;
       LCD_print(3,7," 75% PWM ");
       break;
  }
 
I finally took another approach (see below), and works fine for RF2, RF5, RF8, D0 and D11 PWM lines of this MCU model. There's only RF4 I wasn't able to get any modulated output. I'll need to investigate deeper about this pin... Anyways: rare are the projects you need 6 PWM outputs...

void main(){
int j;

while(1){
TRISDbits.TRISD11 = 0;
RPOR6bits.RP12R = 13;
LATDbits.LATD11=0;
/* Reset PWM */
OC1CON1 = 0x0000;
OC1CON2 = 0x0000;

// config timer3
T3CON = 0x00;
TMR3 = 0x00; //Clear contents of the timer register
PR3 = 0xFFFF;


T3CONbits.TCKPS = 0x03; //1:256 prescaler
// configure PWM
OC1CON2 = 0x001F; /* Sync with This OC module*/
//OC1CON1 = 0x1C08; /* Clock source Fcyc, trigger mode 1*/
OC1CON1 = 0x0408; /* Clock source timer3, trigger mode 1*/
/* enable the PWM */
OC1CON1 = OC1CON1 | 0x0006; /* Mode 6, Edge-aligned PWM Mode */

T3CONbits.TON = 1; //Start Timer3
OC1RS = 1250;

for(;;){
for(j=0; j<1250; j++){
OC1R = j;

if(j==1249){
j=0;
}
}
}
}
}

Thanks for your time and wish you the best for 2021!
RF Can
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top