Mình đang làm mạch điều khiển động cơ DC, nhưng khi mô phỏng với Proteus ko chạy. Ai bjt lỗi gì ko?
Mô phỏng Proteus: http://www.mediafire.com/?tkk2cghepz34vp5
CODE:
#include <16F877A.h>
#fuses NOWDT,PUT,HS,NOPROTECT,NOLVP
#use delay(clock=10000000)
#use fast_io(b)
#define tocdo0 0 //toc do bang 0
#define td_macdinh 80 //toc do mac dinh
#define tocdo1 124 //1/5 toc do cuc dai
#define tocdo2 248 //2/5 toc do cuc dai
#define tocdo3 372 //3/5 toc do cuc dai
#define tocdo4 496 //4/5 toc do cuc dai
#define tocdo5 620 //toc do cuc dai
#byte PORTB =0x06
int16 duty1=0;
int16 duty2=0;
int8 bientocdo=0;
int1 bienthuan=0;
int1 biennghich=0;
void main() //bo PMW1 dk dong o quay thuan
{ //bo PMW2 dk dco quay nghich
set_tris_b(0x0ff); //khoi tao port B la port nhap
setup_ccp1(CCP_PWM); //khoi tao bo PWM1
setup_ccp2(CCP_PWM); //khoi tao bo PWM2
setup_timer_2(T2_div_by_16,154,1); //tao chu ky xung 1ms
//(1/clock)*4*t2div*(period+1)
//(1/10e6)*4*16*(154+1)=1e-3=1ms
set_pwm1_duty(duty1);
set_pwm2_duty(duty2);
//gia tri duty cua PWM duoc tinh theo cong thuc duty1*t2div/clock
//khoi tao duty=0 -> tat dong co
while(true)
{
if (!bit_test(portb,1)) //neu nhan phim FO : chay thuan
{
duty1=tocdo0;
duty2=tocdo0;
set_pwm1_duty(duty1); //cho duty cua 2 bo PMW=0 de
set_pwm2_duty(duty2); //tat dong co
delay_ms(1000); //delay 1s de giam quan tinh dong co
bienthuan=1;
biennghich=0;
goto next;
}
if (!bit_test(portb,2)) //neu nhan phim RE : chay nghich
{
duty1=tocdo0;
duty2=tocdo0;
set_pwm1_duty(duty1); //cho duty cua 2 bo PMW=0 de
set_pwm2_duty(duty2); //tat dong co
delay_ms(1000); //delay 1s de giam quan tinh dco
bienthuan=0;
biennghich=1;
goto next;
}
if (!bit_test(portb,3)) //nhan phim toc do 1
{
bientocdo=1;
goto next;
}
if (!bit_test(portb,4)) //nhan phim toc do 2
{
bientocdo=2;
goto next;
}
if (!bit_test(portb,5)) //nhan phim toc do 3
{
bientocdo=3;
goto next;
}
if (!bit_test(portb,6)) //nhan phim toc do 4
{
bientocdo=4;
goto next;
}
if (!bit_test(portb,7)) //nhan phim toc do 5
bientocdo=5;
next:
if (!bit_test(portb,0)) //neu nhan phim OFF
{
duty1=tocdo0;
duty2=tocdo0;
set_pwm1_duty(duty1); //cho duty cua 2 bo PMW=0 de
set_pwm2_duty(duty2); //tat dong co
bienthuan=0;
biennghich=0;
bientocdo=0; //xoa bien toc do
}
if (bienthuan)
{
duty2 = tocdo0;
switch (bientocdo)
{
case 1: duty1= tocdo1;
break;
case 2: duty1= tocdo2;
break;
case 3: duty1= tocdo3;
break;
case 4: duty1= tocdo4;
break;
case 5: duty1= tocdo5;
break;
default : duty1 = td_macdinh;
break;
}
set_pwm1_duty(duty1); //cho dco chay thuan
set_pwm2_duty(duty2);
}
if (biennghich)
{
duty1 = tocdo0;
switch (bientocdo)
{
case 1: duty2= tocdo1;
break;
case 2: duty2= tocdo2;
break;
case 3: duty2= tocdo3;
break;
case 4: duty2= tocdo4;
break;
case 5: duty2= tocdo5;
break;
default : duty2 = td_macdinh;
break;
}
set_pwm1_duty(duty1); //cho dco chay nghich
set_pwm2_duty(duty2);
}
}
}
Mô phỏng Proteus: http://www.mediafire.com/?tkk2cghepz34vp5
CODE:
#include <16F877A.h>
#fuses NOWDT,PUT,HS,NOPROTECT,NOLVP
#use delay(clock=10000000)
#use fast_io(b)
#define tocdo0 0 //toc do bang 0
#define td_macdinh 80 //toc do mac dinh
#define tocdo1 124 //1/5 toc do cuc dai
#define tocdo2 248 //2/5 toc do cuc dai
#define tocdo3 372 //3/5 toc do cuc dai
#define tocdo4 496 //4/5 toc do cuc dai
#define tocdo5 620 //toc do cuc dai
#byte PORTB =0x06
int16 duty1=0;
int16 duty2=0;
int8 bientocdo=0;
int1 bienthuan=0;
int1 biennghich=0;
void main() //bo PMW1 dk dong o quay thuan
{ //bo PMW2 dk dco quay nghich
set_tris_b(0x0ff); //khoi tao port B la port nhap
setup_ccp1(CCP_PWM); //khoi tao bo PWM1
setup_ccp2(CCP_PWM); //khoi tao bo PWM2
setup_timer_2(T2_div_by_16,154,1); //tao chu ky xung 1ms
//(1/clock)*4*t2div*(period+1)
//(1/10e6)*4*16*(154+1)=1e-3=1ms
set_pwm1_duty(duty1);
set_pwm2_duty(duty2);
//gia tri duty cua PWM duoc tinh theo cong thuc duty1*t2div/clock
//khoi tao duty=0 -> tat dong co
while(true)
{
if (!bit_test(portb,1)) //neu nhan phim FO : chay thuan
{
duty1=tocdo0;
duty2=tocdo0;
set_pwm1_duty(duty1); //cho duty cua 2 bo PMW=0 de
set_pwm2_duty(duty2); //tat dong co
delay_ms(1000); //delay 1s de giam quan tinh dong co
bienthuan=1;
biennghich=0;
goto next;
}
if (!bit_test(portb,2)) //neu nhan phim RE : chay nghich
{
duty1=tocdo0;
duty2=tocdo0;
set_pwm1_duty(duty1); //cho duty cua 2 bo PMW=0 de
set_pwm2_duty(duty2); //tat dong co
delay_ms(1000); //delay 1s de giam quan tinh dco
bienthuan=0;
biennghich=1;
goto next;
}
if (!bit_test(portb,3)) //nhan phim toc do 1
{
bientocdo=1;
goto next;
}
if (!bit_test(portb,4)) //nhan phim toc do 2
{
bientocdo=2;
goto next;
}
if (!bit_test(portb,5)) //nhan phim toc do 3
{
bientocdo=3;
goto next;
}
if (!bit_test(portb,6)) //nhan phim toc do 4
{
bientocdo=4;
goto next;
}
if (!bit_test(portb,7)) //nhan phim toc do 5
bientocdo=5;
next:
if (!bit_test(portb,0)) //neu nhan phim OFF
{
duty1=tocdo0;
duty2=tocdo0;
set_pwm1_duty(duty1); //cho duty cua 2 bo PMW=0 de
set_pwm2_duty(duty2); //tat dong co
bienthuan=0;
biennghich=0;
bientocdo=0; //xoa bien toc do
}
if (bienthuan)
{
duty2 = tocdo0;
switch (bientocdo)
{
case 1: duty1= tocdo1;
break;
case 2: duty1= tocdo2;
break;
case 3: duty1= tocdo3;
break;
case 4: duty1= tocdo4;
break;
case 5: duty1= tocdo5;
break;
default : duty1 = td_macdinh;
break;
}
set_pwm1_duty(duty1); //cho dco chay thuan
set_pwm2_duty(duty2);
}
if (biennghich)
{
duty1 = tocdo0;
switch (bientocdo)
{
case 1: duty2= tocdo1;
break;
case 2: duty2= tocdo2;
break;
case 3: duty2= tocdo3;
break;
case 4: duty2= tocdo4;
break;
case 5: duty2= tocdo5;
break;
default : duty2 = td_macdinh;
break;
}
set_pwm1_duty(duty1); //cho dco chay nghich
set_pwm2_duty(duty2);
}
}
}
Comment