Hello. i am here with rather newbie problem, probably my own creation. mostly related to downloading untested arduino code from the internet and trying to shoehorn it to a different microcontroller just because i have those not arduino :].
I never really worked with timers that much so i can't really decipher why it no work. if the oruginal code used 16 bit timer it would probably have no consequences which atmel chip it works on, because by datasheet both arduino (atmega 328) and atmega 8 have it configured in exact the same way. sadly project used timer 0 which in atmega 8 is vastly cut down. long story short i don't understand the code well enough to come up with the alternative solution.
#define TIMER_PRESCALER 64
#define DccBitTimerCount (F_CPU * 80L / TIMER_PRESCALER / 1000000L)
// 16000000 * 80 / 64 / 1000000 = 20; 20 x 4usecs = 80us
//========================
void beginBitDetection() {
TCCR0A &= B11111100;
attachInterrupt(0, startTimer, RISING);
}
//========================
void startTimer() {
OCR0B = TCNT0 + DccBitTimerCount;
TIMSK0 |= B00000100;
TIFR0 |= B00000100;
}
//========================
ISR(TIMER0_COMPB_vect) {
byte bitFound = ! ((PIND & B00000100) >> 2);
TIMSK0 &= B11111011;
byte nbs = nextBitSlot(bitBuffHead);
if (nbs == bitBuffTail) return;
else {
bitBuffHead = nbs;
bitBuffer[bitBuffHead] = bitFound;
}
}
//========================
Those are parts of code of the program i have problems with. i was able to convert the rest with no issues, since it is a simple terminal talking program collecting informations from incoming model train controller taking up as a whole only half of atmega 8 flash. A bit of a waste of arduino really, which i don't have anyway
.
I am actually compiling and uploading this in arduino ide via usbasp after installing minicore boards. i am not sure if that is a good idea or would winavr be a bit faster after compiling, anyways, when i randomly change timers to ones existing on my ic without even understanding what they do code compiles, but also crashes right after sending the setup message.
if someone kindly explain to me how this code work i probably would be able to climb my way back studying atmega 8 datasheet. i am actually not interested in solution alone, because then i will kind of be out of the loop and still learn nothing.
If i understand it corectly porting it to timer1, the 16 bit one would make the code more universal, because both ic's use the same 16 bit counters but it would bee too slow if it have to work with full count and would have to be heavyly prescaled. but that just my ignorance talking.