There is no calculation in real time : the LUT (the array *IS* a LUT
) is filled once, in setup() function. (it should have been declared as unsigned char array, but it does not make any difference)
This little crappy piece of code was just ran to test the ladder ! And also test the analogWrite() function : it is absolutely unusable, even for an ultra low frequency test ! Incredibly slow. digitalWrite() is definitely *NOT* usable for that. I do not know about direct port manipulations, and have to learn about. I did'nt code at hardware level since late '80 (Turbo C 1.5 and TASM, 25+ years ago). In the Arduino world, I always found people who wrote smart hi level libraries to do the hard job
...
As a test, and exercise, I want to copy ADC rezadings to the ladder. It will be the first step before serializing data to an optocoupler and a 2nd Arduino.
[UPDATE]
Done !
At first, I got strange results : the DAC seemed to be limited to 1KHz. 1KHz seemed to be related to some obscure prescaler, as analogWrite (500 KHz PWM by default). After some readings and some more testings, I discovered someone who already did exactly what I am trying to do. Why reinventing the wheel
?
http://www.instructables.com/id/Arduino-Audio-Input/?ALLSTEPSJust a few keystrokes, and here is the test code :
//Audio out with 38.5kHz sampling rate and interrupts
//by Amanda Ghassaei
//http://www.instructables.com/id/Arduino-Audio-Input/
//Sept 2012
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
*/
void setup()
{
for(int i=0; i<8; i++)
pinMode(30+i, OUTPUT);
cli();//disable interrupts
//set up continuous sampling of analog pin 0
//clear ADCSRA and ADCSRB registers
ADCSRA = 0;
ADCSRB = 0;
ADMUX |= (1 << REFS0); //set reference voltage
ADMUX |= (1 << ADLAR); //left align the ADC value- so we can read highest 8 bits from ADCH register only
//////////////////////////////////////////////////////////////////////////////////////////////
// PRESCALER
ADCSRA |= (1 << 0/*ADPS2*/) | (1 << ADPS0); //set ADC clock with 32 prescaler- 16mHz/32=500kHz
//
//////////////////////////////////////////////////////////////////////////////////////////////
ADCSRA |= (1 << ADATE); //enabble auto trigger
ADCSRA |= (1 << ADIE); //enable interrupts when measurement complete
ADCSRA |= (1 << ADEN); //enable ADC
ADCSRA |= (1 << ADSC); //start ADC measurements
sei();//enable interrupts
//if you want to add other things to setup(), do it here
}
ISR(ADC_vect)
{
PORTC=ADCH<<1; // magic number for test signal scaling..
}
void loop()
{
}
With an even more reduced prescaler, the Mega can process 10 KHz sinus quite well. 1 or 2 KHz BW (Shanon...) will probably be enough for 50 Hz signals (it is intended to run on 8MHz MCU, and with 2 ADC + 2 R2R DAC).
There will be to code a communication protocol. Probes and receiver could be connecteed via optic fiber. The ATmega168 offers 10-bit ADC ; it could be interesting to use 10 bits...
The scope shows 10 KHz signals : blue = analog input from signal generator, yellow = 8-bit R2R output.
Next experiments : 10 bit ADC/DAC, "probe" = Arduino Mega, "receiver" + DAC = Arduino Due (still waiting for the Pro Mini's), and UART communications.