Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

PWM3 not working in PIC12F1572

Status
Not open for further replies.

FrustratedEngineer

Newbie level 6
Joined
Nov 30, 2015
Messages
12
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
122
I have to simply o/p PWM (50% duty cycle; any frequency will do as of now) on RA2 of the PIC12F1572.
I have generated the code using MCC. But, when I tried observing the o/p at RA2 by connecting LED, I dont see LED turning on.
I have tried debugging the code, but PWM isr is never hit.
Any suggestions will be appreciated.

Code:
void PWM3_Initialize(void)
{
    // set the PWM3 to the options selected in the User Interface

     //PHIE disabled; DCIE disabled; OFIE disabled; PRIE disabled; 
    PWM3INTE = 0x00;

     //PHIF cleared; OFIF cleared; DCIF cleared; PRIF cleared; 
    PWM3INTF = 0x00;

     //PS No_Prescalar; CS FOSC; 
    PWM3CLKCON = 0x00;

     //LDS reserved; LDT disabled; LDA do_not_load; 
    PWM3LDCON = 0x00;

     //OFM independent_run; OFS reserved; OFO match_incrementing; 
    PWM3OFCON = 0x00;

     //PWM3PHH 0; 
    PWM3PHH = 0x00;

     //PWM3PHL 0; 
    PWM3PHL = 0x00;

     //PWM3DCH 3; 
    PWM3DCH = 0x03;

     //PWM3DCL 32; 
    PWM3DCL = 0x20;

     //PWM3PRH 6; 
    PWM3PRH = 0x06;

     //PWM3PRL 63; 
    PWM3PRL = 0x3F;

     //PWM3OFH 0; 
    PWM3OFH = 0x00;

     //PWM3OFL 1; 
    PWM3OFL = 0x01;

     //PWM3TMRH 0; 
    PWM3TMRH = 0x00;

     //PWM3TMRL 0; 
    PWM3TMRL = 0x00;
    
     //MODE standard_PWM; POL active_hi; OE enabled; EN enabled; 
    PWM3CON = 0xC0;
    // Clear the PWM3 interrupt flag
    PIR3bits.PWM3IF = 0;
        
    // Enabling PWM3 interrupt.
    PIE3bits.PWM3IE = 1;
    PWM3_DutyCycleSet(1000);
    PWM3_Start();
}    


void PWM3_Start(void)
{
    PWM3CONbits.EN = 1;		
}

void PWM3_Stop(void)
{
    PWM3CONbits.EN = 0;		
}

bool PWM3_CheckOutputStatus(void)
{
    return (PWM3CONbits.OUT);		
}

void PWM3_LoadBufferSet(void)
{
    PWM3LDCONbits.LDA = 1;		
}

void PWM3_PhaseSet(uint16_t phaseCount)
{
    PWM3PHH = (phaseCount>>8);        //writing 8 MSBs to PWMPHH register
    PWM3PHL = (phaseCount);           //writing 8 LSBs to PWMPHL register
}

void PWM3_DutyCycleSet(uint16_t dutyCycleCount)
{
    PWM3DCH = (dutyCycleCount>>8);	//writing 8 MSBs to PWMDCH register
    PWM3DCL = (dutyCycleCount);	//writing 8 LSBs to PWMDCL register		
}

void PWM3_PeriodSet(uint16_t periodCount)
{
    PWM3PRH = (periodCount>>8);	//writing 8 MSBs to PWMPRH register
    PWM3PRL = (periodCount);	//writing 8 LSBs to PWMPRL register		
}

void PWM3_OffsetSet(uint16_t offsetCount)
{
    PWM3OFH = (offsetCount>>8);	//writing 8 MSBs to PWMOFH register
    PWM3OFL = (offsetCount);	//writing 8 LSBs to PWMOFL register		
}

uint16_t PWM3_TimerCountGet(void)
{
    return ((uint16_t)((PWM3TMRH<<8) | PWM3TMRL));       		
}

bool PWM3_IsOffsetMatchOccured(void)
{
    return (PWM3INTFbits.OFIF);		
}

bool PWM3_IsPhaseMatchOccured(void)
{
    return (PWM3INTFbits.PHIF);	
}

bool PWM3_IsDutyCycleMatchOccured(void)
{
    return (PWM3INTFbits.DCIF);		
}

bool PWM3_IsPeriodMatchOccured(void)
{
    return (PWM3INTFbits.PRIF);		
}

void PWM3_ISR(void)
{   
    PIR3bits.PWM3IF = 0;
}


void PIN_MANAGER_Initialize(void)
{
    /**
    LATx registers
    */   
    LATA = 0x00;    

    /**
    TRISx registers
    */    
    TRISA = 0x3B;

    /**
    ANSELx registers
    */   
    ANSELA = 0x17;

    /**
    WPUx registers
    */ 
    WPUA = 0x3F;
    OPTION_REGbits.nWPUEN = 0;

    /**
    ODx registers
    */   
    ODCONA = 0x00;
    
    /**
    APFCONx registers
    */
    APFCON = 0x00;   
    
}
 

It may help to place a second led at the opposite supply rail than your first one uses. Then you can observe what is the polarity of your output pin, from one moment to the next.

Each led needs to have a resistor in line for safety purpose to limit current. A suitable amount is 10-15 mA.

If you see the led's dimly lit at the same time, it might imply that both are flashing alternately at high speed.
 
See if you have to set (enable) PEIE bit and GIE bit of INTCON register as they are needed. GIE is global interrupt enable bit.
 
PWM is working fine... I checked with oscilloscope. the LED was dry-soldered so was not turning ON.

PWM3PRH/L -> changing the values in the reg pair changes the Frequency of PWM.

But, PWM3DCH/L -> Changing the values in the reg pair doesn't change the duty cycle. I have also set LDA and LDT =1. But, duty is const 50%.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top