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] Driving DC Motor with PIC 16F876/16F877

Status
Not open for further replies.

SASSI Sam

Newbie level 6
Joined
Nov 18, 2013
Messages
11
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,283
Activity points
1,356
Hi every body, this code with MikroC to drive a DC Motor in 2 directions
void main() {
short duty = 0; //initial value for duty
TRISC = 0x00; //PORTC as output
TRISB = 0x00; //PORTB as output
portb = 0x00; // init
PWM1_Init(5000); //Initialize PWM1
PWM1_Start(); //start PWM1
PWM1_Set_Duty(duty); //Set current duty for PWM1
while (1) // endless loop

{
PORTB = 0x01; // 1st direction given through RB0
Delay_ms(10);
duty = 255; //max duty
PWM1_Set_Duty(duty);
delay_ms(5000);
PWM1_STOP();

portb=0;
delay_ms(5000); //wait until Motor stop before changing direction..

PORTB = 0x02; // 2nd direction given through RB1
Delay_ms(10);
duty = 255; //max duty
PWM1_Set_Duty(duty);
delay_ms(5000);
PWM1_STOP();
}
}
 

Attachments

  • schema.jpg
    schema.jpg
    50.7 KB · Views: 107

Try this..
Code:
{
///////////////// Dirction 1////////////////////
PORTB = 0x0C; // 1st direction given through RB0
Delay_ms(10);
duty = 255; //max duty
PWM1_Set_Duty(duty);
delay_ms(5000);

PORTB = 0x06; // 1st direction given through RB0
Delay_ms(10);
duty = 255; //max duty
PWM1_Set_Duty(duty);
delay_ms(5000);

PWM1_STOP();
///////////////////////////////////////////

portb=0;
delay_ms(5000); //wait until Motor stop before changing direction..


///////////////// Direction 2/////////////////

PORTB = 0x03; // 2nd direction given through RB1
Delay_ms(10);
duty = 255; //max duty
PWM1_Set_Duty(duty);
delay_ms(5000); 

PORTB = 0x09; // 1st direction given through RB0
Delay_ms(10);
duty = 255; //max duty
PWM1_Set_Duty(duty);
delay_ms(5000);
PWM1_STOP();
////////////////////////////////////
}
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top