[英]Setting up Interrupts on atmega328 in pure C
我目前正在開發一個 Arduino Uno 開發板,我試圖在不使用 Arduino 庫的情況下用純C
編寫它。
我正在工作的項目應該是這樣的:
將 LED PB0
至BP7
設置為ON
和OFF
。
在連接到按鈕的 PD2 上設置中斷。
按下按鈕時,LED 應停止(暫停)。
當再次按下按鈕時,LED 應再次ON
從最后一個OFF
LED 開始。
更准確地說:
play()
被調用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
。
嘗試這樣的事情。
#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 開始。 一探究竟。 有事就換。 我只是給出了想法。 希望能幫助到你 :)
我在@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;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.