lutunovoleg

Untitled

Mar 4th, 2026
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.92 KB | None | 0 0
  1. #include "riscv_csr_encoding.h"
  2. #include "scr1_csr_encoding.h"
  3. #include "mcu32_memory_map.h"
  4. #include <power_manager.h>
  5. #include "pad_config.h"
  6. #include <gpio_irq.h>
  7. #include <epic.h>
  8. #include <csr.h>
  9. #include <gpio.h>
  10. #include "mik32_hal_dac.h"
  11. #include <stdint.h>
  12.  
  13.  
  14. #define GPIO_X ((GPIO_TypeDef*)GPIO_0_BASE_ADDRESS )
  15.  
  16. #define SAMPLES 128U
  17. #define DAC_UREF 1200U
  18. #define DAC_MAX_CODE 4095
  19.  
  20. static uint16_t square_table[SAMPLES];
  21.  
  22. void wait_micros(uint32_t microseconds) {
  23. static const cycles_per_microsecond = 32;
  24. static const cycles_per_iteration = 2;
  25. static const iterations_per_ms = cycles_per_microsecond / cycles_per_iteration;
  26. const iterations_count = iterations_per_ms * microseconds;
  27. for(volatile uint32_t i = 0; i < iterations_count; i++);
  28. }
  29.  
  30. static uint16_t mv_to_dac_code(uint32_t mv){
  31. if(mv > DAC_UREF) return (uint16_t)DAC_MAX_CODE;
  32. return (uint16_t)((mv * DAC_MAX_CODE + DAC_UREF / 2) / DAC_UREF);
  33. }
  34.  
  35.  
  36. static void fill_square_table_500mV(void)
  37. {
  38. uint16_t high = mv_to_dac_code(500); // ~1706
  39. for (uint32_t i = 0; i < SAMPLES / 2; i++)
  40. {
  41. square_table[i] = high;
  42. }
  43. for (uint32_t i = SAMPLES / 2; i < SAMPLES; i++)
  44. {
  45. square_table[i] = 0;
  46. }
  47. }
  48.  
  49.  
  50. static const uint16_t sine_table[SAMPLES] = {
  51. 2048, 2148, 2248, 2348, 2447, 2545, 2642, 2737,
  52. 2831, 2923, 3013, 3100, 3185, 3267, 3346, 3423,
  53. 3495, 3565, 3630, 3692, 3750, 3804, 3853, 3898,
  54. 3939, 3975, 4007, 4034, 4056, 4073, 4085, 4093,
  55. 4095, 4093, 4085, 4073, 4056, 4034, 4007, 3975,
  56. 3939, 3898, 3853, 3804, 3750, 3692, 3630, 3565,
  57. 3495, 3423, 3346, 3267, 3185, 3100, 3013, 2923,
  58. 2831, 2737, 2642, 2545, 2447, 2348, 2248, 2148,
  59. 2048, 1947, 1847, 1747, 1648, 1550, 1453, 1358,
  60. 1264, 1172, 1082, 995, 910, 828, 749, 672,
  61. 600, 530, 465, 403, 345, 291, 242, 197,
  62. 156, 120, 88, 61, 39, 22, 10, 2,
  63. 0, 2, 10, 22, 39, 61, 88, 120,
  64. 156, 197, 242, 291, 345, 403, 465, 530,
  65. 600, 672, 749, 828, 910, 995, 1082, 1172,
  66. 1264, 1358, 1453, 1550, 1648, 1747, 1847, 1947
  67. };
  68.  
  69.  
  70.  
  71. /* static const uint32_t const_FloatApproxArr[] = {
  72. 1706, 1813, 1920, 2026, 2131, 2234, 2334,
  73. 2433, 2528, 2621, 2709, 2794, 2874, 2950,
  74. 3021, 3087, 3147, 3201, 3250, 3293, 3329,
  75. 3359, 3382, 3399, 3409, 3413, 3409, 3399,
  76. 3382, 3359, 3329, 3293, 3250, 3201, 3147,
  77. 3087, 3021, 2950, 2874, 2794, 2709, 2621,
  78. 2528, 2433, 2334, 2234, 2131, 2026, 1920,
  79. 1813, 1706, 1599, 1492, 1387, 1282, 1179,
  80. 1078, 980, 884, 792, 703, 619, 538,
  81. 462, 392, 326, 266, 211, 162, 120,
  82. 84, 54, 30, 13, 3, 0, 3,
  83. 13, 30, 54, 84, 120, 162, 211,
  84. 266, 326, 392, 462, 538, 619, 703,
  85. 792, 884, 980, 1078, 1179, 1282, 1387,
  86. 1492, 1599
  87. };
  88.  
  89. const int const_FloatApproxArrSize = sizeof( const_FloatApproxArr ) / sizeof( const_FloatApproxArr[ 0 ] );
  90. */
  91.  
  92. void main() {
  93.  
  94. PM->CLK_APB_P_SET = PM_CLOCK_APB_P_GPIO_0_M
  95. | PM_CLOCK_APB_P_GPIO_1_M
  96. | PM_CLOCK_APB_P_GPIO_2_M;
  97. PM->CLK_APB_M_SET = PM_CLOCK_APB_M_PAD_CONFIG_M
  98. | PM_CLOCK_APB_M_WU_M
  99. | PM_CLOCK_APB_M_PM_M
  100. | PM_CLOCK_APB_M_EPIC_M;
  101. PM->CLK_AHB_SET |= PM_CLOCK_AHB_SPIFI_M;
  102.  
  103. DAC_HandleTypeDef hdac1;
  104. DAC_HandleTypeDef hdac2;
  105.  
  106. void SystemClock_Config(void);
  107.  
  108. // DAC Init
  109. hdac1.Instance = ANALOG_REG;
  110. hdac1.Instance_dac = HAL_DAC1;
  111. hdac1.Init.DIV = 0; // Делитель частоты
  112. hdac1.Init.EXTRef = DAC_EXTREF_OFF; // Выбор источника опорного напряжения: «1» - внешний; «0» - встроенный
  113. hdac1.Init.EXTClb = DAC_EXTCLB_DACREF; // Выбор источника внешнего опорного напряжения: «1» - внешний вывод; «0» - настраиваемый ОИН
  114. HAL_DAC_Init(&hdac1);
  115.  
  116. // DAC Init
  117. hdac2.Instance = ANALOG_REG;
  118. hdac2.Instance_dac = HAL_DAC2;
  119. hdac2.Init.DIV = 0; // 255
  120. hdac2.Init.EXTRef = DAC_EXTREF_OFF; // Выбор источника опорного напряжения: «1» - внешний; «0» - встроенный
  121. hdac2.Init.EXTClb = DAC_EXTCLB_DACREF; // Выбор источника внешнего опорного напряжения: «1» - внешний вывод; «0» - настраиваемый ОИН
  122. HAL_DAC_Init(&hdac2);
  123.  
  124. fill_square_table_500mV();
  125.  
  126. GPIO_0->DIRECTION_OUT = 1 << 10;
  127. GPIO_1->DIRECTION_OUT = 1 << 12;
  128. GPIO_1->DIRECTION_OUT |= 1 << 13;
  129. uint32_t k = 0, i = 0;
  130. //#define MEANDR
  131. uint16_t vals[2] = {0, 1706};
  132. uint16_t val;
  133. while (1){
  134. #ifdef MEANDR
  135.  
  136. HAL_DAC_SetValue(&hdac1, 0);
  137. for(volatile uint32_t i = 0; i < 128; i++);
  138. HAL_DAC_SetValue(&hdac1, 1706);
  139. for(volatile uint32_t i = 0; i < 128; i++);
  140.  
  141. #else
  142. HAL_DAC_SetValue(&hdac1, sine_table[i++ % SAMPLES]);
  143. wait_micros(1);
  144. #endif
  145.  
  146. }
  147. }
  148.  
Add Comment
Please, Sign In to add comment