chương trình hiện tại mình kết hợp như sau
#include <16F877A.h>
#priority EXT,TIMER1,timer0
#fuses NOWDT,PUT,XT,NOPROTECT,NOLVP,HS
#device *=16
#use delay(clock=20000000)
#use fast_io(b)
#use fast_io(d)
#use fast_io(c)
#byte portb=0x06
#byte portd=0x08
#byte portc=0x07
#byte PORTB = 0x06
unsigned char led1,led2,led3;
int16 count=0 ;
int32 dem=0;
int16 xung=0;
int32 vantoc=0;
int32 x=0;
int32 timer=0;
void convert_bcd(int32 x);
void display();
#int_timer0
void interrupt_timer0()
{
set_timer0(6);
{
++dem;
if(dem >= 20000)// 20000*500us = 10s
dem=0;
{if (input(PIN_C5)==0)
{
while(input(PIN_C5)==0);
count=count+1;
xung=count;
break;}
else
{
xung=count;
break;}
} }
vantoc=(xung/10);//tinh ra van toc (xung/giay)
}
//void docxung()
void convert_bcd(int32 x)
{
led1 = x / 100;
x = x %100;
led2 = x / 10;
led3 = x % 10;
}
void display()
{
char maled7[] = {0x7e,0x18,0xec,0xe6,0xd2,0xb6,0xbe,0x62,0xfe,0xf6 };
portb = (maled7[led3]); output_low(PIN_D5); delay_us(90); output_high(pin_D5);
portb = (maled7[led2]); output_low(PIN_D6); delay_us(90); output_high(pin_D6);
portb = (maled7[led1]); output_low(PIN_D7); delay_us(90); output_high(pin_D7);
}
//Chuong trinh chinh
void main(void)
{
set_tris_b(0);
set_tris_d(0x00);
set_tris_c(0xFf);// dau vao la C7,C6,C5,C4,xung vao chan c5
enable_interrupts(int_timer0);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_2);
enable_interrupts(global);
set_timer0(6);// T_dinhthi =delay= 2*(256 - 6)*1us = 500us
//INitTIMER0=6;prescaler=2(do RTCC_div_2
while(true)
{
// hien thi so xung ra led 7 doan
x=xung;
convert_bcd(x);
display();
}
}
chương trình như trên ý định của mình là xuất ra 3 led 7 đoạn giá trị xung đếm được sau 10s(dùng timer0 delay 10s , trong thời gian đó đếm số xung).Tuy nhiên khi nạp vào chip thì chạy không đúng, mỗi khi có xung thì nó tăng 1 giá trị, trên bảng led (3 led 7 đoạn) giá trị tăng dần chứ không đúng ý đồ.MÌnh đã thử đặt đoạn chương trình đoc xung vào nhiều chỗ nhưng chưa đúng.Mong các bạn giúp đỡ vì thời gian với mình cũng không còn nhiều.
|