Hi all,
Please note that the purpose of this topic is validating the well-known saying: “Necessity is the mother of invention... or innovation”.
I had an ATmega8 and an 8 MHz crystal for it. I needed to output as fast as possible a delta bit stream (its bits are stored as bytes in the MCU SRAM space) whose size could be 96 up to 103 bits.
The outputted serial bits are supposed to generate, after filtering, a sinewave signal.
I ended up, after many months (being not very smart), writing a code (not trivial) that outputs the bits (read from a SRAM table) at a rate of 4 MCU cycles (0.5 us). Therefore, in case of delta Nbits per cycle, the frequency of the generated sinewave is:
F_sine = 1 / (4 * MCU_cycle * Nbits)
Nbits= 96 ==> 20,833 Hz
Nbits= 97 ==> 20,619 Hz
Nbits= 98 ==> 20,408 Hz
Nbits= 99 ==> 20,202 Hz
Nbits= 100 ==> 20,000 Hz
Nbits= 101 ==> 19,802 Hz
Nbits= 102 ==> 19,608 Hz
Nbits= 103 ==> 19,417 kHz
Obviously, the sinewave loop, while running (generating the sinewave), needs to detect the last bit in the SRAM table and restart from the first bit without taking any extra MCU cycle.
For instance, while 96 bits are saved in 12 bytes (12*8-bit), the 97 to 103 bits per cycle have to be saved with an extra byte to include the extra bits, MOD(Nbits,8).
And, in a practical application, if Nbits is odd, the bit stream should be made for 2 sinewave cycles instead of one. For example, if Nbits=97, the size of the bit stream is 194 bits, saved in 24+1 bytes (the extra byte is for the remaining 2 bits).
Sorry, I don’t think any of the readers here has an idea now on how to write such a fast code; not because I am smarter than others but because no one likely ‘needed it’, as I did, in the first place. But, if someone did (needed it), he surely found out how to do it.
For instance, I will be surprised really if someone will be able to find the solution by searching it in the internet.
I will post my work after hearing some serious comments.
Have fun,
Kerim
Added:
Please note that I attached how I did the 4-cycle_per_bit.asm on reply #20