code em thử viết thế nay nhung ko chạy.
các bác xem giúp em với.
xem hộ em làm sai thế nào.mà encoer ko đếm dc vòng quay.lcd cung ko hien thị vòng quay.
em vừa học pic.còn chưa bít nhiều các bác giúp em.
#include "C:\Users\Bac-Huong\Desktop\encoder\encoer\en 1-7.h"
#include <DEF_877A.h>
#include <lcd_lib_4bit.c>
#use delay(clock=20000000)
int nghin,tram,chuc,donvi;
Void tinhtoan_hienthi(int16 x);
int16 count,so_vong,int_count1,x;
#define INTS_PER_SECOND1 19
#INT_RTCC //ngat timer0
Void timer0_isr() //dem so vong quay
{
count++;
}
#INT_Timer1
Void timer1_isr()
{ //ham dc goi khi timer1 tran(65535->0)
if(--int_count1==0);
{
int_count1=INTS_PER_SECOND1; //gan laij gia tri cuan int_count1
so_vong=(count*255+get_timer0())/100;
count=0;
set_timer0(0);
}
}
Void tinhtoan_hienthi(int16 x)
{
nghin=x/1000+0x30;
x=x%1000;
tram=x/100+0x30;
x=x%100;
chuc=x/10+0x30;
donvi=x%10+0x30;
lcd_putcmd(0xC0);
printf(lcd_putchar,"tocdo=");
lcd_putchar(nghin);
lcd_putchar(tram);
lcd_putchar(chuc);
lcd_putchar(donvi);
}
void main()
{
trisc1=0;
trisc2=0;
lcd_Init();
lcd_clear();
setup_timer_2(T2_DIV_BY_4,255,1);
SETUP_CCP1(CCP_PWM);
set_pwm1_duty(0);
lcd_putcmd(0x80);
int_count1=INTS_PER_SECOND1;
setup_timer_0(RTCC_DIV_1|RTCC_EXT_H_TO_L);
set_timer0(0);
set_timer1(15535);
setup_timer_1(T1_INTERNAL|T1_DIV_BY_4);
enable_interrupts(INT_RTCC);
enable_interrupts(INT_TIMER1);
enable_interrupts(GLOBAL);
count=0;
lcd_init();
delay_ms(500);
printf(lcd_putchar,"toc do dong co");
while(true)
{
if(!bit_test(portB,1))
{
lcd_clear();
lcd_putchar("tocdo1");
delay_ms(1000);
set_pwm1_duty(100);
lcd_clear();
so_vong=0;
tinhtoan_hienthi(so_vong);
delay_ms(1000);
}
if(!bit_test(portB,2))
{
lcd_clear();
lcd_putchar("tocdo2");
delay_ms(1000);
set_pwm1_duty(220);
lcd_clear();
so_vong=0;
tinhtoan_hienthi(so_vong);
delay_ms(1000);
}
}
}
các bác xem giúp em với.
xem hộ em làm sai thế nào.mà encoer ko đếm dc vòng quay.lcd cung ko hien thị vòng quay.
em vừa học pic.còn chưa bít nhiều các bác giúp em.
#include "C:\Users\Bac-Huong\Desktop\encoder\encoer\en 1-7.h"
#include <DEF_877A.h>
#include <lcd_lib_4bit.c>
#use delay(clock=20000000)
int nghin,tram,chuc,donvi;
Void tinhtoan_hienthi(int16 x);
int16 count,so_vong,int_count1,x;
#define INTS_PER_SECOND1 19
#INT_RTCC //ngat timer0
Void timer0_isr() //dem so vong quay
{
count++;
}
#INT_Timer1
Void timer1_isr()
{ //ham dc goi khi timer1 tran(65535->0)
if(--int_count1==0);
{
int_count1=INTS_PER_SECOND1; //gan laij gia tri cuan int_count1
so_vong=(count*255+get_timer0())/100;
count=0;
set_timer0(0);
}
}
Void tinhtoan_hienthi(int16 x)
{
nghin=x/1000+0x30;
x=x%1000;
tram=x/100+0x30;
x=x%100;
chuc=x/10+0x30;
donvi=x%10+0x30;
lcd_putcmd(0xC0);
printf(lcd_putchar,"tocdo=");
lcd_putchar(nghin);
lcd_putchar(tram);
lcd_putchar(chuc);
lcd_putchar(donvi);
}
void main()
{
trisc1=0;
trisc2=0;
lcd_Init();
lcd_clear();
setup_timer_2(T2_DIV_BY_4,255,1);
SETUP_CCP1(CCP_PWM);
set_pwm1_duty(0);
lcd_putcmd(0x80);
int_count1=INTS_PER_SECOND1;
setup_timer_0(RTCC_DIV_1|RTCC_EXT_H_TO_L);
set_timer0(0);
set_timer1(15535);
setup_timer_1(T1_INTERNAL|T1_DIV_BY_4);
enable_interrupts(INT_RTCC);
enable_interrupts(INT_TIMER1);
enable_interrupts(GLOBAL);
count=0;
lcd_init();
delay_ms(500);
printf(lcd_putchar,"toc do dong co");
while(true)
{
if(!bit_test(portB,1))
{
lcd_clear();
lcd_putchar("tocdo1");
delay_ms(1000);
set_pwm1_duty(100);
lcd_clear();
so_vong=0;
tinhtoan_hienthi(so_vong);
delay_ms(1000);
}
if(!bit_test(portB,2))
{
lcd_clear();
lcd_putchar("tocdo2");
delay_ms(1000);
set_pwm1_duty(220);
lcd_clear();
so_vong=0;
tinhtoan_hienthi(so_vong);
delay_ms(1000);
}
}
}