【GD32L233C-START】DAC輸出(正弦波、鋸齒波、方波) 1.介紹 GD32L233C采用的是一款M23的內核。這個芯片據說功耗非常的低,低到什么程度呢?等后面我們再進行測試,今天我們主要來測試GD32L233C-START的DAC,既然要測試DAC,示波器是不可少的,這個實驗在家做,然而LZ家里并沒有示波器,不過最近看到一款好東西,LOTO虛擬示波器,看過這款示波器的參數,還不錯。所以入手了一款,測量芯片輸出的DAC應該沒什么問題,接下來開始測試吧。 2.設計 首先需要輸出讓芯片輸出DAC,而且還需要輸出波形,這個稍微費點功夫,之前在GD32L233C-START移植了RTThread,現在在這個代碼的基礎上添加DAC的輸出程序,這個程序移植了其他網友的,代碼我也貼出來,經過測試,代碼沒有啥問題: 比較麻煩的是正弦波的代碼: const float sinus_I_quarter[91] = { 0.0000, 0.0175, 0.0349, 0.0523, 0.0698, 0.0872, 0.1045, 0.1219, 0.1392, 0.1564, // 00 .. 09 0.1736, 0.1908, 0.2079, 0.2250, 0.2419, 0.2588, 0.2756, 0.2924, 0.3090, 0.3256, // 10 .. 19 0.3420, 0.3584, 0.3746, 0.3907, 0.4067, 0.4226, 0.4384, 0.4540, 0.4695, 0.4848, // 20 .. 29 0.5000, 0.5150, 0.5299, 0.5446, 0.5592, 0.5736, 0.5878, 0.6018, 0.6157, 0.6293, // 30 .. 39 0.6428, 0.6561, 0.6691, 0.6820, 0.6947, 0.7071, 0.7193, 0.7314, 0.7431, 0.7547, // 40 .. 49 0.7660, 0.7771, 0.7880, 0.7986, 0.8090, 0.8192, 0.8290, 0.8387, 0.8480, 0.8572, // 50 .. 59 0.8660, 0.8746, 0.8829, 0.8910, 0.8988, 0.9063, 0.9135, 0.9205, 0.9272, 0.9336, // 60 .. 69 0.9397, 0.9455, 0.9511, 0.9563, 0.9613, 0.9659, 0.9703, 0.9744, 0.9781, 0.9816, // 70 .. 79 0.9848, 0.9877, 0.9903, 0.9925, 0.9945, 0.9962, 0.9976, 0.9986, 0.9994, 0.9998, // 80 .. 89 1.0000 // 90 }; #define CIRCLE_QUARTER_1 1 #define CIRCLE_QUARTER_2 2 #define CIRCLE_QUARTER_3 3 #define CIRCLE_QUARTER_4 4 float sinus_lookup (unsigned int angle) { float sin_value; unsigned int circle_quarter; // correct angles outside the accepted angle range into 0 .. 359 if (angle > 359u) angle = angle % 360u; circle_quarter = 1 + (angle / 90u); switch (circle_quarter) { case CIRCLE_QUARTER_1: // 00 .. 89 sin_value = sinus_I_quarter[angle]; break; case CIRCLE_QUARTER_2: // 90 .. 179 sin_value = sinus_I_quarter[180 - angle]; break; case CIRCLE_QUARTER_3: // 180 .. 269 sin_value = -sinus_I_quarter[angle - 180]; break; case CIRCLE_QUARTER_4: // 270 .. 359 sin_value = -sinus_I_quarter[360 - angle]; break; } return sin_value; } void plot_sin(uint32_t f, uint32_t delta_f) { /* 定時周期為T=1/delta_f, f=1/(pMax*T) */ static uint32_t point = 0; uint32_t pMAX = delta_f/f; uint32_t value = 0; if (point++ > pMAX) point = 0; value = (uint32_t)((sinus_lookup(360*point/pMAX)+1)*10000)*2047/10000; dac_software_trigger_enable(); dac_data_set(DAC_ALIGN_12B_R, value); } 接下來是鋸齒波和方波的代碼,這兩個代碼比較簡單: void plot_triangle(uint32_t f, uint32_t delta_f) { /* 定時周期為T=1/delta_f, f=1/(pMax*T) */ static uint32_t point = 0; uint32_t pMAX = delta_f/f; uint32_t pMAX2 = pMAX/2; uint32_t value = 0; if (++point > pMAX) point = 0; if (point < pMAX2) { value = point * 4095 / pMAX2; } else { value = (pMAX - point) * 4095 / pMAX2; } dac_software_trigger_enable(); dac_data_set(DAC_ALIGN_12B_R, value); } void plot_square(uint32_t f, uint32_t delta_f) { /* 定時周期為T=1/delta_f, f=1/(pMax*T) */ static uint32_t point = 0; uint32_t pMAX = delta_f/f; uint32_t pMAX2 = pMAX/2; uint32_t value = 0; if (++point > pMAX) point = 0; if (point < pMAX2) { value = 0; } else { value = 0xFFF; } dac_software_trigger_enable(); dac_data_set(DAC_ALIGN_12B_R, value); } 最后需要開啟一個定時器,還有DAC的初始化: void timerx_init(uint32_t timer_periph, uint16_t period, uint16_t prescaler) { /* TIMER1 configuration: input capture mode ------------------- the external signal is connected to TIMER1 CH0 pin (PA0) the rising edge is used as active edge the TIMER1 CH0CV is used to compute the frequency value ------------------------------------------------------------ */ timer_parameter_struct timer_initpara; timer_ic_parameter_struct timer_icinitpara; /* enable the peripherals clock */ rcu_periph_clock_enable(RCU_TIMER2); /* deinit a TIMER */ timer_deinit(timer_periph); /* initialize TIMER init parameter struct */ timer_struct_para_init(&timer_initpara); /* TIMER1 configuration */ timer_initpara.prescaler = prescaler; timer_initpara.alignedmode = TIMER_COUNTER_EDGE; timer_initpara.counterdirection = TIMER_COUNTER_UP; timer_initpara.period = period; timer_initpara.clockdivision = TIMER_CKDIV_DIV1; timer_init(timer_periph, &timer_initpara); /* TIMER1 CH0 input capture configuration */ timer_icinitpara.icpolarity = TIMER_IC_POLARITY_RISING; timer_icinitpara.icselection = TIMER_IC_SELECTION_DIRECTTI; timer_icinitpara.icprescaler = TIMER_IC_PSC_DIV1; timer_icinitpara.icfilter = 0x00; timer_input_capture_config(timer_periph, TIMER_CH_0, &timer_icinitpara); /* auto-reload preload enable */ timer_auto_reload_shadow_enable(timer_periph); /* clear channel 0 interrupt bit */ timer_interrupt_flag_clear(timer_periph, TIMER_INT_CH0); /* channel 0 interrupt enable */ timer_interrupt_enable(timer_periph, TIMER_INT_CH0); /* enable a TIMER */ timer_enable(timer_periph); } #define DAC_WAVE_TEST void timer2_init(void) { timer_deinit(TIMER2); rcu_periph_clock_enable(RCU_TIMER2); #ifdef DAC_WAVE_TEST timerx_init(TIMER2, 639, 9); // 100KHz 0.1ms #endif timer_interrupt_enable(TIMER2, TIMER_INT_UP); nvic_irq_enable(TIMER2_IRQn, 3); } void TIMER2_IRQHandler(void) { #ifdef DAC_WAVE_TEST plot_sin(100, 10000); //正弦波 //plot_triangle(1, 10000); //鋸齒波 //plot_square(1, 10000); //方波 #endif timer_interrupt_flag_clear(TIMER2, TIMER_INT_FLAG_UP); } void dac1_init(void) { rcu_periph_clock_enable(RCU_GPIOA); rcu_periph_clock_enable(RCU_DAC); gpio_mode_set(GPIOA, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, GPIO_PIN_4); dac_deinit(); /* software trigger */ dac_trigger_enable(); dac_trigger_source_config(DAC_TRIGGER_SOFTWARE); /* no noise wave */ dac_wave_mode_config(DAC_WAVE_DISABLE); /* noise wave - triangle */ //dac_wave_mode_config(DAC_WAVE_MODE_TRIANGLE); //dac_triangle_noise_config(DAC_TRIANGLE_AMPLITUDE_4095); /* noise wave - lfsr */ //dac_wave_mode_config(DAC_WAVE_MODE_LFSR); //dac_lfsr_noise_config(DAC_LFSR_BITS11_0); dac_output_buffer_enable(); /* enable DAC and set data */ dac_enable(); dac_software_trigger_enable(); dac_data_set(DAC_ALIGN_12B_R, 0); } 這樣就可以輸出正弦波、鋸齒波和方波了。 3.波形測量 接下來我們來看一下輸出的波形是否符合要求,首先需要將【GD32L233C-START】開發板連接LOTO示波器,分別連接GND和PA4管教,連接效果圖如下圖1所示。 ![]() 然后讓DAC輸出正弦波,看一下波形如何。 ![]() 從上圖2中可以看到,一個周期大約在10ms,所以正弦波的周期為100Hz,輸出還是聽精準的。 然后輸出鋸齒波看看波形如何。 ![]() 從上圖3可以得出,鋸齒波的波形頻率為1Hz。 最后我們來看看方波的波形圖如何。 ![]() 從上圖4可以看到,方波的波形頻率為1Hz。 從上面波形可以得出,【GD32L233C-START】的定時器比較精準,DAC的輸出值也比較穩定,性能還是不錯的! 4.總結 剛入手的LOTO示波器還不錯,測量的精度挺高的,不過還有好多設置沒弄明白,等后續多琢磨琢磨。做電子的示波器是必不可少的,我先替你們測試測試這個示波器如何。 GD32L233C是新出來的芯片,整體功能還需要多進行測試,它最突出的低功耗后續要好好測量一下,這次就先到這里了。 |