internet.com
10-11-2011, 01:06 AM
Em muốn cho led đảo trạng thái sau mỗi 1s. Pic 16f877a, XTAL 4MHz. Led được kết nối với chân RD0. Em mô phỏng chưa chạy. Mong các anh chị giúp đỡ!:)
Đây là code em viết:
// MCU : 16f877a
// XTAL : 4MHz
// Connection: A led is connected to RD0.
// Desctiption: Timer0 has overflowed: the led will blink after every 1 second.
#include <htc.h>
__CONFIG(XT & WDTDIS & LVPDIS & DEBUGDIS);
#define _XTAL_FREQ 4000000
unsigned char count=0;
//Ham main *******************************
void main(void)
{
ANSEL=0;
ANSELH=0;
TRISD=0;
PORTD=0x00;
T0SE=0; //USE INTERNAL CLOCK
PSA=0; //USE PRESCALER
PS2=0; PS1=1; PS0=1; //1:16
T0IF=0; //Clear int flag
T0IE=1; //Enable tmr0 int
GIE=1; //Enable glb int
TMR0=0x06; //TMR0 init
while(1); //Waiting for int, so we dont do anything
}
void interrupt isr(){
if(T0IE && T0IF){ //Check if int flag is set
++count;
if(count==250){ //250*1us*(256-6)*16=1s
RD0^=1; //Blink the led
count=0;
}
T0IF=0;
TMR0=0x06;
}
}
Đây là code em viết:
// MCU : 16f877a
// XTAL : 4MHz
// Connection: A led is connected to RD0.
// Desctiption: Timer0 has overflowed: the led will blink after every 1 second.
#include <htc.h>
__CONFIG(XT & WDTDIS & LVPDIS & DEBUGDIS);
#define _XTAL_FREQ 4000000
unsigned char count=0;
//Ham main *******************************
void main(void)
{
ANSEL=0;
ANSELH=0;
TRISD=0;
PORTD=0x00;
T0SE=0; //USE INTERNAL CLOCK
PSA=0; //USE PRESCALER
PS2=0; PS1=1; PS0=1; //1:16
T0IF=0; //Clear int flag
T0IE=1; //Enable tmr0 int
GIE=1; //Enable glb int
TMR0=0x06; //TMR0 init
while(1); //Waiting for int, so we dont do anything
}
void interrupt isr(){
if(T0IE && T0IF){ //Check if int flag is set
++count;
if(count==250){ //250*1us*(256-6)*16=1s
RD0^=1; //Blink the led
count=0;
}
T0IF=0;
TMR0=0x06;
}
}