微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在 atmega328p 中启用 timer0 中断?

如何解决如何在 atmega328p 中启用 timer0 中断?

我正在尝试使用 timer0 中断用 atmega328p 制作一个闪烁的 LED。

这是一个非常基本的东西,但 microchip Studio 给了我这个:

========== Build: 0 succeeded or up-to-date,1 Failed,0 skipped=======

如何解决这个问题?代码附在下面:

#include<avr/io.h>
#include<avr/interrupt.h>
#include<util/delay.h>
    
#define F_cpu 1000000UL
    
int main(void)
{
  //define inputs and outputs
  DDRC = (1<<DDC4); // portc4 as output
  TCNT0 = 0x00;
  TCCR0A = 0x00;
  TCCR0B = (1<<CS00) | (1<<CS01);
  TimsK0 = (1<<TOIE0);
  sei(); // set global interrupts
  while (1) 
  {
    PORTC4 |= (1<<PORTC4);
  }
}
    
ISR(TIMER0_OVF_vect) {
  TCNT0 = 0x00;
  PORTC &= ~(1<<PORTC4);    
}

解决方法

纠正了一些事情,也许这会有所帮助:


#define F_CPU 1000000UL

#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>


ISR(TIMER0_OVF_vect) {
    // TCNT0 = 0x00;  // Not necessary,it is done by hardware
    PORTC = ~(1<<PINC4);
}

int main(void)
{
    //define inputs and outputs
    DDRC = (1<<PINC4);
    
    // TCNT0 = 0x00;    // Autoset to zero after initialisation
    // TCCR0A = 0x00;   // Autoset at startup
    
    // Timer 0
    // Mode: Overflow,Interrupt
    // Prescaler: /64
    TCCR0B = (1<<CS01) | (1<<CS00);
    TIMSK0 = (1<<TOIE0);
    
    sei(); // set global interrupts
    
    while (1)
    {
        // PORTC |= (1<<PINC4); // is done in interrupt
    }
}

Build succeeded.

========== Rebuild All: 1 succeeded,0 failed,0 skipped ==========

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。