Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Viết chương trình C để tạo xung tần số 10Hz chu kỳ

nhiệm vụ D =50% trên chân RB0. Dùng Timer0, ở


chế độ 8-bit để tạo hàm trễ, với hệ số chia trước lớn
nhất, cho biết tần số dao động thạch anh là 20MHz.
Bài 1:
#include <18F458.h>
#use delay(clock=20000000)
#bit co_tran=0x0c.0 //Khai báo bit cờ ngắt của Timer0
int dem;
void main()
{
dem=0;
set_tris_b(0x00);
setup_timer_0(T0_INTERNAL|T0_DIV_256);
set_timer0(60);// gia tri dat ban dau la 60 -> sau 196 chu
ky thi timer 0 tràn=> tổng thời gian tràn là 10 ms
while(1)
{
Output_high(pin_B0);

if(co_tran ==1)// thoi gian timer chay la 10ms


{
co_tran =0; // xoa co tran
set_timer0(60); dem++;
if(dem==5) output_low(PIN_b0);
if(dem==10)
{
dem=0; output_high(PIN_b0);
}
}
}
}

You might also like