السلام عليكم ورحمة الله وبركاته
اخواني الاعزاء
اقدم اليكم اليوم مشروعي وهو التحكم بسرعة الانكودر بواسطة عدد الpulse التي يملكها وقد عملت هذا المشروع بواسطة المايكرو سي
واشكر اعضاء هذا المنتدى لمساهمتهم بانجاز هذا المشرع
unsigned int count=0; // This count use in interrupt routine
unsigned countOld = 0; // Counter for the previous time
unsigned int dutyCycle =100;
unsigned int difference;
//float speed;
void interrupt() // enter when RB0 Falling
{
//ing B0 and B1, Position and Direction Find
//{
count++; // If "+" direction
//}
//else // If "-" direction
//{
//count--;
//}
INTCON.INTF=0; // Clear flag
}
void main()
{
TRISB = 0xff;
TRISA = 0;
PORTA = 0; // turn all LEDs OFF
TRISC = 0; // Set PORTC to $FF
PORTC = 0xff; // PORTC is output
INTCON=0b11010000; // enable RB0 interrupts
OPTION_REG=0b01000000; // Enable portB internal PULL-UP resistors
// And Rising edge of RB0 int
PWM1_Init(20000);
PWM1_Start();
PWM1_Set_Duty(dutyCycle); // Whatever speed
while(1){
countOld = count;
Delay_ms(1000);
difference = count - countOld;
if (difference < 3000) // xceeds 4192(2 round), Stop
{
PORTA = 0;
if(dutyCycle < 254)
dutyCycle++;
}
else{
PORTA = 0xff;
if(dutyCycle > 0)
dutyCycle--;
}
PWM1_Set_Duty(dutyCycle);
}
}