[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
    50.7 KB · Views: 110

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.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…