From eb6d122bd48705444e48d28720475cad761492a6 Mon Sep 17 00:00:00 2001 From: heck Date: Sun, 6 Aug 2023 15:01:33 +0200 Subject: [PATCH] toggle DAC on timer interrupt --- Core/Src/cppmain.cc | 28 +++++++++++++++++++--------- Core/Src/tim.c | 2 +- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/Core/Src/cppmain.cc b/Core/Src/cppmain.cc index 8d6d1b8..3d4d0b0 100644 --- a/Core/Src/cppmain.cc +++ b/Core/Src/cppmain.cc @@ -36,14 +36,14 @@ void log(std::string &&msg) // DAC // --- -void set_dac_1(uint32_t val) +void set_dac_1(uint16_t val) { - HAL_DAC_SetValue(&hdac, DAC_CHANNEL_1, DAC_ALIGN_12B_L, val); + HAL_DAC_SetValue(&hdac, DAC_CHANNEL_1, DAC_ALIGN_12B_R, val); } -void set_dac_2(uint32_t val) +void set_dac_2(uint16_t val) { - HAL_DAC_SetValue(&hdac, DAC_CHANNEL_2, DAC_ALIGN_12B_L, val); + HAL_DAC_SetValue(&hdac, DAC_CHANNEL_2, DAC_ALIGN_12B_R, val); } @@ -65,7 +65,15 @@ void led_green_off() HAL_GPIO_WritePin(LED_GREEN_GPIO_Port, LED_GREEN_Pin, GPIO_PIN_RESET); } - +void dac1_toggle() { + static bool dac_state{ 0 }; + dac_state = !dac_state; + if (dac_state) { + set_dac_1(0b0000111111111111); + } else { + set_dac_1(0b0000000000000000); + } +} // MAIN // ---- @@ -77,10 +85,13 @@ void irq1() void timer_cb() { + dac1_toggle(); led_green_toggle(); log("timerUP"); } +// dont forget to start the DAC +// HAL_DAC_Start(&hdac, DAC_CHANNEL_1); void bytebeat() { HAL_DAC_Start(&hdac, DAC_CHANNEL_1); @@ -96,11 +107,10 @@ void bytebeat() void cppmain(void) { + HAL_DAC_Start(&hdac, DAC_CHANNEL_1); HAL_TIM_Base_Start_IT(&htim2); - while(true) { + while (true) { log("huhu"); - HAL_Delay(2000); + HAL_Delay(1000); } - - } diff --git a/Core/Src/tim.c b/Core/Src/tim.c index a064ebb..54f255b 100644 --- a/Core/Src/tim.c +++ b/Core/Src/tim.c @@ -43,7 +43,7 @@ void MX_TIM2_Init(void) htim2.Instance = TIM2; htim2.Init.Prescaler = 0; htim2.Init.CounterMode = TIM_COUNTERMODE_UP; - htim2.Init.Period = 4294967295; + htim2.Init.Period = 429400; htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; if (HAL_TIM_Base_Init(&htim2) != HAL_OK)