Ishah
Member level 1
- Joined
- Mar 13, 2013
- Messages
- 33
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1,286
- Activity points
- 1,502
Hi,
Im using a PIC16F877A with L298 to control a 12V DC Motor.
I want to control the speed of the motor by PWM.
I already draw a circuit with external OSC 20MHz and some MicroC code.
Can anyone checking on it and gives
some guidelines or code (MicroC) on how to program a PIC16F877A.
Thanks.
Im using a PIC16F877A with L298 to control a 12V DC Motor.
I want to control the speed of the motor by PWM.
I already draw a circuit with external OSC 20MHz and some MicroC code.
Can anyone checking on it and gives
some guidelines or code (MicroC) on how to program a PIC16F877A.
Thanks.
Code:
unsigned short current_duty, old_duty, current_duty1, old_duty1;
//define motor1 ports
sbit motor1a at RD0_bit;
sbit motor1b at RD1_bit;
//define motor2 ports
sbit motor2a at RD2_bit;
sbit motor2b at RD3_bit;
//define controller 1 ports
#define increase PORTB.F1
#define decrease PORTB.F0
#define forward PORTB.F2
#define reverse PORTB.F3
void InitMain() {
TRISB = 0XFF; // configure PORTB pins as input
PORTB = 0;
TRISD = 0X00; // configure PORTD pins as output
PORTD = 0;
TRISC = 0XFF; // configure PORTC pins as input
PORTC = 1;
CMCON=0X07; // or CMCON register = 0X00000111
ADCON1=0X07; // Configure ADCON1, or ADCON1=0X06;
CCP1CON = 12; //PWM mode = 0;
PORTD = 255; //100% PWM duty ratio
TRISD = 255;
PWM1_Init(5000); // Initialize PWM1 module at 5KHz
PWM2_Init(5000); // Initialize PWM2 module at 5KHz
}
void main() {
InitMain();
current_duty = 0X00; // initial value for current_duty
current_duty1 = 0X00; // initial value for current_duty1
PWM1_Start(); // start PWM1
PWM2_Start(); // start PWM2
PWM1_Set_Duty(current_duty); // Set current duty for PWM1
PWM2_Set_Duty(current_duty1); // Set current duty for PWM2
while (1) { // endless loop
if (forward == 1) { // button on RB0 pressed
motor1a = 0;
motor1b = 1; //Motor Forward
Delay_ms(500); //1 Second Delay
current_duty++; // increment current_duty
PWM1_Set_Duty(current_duty);
}
if (reverse == 1) { // button on RB1 pressed
motor1a = 1;
motor1b = 0; //Motor Reverse
Delay_ms(30); //1 Second Delay
Delay_ms(40);
current_duty--; // decrement current_duty
PWM1_Set_Duty(current_duty);
}
if (increase == 1) { // button on RB2 pressed
Delay_ms(40);
current_duty1++; // increment current_duty1
PWM2_Set_Duty(current_duty1);
}
if (decrease == 1) { // button on RB3 pressed
Delay_ms(40);
current_duty1--; // decrement current_duty1
PWM2_Set_Duty(current_duty1);
}
Delay_ms(5); // slow down change pace a little
}
}