# PICAXE very short interval timer (for playing sound)

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!

-
This is a engineering site, so we don't say .0000226 seconds here. Find the power of 1000 so that there are 1-3 digits left of the decimal point and use the appropriate scaling prefix. In this case that would be 22.6 microseconds, or 22.6 us. Actually the "u" should really be the lower case greek mu, so use that when available, like in HTML of questions or answers. Unfortunately comments like this don't translate special character constructs like "&micro;" like real HTML does, so we're stuck with "u" here. –  Olin Lathrop May 27 '12 at 23:28
@Olin Lathrop Actually for me there are no problems writing say $10 \mbox{ } \mu F$ in comments using LaTeX. –  AndrejaKo May 28 '12 at 0:28
@Andre: And then you don't have real HTML anymore and that awful mathjax or whatever interpreter takes forever and messes up other things on your page. There is already a standard for such thing built into HTML. Unfortunatly comments here don't allow enough HTML thru, although questions and answers do. –  Olin Lathrop May 28 '12 at 13:36
add comment

## 1 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.

-
He's not using a PIC, he's using a PICAXE, which is a normal PIC16 with a token-interpreter on it. The people selling these have some PC software that takes a program in BASIC, tokenizes it, and dumps it to the MCU, which stores it in it's EEPROM or flash. It's basically a compact Basic-Stamp. I'm not even sure if they support interrupts. –  Connor Wolf May 28 '12 at 9:30
@Fake: The OP didn't specify what exactly a PICAXE was, so I assumed one of the many PIC development boards. If this thing has a interpreter between you and the hardware then you're probably out of luck, at least using it as intended. Ditch the silly sugar coating and use the PIC directly. Then what I said becomes possible. –  Olin Lathrop May 28 '12 at 13:34
Ya, the PICAXE does definitely support interrupts so I should be able to do what @OlinLathrop suggested fairly easily. As for dumping the PICAXE system all together and gettin some regular PICs, I'm working on that, the PICAXE platform is just kind of a crutch until I'm not trying to prototype stuff in my cramped dorm room lol. –  Chris May 28 '12 at 21:20
add comment