View Single Post
Old 25-02-2012, 06:30 AM   #3
Aducbkfet
Nhập môn đệ tử
 
Tham gia ngày: Sep 2010
Bài gửi: 5
:
Mình cũng nghĩ như bạn alycuong là cái set_timer1(3035). Thế thì mỗi lần ngắt Timer nó sẽ đếm được 0.5s do đó cần 2 lần ngắt timer để đếm được 1s. Nhưng ko hiểu sao mình mô phỏng thấy 1s gì mà nhanh khiếp. Mọi người xem hộ mình cái code nhé:
#include <16F877A.h>
#include <def_877A.h>
#fuses NOWDT,PUT,XT,NOPROTECT
#use delay(clock=4000000)


int16 count; //bien dem
/*Khoi tao port*/
void PortInit()
{
TRISD = 0x00; //PORTD la cong xuat
PORTD = 0xFF; //Keo chan PORTD len tro khang cao
}
/*Khoi tao Timer1*/
void Timer1Init()
{
set_timer1(3035);
setup_timer_1(RTCC_INTERNAL|RTCC_DIV_8); //Chon tan so Timer1 la Internal va chia tan 8
enable_interrupts(int_timer1); //Cho phep ngat tran Timer1
enable_interrupts(global); //Cho phep ngat toan cuc
count = 0;
}
//Chuong trinh ngat TMR0
#int_timer1
void interrupt_timer1()
{
set_timer1(3035);
++count;
if(count == 2) //
{
count=0;
PORTD^=0xFF; //Dao tat ca bit portD
}
}
//Chuong trinh chinh
void main(void)
{
PortInit(); //khoi tao port
Timer1Init(); //khoi tao timer1

while(1)
{
interrupt_timer1();
}
}
Aducbkfet vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn