📄 timer.c
字号:
#include <iom8v.h>
#include <macros.h>
#include <math.h>
#define Uchar unsigned char
#define Uint unsigned int
#define Ulong unsigned long
Uchar time_init;
Uchar CNT1;
Uchar CNT2;
Uchar CNT3;
Uchar LMT1;
Uchar LMT2;
Uchar LMT3;
//TIMER1 initialize - prescale:1024
// WGM: 0) Normal, TOP=0xFFFF
// desired value: 1Hz
// actual value: 1.000Hz (0.0%)
void timer1_init(void)
{
TCCR1B = 0x00; //stop
TCNT1H = 0xfe; //setup
TCNT1L = 0x00;
OCR1AH = 0x0F;
OCR1AL = 0x42;
OCR1BH = 0x0F;
OCR1BL = 0x42;
ICR1H = 0x0F;
ICR1L = 0x42;
TCCR1A = 0x00;
TCCR1B = 0x01; //start Timer
}
#pragma interrupt_handler timer1_ovf_isr:9
void timer1_ovf_isr(void)
{
TCNT1H = 0xfe; //reload counter high value
TCNT1L = 0x00; //reload counter low value
++CNT1;
if(CNT1>=LMT1)
{
CNT1=0;
if(++CNT2>=LMT2)
{
CNT2=0;
CNT3=~CNT3;
}
}
time_init=1;
}
/*void flush(UCHAR ios,UCHAR ioc)
{
UCHAR a;
if(time_init)
{
time_init=0;
if(CNT1<=CNT2)
{
PORTB=ios;
}
else
PORTB=ioc;
}
}
*/
void main(void)
{
OSCCAL=0X9d;
CLI(); //disable all interrupts
timer1_init();
PORTB = 0x00;
DDRB = 0xff;
MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x04; //timer interrupt sources
time_init=0;
LMT1=250;
LMT2=250;
LMT3=1;
CNT1=CNT2=CNT3=0;
SEI(); //re-enable interrupts
while(1)
{
if (!CNT3)
{
if(time_init)
{
time_init=0;
if(CNT1<=CNT2)
{
PORTB=1;
}
else
PORTB=0;
}
}
else
if(time_init)
{
time_init=0;
if(CNT1<=CNT2)
{
PORTB=0;
}
else
PORTB=1;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -