|
今天用到C/T的計數(shù)功能,一上午沒有搞定,很郁悶。下午才搞明白,51內(nèi)核C/T的外部脈沖計數(shù)功能,原來是不開中斷的。暈倒!!!大半天的功夫都浪費(fèi)了!!記錄下來以備忘!
#include <reg52.h>
void timer0Init(void)
{
TMOD &= 0xF0;
TMOD |= 0x05;
TH0 = 0xFF;
TL0 = 0xFD;
TR0 = 1;
}
void timer1Init(void)
{
TMOD &= 0x0F;
TMOD |= 0x50;
TH1 = 0xFF;
TL1 = 0xFD;
TR1 = 1;
}
void SystemInit(void)
{
EA = 0;
P1 = P2 = P3 = P0 = 0xFF;
timer0Init();
timer1Init();
EA = 1;
}
void main(void)
{
// unsigned int temp;
unsigned char count;
SystemInit();
while(1)
{
// temp = TL0|(TH0<<8);
if(TF0)
{
TF0 = 0;
TH0 = 0xFF;
TL0 = 0xFD;
// P1 = temp;
if(count++ >= 3)
{
count = 0;
P1 = ~P1;
}
}
if(TF1)
{
TF1 = 0;
TH1 = 0xFF;
TL1 = 0xFD;
// P1 = temp;
if(count++ >= 3)
{
count = 0;
P0 = ~P0;
}
}
else
{
P2 = count;
}
}
}
|
|