View Single Post
Old 12-02-2015, 10:45 AM   #81
saunai
Nhập môn đệ tử
 
Tham gia ngày: Sep 2014
Bài gửi: 5
:
Nhờ cao thủ check dùm đoạn code đk động cơ. THank!!
#include <16F877A.h>
#include <def_877a.h>
#fuses NOWDT,PUT,HS,NOPROTECT,NOLVP
#use delay(clock=20000000)
#include <lcd_lib_4bit.c>
#use fast_io(a)
#use fast_io(b)
#use fast_io(d)

#define tocdo0 0
#define td_macdinh 10
#define tocdo1 100
#define tocdo2 150
#define tocdo3 200
#define tocdo4 250
#define tocdo5 300


int16 bientocdo=0;
INT16 COUNT=0;
INT8 SOLANLAP=0;
int8 nghin,tram,chuc,donvi;
int16 bientocdodat=0;
int16 duty1=0;
int16 duty2=0;
INT16 TOCDOENCODER=100;
INT16 SOXUNGDEM_1PHUT=0;
//int16 x;
int1 bienthuan=0;
int1 biennghich=0;
INT chieu;

#INT_RTCC // Ngat khi timer0 tran

VOID TIMER0() // tao bo dem timer0 dem so xung vao.............RA4
{
COUNT++;
}

#INT_TIMER1

VOID TIMER1() // dinh thoi 1s bang timer1 va tinh toan toc do....
{
set_timer1(15535); // timer 1 se dem 50 000 lan phuc vu cho dinh thoi 1S

solanlap=solanlap+1;

if (solanlap==25)
{
SOXUNGDEM_1PHUT=(((COUNT*255)+GET_TIMER0())*60)/TOCDOENCODER;
SOLANLAP=0;
SET_TIMER0(0);
COUNT=0;
}

}

void tinhtoan_hienthi(int16 x) // thuc hien tach so va dua ra hien thi
{
nghin = x / 1000 ;
x = x % 1000;
tram = x / 100 ; // tách so hàng tram
x = x % 100; // chia lay phan du
chuc = x / 10 ; // tách lay phan chuc
donvi = x % 10 ; // phan don vi

if (chieu==1) lcd_putchar('+');
else lcd_putchar('-');
lcd_putchar(nghin + 0x30);
lcd_putchar(tram+ 0x30);
lcd_putchar(chuc+ 0x30);
lcd_putchar(donvi+ 0x30);
}


void main(void)
{
set_tris_a(0x0ff);
set_tris_b(0x00);
set_tris_d(0xFF);
lcd_putcmd(0x80);
lcd_init();
delay_ms(5);
setup_timer_0 (RTCC_DIV_1|RTCC_EXT_H_TO_L); // Timer0 is Counter
set_timer0(0);
setup_timer_1(T1_INTERNAL | T1_DIV_BY_4); // Timer1 is Timer
set_timer1(12140);
enable_interrupts(INT_RTCC);
enable_interrupts(INT_TIMER1);
enable_interrupts(GLOBAL);
count = 0;
setup_ccp1(CCP_PWM); //khoi tao bo PWM1
setup_ccp2(CCP_PWM); //khoi tao bo PWM2

setup_timer_2(T2_div_by_16,255,1); //tao chu ky xung Tpwm =(PR2+1)*4*Tosc*N = (255+1)*4*(1/20Mhz)*16 = 0.8 ms


set_pwm1_duty(duty1); //gia tri duty cua PWM duoc tinh
set_pwm2_duty(duty2); //theo cong thuc duty1*t2div/clock
//khoi tao duty=0 ->tat dong co
while(true)
{
if (!bit_test(portD,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 dc
bienthuan=1;
biennghich=0;
goto next;
}

if (!bit_test(portD,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 dc
bienthuan=0;
biennghich=1;
goto next;
}

if (!bit_test(portD,3)) //nhan phim toc do 1
{
bientocdo=1;
goto next;
}

if (!bit_test(portD,4)) //nhan phim toc do 2
{
bientocdo=2;
goto next;
}

if (!bit_test(portD,5)) //nhan phim toc do 3
{
bientocdo=3;

goto next;
}

if (!bit_test(portD,6)) //nhan phim toc do 4
{
bientocdo=4;

goto next;
}

if (!bit_test(portD,7)) //nhan phim toc do 5
bientocdo=5;

next:
if (!bit_test(portD,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
bientocdodat=0;
}

if (bienthuan)
{
duty2 = tocdo0;
chieu=1;
switch (bientocdo)
{
case 1: duty1= tocdo1;
bientocdodat=100;
break;
case 2: duty1= tocdo2;
bientocdodat=150;
break;
case 3: duty1= tocdo3;
bientocdodat=200;
break;
case 4: duty1= tocdo4;
bientocdodat=250;
break;
case 5: duty1= tocdo5;
bientocdodat=300;
break;
default : duty1 = td_macdinh;
bientocdodat=10;
break;
}
set_pwm1_duty(duty1); //cho dco chay thuan
set_pwm2_duty(duty2);

}

if (biennghich)
{
duty1 = tocdo0;
chieu=0;
switch (bientocdo)
{
case 1: duty2= tocdo1;
bientocdodat=100;
break;
case 2: duty2= tocdo2;
bientocdodat=150;
break;
case 3: duty2= tocdo3;
bientocdodat=200;
break;
case 4: duty2= tocdo4;
bientocdodat=250;
break;
case 5: duty2= tocdo5;
bientocdodat=300;
break;
default : duty2 = td_macdinh;
bientocdodat=10;
break;
}
set_pwm1_duty(duty1); //cho dco chay nghich
set_pwm2_duty(duty2);

}

lcd_putcmd(0x80);
lcd_putchar("Actual:");
lcd_putcmd(0x88);
TINHTOAN_HIENTHI(SOXUNGDEM_1PHUT);
delay_ms(1);
lcd_putcmd(0xc0);
lcd_putchar("Desired:");
lcd_putcmd(0xc8);
TINHTOAN_HIENTHI(bientocdodat);

}
}
saunai vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn