私の質問は、リアルタイム データ ロギングとマルチ割り込みについてです。winAVR で MCU-ATMega 1280 をプログラムして、直交エンコーダ (20um/ピッチ) からパルスを読み取り、データをフラッシュ メモリ (Microchip SST25VF080B、シリアル SPI プロトコル) に保存しようとしています。エンコーダが動作を終了した後 (約 2 分)、MCU はデータをメモリから画面にエクスポートします。私のコードは以下です。
しかし、なぜそれが正しく実行されないのかわかりません。バグには 2 種類あります。1 つは突然トレンドから外れてしまうバグ、もう 1 つはエンコーダの動作が遅いのに値が突然ジャンプするバグです。ジャンプはターンがあるときだけ現れるようです。
ジャンプを除いて、私が予想したようにトレンドが発生するため、問題はデータの保存にのみあると思います。プログラムで行ったように、両方の ISR を実行するかどうかを尋ねたいだけです。ISR の実行中に別の ISR が介入する場合はありますか? atmega 1280 のデータシートによると、1 つの ISR が発生している場合、前の割り込みがそのルーチンを終了した後、他の割り込みは発生しないようです。
#include <stdlib.h>
#include <stdio.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include "USART.h" // this header is for viewing the data on the computer
#include "flashmemlib.h" // this header contains the function to read n
//write on the memory
#define MISO PB3
#define MOSI PB2
#define SCK PB1
#define CS PB0
#define HOLD PB6
#define WP PB7
#define sigA PD0 //INT0
#define sigB PD2 //INT2
#define LED PD3
uint8_t HADD,MADD,LADD, HDATA, LDATA,i; //HADD=high address, MADD-medium address, LADD-low address
volatile int buffer[8]; //this buffer will store the encoder pulse
uint32_t address = 0;
uint16_t DATA16B = 0;
int main(void)
{
INITIALIZE(); //initialize the IO pin, timer CTC mode, SPI and USART protocol
for(i=0;i<8;i++)
buffer[i]=0;
sei();
//AAI process- AAI is just one writing mode of the memory
AAIInit(address,0);
while (address < 50) //just a dummy loop which lasts for 5 secs (appox)
{
_delay_ms(100);
address++;
}
AAIDI();//disable AAI process
cli(); //disable global interrupt
EIMSK &= ~(1<<INT0);
TIMSK1 &= ~(1<<OCIE1A);
//code for reading procedure. i thought this part is unnecessary because i am quite //confident that it works correcly
return (0);
}
ISR(INT0_vect) // this interrupt is mainly for counting the number of encoder's pulses
{ // When an interrupt occurs, we only have to check the level of
// of pB to determine the direction
PORTB &= ~(1<<HOLD);
for(i=0;i<8;i++)
buffer[i+1]=buffer[i];
if (PIND & (1<<sigB))
buffer[0]++;
else buffer[0]--;
PORTB |= (1<<HOLD);
}
ISR(TIMER0_COMPA_vect) //after around 1ms, this interrupt is triggered. it is for storing the data into the memory.
{
HDATA =(buffer[7]>>8)&0xFF;
LDATA = buffer[7]&0xFF;
PORTB &= ~(1<<CS);
SEND(AD);
SEND(HDATA);
SEND(LDATA);
PORTB |=(1<<CS);
}
void SEND(volatile uint8_t data)
{
SPDR = data; // Start the transmission
while (!(SPSR & (1<<SPIF))){} // Wait the end of the transmission
}
uint8_t SREAD(void)
{
uint8_t data;
SPDR = 0xff; // Start the transmission
while (!(SPSR & (1<<SPIF))){} // Wait the end of the transmission
data=SPDR;
return data;
}