Chào bạn! Chương trình nháy led port C của bạn có thể như sau, bạn tham khảo:
Code:
// MCU : 16f887
// XTAL : 4MHz
#include <htc.h>
__CONFIG(XT & WDTDIS & LVPDIS & DEBUGDIS);
__CONFIG(BORV21);
#define _XTAL_FREQ 4000000
unsigned char count=0;
//Ham main *******************************
void main(void){
ANSEL=0; //ALL PORTS ARE DIGITAL
ANSELH=0;
TRISC=0;
PORTC=0;
T0CS=0; //USE INTERNAL CLOCK
PSA=0; //USE PRESCALER
PS2=0; PS1=1; PS0=0; //1:8
T0IF=0; //Clear int flag
T0IE=1; //Enable tmr0 int
GIE=1; //Enable glb int
TMR0=5; //Timer0 will overflow after: 8*250*1us = 2ms
while(1); //Waiting for int
}
void interrupt isr(){ //Do not rename isr ("interrupt isr()")
if(T0IE && T0IF){
T0IF = 0;
TMR0=5;
++count;
if(count == 125){ //125*2ms = 250ms
count = 0;
PORTC ^= 0xff;
}
}
}