tc._c
来自「avr atmega168v Timer counter的程序」· _C 代码 · 共 56 行
_C
56 行
// **************************************************************
// *** 实验: Timer/Counter ***
// *** 2007年10月13日 ***
// *** 目标MCU:MEGA168v 晶振:内部 8MHZ ***
// *** fuse_config:CLKDIV8 disabled ***
// **************************************************************
//set_bit:PORTD |=(1<<PORTD3);
//clr_bit:PORTD &=~(1<<PORTD3);
#include<iom168v.h>
#include<macros.h>
#include<myfun.h>
void PORT_Init(void)
{
DDRD = 0xFF;
}
//*************************************************//
//******************* TC **********************//
//*************************************************//
unsigned int counter=0;
//prescale:8;WGM: Normal;
//desired value: 5KHz
//actual value: 5.009KHz (0.2%)
void timer0_init(void)
{
TCCR0B = 0x00; //stop
TCNT0 = 0x10; //set count
TCCR0A = 0x00;
TIMSK0 = 0x01; //enable timer overflow interrupt
TCCR0B = 0x02; //start timer
}
#pragma interrupt_handler timer0_ovf_isr:17
void timer0_ovf_isr(void)
{
TCNT0 = 0x10; //set count
counter++;
if(counter == 4000)
PORTD |= (1<<PORTD3);
if(counter == 8000)
{
counter = 0;
PORTD &= ~(1<<PORTD3);
}
}
//*************************************************//
//****************** TC_over **********************//
//*************************************************//
void main(void)
{
CLI();
PORT_Init();
timer0_init();
SEI();
while(1) ;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?