首页 > 解决方案 > 如何在 Atemga128 中使用 Timer/Counter2...?

问题描述

我在使用定时器/计数器 02 时遇到了麻烦,我使用 Atmega128 8 位处理器,它有 16MHz 晶体。我知道如何使用 Timer00,但我不能使用 Timer02

我了解到计时器 00 和 02 非常相似。

下面,有我的短代码..请看代码..我需要你的帮助..

谢谢

#include <avr/io.h>
#include <avr/interrupt.h>
#define F_CPU 16000000UL
#include <avr/delay.h>

#define FALSE 0
#define TRUE 1

/*COUNT VARIABLE, SWITCH VARIABLE*/
unsigned int cnt_LED= 0; //LED TIMER0 COUNT 
unsigned int cnt_FND= 0; // FND TIMER2 COUNT
unsigned short FND_sw=0;
/********************************/


/*Function List*/
void LED();
void FND_TURN_ON();
/***************/



/**** LED TIMER2 ****/
ISR(TIMER2_OVF_vect) // 0.002*1500= 3s
{
    TCNT2 = 0x82;
    if(++cnt_LED ==1500) {
       PORTA = 0x00;
       cnt_LED = 0;
    }
}
/*******************/

int main()
{
    sei();
    LED();
    while(1)
    {

    }

}
    void LED()
    {
        /*LED Register*/
        DDRA = 0xff;
        TCCR2 = 0x06; //B 0000 0101
        TCNT2 = 0x82;

        /*************/

    PORTA = 0xff; //led on
        TIMSK = 0x40;

 }

标签: c

解决方案


首先你在中断程序中切换led。我认为您的意图是切换它。

其次,计数器必须是易变的。


推荐阅读