?? cycle_1.c
字號:
/**********************************************
http://www.sl.com.cn
雙龍電子公司----占空比測量演示程序(ICCAVR)
作者:詹衛(wèi)前
1、學習定時器T1捕捉(捕獲)中斷的使用
**********************************************/
#include <io8515.h>
#include <math.h>
#pragma interrupt_handler Icp_timer1:4 // 設(shè)置ICP中斷
/* 七段譯碼字形表 */
const unsigned char tabel[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
/* 顯示緩沖區(qū) */
unsigned char ledbuff[]={0x3f,0x3f,0x3f,0x3f,0x3f,0x3f};
unsigned int count_l;
unsigned int count_h;
/******************************
1ms延時程序
******************************/
void delay_1ms(void)
{
unsigned int i;
for(i=1;i<1142;i++)
;
}
/******************************
六路動態(tài)掃描顯示電路
******************************/
void display(void)
{
unsigned char i;
for (i=0;i<6;i++)
{
PORTB=ledbuff[i];//將顯示緩沖區(qū)數(shù)據(jù)送PORTB口
PORTD=~(1<<i); //開始顯示
delay_1ms(); //每一位顯示保持一定時間
PORTD|=(1<<i); //關(guān)閉顯示
}
}
/************************************
將count十六進制數(shù)據(jù)轉(zhuǎn)換為LED七段碼
************************************/
void hextobcd(unsigned int count)
{
unsigned char i,temp;
for (i=0;i<6;i++)
{temp=count%10;
ledbuff[i]=tabel[temp];
count=count/10;
}
}
/******************************
MCU初始化
******************************/
void mcu_init(void)
{
DDRD=0x3f;
DDRB=0xff;
PORTD=0xff;
PORTB=0xff;//端口初始化
TIMSK =0x08;//使能T1捕捉中斷
TCCR1A=0x00;
TCCR1B=0xC2;//CK/8,捕捉周期的單位為1us
ICR1=0;
TCNT1=0;
}
/************************************************
主程序:測量ICP引腳上信號的占空比
************************************************/
void main()
{
unsigned char n;
unsigned long x,y;
mcu_init();
SREG|=0x80; //使能全局中斷
for(;;)
{
x=count_h*100;
y=count_l+count_h;
n=x/y; //計算占空比
hextobcd(n);
display(); //顯示測量值
}
}
/**************************************
捕捉中斷處理程序
**************************************/
void Icp_timer1(void)
{
if (TCCR1B&0x40)
count_l=ICR1; //脈沖低電平寬度
else
count_h=ICR1; //脈沖高電平寬度
ICR1=0;
TCNT1=0;
TCCR1B^=0x40;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -