[PIC TUTORIAL] Bài 2 Bộ định thời tạo xung vuông có tần số 50Hz, duty 50% tại RD0 và xung vuông tần số 100Hz, 50% duty
Sơ đồ mạch điện
Chương trình mẫu
DOWNLOAD FILE MÔ PHỎNG + CODE
Chương trình mẫu
#include "16f887.h"
#include "def_16f887.h"
#fuses HS,PUT,NOWDT,NOPROTECT,NOLVP
#use delay(Clock = 8000000) //Thach anh 8MHz
#int_TIMER1
void TIMER1_isr(void)
{
set_timer1(55536);
RD1 = !RD1; //Tao xung 100hz.
if(RD1==0) //RD1 = 0 thi dao trang thai RD0.
RD0 = !RD0; //Tao xung 50hz.
}
void main()
{
//Khoi tao T1 va ngat.
setup_timer_1(T1_INTERNAL|T1_DIV_BY_1); //T1 dem xung noi, ti le chia 1
enable_interrupts(INT_TIMER1);
enable_interrupts(GLOBAL);
set_timer1(55536); //f = 100hz => CK xung = 10ms, he so duty 50% => muc cao = muc thap = 5ms
//Thach anh 8 MHz => CK may = 4/8 = 0,5us
//5ms = 10000 xung = 65536 - 55536
//Thiet lap cac pin xuat nhap
set_tris_d(0x00); //PORTD xuat du lieu.
//----------------------------
while(1);
}
DOWNLOAD FILE MÔ PHỎNG + CODE
No comments