HC32F4A0 development board DAC signal generation test
1 Overview
1.1 Development Board Interface
HC32F4A0 is equipped with two 12-bit digital-to-analog voltage converter units DAC1 and DAC2. Each DAC unit contains two D/A conversion channels, which can be converted independently or synchronously through synchronous update of conversion data. Each conversion channel is equipped with an output amplifier, which can directly drive external loads without external op amps. Independent pin input reference voltages VREFH and VREFL can be used to improve conversion accuracy.
Referring to the official routines, it is planned to generate four waveforms: sine wave, square wave, sawtooth wave, and triangle wave, and use buttons K1 ~ K5 to control the generation and closing of the waveform.
1.2 Development Board Schematic Diagram
The DAC peripheral circuit and pin assignment are shown in Figures 1 and 2.
Figure 1 DAC peripheral circuit diagram
Figure 2 Pin assignment diagram
1.3 Test Equipment
The equipment used in this test is shown in Table 1, and the physical equipment connection diagram is shown in Figure 3:
Test Equipment
|
unit |
quantity |
EV_F4A0_LQ176_REV1.0 development board |
piece |
one |
ARM Emulator |
indivual |
one |
5V power adapter |
indivual |
one |
RIGOL DHO804 Oscilloscope |
tower |
one |
Table 1 Test equipment
Figure 3 Development board physical connection diagram
2. Code Writing
Square wave generation function
static void Pulse_Init(uint32_t pPulseTable[], uint32_t u32count)
{
uint32_t i;
float32_t pulse;
for (i = 0U; i < u32count; i++) {
if(i<=u32count/2-1)
pulse=u32count;
else
pulse=0;
#if (DAC_DATA_ALIGN == DAC_DATA_ALIGN_LEFT)
{
pPulseTable[i] = (uint32_t)pulse << 9;
}
#else
{
pSinTable[i] = (uint32_t)fSin;
}
#endif
}
}
Sawtooth wave generation function
static void Sawtooth_Init(uint32_t pSawTable[], uint32_t u32count)
{
uint32_t i;
float32_t sawtooth;
for (i = 0U; i < u32count; i++) {
sawtooth=i;
#if (DAC_DATA_ALIGN == DAC_DATA_ALIGN_LEFT)
{
pSawTable[i] = (uint32_t)sawtooth << 9;
}
#else
{
pSinTable[i] = (uint32_t)fSin;
}
#endif
}
}
Triangle wave generation function
static void Triangle_Init(uint32_t pTriangleTable[], uint32_t u32count)
{
uint32_t i;
uint32_t j=0;
float32_t triangle;
for (i = 0U; i < u32count; i++) {
if(i<=u32count/2-1)
j=j+1;
else
j=j-1;
triangle=j;
#if (DAC_DATA_ALIGN == DAC_DATA_ALIGN_LEFT)
{
pTriangleTable[i] = (uint32_t)triangle*2 << 9;
}
#else
{
pSinTable[i] = (uint32_t)fSin;
}
#endif
}
}
Main function
int32_t main(void)
{
uint16_t u16OutputCnt = 0U;
en_key_event_t enEvent;
uint8_t u8IsConversionStart = 0U;
uint32_t flag=0U;
LL_PERIPH_WE(LL_PERIPH_GPIO | LL_PERIPH_FCG | LL_PERIPH_PWC_CLK_RMU | \
LL_PERIPH_EFM | LL_PERIPH_SRAM);
BSP_CLK_Init();
BSP_IO_Init();
BSP_LED_Init();
BSP_KEY_Init();
/* Init MAU for generating sine data*/
MAU_Init();
/* Init sine data table */
static uint32_t gu32SinTable[SINE_DOT_NUMBER];
SinTable_Init(gu32SinTable, SINE_DOT_NUMBER);
static uint32_t gu32SawTable[SINE_DOT_NUMBER];
Sawtooth_Init(gu32SawTable, SINE_DOT_NUMBER);
static uint32_t gu32PulseTable[SINE_DOT_NUMBER];
Pulse_Init(gu32PulseTable, SINE_DOT_NUMBER);
static uint32_t gu32TriangleTable[SINE_DOT_NUMBER];
Triangle_Init(gu32TriangleTable, SINE_DOT_NUMBER);
DAC_ConversionInit();
LL_PERIPH_WP(LL_PERIPH_GPIO | LL_PERIPH_FCG | LL_PERIPH_PWC_CLK_RMU | \
LL_PERIPH_EFM | LL_PERIPH_SRAM);
for (;;) {
enEvent = Key_Event();
switch (enEvent) {
case E_KEY1_PRESSED:
if (u8IsConversionStart == 1U) {
break;
}
flag=1;
u16OutputCnt = 0U;
u8IsConversionStart = 1U;
BSP_LED_On(LED_BLUE);
DAC_StartConversion();
break;
case E_KEY2_PRESSED:
if (u8IsConversionStart == 1U) {
break;
}
flag=2;
u16OutputCnt = 0U;
u8IsConversionStart = 1U;
BSP_LED_On(LED_BLUE);
DAC_StartConversion();
break;
case E_KEY3_PRESSED:
if (u8IsConversionStart == 1U) {
break;
}
flag=3;
u16OutputCnt = 0U;
u8IsConversionStart = 1U;
BSP_LED_On(LED_BLUE);
DAC_StartConversion();
break;
case E_KEY4_PRESSED:
if (u8IsConversionStart == 1U) {
break;
}
flag=4;
u16OutputCnt = 0U;
u8IsConversionStart = 1U;
BSP_LED_On(LED_BLUE);
DAC_StartConversion();
break;
case E_KEY5_PRESSED:
if (u8IsConversionStart == 0U) {
break;
}
u8IsConversionStart = 0U;
DAC_StopConversion();
BSP_LED_Off(LED_BLUE);
break;
default:
break;
}
if (u8IsConversionStart == 0U) {
continue;
}
if (flag==1){
DAC_SetConversionData(gu32SinTable[u16OutputCnt]);
}
else if(flag==2){
DAC_SetConversionData(gu32SawTable[u16OutputCnt]);
}
else if(flag==3){
DAC_SetConversionData(gu32PulseTable[u16OutputCnt]);
}
else if(flag==4){
DAC_SetConversionData(gu32TriangleTable[u16OutputCnt]);
}
else{
}
/* Loop output */
if (++u16OutputCnt >= SINE_DOT_NUMBER) {
u16OutputCnt = 0U;
}
}
}
3 Experimental Phenomena
3.1 Experimental Phenomenon Analysis
Press K1, K2, K3, and K4 on the development board, and the oscilloscope will display sine wave, sawtooth wave, square wave, and triangle wave respectively. Pressing K5 will turn off the DAC output.
3.2 Experimental Phenomena
As shown in Figures 4, 5, 6, and 7
Figure 4 Sine wave
Figure 5 Sawtooth wave
Figure 6 Square wave
Figure 7 Triangle wave