中断测频率
先来看张图
图中标明 ITx=0 时 下降沿触发中断; ITx=1 时 上升沿,下降沿均可触发中断,这里要注意不是随便一个IO口有上升沿或下降沿时都触发中断,比如我的INT0 对应的端口是P32 ,所以 只有P32端口有上升沿、下降沿时才会触发外部中断0。
#include<stc15f2k60s2.h>
#include<intrins.h>
typedef unsigned char uchar;
typedef unsigned int uint;
uchar code smg[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff};
int yi,er,san,si,wu,liu,qi,ba,pin;
int t,maichong,maichongshu;
void shangdian();
void delay1ms(int ms);
void display1();
void display2();
void Timer1Init(void); //100微秒@11.0592MHz;
void zhongduan0();
main()
{
shangdian();
Timer1Init();
zhongduan0();
while(1)
{
yi=pin/10000;
er=pin%10000/1000;
san=pin%10000%1000/100;
si=pin%10000%1000%100/10;
wu=pin%10;
display1();
display2();
}
}
void shangdian()
{
P2=0XA0;P0=0X00;
P2=0X80;P0=0XFF;
P2=0XC0;P0=0XFF;
P2=0XE0;P0=0XFF;
}
void delay1ms(int ms)
{
int i;
for(;ms>0;ms--)
{
for(i=845;i>0;i--) ;
}
}
void display1()
{
P2=0XC0;P0=0X01;
P2=0XE0;P0=smg[yi];
delay1ms(1);
P2=0XC0;P0=0X02;
P2=0XE0;P0=smg[er];
delay1ms(1);
P2=0XC0;P0=0X04;
P2=0XE0;P0=smg[san];
delay1ms(1);
P2=0XC0;P0=0X08;
P2=0XE0;P0=smg[si];
delay1ms(1);
}
void display2()
{
P2=0XC0;P0=0X10;
P2=0XE0;P0=smg[wu];
delay1ms(1);
P2=0XC0;P0=0X20;
P2=0XE0;P0=smg[liu];
delay1ms(1);
P2=0XC0;P0=0X40;
P2=0XE0;P0=smg[qi];
delay1ms(1);
P2=0XC0;P0=0X80;
P2=0XE0;P0=smg[ba];
delay1ms(1);
P2=0XC0;P0=0X80;
P2=0XE0;P0=0xff;
}
void Timer1Init(void) //1毫秒@11.0592MHz
{
AUXR |= 0x40; //定时器时钟1T模式
TMOD &= 0x0F; //设置定时器模式
TL1 = 0xCD; //设置定时初值
TH1 = 0xD4; //设置定时初值
TF1 = 0; //清除TF1标志
TR1 = 1; //定时器1开始计时
ET1=1;
EA=1;
}
void time1() interrupt 3
{
t++;
if(t==1000)
{
t=0;
pin=maichongshu;
maichongshu=0;
}
}
void zhongduan0()
{
EX0=1;
IT0=1;//为1仅支持下降沿,为0下降/上升
EA=1;
}
void duan0() interrupt 0
{
if(P32==0)
{
maichongshu++;
}
}