/**
* V. Hunter Adams ([email protected])
*/
#include "pico/stdlib.h"
#include <math.h>
#include "hardware/timer.h"
#include "hardware/irq.h"
#include <stdio.h>
#define ALARM_NUM 0
#define two32 4294967296.0 // 2^32
#define Fs 50000
#define DELAY 20
volatile unsigned int phase_accum_main;
volatile unsigned int phase_incr_main = (100.0*two32)/Fs ;//
#define sine_table_size 256
volatile int sin_table[sine_table_size] ;
#define ALARM_IRQ TIMER_IRQ_0
// Main (runs on core 0)
uint16_t DAC_data;
void print_uint(uint32_t num) {
char buf[10];
int i = 0;
if (num == 0) {
putchar('0');
} else {
while (num > 0 && i < sizeof(buf)) {
buf[i++] = '0' + (num % 10);
num /= 10;
}
while (i--) {
putchar(buf[i]);
}
}
putchar(','); putchar(' ');
}
static void alarm_irq(void) {
// Clear the alarm irq
hw_clear_bits(&timer_hw->intr, 1u << ALARM_NUM);
// Reset the alarm register
timer_hw->alarm[ALARM_NUM] = timer_hw->timerawl + DELAY;
// DDS phase and sine table lookup
phase_accum_main += phase_incr_main ;
DAC_data= (sin_table[phase_accum_main>>24]+2048);
print_uint(DAC_data);
}
int main() {
// === build the sine lookup table =======
// scaled to produce values between 0 and 4096
int ii;
for (ii = 0; ii < sine_table_size; ii++){
sin_table[ii] = (int)(2047*sin((float)ii*6.283/(float)sine_table_size));
}
stdio_init_all();
// Enable the interrupt for the alarm (we're using Alarm 0)
hw_set_bits(&timer_hw->inte, 1u << ALARM_NUM) ;
// Associate an interrupt handler with the ALARM_IRQ
irq_set_exclusive_handler(ALARM_IRQ, alarm_irq) ;
// Enable the alarm interrupt
irq_set_enabled(ALARM_IRQ, true) ;
// Write the lower 32 bits of the target time to the alarm register, arming it.
timer_hw->alarm[ALARM_NUM] = timer_hw->timerawl + DELAY ;
// Initialize the LED pin
// Loop
while (true) {
}
}