📄 timer1._c
字号:
#define TIMER1_C
#include "includes.h"
//TIMER1 initialisation - prescale:64
// WGM: 0) Normal, TOP=0xFFFF
// desired value: 200000uSec
// actual value: 200000.000uSec (0.0%)
unsigned char cap[4]={0,};
void timer1_init(void)
{
TCCR1B = 0x00; //stop
TCNT1H = 0xCF; //setup 200 000 uSec
TCNT1L = 0x2C;
OCR1AH = 0x30;
OCR1AL = 0xD4;
OCR1BH = 0x30;
OCR1BL = 0xD4;
ICR1H = 0x30;
ICR1L = 0xD4;
TCCR1A = 0x00;
//TCCR1B = 0xC3; //start Timer
}
#pragma interrupt_handler timer1_capt_isr:6
void timer1_capt_isr(void)
{
//timer 1 input capture event, read (int)value in ICR1 using;
capt_timer1=ICR1L; //Read low byte first (important)
capt_timer1|=(int)ICR1H << 8; //Read high byte and shift into top byte
timer1_stop();//stop timer1
TIMSK = 0x00; //disable capt interupt
TCNT1H = 0xCF /*INVALID SETTING*/; //reload counter high value
TCNT1L = 0x2C /*INVALID SETTING*/; //reload counter low value
capt_timer1=capt_timer1-0xcf2c;//DATA SIGNIAL PROCESSING
capt_timer1=(capt_timer1/625)*speed/100/2.55;//DATA SIGNIAL PROCESSING
cap[3]='0'+capt_timer1/1000;
capt_timer1%=1000;
cap[2]='0'+capt_timer1/100;
capt_timer1%=100;
cap[1]='0'+capt_timer1/10;
capt_timer1%=10;
cap[0]='0'+capt_timer1;
LCD_DisplayString(2,1,"LENTH= ");
LCD_Cursor (2,8);
LCD_DisplayCharacter (cap[3]);
LCD_Cursor (2,9);
LCD_DisplayCharacter (cap[2]);
LCD_Cursor (2,10);
LCD_DisplayCharacter (cap[1]);
LCD_Cursor (2,11);
LCD_DisplayCharacter ('.');
LCD_Cursor (2,12);
LCD_DisplayCharacter (cap[0]);
LCD_DisplayString(2,13,"m ");
}
#pragma interrupt_handler timer1_ovf_isr:9
void timer1_ovf_isr(void)
{
//TIMER1 has overflowed
timer1_stop();
TIMSK = 0x00; //disable ovf interupt
TCNT1H = 0xCF /*INVALID SETTING*/; //reload counter high value
TCNT1L = 0x2C /*INVALID SETTING*/; //reload counter low value
LCD_DisplayString(2,1,"OUT OF RANGE !");//(ROW(1-2),COLUMN(1-16),STRING)
}
void timer1_start(void)
{TCCR1B = 0x00;//STOP TIMER1
TCNT1H = 0xCF;//200 000 us
TCNT1L = 0x2C ;
TCCR1B = 0xC3;
}
void timer1_stop(void)
{
TCCR1B=0x00;//STOP TIMER1
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -