Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "riscv_csr_encoding.h"
- #include "scr1_csr_encoding.h"
- #include "mcu32_memory_map.h"
- #include <power_manager.h>
- #include "pad_config.h"
- #include <gpio_irq.h>
- #include <epic.h>
- #include <csr.h>
- #include <gpio.h>
- #include "mik32_hal_dac.h"
- #include <stdint.h>
- #define GPIO_X ((GPIO_TypeDef*)GPIO_0_BASE_ADDRESS )
- #define SAMPLES 128U
- #define DAC_UREF 1200U
- #define DAC_MAX_CODE 4095
- static uint16_t square_table[SAMPLES];
- void wait_micros(uint32_t microseconds) {
- static const cycles_per_microsecond = 32;
- static const cycles_per_iteration = 2;
- static const iterations_per_ms = cycles_per_microsecond / cycles_per_iteration;
- const iterations_count = iterations_per_ms * microseconds;
- for(volatile uint32_t i = 0; i < iterations_count; i++);
- }
- static uint16_t mv_to_dac_code(uint32_t mv){
- if(mv > DAC_UREF) return (uint16_t)DAC_MAX_CODE;
- return (uint16_t)((mv * DAC_MAX_CODE + DAC_UREF / 2) / DAC_UREF);
- }
- static void fill_square_table_500mV(void)
- {
- uint16_t high = mv_to_dac_code(500); // ~1706
- for (uint32_t i = 0; i < SAMPLES / 2; i++)
- {
- square_table[i] = high;
- }
- for (uint32_t i = SAMPLES / 2; i < SAMPLES; i++)
- {
- square_table[i] = 0;
- }
- }
- static const uint16_t sine_table[SAMPLES] = {
- 2048, 2148, 2248, 2348, 2447, 2545, 2642, 2737,
- 2831, 2923, 3013, 3100, 3185, 3267, 3346, 3423,
- 3495, 3565, 3630, 3692, 3750, 3804, 3853, 3898,
- 3939, 3975, 4007, 4034, 4056, 4073, 4085, 4093,
- 4095, 4093, 4085, 4073, 4056, 4034, 4007, 3975,
- 3939, 3898, 3853, 3804, 3750, 3692, 3630, 3565,
- 3495, 3423, 3346, 3267, 3185, 3100, 3013, 2923,
- 2831, 2737, 2642, 2545, 2447, 2348, 2248, 2148,
- 2048, 1947, 1847, 1747, 1648, 1550, 1453, 1358,
- 1264, 1172, 1082, 995, 910, 828, 749, 672,
- 600, 530, 465, 403, 345, 291, 242, 197,
- 156, 120, 88, 61, 39, 22, 10, 2,
- 0, 2, 10, 22, 39, 61, 88, 120,
- 156, 197, 242, 291, 345, 403, 465, 530,
- 600, 672, 749, 828, 910, 995, 1082, 1172,
- 1264, 1358, 1453, 1550, 1648, 1747, 1847, 1947
- };
- /* static const uint32_t const_FloatApproxArr[] = {
- 1706, 1813, 1920, 2026, 2131, 2234, 2334,
- 2433, 2528, 2621, 2709, 2794, 2874, 2950,
- 3021, 3087, 3147, 3201, 3250, 3293, 3329,
- 3359, 3382, 3399, 3409, 3413, 3409, 3399,
- 3382, 3359, 3329, 3293, 3250, 3201, 3147,
- 3087, 3021, 2950, 2874, 2794, 2709, 2621,
- 2528, 2433, 2334, 2234, 2131, 2026, 1920,
- 1813, 1706, 1599, 1492, 1387, 1282, 1179,
- 1078, 980, 884, 792, 703, 619, 538,
- 462, 392, 326, 266, 211, 162, 120,
- 84, 54, 30, 13, 3, 0, 3,
- 13, 30, 54, 84, 120, 162, 211,
- 266, 326, 392, 462, 538, 619, 703,
- 792, 884, 980, 1078, 1179, 1282, 1387,
- 1492, 1599
- };
- const int const_FloatApproxArrSize = sizeof( const_FloatApproxArr ) / sizeof( const_FloatApproxArr[ 0 ] );
- */
- void main() {
- PM->CLK_APB_P_SET = PM_CLOCK_APB_P_GPIO_0_M
- | PM_CLOCK_APB_P_GPIO_1_M
- | PM_CLOCK_APB_P_GPIO_2_M;
- PM->CLK_APB_M_SET = PM_CLOCK_APB_M_PAD_CONFIG_M
- | PM_CLOCK_APB_M_WU_M
- | PM_CLOCK_APB_M_PM_M
- | PM_CLOCK_APB_M_EPIC_M;
- PM->CLK_AHB_SET |= PM_CLOCK_AHB_SPIFI_M;
- DAC_HandleTypeDef hdac1;
- DAC_HandleTypeDef hdac2;
- void SystemClock_Config(void);
- // DAC Init
- hdac1.Instance = ANALOG_REG;
- hdac1.Instance_dac = HAL_DAC1;
- hdac1.Init.DIV = 0; // Делитель частоты
- hdac1.Init.EXTRef = DAC_EXTREF_OFF; // Выбор источника опорного напряжения: «1» - внешний; «0» - встроенный
- hdac1.Init.EXTClb = DAC_EXTCLB_DACREF; // Выбор источника внешнего опорного напряжения: «1» - внешний вывод; «0» - настраиваемый ОИН
- HAL_DAC_Init(&hdac1);
- // DAC Init
- hdac2.Instance = ANALOG_REG;
- hdac2.Instance_dac = HAL_DAC2;
- hdac2.Init.DIV = 0; // 255
- hdac2.Init.EXTRef = DAC_EXTREF_OFF; // Выбор источника опорного напряжения: «1» - внешний; «0» - встроенный
- hdac2.Init.EXTClb = DAC_EXTCLB_DACREF; // Выбор источника внешнего опорного напряжения: «1» - внешний вывод; «0» - настраиваемый ОИН
- HAL_DAC_Init(&hdac2);
- fill_square_table_500mV();
- GPIO_0->DIRECTION_OUT = 1 << 10;
- GPIO_1->DIRECTION_OUT = 1 << 12;
- GPIO_1->DIRECTION_OUT |= 1 << 13;
- uint32_t k = 0, i = 0;
- //#define MEANDR
- uint16_t vals[2] = {0, 1706};
- uint16_t val;
- while (1){
- #ifdef MEANDR
- HAL_DAC_SetValue(&hdac1, 0);
- for(volatile uint32_t i = 0; i < 128; i++);
- HAL_DAC_SetValue(&hdac1, 1706);
- for(volatile uint32_t i = 0; i < 128; i++);
- #else
- HAL_DAC_SetValue(&hdac1, sine_table[i++ % SAMPLES]);
- wait_micros(1);
- #endif
- }
- }
Add Comment
Please, Sign In to add comment