Hi,
I want to make a simple state analyzer with a MEGA2560. The lower 3 bits of Port K shall be recorded with following sketch:
#include <avr/io.h>
#define MAX_SIZE (1000)
uint8_t regA, regB;
uint8_t dat[MAX_SIZE];
uint16_t idx = 0;
uint8_t vcnt = 0; // repeat counter
uint8_t *ptdat = dat;
void setup(void) {
pinMode(A8, INPUT);
pinMode(A9, INPUT);
pinMode(A10, INPUT);
regA = (PORTK & 7); // lower 3 bits
}
void loop(void)
{
if (idx < MAX_SIZE) {
regB = (PORTK & 7);
if (regB == regA) {
if (vcnt < 31) vcnt++;
else {
*ptdat = regB | 248; // 31<<3 to upper 5 bits
ptdat++;
vcnt = 0;
idx++;
}
}
else { // new value
*ptdat = regB | (vcnt << 3);
ptdat++;
vcnt = 0;
idx++;
regA=regB;
}
}
}
With this code the compiler generates all variables in RAM.
To speed up the code regA, regB, idx, vcnt, and *ptdat could be placed into registers of the controller.
Is this possible without coding in assembler?
