• 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.

PIC18f4550 CCP1 Module set in PWM not working

Status
Not open for further replies.

Cantafford

Member
Hello,

I'm trying to set the CCP1 module of the PIC18f4550 microcontroller in PWM mode to control a DC motor.
I followed the instructions for setting the module in the PWM mode:
Code:
1. Set the PWM period by writing to the PR2
register.
2. Set the PWM duty cycle by writing to the
CCPRxL register and CCPxCON<5:4> bits.
3. Make the CCPx pin an output by clearing the
appropriate TRIS bit.
4. Set the TMR2 prescale value, then enable
Timer2 by writing to T2CON.
5. Configure the CCPx module for PWM operation.
So I wrote the following code:
Code:
/*
 * File:   main.c
 * Author: Paul
 *
 * Created on June 20, 2016, 7:36 PM
 */

#include <stdio.h>
#include <stdlib.h>
#include "header.h"

void main()
{
    //OSCCON = 0x7F; // internal 8Mhz oscillator
    TRISAbits.RA0 = 0; TRISAbits.RA1 = 0; // for direction
    TRISCbits.RC2 = 0; // CCP1CON set as output

    LATAbits.LATA0 = 1; LATAbits.LATA1 = 0; // arbitrary direction
   
    while(1)
    {
     PR2 = 0b01010010 ;
     T2CON = 0b00000111 ;
     CCPR1L = 0b01010010 ;
     CCP1CON = 0b00111100 ;
    }
}
Now when I simulate this...the motor runs with a fixed speed. Doesn't matter what I write in the endless while(1) loop the motor will have the same speed. Even if I don't write anything at all in the while(1) loop the motor will still have that same speed. No matter what values I put in there the motor will run with same speed it will ignore the pwm I set. I mention that I'm using an online calculator to set the motor's speed.

This is an easy task. But I took a break from programming for like 2 months and like always after taking a break I always find myself having difficulties in solving the basic simple tasks. So please help me identify the problem and be able to modify the DC motor speed via CCP1Con module. Thank you.
 

Ian Rogers

User Extraordinaire
Forum Supporter
Most Helpful Member
I'm sure you understand the PR2 / CPPRxL relation ship.... If the CCPRxL is equal or higher than PR2, the speed will be constant!!
When I do PWM I almost always set PR2 to 255, Then I can have the full 10 bits.... I set the FOSC and TIMER2 to suit!!
 

Jon Wilder

Active Member
You don't need nor would you want those statements in the while loop. They should go before the while loop as they only need to run once.

Code:
    TRISCbits.RC2 = 0;      // RC2 PWM output
    PR2 = 52;
    T2CON = 0b00000111;
    CCPR1L = 52;
    CCP1CON = 0b00001100;

    while(1);
Since PR2 = 52, you don't want to write a value greater than 52 to CCPR1L (the value written to PR2 sets the duty cycle high limit in conjunction with the PWM period). Varying CCPR1L between 0-52 should manipulate the speed of the motor.
 
Last edited:

Cantafford

Member
Hello guys,

I solved the problem sorry I forgot to post here to close the thread. The problem was the ADC's register that you have to modify in order to set the A/D channels to be either digital or analog. They are analog by default :D and I always forget about that register because not all MCUs have it. That solved my problem. Thank you for the help.
 
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading
Top