You are on page 1of 4

//******************************************************************************

**********
//******DIEU KHIEN DONG CO BANG NUT BAM...
//******2 NUT CAI DAT 4 CAP TOC DO
//******2 NUT CHO CHIEU THUAN VA CHIEU NGUOC
//******1 NUT DUNG DONG CO
//******2 NUT TANG GIAM TOC DO - TINH CHINH (1 vong/phut)
//******************************************************************************
**********
#include <16F877A.h>
#include <defs_16f877a.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#device *=16 adc=10
#use delay(clock=8000000)
#include <lcd_lib_4bit.c>
#bit phim1 =0x06.0 // dinh dia chi bit cho cac port
#bit phim2 =0x06.1
#bit phim3 =0x06.2
#bit phim4 =0x06.3
#bit phim5 =0x06.4
#bit phim6 =0x06.5
#bit phim7 =0x06.6
#bit phim8 =0x06.7
int8 nghin,tram,chuc,donvi;
void tinhtoan_hienthi(int16 x);
int16 x;
//------DAT BIEN TOAN CUC----------------//
INT16 COUNT255=0;
INT16 SOLANLAP=0;
INT chieu;
INT16 TOCDOENCODER=200;
INT16 SOXUNGDEM_1PHUT=0;
int16 tocdo_caidat=240;
int16 value=50;
//..............................................................................
.........
//chuong trinh con check phim:
void checkphim()
{
if (!phim5) //tang dan toc do
{
while(!phim5);
DELAY_MS(5);
tocdo_caidat=tocdo_caidat+1;
}
if (!phim7) //giam dan toc do
{
while(!phim7);
DELAY_MS(5);
tocdo_caidat=tocdo_caidat-1;
}

}



#int_RB // Ngat port B
void ngat_RB()
{
//..............
// cai dat toc do cho dong co
RBIF=0; // Xoa co ngat RB
if (!phim5)
{
while(!phim5);
DELAY_MS(5);
tocdo_caidat=100;
}
if (!phim4)
{
while(!phim4);
DELAY_MS(5);
tocdo_caidat=200;
}

}

#INT_RTCC // Ngat khi timer0 tran
VOID BODEMTIMER0() // tao bo dem timer0 dem so xung vao....
.........
{
COUNT255++;
}
#INT_TIMER1
VOID SOXUNGDEM_1S() // dinh thoi 1s bang timer1 va tinh toan t
oc do....
{
solanlap=solanlap+1; //so lan lap bang 5 vi de dinh thoi 1s voi
bo chia tan la 8.osc=8Mhz can dem 2000 000
//chia tan 8 se con 250 000 lenh qua lon s
o voi gtri co the dem 65 535 cua tmr1
// de dem dc 250 000 lenh,trong 1 chu ky ta
dem 50 000 lenh va lap 5 lan
if (solanlap==5)
{ SOXUNGDEM_1PHUT=(COUNT255*255+GET_TIMER0())*2*60/TOCDOENCODER;

// tang giam value dua gia tri vao pwm duty de dieu khien
tang giam toc do tren dong co

if (SOXUNGDEM_1PHUT>tocdo_caidat)
{
if(SOXUNGDEM_1PHUT>tocdo_caidat+5)
value=value+50;
else
value=value+30;
}
else
{if(SOXUNGDEM_1PHUT<tocdo_caidat-5)
value=value-50;
else
value=value-30;
}

//setup lai gia tri dat...
SOLANLAP=0;
SET_TIMER0(0);
COUNT255=0;
}

set_timer1(15535); // timer 1 se dem 50 000 lan phuc vu cho dinh thoi 1S
}
void tinhtoan_hienthi(int16 x) // thuc hien tach so va dua ra hien thi
{nghin = x / 1000 + 0x30;
x = x % 1000;
tram = x / 100 + 0x30; // tch s? hng tram
x = x % 100; // chia l?y ph?n d?
chuc = x / 10 + 0x30; // tch l?y ph?n ch?c
donvi = x % 10 + 0x30; // ph?n don v?
if (chieu==1) lcd_putchar('-');
else lcd_putchar('+');
lcd_putchar(nghin);
lcd_putchar(tram);
lcd_putchar(chuc);
lcd_putchar(donvi);
}
void main() {
SET_TRIS_A(0X11);
SET_TRIS_D(0X00);
SET_TRIS_b(0Xff);
lcd_putcmd(0x80);
lcd_init();
delay_ms(10);
enable_interrupts(int_RB);
ext_int_edge(H_to_L);
setup_timer_0 (RTCC_EXT_H_TO_L|RTCC_DIV_2); // Timer0 is Counter
set_timer0(0);
setup_timer_1(T1_INTERNAL | T1_DIV_BY_8); // Timer1 is Timer
set_timer1(15535);
enable_interrupts(INT_RTCC);
enable_interrupts(INT_TIMER1);
enable_interrupts(GLOBAL);
setup_ccp1(CCP_PWM);
setup_ccp2(CCP_PWM);

DELAY_MS(10);
while( TRUE )
{

DELAY_MS(5);
if (!phim1)
{ delay_ms(5) ;
set_pwm1_duty(0) ; // dung dong co
set_pwm2_duty(0) ;
}

if (!phim2) // dao chieu dong co
{

DELAY_MS(5);
chieu=0; // dong co chay thuan
checkphim();
set_pwm1_duty(value); // cho Pwm1 hoat dong
set_pwm2_duty(0) ; // pwm 2 dung
}
if (!phim3)
{ DELAY_MS(5);
chieu=1; // dong co chay ngich
checkphim();
set_pwm2_duty(value); // cho Pwm2 hoat dong
set_pwm1_duty(0) ; // pwm 1 dung
}
lcd_putcmd(0x80);
lcd_putchar("v thuc:");
lcd_putcmd(0x88);
TINHTOAN_HIENTHI(SOXUNGDEM_1PHUT);
delay_ms(10);
lcd_putcmd(0xc0);
lcd_putchar("v dat:");
lcd_putcmd(0xc8);
TINHTOAN_HIENTHI(tocdo_caidat);
}
}

You might also like