【问题标题】:Setting up Interrupts on atmega328 in pure C用纯 C 在 atmega328 上设置中断
【发布时间】:2019-01-23 16:26:01
【问题描述】:

我目前正在开发一个 Arduino Uno 板,我正在尝试在不使用 Arduino 库的情况下以纯 C 编写它。

我正在做的项目应该是这样的:

  • 将 LED 指示灯 PB0 设置为 BP7 ON 和 OFF。

  • 在连接到 Button 的 PD2 上设置中断。

  • 按下按钮时,LED 应停止(暂停)。

  • 再次按下按钮时,LED 应再次变为 ON
    从 OFF 的最后一个 LED 开始。

更准确地说:

  • 函数play()被调用
  • LED 开始一个接一个地闪烁
  • 如果我按下按钮,play() 功能应该停止

这意味着如果连接到PB3 的 LED 上次 LED 亮起,当我再次按下按钮时,play() 功能应该从 PB4 继续。

这是我目前所拥有的:

#ifndef F_CPU
#define F_CPU 16000000UL
#endif

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

void play( void );

volatile uint8_t buttonWasPressed = 0;
const unsigned char LEDS[] = { PB0, PB1, PB2, PB3, PB4, PB5 };

int main(void)
{
    DDRB = 0xFF;  /// Set PORTB as OUTPUT

    DDRD  |=  ( 1 << PD7 );     /// Set PIN7 IN DDRD as INPUT
    PORTD &= ~( 1 << PD7 );     /// Set PIN7 on PORTD to LOW
    DDRD  &= ~( 1 << PD2 );     // Clear the PD2 pin and set PD2 (PCINT0 pin) now as input
    PORTD |= (1 << PD2);        // Set PIN PD2 as INPUT with pull-up enabled

    EICRA |= (1 << ISC00);      // set INT0 to trigger on ANY logic change
    EIMSK |= (1 << INT0);       // Turns on INT0
    sei();                      // turn on interrupts

    while(1)
    {
        play();
    }
}

ISR (INT0_vect)
{

    uint8_t buttonState = PIND & ( 1 << PD2 );
    if ( buttonState )
    {
        if ( buttonWasPressed == 0 )
        {
            PORTD ^= ( 1 << PD7 );  /// SET PIN 4 IN PORT B TO LOW
            _delay_ms( 200 );
            buttonWasPressed = 1;   /// update button state
        }
    }
    else                            /// The button is not pressed
    {
        buttonWasPressed = 0;       /// Update the button state
    }
}

void play( void )
{
    for ( uint8_t i = 0 ; i < 6 ; i++ )
    {
        PORTB |= ( 1 << LEDS[i] );  ///Turn LED ON
        _delay_ms( 250 );
        PORTB &= ~( 1 << LEDS[i] ); ///Turn LED OFF
        _delay_ms( 250 );
    }
}

此时,play() 函数将永远运行,如果我按下按钮,连接到 PD7 的 LED 将变为 ON 和 OFF。

【问题讨论】:

  • 我肯定会忽略 ISR“原型”。
  • @KIIV 我不知道。我会删除它。
  • @AlexK。这与arduino 无关,而是关于我需要继续的C 代码。我认为AVR 已经结束了
  • buttonWasPressed 对您的主代码有何影响?

标签: c avr atmega


【解决方案1】:

试试这样的。

#ifndef F_CPU
#define F_CPU 16000000UL
#endif

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

void play( unsigned int a );
ISR (INT0_vect);

volatile uint8_t buttonWasPressed = 0;
const unsigned char LEDS[] = { PB0, PB1, PB2, PB3, PB4, PB5 };
unsigned int ledNum = 0;

int main(void)
{
    DDRB = 0xFF;  /// Set PORTD as OUTPUT

    DDRD  |=  ( 1 << PD7 );     /// Set PIN7 IN DDRD as INPUT
    PORTD &= ~( 1 << PD7 );     /// Set PIN7 on PORTD to LOW
    DDRD  &= ~( 1 << PD2 );     // Clear the PD2 pin and set PD2 (PCINT0 pin) now as input
    PORTD |= (1 << PD2);        // Set PIN PD2 as INPUT with pull-up enabled

    EICRA |= (1 << ISC00);      // set INT0 to trigger on ANY logic change
    EIMSK |= (1 << INT0);       // Turns on INT0
    sei();                      // turn on interrupts

    while(1)
    {
        if(buttonWasPressed == 1)
            play(ledNum);

    }
}

ISR (INT0_vect)
{

    uint8_t buttonState = PIND & ( 1 << PD2 );
    if ( buttonState )
    {
        if ( buttonWasPressed == 0 )
        {
            PORTD ^= ( 1 << PD7 );  /// SET PIN 4 IN PORT B TO LOW
            _delay_ms( 200 );
            buttonWasPressed = 1;   /// update button state
        }
    }
    else                            /// The button is not pressed
    {
        buttonWasPressed = 0;       /// Update the button state
    }
}

void play( unsigned int a )
{
    for ( uint8_t i = a ; i < 6 ; i++ )
    {
        PORTB |= ( 1 << LEDS[i] );  ///Turn LED ON
        _delay_ms( 250 );
        PORTB &= ~( 1 << LEDS[i] ); ///Turn LED OFF
        _delay_ms( 250 );
        ledNum=i;
    }
}

通过定义一个变量来保存它打开的 LED,然后每当它恢复时,它将从最后一个点亮的 LED 开始。看看这个。有事就换。我只是给出了这个想法。希望对你有帮助:)

【讨论】:

  • 几乎可以工作,但是一旦我按下按钮,功能play() 就会停止,如果我再次按下按钮,只有PD7 LED 会闪烁。
【解决方案2】:

我在@Althaf1467 的帮助下设法修复了它。

工作代码是:

#ifndef F_CPU
#define F_CPU 16000000UL
#endif

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

#define LEDS_LENGTH 6

void play( void );

volatile uint8_t state = 0 ;
const unsigned char LEDS[] = { PB0, PB1, PB2, PB3, PB4, PB5 };

int main( void )
{
    DDRB = 0xFF;                    /// Set PORTD as OUTPUT

    DDRD  |=  ( 1UL << PD7   );     /// Set PIN7 IN DDRD as INPUT
    PORTD &= ~( 1UL << PD7   );     /// Set PIN7 on PORTD to LOW
    DDRD  &= ~( 1UL << PD2   );     /// Clear the PD2 pin and set PD2 (PCINT0 pin) now as input
    PORTD |=  ( 1UL << PD2   );     /// Set PIN PD2 as INPUT with pull-up enabled

    EICRA |=  ( 1UL << ISC00 );     /// set INT0 to trigger on ANY logic change
    EIMSK |=  ( 1UL << INT0  );     /// Turns on INT0
    sei();                          /// turn on interrupts

    while(1)
    {
        play();
    }
}

ISR ( INT0_vect )
{
    if ( PIND & ( 1UL << PD2 ) )
    {
        PORTD ^= ( 1UL << PD7 );    /// SET PIN 4 IN PORT B TO LOW
        state ^= ( 1 << 0 );        /// Swap the Buton State from 0 to 1 and back to 0 ...
        _delay_ms( 500 );
    }
}

void play( void )
{
    static uint8_t step = 0;
    while( step < LEDS_LENGTH )
    {
        if ( state == 1 )
        {
            break;
        }
        PORTB |=  ( 1UL << LEDS[ step ] );  /// Turn LED ON
        _delay_ms( 250 );

        PORTB &= ~( 1UL << LEDS[ step ] );  /// Turn LED OFF
        _delay_ms( 250 );

        step++;
    }

    if ( step == LEDS_LENGTH )
    {
        step = 0;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-20
    • 1970-01-01
    • 2012-03-29
    • 2011-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多