setup_timer_2 (mode, period, postscale)
// set cho bộ chia trước
mode :T2_DISABLED, T2_DIV_BY_1, T2_DIV_BY_4, T2_DIV_BY_16
// set khoảng tràn cho timer2, khác các timer khác nghe.
// vd bạn set là period=5 thì sau 5+1(set cờ báo tràn) xung nhịp nó tràn
period : int 0-255
// set cho bộ chia sau
postscale :1-16
để tạo khoảng định thời 1S các bạn tham khảo code sau:
#include <16F877A.h>
#FUSES NOWDT //No Watch Dog Timer
#FUSES hs //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD)
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#use delay(clock=20000000)
#define output PIN_A1
static int count=0;
void main()
{
setup_timer_2(T2_DIV_BY_16, 0x06, 10);
enable_interrupts(GLOBAL);
enable_interrupts(INT_TIMER2);
while(true);
}
#INT_TIMER2
void fulse_1s(){
setup_timer_2(T2_DIV_BY_16, 249, 16);
// timer2 tang len 1 sau 0.2*16=3.2us
// tran sau 3.2*(249+1)= 800us
// xay ra ngat sau 800*16= 12800us
count++;
if(count==78){ // 1000000/12800=78.125
output_toggle(output);
count=0;
}
}
|