Learning How The Hell ARM Works
So This week was mostly spent reading about the differences between Atmel/Harvard and ARM.
One of the main differences/annoyances is that ARM chips need to be wiped every time they
are programmed. The arduino library states that it does this automatically, but has proven
unreliable in practice. So I have gotten use to manually erasing programs on the Due before
uploading a new one.
One helpful feature is that the Due gives you a much more verbose series on updates as you
upload a sketch. We were wondering how much analogWrite slowed us down? Well, I didn't get
timings, but I got pages taken to store. Without it, the program I wrote takes about 47 pages,
but with it analogWrite it takes 107. 60 more pages to make a single analogWrite.
My problems getting the Due to actually use the DAC have not stopped, but I did manage to get
the normal analogue pins to write out 12 bit values. The problem is that it is too slow.
The next week will be spent running optimizations (look-up tables, direct addressing, ect).
Here is this weeks code:
/* Synth.ino
----------------------------------------------------------
Description:
A sketch to generate sound waves for audio processing
with an arduino Due.
State:
"in early developement" (AKA not functioning yet).
Something is slowing down the cycles to the point
where it just sounds like a speaker-pop. All
the coret values are beings processed, just not
quickly enough.
----------------------------------------------------------
References:
http://makezine.com/projects/make-35/advanced-arduino-sound-synthesis/
https://www.arduino.cc/en/Tutorial/SecretsOfArduinoPWM
----------------------------------------------------------
Contact: ldavis@marlboro.edu
https://github.com/SafeCamp
https://safecamp.github.io/
----------------------------------------------------------
Logan Davis | 3/5/16 | Arduino 1.6.7
----------------------------------------------------------
*/
#include <avr/pgmspace.h>
//-----WAVE FORM NAMES-----//
#define TRIANGLE 0
#define SAW 1
#define SQUARE 2
#define WHITENOISE 3
#define SINE 4
//-----PIN NAMES------//
#define WAVESELECT A0
#define FREQ A1
#define AUDIO_PORT DAC0
int declining_triangle[2048] = {0};
unsigned int i = 0;
void setup(){
/* Set PWM pin to OUTPUT */
pinMode(AUDIO_PORT, OUTPUT);
analogWriteResolution(8);
gen_downward_slope(declining_triangle);
//Serial.begin(9600);
}
void loop() {
/*check to see if the user wants a different wave
* if the selection is different, change waveforms.
* After that check, output waveform value and
* increase index.
*/
analogWriteResolution(12);
int output = 0;
//byte choice = map(analogRead(WAVESELECT), 0, 1023, 0, 4);
byte choice = 1;
switch(choice){
case TRIANGLE:
output = gen_triangle(i);
break;
case SAW:
output = gen_saw(i);
break;
case SQUARE:
output = gen_square(i);
break;
case WHITENOISE:
output = gen_noise();
break;
case SINE:
output = gen_sine(i);
break;
}
//Serial.println(i);
analogWrite(A11,output);
i++;
delayMicroseconds(analogRead(FREQ));
}
int gen_sine(int i){
return int(2048 + (2047 * sin((6.283185 / 4096) * (i % 4096))));
}
int gen_noise(){
return random(4095);
}
int gen_square(int i){
if(i < 2048){
return 0;
} else {
return 4095;
}
}
int gen_saw(int i){
return i % 4096;
}
int gen_triangle(int i){
if(i < 2048){
return i;
} else {
return declining_triangle[i-2048];
}
}
void gen_downward_slope(int slope[]){
int i = 0;
int value = 4095;
for(i = 0;i < 2048;i++){
slope[i] = value;
value -= 2;
}
}