firmware: recalculate SAMPLE_FREQ for accurate frequency output

main
EmaMaker 2021-05-02 21:25:35 +02:00
parent 4467cc306f
commit d09cf0961b
7 changed files with 58 additions and 5 deletions

View File

@ -4,7 +4,7 @@
// Sampling frequency of the DAC
// Since the teensy runs on 168MHz and Fastest + LTO, using the DAC sampling frequency (1428571Hz) from the Datasheet kinda falls apart
#define SAMPLE_FREQ 5316436
#define SAMPLE_FREQ 6222000
//Max Freq with good results achievable with Teensy 3.5 DAC
#define MAX_ACHIEVABLE_FREQ 1000000

View File

@ -1,7 +1,9 @@
#pragma once
#define SQUARE_PIN 33
#define SQUARE_PIN A22 //DAC1
void setupSquare();
void generateSquare(float, int);
void generateSquareDAC1(float, int);

View File

@ -8,8 +8,8 @@
#define extr extern
#endif
#define MAX_VALUE 1024
#define MAX_VALUE_HALF 512
#define MAX_VALUE 4096
#define MAX_VALUE_HALF 2048
//How much time (nanoseconds) is required for an analogWriteDAC0() call
#define ANALOG_WRITE_TIME_NS 477
@ -20,6 +20,10 @@
// Overhead when counting cycles
#define CYCLE_OVERHEAD 0
#define DAC1_SEL 23
#define DAC0_SEL 22
FASTRUN void startGenerating();
FASTRUN void stopGenerating();
FASTRUN void delayCycles(unsigned long);

View File

@ -4,11 +4,15 @@
#include "sine.h"
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
setupWaves();
setupSine();
setupSquare();
}
FASTRUN void loop() {
generateSine(1000000);
generateSine(8150);
}

View File

@ -30,6 +30,9 @@ typedef int16_t __attribute__((__may_alias__)) aliased_int16_t;
FASTRUN void generateSine(float frequency){
startGenerating();
digitalWriteFast(DAC0_SEL, HIGH);
digitalWriteFast(DAC1_SEL, LOW);
// Phase accumulator
float phase = 0;

View File

@ -11,6 +11,9 @@ Lower frequency waves using nanoseconds is not needed and could actually causes
// Use FASTRUN to run code in RAM
FASTRUN void generateSquare(float frequency, int duty){
digitalWriteFast(DAC1_SEL, HIGH);
digitalWriteFast(DAC0_SEL, LOW);
startGenerating();
if(frequency < 1000){
unsigned long periodMS = (1/ frequency) * pow(10, 6);
@ -42,5 +45,39 @@ FASTRUN void generateSquare(float frequency, int duty){
delayCycles(dutyLowCycles);
}
}
}
FASTRUN void generateSquareDAC1(float frequency, int duty){
startGenerating();
if(frequency < 1000){
unsigned long periodMS = (1/ frequency) * pow(10, 6);
unsigned long dutyHigh = periodMS * duty * 0.01;
unsigned long dutyLow = periodMS * duty * 0.01;
while(generateWave){
analogWriteDAC1(MAX_VALUE);
delayMicroseconds(dutyHigh);
analogWriteDAC1(0);
delayMicroseconds(dutyLow);
}
}else{
unsigned long periodNS = (1/ frequency) * pow(10, 9);
//Duration of the logic HIGH level
unsigned long dutyHigh = periodNS * duty * 0.01;
//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);
while(generateWave){
analogWriteDAC1(MAX_VALUE);
delayCycles(dutyHighCycles);
analogWriteDAC1(0);
delayCycles(dutyLowCycles);
}
}
}

View File

@ -4,6 +4,9 @@
#include "wave.h"
void setupWaves(){
pinMode(DAC0_SEL, OUTPUT);
pinMode(DAC1_SEL, OUTPUT);
// Info from teensy forum, access cycle counter. This will be needed for square waves in particular
ARM_DEMCR |= ARM_DEMCR_TRCENA;
ARM_DWT_CTRL |= ARM_DWT_CTRL_CYCCNTENA;