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:
So I wrote the following code:
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.
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.