資源簡介
本設計利用定時器中斷功能實現了頻率測量的功能,模塊化的程序可讀性強。

代碼片段和文件信息
#include
#define?uchar?unsigned?char
#define?uint??unsigned?int
uchar?code?table[11]?=?{0x3f0x060x5b0x4f0x660x6d0x7d0x070x7f0x6f};
long?uint?pulse=0;//計數輸入的方波的個數;
long?uint?freq=0;//頻率
uchar?temp=0;?//臨時變量
uchar?f;
void?delay_1ms(uint?i)?//延時函數,約1ms
{
????????uint?xj;
????????for(j=0;j????????for(x=0;x<=148;x++);???????
}
void?Time0_Init()//定時器和計數器的初始化函數
{
????????EA=1;?//打開全局中斷
????????TMOD?=?0x15;//設定定時器1和計數器0的工作方式為方式1
???????????????
????????TH0??=?0xff;//計數器0的初值
????????TL0??=?0xff;//計數器0的初值
????????ET0=1;?//允許計數器0中斷
????????TR0=1;?//打開計數器0,開始計數
????????TH1??=?0x3c;//定時器1的初值
????????TL1??=?0xb0;//定時器1的初值
????????ET1=1;????????//允許定時器1中斷
????????TR1?=?1;//打開定時器1,開始計數???????????????
}
void?main()
{
????????Time0_Init();?//初始化定時器和計數器
???????
????????while(1)
????????{
???????????????????????
????????????????P2=0xf7;??//以下為數碼管顯示函數
????????????????P0=0x00;
????????????????P0=table[freq/1000%10];
????????????????delay_1ms(2);
????????????????P2=0xfb;
????????????????P0=0x00;
????????????????P0=table[freq/100%10];
????????????????delay_1ms(2);
????????????????P2=0xfd;
????????????????P0=0x00;
????????????????P0=table[freq/10%10];
????????????????delay_1ms(2);
????????????????P2=0xfe;
????????????????P0=0x00;
????????????????P0=table[freq%10];
????????????????delay_1ms(1);???????
????????}
}
void?ISQ_timer0(void)?interrupt?1?//用來計數脈沖數,每一個方波脈沖,計數器進入中斷,脈沖數加一
{
????????TR0?=?0;
????????pulse++;
????????TH0??=?0xff;
????????TL0??=?0xff;
????????TR0?=?1;
}
/****************************************************************
????????用來設定時間,定時器的周期為50ms,當經過20次后,
????????也即1s后,關斷定時器和計數器,將一秒內計數的脈沖數賦給頻率pulse
????????一秒內采集的脈沖數即為輸入方波的頻率
****************************************************************/
void?ISQ_time1()?interrupt?3??
{
????????TH1??=?0x3c;
????????TL1??=?0xb0;
????????temp++;
????????if(temp==20)
????????{
????????????????TR0?=?0;
????????????????TR1?=?0;
????????????????temp=0;
????????????????freq=202642400/pulse*100/pulse;
????????????????pulse=0;//將脈沖數清零,重新計數
????????????????TR0?=?1;//打開計數器0,進入下一個循環
????????????????TR1?=?1;//打開定時器1,進入下一個循環???????????????????????
????????}???????
}
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2018-08-15?23:53??test_fre_x\
?????文件????????6376??2009-05-07?14:37??test_fre_x\STARTUP.A51
?????文件???????14070??2018-07-08?15:12??test_fre_x\STARTUP.LST
?????文件?????????749??2018-07-08?15:12??test_fre_x\STARTUP.OBJ
?????文件????????2429??2018-07-06?17:02??test_fre_x\day5_tf.c
?????文件????????1490??2018-07-08?15:12??test_fre_x\frequence.LST
?????文件????????2034??2018-07-08?15:12??test_fre_x\frequence.OBJ
?????文件?????????293??2018-07-06?22:45??test_fre_x\frequence.c
?????文件?????????131??2018-07-06?22:51??test_fre_x\frequence.h
?????文件?????????894??2018-07-06?22:50??test_fre_x\it.LST
?????文件?????????592??2018-07-06?22:50??test_fre_x\it.OBJ
?????文件??????????52??2018-07-06?22:50??test_fre_x\it.__i
?????文件??????????53??2018-07-06?22:51??test_fre_x\it.c
?????文件????????4113??2018-07-08?15:18??test_fre_x\lcd.LST
?????文件????????6832??2018-07-08?15:18??test_fre_x\lcd.OBJ
?????文件??????????53??2018-07-08?15:18??test_fre_x\lcd.__i
?????文件????????1499??2018-07-08?15:18??test_fre_x\lcd.c
?????文件?????????322??2018-07-08?14:52??test_fre_x\lcd.h
?????文件????????6213??2018-07-06?21:03??test_fre_x\main.LST
?????文件????????8590??2018-07-06?21:03??test_fre_x\main.OBJ
?????文件????????3451??2018-07-06?21:03??test_fre_x\main.c
?????文件????????4060??2018-07-08?15:23??test_fre_x\test_freq.LST
?????文件????????4858??2018-07-08?15:23??test_fre_x\test_freq.OBJ
?????文件??????????59??2018-07-08?15:23??test_fre_x\test_freq.__i
?????文件????????1464??2018-07-08?15:23??test_fre_x\test_freq.c
?????文件???????12740??2018-07-08?15:23??test_fre_x\test_frequency
?????文件????????4390??2018-07-05?23:37??test_fre_x\test_frequency.LST
?????文件???????14749??2018-07-08?15:23??test_fre_x\test_frequency.M51
?????文件???????????0??2018-08-15?23:11??test_fre_x\test_frequency.build_log.htm
?????文件????????1573??2018-07-05?20:28??test_fre_x\test_frequency.c
?????文件????????2471??2018-07-08?15:23??test_fre_x\test_frequency.hex
............此處省略9個文件信息
- 上一篇:電機測速程序的實現很使用 可讀性強
- 下一篇:電工學答案電工學答案.rar
評論
共有 條評論