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.

Dc motor control with pic microcontroller ccs compiler

Status
Not open for further replies.

javy87

Newbie level 1
Joined
Feb 20, 2013
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,287
How to control a dc motor with pic usin ccs compiler ? what are the basics i have to know ?
I have the basics of programming in ccs, and i know how a dc motor works, but i don't know how to control it with pic microcontrollers.
how necessary is to use the technique of pwm to control a dc motor? is really necessary use of pwm ?

Thanls
 

To control the speed, PWM is the easy and efficient method. Here you can find a circuit and an example code for DC motor control with PIC16F877A, using PWM. While the program has been written for mikroC compiler, you should have no problem in "porting" it to CCS.

https://www.edaboard.com/blog/1812/

From there:
Code:
//-------------------------------------------------------------------------------------------------------

//Programmer: Syed Tahmid Mahbub
//Compiler: mikroC PRO for PIC v4.60

unsigned int ADR;

void main() {
     PORTC = 0;
     TRISC = 0; //all PORTC pins output
     PORTA = 0;
     TRISA = 0x01; //RA0 input for pot
     CMCON = 7; //Disable analog comparators
     T2CON = 0; //Prescaler 1:1, TMR2 off
     ADCON1 = 14; //AN0 only analog
     ADC_Init(); //mikroC initializes ADC
     CCP1CON = 12; //PWM mode
     PR2 = 249; //16kHz PWM frequency
     while (1){
           ADR = ADC_Get_Sample(0) >> 2; //Get ADC reading for channel 0
           //and divide reading by 4
           if (ADR < 10){ //Setting lower bound
              TMR2ON_bit = 0; //Stop Timer and so stop PWM
              TMR2 = 0; //Clear Timer
              RC2_bit = 0; //Clear PWM output
           }
           else{
                if (ADR > 240){ //Setting upper bound
                   TMR2ON_bit = 0; //Stop Timer and so stop PWM
                   TMR2 = 0; //Clear Timer
                   RC2_bit = 1; //Keep PWM output high
                }
                else{ //Within upper and lower bounds
                      CCPR1L = ADR; //Set PWM duty cycle
                      TMR2ON_bit = 1; //If not on, turn on TMR2. If on, keep on
                }
           }
     }
}

//-------------------------------------------------------------------------------------------------------

Hope this helps.
Tahmid.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top