Chirp Signal Generator

Dependencies:   mbed

Fork of TAU_ZOOLOG_Playback_Rev1_1 by Yossi_Students

Revision:
16:8274b4ad84ee
Parent:
13:24d8512fc722
Child:
17:c0c17da42990
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Mon Aug 21 11:58:40 2017 +0000
@@ -0,0 +1,118 @@
+/*
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+% Generate Chirp Signal - 21/08/2017    %
+% Arkadi Rafalovich - % Arkadiraf@gmail.com         %
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+Pinout:
+DAC -- PA_4 -- A2
+
+I/O -- PA_5 -- D13 (Status LED, Condition)
+I/O -- PA_6 -- D12 (Toggle Pin, Loop Freq)
+
+*/
+#include "mbed.h"
+#include "chirp.h"
+
+// Serial over USB as input device
+Serial pc(SERIAL_TX, SERIAL_RX);
+
+// mbed variables, Settings
+AnalogOut out(PA_4);
+
+// digital pins
+DigitalOut led(LED1);
+DigitalOut outPulse(PA_6); // Toggle pin, Loop Freq
+
+// User Button as interrupt
+DigitalIn mybutton(USER_BUTTON);
+
+//DAC declarations
+DAC_HandleTypeDef hdac1;
+
+// Dac Register for direct method of setting DAC value`s
+__IO uint32_t Dac_Reg = 0;
+
+// Variables
+bool toggle_state=0;
+
+// nop operation
+inline void NOP()
+{
+    __ASM volatile ("nop");    // one tick operation, Use to adjust frequency by slowing down the proccess
+}
+
+/* DAC1 init function */
+void DAC1_Init(void);
+
+// Main procedure
+int main()
+{
+    DAC1_Init();
+
+    HAL_DAC_Start(&hdac1, DAC_CHANNEL_1);
+
+    // define Dac Register for direct method of setting DAC value`s
+    Dac_Reg = (uint32_t) (hdac1.Instance);
+    Dac_Reg += __HAL_DHR12R1_ALIGNEMENT(DAC_ALIGN_12B_R);
+
+    // set outputs
+    outPulse.write(0);
+    led.write(0);
+    // Output value using DAC
+    // HAL_DAC_SetValue(&hdac1, DAC_CHANNEL_1, DAC_ALIGN_12B_R, ADCValueOut);
+    *(__IO uint32_t *) Dac_Reg = (uint16_t)(4095/2);
+
+    // Infinite loop
+    while(true) {
+        if (mybutton.read()==0) { // if button pressed, generate pulse out
+            led.write(1);
+            wait(0.005);
+            // generate chirp out
+            for (int ii=0; ii<NUM_SAMPLES; ii++) {
+                // toogle io for loop frequency
+                toggle_state=!toggle_state;
+                outPulse.write(toggle_state);
+                // generate delay for 1MHz Sample rate
+                for (int jj=0; jj<32; jj++) {
+                    NOP();
+                }
+                NOP();
+                NOP();
+                NOP();
+                NOP();
+                // Output value using DAC
+                // HAL_DAC_SetValue(&hdac1, DAC_CHANNEL_1, DAC_ALIGN_12B_R, ADCValueOut);
+                *(__IO uint32_t *) Dac_Reg = chirpData[ii];
+            }
+            // Output value using DAC
+            // HAL_DAC_SetValue(&hdac1, DAC_CHANNEL_1, DAC_ALIGN_12B_R, ADCValueOut);
+            *(__IO uint32_t *) Dac_Reg = (uint16_t)(4095/2);
+            led.write(0);
+            wait(1);
+        } // end button press
+        led.write(0);
+    }// end while(True)
+}
+
+
+// init dac
+
+/* DAC1 init function */
+void DAC1_Init(void)
+{
+    DAC_ChannelConfTypeDef sConfig;
+
+    // DAC Initialization
+    hdac1.Instance = DAC;
+    if(HAL_DAC_Init(&hdac1) != HAL_OK) {
+        printf("!!! Error in DAC initialization !!!\n");
+    }
+
+    // DAC channel OUT1 config
+    sConfig.DAC_Trigger = DAC_TRIGGER_NONE;
+    sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE;
+    if (HAL_DAC_ConfigChannel(&hdac1, &sConfig, DAC_CHANNEL_1) != HAL_OK) {
+        printf("!!! Error in DAC channel initialization !!!\n");
+    }
+}