PICAXE very short interval timer (for playing sound)

audiooptimizationpicaxesoundtiming

I have a PICAXE (08M2) that I'm using to play sound, using the Roman Black algorithm mentioned in the answer to this question. My question, then, is this: How do I get the PICAXE to wait for the appropriate time interval (1/bit rate = 1/44100 = 22.6 μS) (I think) between each bit? As far as I know, the WAIT command only goes down to one millisecond, much longer than I need. Furthermore, I'm loading the sound bits in from an I2C EEPROM chip (with a 440kHz bus), so how do I factor in the time it takes to load each byte in from there, perhaps into a 4-byte (32 bit) buffer? It seems to me that any slight variation in the timing of the code would result in the sound getting pretty distorted, so how does one compensate for this? I'm (obviously) pretty new at this, so thanks for any insights!

Best Answer

If you want to do something regularly every 23 µs, use a interrupt. You didn't say what PIC your board uses or what its clock rate is, but even if it's a old 16F at 20 MHz clock, it can still do 5 MIPS, or 5 instructions per microcsecond. That would leave 115 instructions per 23 µs interrupt. That could be tight if it has to go to the EEPROM every time.

If this algorithms only needs a individual bit every 23 µs, then perhaps a foreground loop can read the EEPROM a byte at a time into a FIFO and the interrupt routine only process a single bit each time with one FIFO fetch every 8 interrupts. That should be quite doable in well under 100 instructions.