it supposes to be like generate random pseudo sequence with 10Khz...well the result is random yet the frequency is just like 4KHz. anyway could make it 10Khz?Thansks
The random pseudo sequence (white noise) is usually generated by shift registers with an XORed feedback (from a few shiftreg bits). My old pic12c508 @4MHz does 20kHz in asm (~10hrs random seq period, afair), so arduino could be faster..
For example (an example of a 32-bit maximal period Galois LFSR from wikipedia above):
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
}
void loop() {
// put your main code here, to run repeatedly:
unsigned long lfsr = 1u;
unsigned bit;
unsigned long period = 0;
long t = micros();
do {
/* taps: 32 31 29 1; feedback polynomial: x^32 + x^31 + x^29 + x + 1 */
lfsr = (lfsr >> 1) ^ (-(lfsr & 1u) & 0xD0000001u);
++period; // remove this in your code
if (period==10000) Serial.println(micros()-t); // << remove this in your code then, put here the bit output
} while(lfsr != 1u);
period = 0;
}
Prints:
35844
So a loop takes 3.58usecs. It takes less when you remove the print line there, but you have output a bit so it might be the same (or even shorter)
The actual loop:
unsigned long lfsr = 1u;
for(;;) {
/* taps: 32 31 29 1; feedback polynomial: x^32 + x^31 + x^29 + x + 1 */
lfsr = (lfsr >> 1) ^ (-(lfsr & 1u) & 0xD0000001u);
// << HERE do output a bit to an output pin
// << HERE put a delay when required
}