View Single Post
Old 21-07-2010, 07:03 PM   #8
HANC
Đệ tử 1 túi
 
HANC's Avatar
 
Tham gia ngày: Jul 2010
Bài gửi: 15
:
Angry vấn đề là đọc xung encoder điều khiển motor dc

em là thành viên mới của diễn đàn mong mọi người chỉ dùm em ổn định tốc độ động cơ với,đề tài trong lớp của em.cụ thể encoder 400 xung em đưa vào timer0 (pic 16f877a)chân AR4 đọc em muốn 0.5s lấy mẫu 1 lần tiếp theo làm như thế nào,em đọc trên diễn đàn cũng chưa hiểu lắm.
Em có xem code của tác giả nào đó như thế này
#include <16f877a.h>
#include <def_877a.h>
#FUSES NOWDT, HS, NOPUT, NOPROTECT, NODEBUG, NOBROWNOUT, NOLVP, NOCPD, NOWRT
#use delay(clock=8000000)
#include <lcd_lib_4bit.c>
int16 cnt,cnt2,xung,v;
int8 a,b,c;
#int_TIMER0
void TIMER0_isr(void) //TIMER0 dem xung ve
{
set_timer0(255);
if(tmr0if==1)
{
cnt++;
tmr0if=0;
}
}
#int_TIMER1 //TIMER1 dem tgian,tinh van toc
void TIMER1_isr(void)
{
set_timer1(5565);
if(tmr1if==1)
{
xung=get_timer0()*4;
v=(xung*60)/((100*60*0.001));
tmr1if=0;
}
}
void Lcd()
{
while(1)
{
a=(int)v/100;
b=(int)(v-a*100)/10;
c=(int)v%10;
LCD_putcmd(0xC0);
LCD_putchar("xung encoder");
LCD_putcmd(0x80);
LCD_putchar(a+48);
LCD_putcmd(0x81);
LCD_putchar(b+48);
LCD_putcmd(0x82);
LCD_putchar(c+48);
}
}
void Ngat()
{
set_timer0(255);
setup_timer_0(RTCC_EXT_L_TO_H|RTCC_DIV_4);
enable_interrupts(INT_TIMER0);
setup_timer_1(T1_INTERNAL|T1_DIV_BY_2);
set_timer1(5565);
enable_interrupts(INT_TIMER1);
enable_interrupts(GLOBAL);
}
void PWM()
{
setup_timer_2(T2_DIV_BY_16,255,1);
setup_ccp1(CCP_PWM);
setup_ccp2(CCP_PWM);
set_pwm1_duty(1000);
set_pwm2_duty(1022);
}
void main()
{
set_tris_d(0);
set_tris_a(0xff); //Cho tin hieu di vao RA4
set_tris_c(0);
LCD_Init();
PWM();
Ngat();
Lcd();
delay_ms(100);
}
mong các anh chị giải thích(các công thức trên,timer0,timer1,timer2)
cảm ơn nhiều (code trên em thử không thấy đọc được)
HANC vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn