السلام عليكم
اريد مساعدة في كتابة برنامج خاص بالتحكم بسرعة dc motor بواسطة PI(Proportional, Integral Controllers ) لمعالج PIC16F877A وبواسطة برنامج mikroC PRO for PIC
وعندي هذا البرنامج لكنه خاص بالمعالج ATMEGA 8 ووجدته في مشروع الطالب علا جزماتي من سوريا وهذا رابط للمشروع لرؤيته في ملف pdf:
http://www.4shared.com/document/gAvC8mdi/__online.html وأريد من الاخوة المساعدة في كتابته لمعالج PIC16F877A لأنني جديد في البرمجة بلغة C
وهذا هو الكود الذي أريد تعديله :
كود:
#include <mega8.h>
#include <stdio.h>
int time_index=0;
float a , alpha,Ua ;
float oldalpha=127; // 50% duty cycle
float Kv=1.7587;
float Kpv=0.3645;
float Kiv= 254.5755;
int counter=0;
float s_rpm , current_s_rpm ,error_star,Wref_star;
float Wref_star_old =0;
float Wref = 209;
float error=0;
float error_old=0;
float TPWM =0.00025; ///1/4KHZ
interrupt [EXT_INT0] void ext_int0_isr(void)
{
++counter;
if (TCNT0==0x06)
{ current_s_rpm = 1.07 * counter; //// speed = {1 RPM * 60 * counter } / 56
counter=0; }
}
interrupt [TIM0_OVF] void timer0_ovf_isr(void)
{
TCNT0=0x06;
if(time_index++==500){
s_rpm = current_s_rpm ;
time_index=0;}
}
/// clk/64=125 KHZ ==> T= 8 micro second
// 8 micro second * 256(max value for Timer0) = 2.05 ms
// ==> we will take only 250 ==> 256‐250 = 6 = TCNT0
// 8 micro second * 250 = 2 ms ==> time_index=500
// 2ms * 500 = 1 second
void main(void)
{
// Input/Output Ports initialization
// Port B initialization
// Func0=In Func1=Out Func2=Out Func3=In Func4=In Func5=In Func6=In Func7=In
// State0=T State1=0 State2=0 State3=T State4=T State5=T State6=T State7=T
PORTB=0x00;
DDRB=0x06;
// Port C initialization
// Func0=In Func1=In Func2=In Func3=In Func4=In Func5=In Func6=In
// State0=T State1=T State2=T State3=T State4=T State5=T State6=T
PORTC=0x00;
DDRC=0x00;
// Port D initialization
// Func0=In Func1=In Func2=In Func3=In Func4=In Func5=In Func6=In Func7=In
// State0=T State1=T State2=T State3=T State4=T State5=T State6=T State7=T
PORTD=0x00;
DDRD=0x00;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 125.000 kHz
TCCR0=0x03;
TCNT0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 8000.000 kHz
// Mode: Fast PWM top=00FFh
// OC1A output: Non‐Inv.
// OC1B output: Inverted
// Noise Canceler: Off
// Input Capture on Falling Edge
TCCR1A=0xB1;
TCCR1B=0x09;
TCNT1H=0x00;
TCNT1L=0x00;
OCR1AH=0x00;
OCR1AL=0x7F;
OCR1BH=0x00;
OCR1BL=0x7F;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
TCCR2=0x00;
ASSR=0x00;
TCNT2=0x00;
OCR2=0x00;
// External Interrupt(s) initialization
// INT0: On
// INT0 Mode: Low level
// INT1: Off
GICR=0x40;
MCUCR=0x00;
GIFR=0x40;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x01;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
// Analog Comparator Output: Off
ACSR=0x80;
SFIOR=0x00;
// Global enable interrupts
#asm("sei")
while (1)
{
error= Wref ‐ s_rpm;
Wref_star = Wref_star_old + Kpv*error_old + Kiv*TPWM*error_old; // pi
error_star = Wref_star ‐ s_rpm ;
Ua = Kv*error_star ; // p
Wref_star_old = Wref_star ;
error_old = error ;
a = 0.083333 *Ua ; // a = Ua /12
alpha = 255 *a; // 8 bit pwm
oldalpha = alpha ;
OCR1AH=0x00;
OCR1AL=oldalpha;
OCR1BH=0x00;
OCR1BL=oldalpha; };}
الشكل التالي يوضح الدارة العملية التي تمت محاكاتها بواسطة برنامج 7 Proteus :