better square waves, still a bit unprecise in frequency

main
EmaMaker 2021-06-10 21:34:20 +02:00
parent 10da039759
commit 87b1c51cf0
3 changed files with 44 additions and 44 deletions

View File

@ -8,8 +8,8 @@
#define extr extern
#endif
#define MAX_VALUE 4096
#define MAX_VALUE_HALF 2048
#define MAX_VALUE 512
#define MAX_VALUE_HALF 256
//How much time (nanoseconds) is required for an analogWriteDAC0() call
#define ANALOG_WRITE_TIME_NS 477
@ -18,8 +18,7 @@
// How much time a single clock cycle takes
#define CLOCK_TO_NS 9
// Overhead when counting cycles
#define CYCLE_OVERHEAD 0
#define CYCLE_OVERHEAD 12
#define DAC1_SEL 23
#define DAC0_SEL 22

View File

@ -25,48 +25,49 @@ void setup() {
}
void loop() {
if(receivingInfo){
generateSquareDAC1(100000, 50);
// if(receivingInfo){
Serial1.write(42); //The answer to life, universe and everything else
digitalWriteFast(LED_BUILTIN, HIGH);
// Serial1.write(42); //The answer to life, universe and everything else
// digitalWriteFast(LED_BUILTIN, HIGH);
Serial1.flush();
// Serial1.flush();
delay(1000);
receivingInfo = false;
valid_flag = false;
// delay(1000);
// receivingInfo = false;
// valid_flag = false;
while(!valid_flag){
while(Serial1.available()) s = Serial1.readStringUntil('W');
Serial.println(s);
//Check if it's a valid string, it should start with 'w'
if(!s.startsWith('w')) continue;
// while(!valid_flag){
// while(Serial1.available()) s = Serial1.readStringUntil('W');
// Serial.println(s);
// //Check if it's a valid string, it should start with 'w'
// if(!s.startsWith('w')) continue;
// Get type
tmp_type = s.substring(s.indexOf('t')+1, s.indexOf('T'));
type = tmp_type.toInt();
// Get duty
tmp_duty = s.substring(s.indexOf('d')+1, s.indexOf('D'));
duty = tmp_duty.toInt();
// Get frequency
tmp_frequency = s.substring(s.indexOf('f')+1, s.indexOf('F'));
frequency = tmp_frequency.toInt();
// // Get type
// tmp_type = s.substring(s.indexOf('t')+1, s.indexOf('T'));
// type = tmp_type.toInt();
// // Get duty
// tmp_duty = s.substring(s.indexOf('d')+1, s.indexOf('D'));
// duty = tmp_duty.toInt();
// // Get frequency
// tmp_frequency = s.substring(s.indexOf('f')+1, s.indexOf('F'));
// frequency = tmp_frequency.toInt();
for(int i = 0; i < 100; i++) Serial1.write(69);
valid_flag = true;
// for(int i = 0; i < 100; i++) Serial1.write(69);
// valid_flag = true;
valid_flag = true;
Serial.print("Type: " );
Serial.println(type);
Serial.print("Duty: " );
Serial.println(duty);
Serial.print("Frequency: " );
Serial.println(frequency);
// valid_flag = true;
// Serial.print("Type: " );
// Serial.println(type);
// Serial.print("Duty: " );
// Serial.println(duty);
// Serial.print("Frequency: " );
// Serial.println(frequency);
digitalWriteFast(LED_BUILTIN, LOW);
generate(type, frequency, duty);
}
}
// digitalWriteFast(LED_BUILTIN, LOW);
// generate(type, frequency, duty);
// }
// }
}
void communicate(){

View File

@ -15,7 +15,7 @@ FASTRUN void generateSquare(float frequency, int duty){
digitalWriteFast(DAC1_SEL, HIGH);
startGenerating();
if(frequency < 1000){
if(frequency < 100000){
unsigned long periodMS = (1/ frequency) * pow(10, 6);
unsigned long dutyHigh = periodMS * duty * 0.01;
unsigned long dutyLow = periodMS * duty * 0.01;
@ -35,8 +35,8 @@ FASTRUN void generateSquare(float frequency, int duty){
//Duration of the logic LOW level
unsigned long dutyLow = periodNS - dutyHigh;
unsigned long dutyHighCycles = (unsigned long) (dutyHigh/CLOCK_TO_NS);
unsigned long dutyLowCycles = (unsigned long)(dutyLow/CLOCK_TO_NS);
unsigned long dutyHighCycles = (unsigned long) (dutyHigh/CLOCK_TO_NS+0.5f);
unsigned long dutyLowCycles = (unsigned long) (dutyLow/CLOCK_TO_NS+0.5f);
while(generateWave){
digitalWriteFast(SQUARE_PIN, HIGH);
@ -53,7 +53,7 @@ FASTRUN void generateSquareDAC1(float frequency, int duty){
digitalWriteFast(DAC1_SEL, HIGH);
startGenerating();
if(frequency < 1000){
if(frequency <= 50000){
unsigned long periodMS = (1/ frequency) * pow(10, 6);
unsigned long dutyHigh = periodMS * duty * 0.01;
unsigned long dutyLow = periodMS * duty * 0.01;
@ -73,8 +73,8 @@ FASTRUN void generateSquareDAC1(float frequency, int duty){
//Duration of the logic LOW level
unsigned long dutyLow = periodNS - dutyHigh;
unsigned long dutyHighCycles = (unsigned long) (dutyHigh/CLOCK_TO_NS);
unsigned long dutyLowCycles = (unsigned long)(dutyLow/CLOCK_TO_NS);
unsigned long dutyHighCycles = (unsigned long) (dutyHigh/CLOCK_TO_NS+0.5f);
unsigned long dutyLowCycles = (unsigned long) (dutyLow/CLOCK_TO_NS+0.5f);
while(generateWave){
analogWriteDAC1(MAX_VALUE);