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.

[PIC] Trouble generating PWM using Pic 16F1503

Status
Not open for further replies.

rocky79

Member level 5
Joined
Sep 20, 2010
Messages
83
Helped
1
Reputation
2
Reaction score
0
Trophy points
1,286
Activity points
1,903
I am trying to send a PWM signal on PortC5 (PWM1) using the latest CCS compiler(5.026).
This is the manual method of sending a PWM and it doesn't seem to be working.
I have followed the PWM Setup and generation on page 221 of PIC16f1503 datasheet. I just couldn't get it to work. Anyone have anyidea what else I should be doing?



Code:
#include <16F1503.h>
#fuses   INTRC_IO, NOWDT, NOPROTECT, NOMCLR
#use     delay(clock = 4000000)
#USE     FAST_IO (C)
//----------------------------------- ---------
//GPIO BIT ASSIGNMENT FOR I/O REGISTERS PORT A
//--------------------------------------------
//address of the TRISIO registe
#byte   TRISC = getenv("SFR:TRISC") 
#byte   PORTC = getenv("SFR:PORTC")
#bit    PORTC_1=PORTC.1

#word   ANSELC = getenv("SFR:ANSELC")   

// setting up PWM parameters
#byte PR2= getenv("SFR:PR2")
#byte T2CON=getenv("SFR:T2CON")

#word PWM1CON= getenv("SFR:PWM1CON") 
#word PWM1DCH=getenv("SFR:PWM1DCH") 
#word PWM1DCL=getenv("SFR:PWM1DCL") 
#bit  PWM1DCL_7=PWM1DCL.7
#bit  PWM1DCL_6=PWM1DCL.6       
                            

#byte PIR1=getenv("SFR:PIR1")  
#bit PIR1_1=PIR1.1           

int16 duty;
#bit duty_0=duty.0
#bit duty_1=duty.1

void pwm(int16 value);

void main()
{
int16 i;
//----------------------manual PWM config--------------------------------
   //SET ALL PORT C PIN DIGITAL
   ANSELC=0x0;
   //Disable the PWM pin (CCPx) output driver(s) by
   //setting the associated TRIS bit(s) to INPUT
   //TRISC= 0b00111111;                        
   //TRISC= 0x0;//OUTPUT
   //Clear the PWM1CON register.
   PWM1CON=0x0; 
   //Load the PR2 register with the PWM period value.
   PR2=255; // 255hz   
   //Clear the PWMxDCH register and bits <7:6> of
   //the PWMxDCL register.
   //Load the CCPRxL register and the DCxBx bits of
   //the CCPxCON register, with the PWM duty cycle
   //value.
   duty = 0;                   // set duty cycle to 0
   //Clear the PWMxDCH register and bits <7:6> of
   //the PWMxDCL register.
   PWM1DCH=0x0;
   PWM1DCL_6 = 0;         // Store duty to registers as
   PWM1DCL_7 = 0;         // a 10-bit word
    delay_ms(5);
   //Configure and start Timer2:
   //clear the TMR21F interrupt flag bit of PIR1 register
   PIR1_1=0;
   delay_ms(5);
   //START TIMER2 Set prescaler(64) AND POSTSCALER
   T2CON=0b00000111;
   //-----------------------------------------------
   //Configure the T2CKPS bits of the T2CON register
   //with the Timer2 prescale value.
   //-----------------------------------------------
   //0000 =1:1 Postscaler
   //1    = Timer2 is on                           
   //10 = Prescaler is 16
   //11 = Prescaler is 64
   //------------------------------------------------
   //------------------------------------------------
   //Enable PWM output pin and wait until Timer2
   //overflows, TMR2IF bit of the PIR1 register is set.
   //------------------------------------------------
   PWM1CON=0b10000000; 

   wait_for_TMR2IF:            
   if (PIR1_1 == 0)
     {
     goto wait_for_TMR2IF;
     }    
   //Enable the PWMx pin output driver(s) by clearing
   //the associated TRIS bit(s).
   TRISC=0x0;
   //and setting PWM1OE bit of the PWM1CON registeR
   PWM1CON=0b11000000;
   //SET A PWM VALUES To START
   duty=0;
   PWM1DCL_6 = duty_0;    // Store duty to registers as
   PWM1DCL_7 = duty_1;    // a 10-bit word
   PWM1DCH = duty >> 2;    // store the remaining low byte}
                
    while(1) 
    {
 
    for(i=0;i<1020;++i)
        {
        //Output a PWM wave
       //set_pwm1_duty(i);
       pwm(i);
       delay_ms(3);
        }
           
     }     
}

void pwm(int16 value)
{
   //int16 value;
   duty=value;
   PWM1DCL_6 = duty_0;    // Store duty to registers as
   PWM1DCL_7 = duty_1;    // a 10-bit word
   PWM1DCH = duty >> 2;    // store the remaining high byte}
}
 
Last edited:

No one has experienced this issue before on the 16F1503?

I usually have no issue in generating PWM, but this chip is giving me trouble.

I have followed the instructions in section" 22.1.9 SETUP FOR PWM OPERATION" one by one. Are there anything else that I need to set beside the instructions on the datasheet?
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top