C 用ATMega 1280单片机实现实时数据记录的多中断
我的问题是关于实时数据记录和多中断。 我试图通过winAVR对一个MCU ATMega 1280进行编程,使其从正交编码器(20um/节距)读取脉冲,并将数据存储到闪存中(微芯片SST25VF080B,串行SPI协议)。编码器完成操作后(大约2分钟),MCU将数据从内存导出到屏幕。我的代码如下 但我不知道为什么它没有正确运行。有两种错误:一种错误是某些点突然偏离趋势,另一种错误是编码器运行缓慢但突然跳值。跳跃似乎只有在转弯时才会出现 我认为问题可能只存在于数据存储中,因为趋势与我预期的一样,除了跳跃。我只是想问我是否像在程序中那样运行两个ISR。是否存在一个ISR在运行时会被另一个ISR干预的情况?根据atmega 1280数据表,当一个ISR发生时,在前一个中断完成其例程后,似乎不允许发生其他中断C 用ATMega 1280单片机实现实时数据记录的多中断,c,logging,real-time,interrupt,winavr,C,Logging,Real Time,Interrupt,Winavr,我的问题是关于实时数据记录和多中断。 我试图通过winAVR对一个MCU ATMega 1280进行编程,使其从正交编码器(20um/节距)读取脉冲,并将数据存储到闪存中(微芯片SST25VF080B,串行SPI协议)。编码器完成操作后(大约2分钟),MCU将数据从内存导出到屏幕。我的代码如下 但我不知道为什么它没有正确运行。有两种错误:一种错误是某些点突然偏离趋势,另一种错误是编码器运行缓慢但突然跳值。跳跃似乎只有在转弯时才会出现 我认为问题可能只存在于数据存储中,因为趋势与我预期的一样,除了
#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;
}
#包括
#包括
#包括
#包括
#包括“USART.h”//此标题用于查看计算机上的数据
#include“flashmemlib.h”//此标头包含读取n的函数
//写在存储器上
#定义味噌PB3
#定义MOSI-PB2
#定义SCK PB1
#定义CS PB0
#定义保持PB6
#定义WP PB7
#定义sigA PD0//INT0
#定义sigB-PD2//INT2
#定义LED PD3
uint8_t HADD,MADD,LADD,HDATA,LDATA,i//HADD=高地址、MADD中地址、LADD低地址
易失性int缓冲区[8]//该缓冲器将存储编码器脉冲
uint32_t地址=0;
uint16_t数据16b=0;
内部主(空)
{
初始化();//初始化IO引脚、定时器CTC模式、SPI和USART协议
对于(i=0;i在INT0的中断服务例程中,您正在写入:
for(i=0;i<8;i++)
buffer[i+1]=buffer[i];
AVR将根据ATmega1280数据表,按照您所述管理中断。或者,如果您希望通过另一个中断来允许ISR向量中断,则需要执行以下操作(示例):
for(i=0;i<7;i++)
buffer[i+1]=buffer[i];
ISR(INT0_vect, ISR_NOBLOCK)
{...
...}