Trích:
Nguyên văn bởi dinhminh09
đề nghị úp lên media fire down cho dễ. không thì copy cái code paste vào luôn
muốn đến v/p thì trước hết phải biết cái encoder mỗi vòng cho bao nhiêu xung.
cho một timer định thời là 1 phút ngắt lấy số xung ra tính. tổng số xung trong 1 phút / số xung trong 1 vòng quay
|
#include <16F877A.h>
#include <def_877a.h>
#fuses NOWDT,PUT,HS,NOPROTECT,NOLVP
#use delay(clock=8000000)
#include <lcd_lib_4bit.c>
#use fast_io(a)
#use fast_io(b)
#use fast_io(d)
#define tocdo0 0 //toc do bang 0
#define td_macdinh 10 //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
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=200;
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; //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 so 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=(((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 +0x30 ;
x = x % 1000;
tram = x / 100 + 0x30; // tách so hàng tram
x = x % 100; // chia lay phan du
chuc = x / 10 + 0x30; // tách lay phan chuc
donvi = x % 10 + 0x30; // phan don vi
if (chieu==1) lcd_putchar('+');
else lcd_putchar('-');
lcd_putchar(nghin);
lcd_putchar(tram);
lcd_putchar(chuc);
lcd_putchar(donvi);
}
void main(void)
{
set_tris_a(0x0ff);
set_tris_b(0x00);
set_tris_d(0xFF); //khoi tao port B la port nhap
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,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); //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=128;
break;
case 2: duty1= tocdo2;
bientocdodat=248;
break;
case 3: duty1= tocdo3;
bientocdodat=372;
break;
case 4: duty1= tocdo4;
bientocdodat=496;
break;
case 5: duty1= tocdo5;
bientocdodat=621;
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=128;
break;
case 2: duty2= tocdo2;
bientocdodat=248;
break;
case 3: duty2= tocdo3;
bientocdodat=372;
break;
case 4: duty2= tocdo4;
bientocdodat=496;
break;
case 5: duty2= tocdo5;
bientocdodat=621;
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("v thuc:");
lcd_putcmd(0x88);
TINHTOAN_HIENTHI(SOXUNGDEM_1PHUT);
delay_ms(1);
lcd_putcmd(0xc0);
lcd_putchar("v dat:");
lcd_putcmd(0xc8);
TINHTOAN_HIENTHI(bientocdodat);
}
}
mong sự giúp đỡ