合泰HT32单片机使用PDMA+ADC采集多路模拟值并显示在OLED屏上

Using PDMA+ADC on Holtek HT32F52352 MCU to Sample Multiple Analog Channels and Display on 0.96-inch OLED

Recently I joined the Holtek Cup competition and wrote a short tutorial for everyone.

Holtek HT32 MCU Development Environment Setup Guide: https://blog.zeruns.com/archives/709.html

Electronics / MCU Technical Discussion Group: 2169025065

Result Picture

ADC & PDMA Introduction

ADC – Analog-to-Digital Converter: HT32F52352 integrates a 12-bit successive-approximation (SAR) ADC with 12 external analog input channels and 2 internal channels (VDD & GND), supporting up to 1 Msps sampling rate.

PDMA – Peripheral Direct Memory Access: HT32F52352 provides 6 PDMA channels, but only CH0 can be used with the ADC.

The ADC conversion data register ADC_DR is 32-bit, yet only the lower 16 bits are valid. When PDMA is configured in 16-bit data-width mode, the upper 16 invalid bits are also transferred to the next location. Therefore PDMA must use 32-bit data-width mode, and the final data is masked with & 0x0000FFFF to discard the upper 16 bits. If anyone has a better solution, feel free to discuss it in the comments.

Purchase links for required items:

ESK32 Development Board: https://s.click.taobao.com/ndAFyKu

DAPLINK: https://s.click.taobao.com/Lt4FyKu

Dupont Wires: https://s.click.taobao.com/QVTFyKu

0.96-inch OLED: https://s.click.taobao.com/XLU9ZJu

Source Code

Complete project download: https://url.zeruns.com/HT32_PDMA_ADC

Main source files below:

main.c

#include "ht32.h"
#include "GPIO.h"
#include "BFTM0.h"
//#include "GPTM0.h"
//#include "GPTM1.h"
#include "delay.h"
#include "OLED.h"
#include "WDT.h"
#include "ADC.h"

int main(void)
{
  GPIO_Configuration();  // Initialize GPIO
  BFTM0_Configuration(); // Initialize BFTM0 timer
  GPTM0_Configuration(); // Initialize GPTM0 timer
  GPTM1_Configuration(); // Initialize GPTM1 timer
  WDT_Configuration();   // Initialize watchdog
  OLED_Init();           // Initialize OLED
  ADC_Configuration();   // Initialize ADC

  OLED_ShowString(1, 1, "AD0:"); // Display string “AD0:” at row 1, col 1
  OLED_ShowString(2, 1, "AD1:");
  OLED_ShowString(3, 1, "AD2:");
  OLED_ShowString(4, 1, "AD3:");
  OLED_ShowString(1, 11, ".");OLED_ShowString(1, 15, "V");
  OLED_ShowString(2, 11, ".");OLED_ShowString(2, 15, "V");

  uint16_t count1 = 0;

  while (1)
  {
    if (HT_CKCU->APBCCR1 & (1 << 4)) // Check if watchdog clock is enabled
      WDT_Restart();                 // Reload watchdog counter

    OLED_ShowNum(3, 12, count1, 5);
    OLED_ShowNum(4, 12, count2, 5);

    OLED_ShowNum(1, 5, AD_Value[0] & 0x0000FFFF, 4); // Display ADC sample
    float Voltage0 = (AD_Value[0] & 0x0000FFFF) / 4096.0 * 3.3; // Convert to voltage
    OLED_ShowNum(1, 10, (uint8_t)Voltage0, 1);                  // Integer part
    OLED_ShowNum(1, 12, (uint16_t)(Voltage0 * 1000) % 1000, 3); // Fraction part

    OLED_ShowNum(2, 5, AD_Value[1] & 0x0000FFFF, 4);
    float Voltage1 = (AD_Value[1] & 0x0000FFFF) / 4096.0 * 3.3;
    OLED_ShowNum(2, 10, (uint8_t)Voltage1, 1);
    OLED_ShowNum(2, 12, (uint16_t)(Voltage1 * 1000) % 1000, 3);

    OLED_ShowNum(3, 5, AD_Value[2] & 0x0000FFFF, 4);
    OLED_ShowNum(4, 5, AD_Value[3] & 0x0000FFFF, 4);
    // https://blog.zeruns.com
    GPIO_WriteOutBits(HT_GPIOC, GPIO_PIN_14, RESET); // Set PC14 low
    GPIO_WriteOutBits(HT_GPIOC, GPIO_PIN_15, SET);   // Set PC15 high
    Delay_ms(100);                                   // Delay 100 ms
    GPIO_WriteOutBits(HT_GPIOC, GPIO_PIN_14, SET);
    GPIO_WriteOutBits(HT_GPIOC, GPIO_PIN_15, RESET);
    Delay_ms(100);
    count1++;
  }
}

ADC.c

#include "ADC.h"

uint32_t AD_Value[4];

void ADC_Configuration(void)
{
    CKCU_PeripClockConfig_TypeDef CKCUClock = {{0}}; // Struct for clock config
    CKCUClock.Bit.PA = 1;                       // Enable GPIOA clock
    CKCUClock.Bit.ADC = 1;                      // Enable ADC clock
    CKCUClock.Bit.AFIO = 1;                     // Enable AFIO clock
    CKCUClock.Bit.PDMA = 1;                     // Enable PDMA clock
    CKCU_PeripClockConfig(CKCUClock, ENABLE);   // Enable peripheral clocks

    ADC_Reset(HT_ADC);                          // Reset ADC
    CKCU_SetADCPrescaler(CKCU_ADCPRE_DIV4);     // Set ADC clock prescaler

    AFIO_GPxConfig(GPIO_PA, AFIO_PIN_0, AFIO_FUN_ADC); // Configure PA0 as ADC
    AFIO_GPxConfig(GPIO_PA, AFIO_PIN_1, AFIO_FUN_ADC); 
    AFIO_GPxConfig(GPIO_PA, AFIO_PIN_2, AFIO_FUN_ADC); 
    AFIO_GPxConfig(GPIO_PA, AFIO_PIN_3, AFIO_FUN_ADC);
    // https://blog.zeruns.com
    ADC_RegularChannelConfig(HT_ADC, ADC_CH_0, 0); // Insert channel 0 into regular group sequence 0
    ADC_RegularChannelConfig(HT_ADC, ADC_CH_1, 1); // Insert channel 1 into sequence 1
    ADC_RegularChannelConfig(HT_ADC, ADC_CH_2, 2);
    ADC_RegularChannelConfig(HT_ADC, ADC_CH_3, 3);
    // https://blog.vpszj.cn
    ADC_RegularGroupConfig(HT_ADC, CONTINUOUS_MODE, 4, 1); // Continuous mode, length 4
    ADC_RegularTrigConfig(HT_ADC, ADC_TRIG_SOFTWARE);      // Software trigger
    ADC_SamplingTimeConfig(HT_ADC, 16);                    // Sample time 16 cycles
    // ADC_IntConfig(HT_ADC, ADC_INT_CYCLE_EOC, ENABLE);   // Enable ADC interrupt
    // NVIC_EnableIRQ(ADC_IRQn);                            // Enable ADC IRQ

    PDMACH_InitTypeDef PDMACH_InitStructure;                    // PDMA config struct
    PDMACH_InitStructure.PDMACH_SrcAddr = (u32)(&HT_ADC->DR);   // Source: ADC_DR
    PDMACH_InitStructure.PDMACH_DstAddr = (u32)AD_Value;        // Destination buffer
    PDMACH_InitStructure.PDMACH_AdrMod = SRC_ADR_LIN_INC | DST_ADR_LIN_INC | AUTO_RELOAD;
    PDMACH_InitStructure.PDMACH_Priority = H_PRIO;              // High priority
    PDMACH_InitStructure.PDMACH_BlkCnt = 4;                     // Transfer 4 blocks
    PDMACH_InitStructure.PDMACH_BlkLen = 1;
    PDMACH_InitStructure.PDMACH_DataSize = WIDTH_32BIT;         // 32-bit width
    PDMA_Config(PDMA_CH0, &PDMACH_InitStructure);               // Init PDMA CH0

    // PDMA_IntConfig(PDMA_CH0, (PDMA_INT_GE | PDMA_INT_TC), ENABLE); // Enable PDMA IRQ

    PDMA_EnaCmd(PDMA_CH0, ENABLE);                          // Enable PDMA CH0
    ADC_PDMAConfig(HT_ADC, ADC_PDMA_REGULAR_CYCLE, ENABLE); // Enable ADC PDMA trigger
    ADC_Cmd(HT_ADC, ENABLE);                                // Enable ADC
    ADC_SoftwareStartConvCmd(HT_ADC, ENABLE);               // Software start
    PDMA_SwTrigCmd(PDMA_CH0, ENABLE);                       // Software trigger PDMA
}

ADC.h

#ifndef __ADC_H
#define __ADC_H 		   
#include "ht32.h"

extern uint32_t AD_Value[4];

void ADC_Configuration(void);

#endif
```**OLED.c**

```c++
#include "ht32.h"
#include "OLED_Font.h"

/*Pin configuration*/
#define OLED_SCL GPIO_PIN_7
#define OLED_SDA GPIO_PIN_8
#define OLED_W_SCL(x)		GPIO_WriteOutBits(HT_GPIOB, OLED_SCL, (FlagStatus)(x))
#define OLED_W_SDA(x)		GPIO_WriteOutBits(HT_GPIOB, OLED_SDA, (FlagStatus)(x))

/*Pin initialization*/
void OLED_I2C_Init(void)
{
    CKCU_PeripClockConfig_TypeDef CKCUClock = {{ 0 }};	//Define structure to configure clock
    CKCUClock.Bit.PB    = 1;							//Enable GPIOB clock
    CKCU_PeripClockConfig(CKCUClock, ENABLE);			//Enable peripheral clock

    GPIO_SetOutBits         (HT_GPIOB, OLED_SCL);		//Set IO pin to output high level
    GPIO_DirectionConfig    (HT_GPIOB, OLED_SCL, GPIO_DIR_OUT);	//Set IO pin as output mode
    GPIO_OpenDrainConfig    (HT_GPIOB, OLED_SCL, ENABLE);		//Set IO pin as open-drain output mode
    GPIO_PullResistorConfig (HT_GPIOB, OLED_SCL, GPIO_PR_UP);	//Set IO pin as pull-up output mode
	//GPIO_DriveConfig		(HT_GPIOB, OLED_SCL,GPIO_DV_12MA);	//Set IO pin output current mode

    GPIO_SetOutBits         (HT_GPIOB, OLED_SDA);
    GPIO_DirectionConfig    (HT_GPIOB, OLED_SDA, GPIO_DIR_OUT);
    GPIO_OpenDrainConfig    (HT_GPIOB, OLED_SDA, ENABLE);
    GPIO_PullResistorConfig (HT_GPIOB, OLED_SDA, GPIO_PR_UP);
	//GPIO_DriveConfig		(HT_GPIOB, OLED_SDA,GPIO_DV_12MA);
	
	OLED_W_SCL(1);
	OLED_W_SDA(1);
}

/**
  * @brief  I2C start
  * @param  None
  * @retval None
  */
void OLED_I2C_Start(void)
{
	OLED_W_SDA(1);
	OLED_W_SCL(1);
	OLED_W_SDA(0);
	OLED_W_SCL(0);
}

/**
  * @brief  I2C stop
  * @param  None
  * @retval None
  */
void OLED_I2C_Stop(void)
{
	OLED_W_SDA(0);
	OLED_W_SCL(1);
	OLED_W_SDA(1);
}

/**
  * @brief  I2C send one byte
  * @param  Byte Byte to be sent
  * @retval None
  */
void OLED_I2C_SendByte(uint8_t Byte)
{
	uint8_t i;
	for (i = 0; i < 8; i++)
	{
		OLED_W_SDA(Byte & (0x80 >> i));
		OLED_W_SCL(1);
		OLED_W_SCL(0);
	}
	OLED_W_SCL(1);	//Extra clock, no ACK handling
	OLED_W_SCL(0);
}

/**
  * @brief  OLED write command
  * @param  Command Command to be written
  * @retval None
  */
void OLED_WriteCommand(uint8_t Command)
{
	OLED_I2C_Start();
	OLED_I2C_SendByte(0x78);		//Slave address
	OLED_I2C_SendByte(0x00);		//Write command
	OLED_I2C_SendByte(Command); 
	OLED_I2C_Stop();
}

/**
  * @brief  OLED write data
  * @param  Data Data to be written
  * @retval None
  */
void OLED_WriteData(uint8_t Data)
{
	OLED_I2C_Start();
	OLED_I2C_SendByte(0x78);		//Slave address
	OLED_I2C_SendByte(0x40);		//Write data
	OLED_I2C_SendByte(Data);
	OLED_I2C_Stop();
}

/**
  * @brief  OLED set cursor position
  * @param  Y Coordinate in downward direction from top-left origin, range: 0~7
  * @param  X Coordinate in rightward direction from top-left origin, range: 0~127
  * @retval None
  */
void OLED_SetCursor(uint8_t Y, uint8_t X)
{
	OLED_WriteCommand(0xB0 | Y);					//Set Y position
	OLED_WriteCommand(0x10 | ((X & 0xF0) >> 4));	//Set X position low 4 bits
	OLED_WriteCommand(0x00 | (X & 0x0F));			//Set X position high 4 bits
}

/**
  * @brief  OLED clear screen
  * @param  None
  * @retval None
  */
void OLED_Clear(void)
{  
	uint8_t i, j;
	for (j = 0; j < 8; j++)
	{
		OLED_SetCursor(j, 0);
		for(i = 0; i < 128; i++)
		{
			OLED_WriteData(0x00);
		}
	}
}

/**
  * @brief  OLED partial clear
  * @param  Line Line position, range: 1~4
  * @param  start Column start position, range: 1~16
  * @param  end Column start position, range: 1~16
  * @retval None
  */
void OLED_Clear_Part(uint8_t Line, uint8_t start, uint8_t end)
{  
	uint8_t i,Column;
	for(Column = start; Column <= end; Column++)
	{
		OLED_SetCursor((Line - 1) * 2, (Column - 1) * 8);		//Set cursor position in upper half
		for (i = 0; i < 8; i++)
		{
			OLED_WriteData(0x00);			//Display upper half content
		}
		OLED_SetCursor((Line - 1) * 2 + 1, (Column - 1) * 8);	//Set cursor position in lower half
		for (i = 0; i < 8; i++)
		{
			OLED_WriteData(0x00);		//Display lower half content
		}
	}
}

/**
  * @brief  OLED display one character
  * @param  Line Line position, range: 1~4
  * @param  Column Column position, range: 1~16
  * @param  Char Character to display, range: ASCII visible characters
  * @retval None
  */
void OLED_ShowChar(uint8_t Line, uint8_t Column, char Char)
{     	
	uint8_t i;
	OLED_SetCursor((Line - 1) * 2, (Column - 1) * 8);		//Set cursor position in upper half
	for (i = 0; i < 8; i++)
	{
		OLED_WriteData(OLED_F8x16[Char - ' '][i]);			//Display upper half content
	}
	OLED_SetCursor((Line - 1) * 2 + 1, (Column - 1) * 8);	//Set cursor position in lower half
	for (i = 0; i < 8; i++)
	{
		OLED_WriteData(OLED_F8x16[Char - ' '][i + 8]);		//Display lower half content
	}
}

/**
  * @brief  OLED display string
  * @param  Line Starting line position, range: 1~4
  * @param  Column Starting column position, range: 1~16
  * @param  String String to display, range: ASCII visible characters
  * @retval None
  */
void OLED_ShowString(uint8_t Line, uint8_t Column, char *String)
{
	uint8_t i;
	for (i = 0; String[i] != '\0'; i++)
	{
		OLED_ShowChar(Line, Column + i, String[i]);
	}
}

/**
  * @brief  OLED power function
  * @retval Return value equals X to the power of Y
  */
uint32_t OLED_Pow(uint32_t X, uint32_t Y)
{
	uint32_t Result = 1;
	while (Y--)
	{
		Result *= X;
	}
	return Result;
}

/**
  * @brief  OLED display number (decimal, positive)
  * @param  Line Starting line position, range: 1~4
  * @param  Column Starting column position, range: 1~16
  * @param  Number Number to display, range: 0~4294967295
  * @param  Length Length of number to display, range: 1~10
  * @retval None
  */
void OLED_ShowNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length)
{
	uint8_t i;
	for (i = 0; i < Length; i++)							
	{
		OLED_ShowChar(Line, Column + i, Number / OLED_Pow(10, Length - i - 1) % 10 + '0');
	}
}

/**
  * @brief  OLED display number (decimal, signed)
  * @param  Line Starting line position, range: 1~4
  * @param  Column Starting column position, range: 1~16
  * @param  Number Number to display, range: -2147483648~2147483647
  * @param  Length Length of number to display, range: 1~10
  * @retval None
  */
void OLED_ShowSignedNum(uint8_t Line, uint8_t Column, int32_t Number, uint8_t Length)
{
	uint8_t i;
	uint32_t Number1;
	if (Number >= 0)
	{
		OLED_ShowChar(Line, Column, '+');
		Number1 = Number;
	}
	else
	{
		OLED_ShowChar(Line, Column, '-');
		Number1 = -Number;
	}
	for (i = 0; i < Length; i++)							
	{
		OLED_ShowChar(Line, Column + i + 1, Number1 / OLED_Pow(10, Length - i - 1) % 10 + '0');
	}
}

/**
  * @brief  OLED display number (hexadecimal, positive)
  * @param  Line Starting line position, range: 1~4
  * @param  Column Starting column position, range: 1~16
  * @param  Number Number to display, range: 0~0xFFFFFFFF
  * @param  Length Length of number to display, range: 1~8
  * @retval None
  */
void OLED_ShowHexNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length)
{
	uint8_t i, SingleNumber;
	for (i = 0; i < Length; i++)							
	{
		SingleNumber = Number / OLED_Pow(16, Length - i - 1) % 16;
		if (SingleNumber < 10)
		{
			OLED_ShowChar(Line, Column + i, SingleNumber + '0');
		}
		else
		{
			OLED_ShowChar(Line, Column + i, SingleNumber - 10 + 'A');
		}
	}
}

/**
  * @brief  OLED display number (binary, positive)
  * @param  Line Starting line position, range: 1~4
  * @param  Column Starting column position, range: 1~16
  * @param  Number Number to display, range: 0~1111 1111 1111 1111
  * @param  Length Length of number to display, range: 1~16
  * @retval None
  */
void OLED_ShowBinNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length)
{
	uint8_t i;
	for (i = 0; i < Length; i++)							
	{
		OLED_ShowChar(Line, Column + i, Number / OLED_Pow(2, Length - i - 1) % 2 + '0');
	}
}

/**
  * @brief  OLED initialization
  * @param  None
  * @retval None
  */
void OLED_Init(void)
{
	uint32_t i, j;
	
	for (i = 0; i < 1000; i++)			//Power-on delay
	{
		for (j = 0; j < 1000; j++);
	}
	
	OLED_I2C_Init();			//Port initialization
	
	OLED_WriteCommand(0xAE);	//Display off
	
	OLED_WriteCommand(0xD5);	//Set display clock divide ratio/oscillator frequency
	OLED_WriteCommand(0x80);
	
	OLED_WriteCommand(0xA8);	//Set multiplex ratio
	OLED_WriteCommand(0x3F);
	
	OLED_WriteCommand(0xD3);	//Set display offset
	OLED_WriteCommand(0x00);
	
	OLED_WriteCommand(0x40);	//Set display start line
	
	OLED_WriteCommand(0xA1);	//Set segment remap, 0xA1 normal 0xA0 reverse left-right
	
	OLED_WriteCommand(0xC8);	//Set COM output scan direction, 0xC8 normal 0xC0 reverse up-down

	OLED_WriteCommand(0xDA);	//Set COM pins hardware configuration
	OLED_WriteCommand(0x12);
	
	OLED_WriteCommand(0x81);	//Set contrast control
	OLED_WriteCommand(0xCF);

	OLED_WriteCommand(0xD9);	//Set pre-charge period
	OLED_WriteCommand(0xF1);

	OLED_WriteCommand(0xDB);	//Set VCOMH deselect level
	OLED_WriteCommand(0x30);

	OLED_WriteCommand(0xA4);	//Set entire display on/off

	OLED_WriteCommand(0xA6);	//Set normal/inverse display

	OLED_WriteCommand(0x8D);	//Set charge pump
	OLED_WriteCommand(0x14);

	OLED_WriteCommand(0xAF);	//Display on
		
	OLED_Clear();				//OLED clear screen
}

OLED.h

#ifndef __OLED_H
#define __OLED_H

void OLED_Init(void);
void OLED_Clear(void);
void OLED_ShowChar(uint8_t Line, uint8_t Column, char Char);
void OLED_ShowString(uint8_t Line, uint8_t Column, char *String);
void OLED_ShowNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length);
void OLED_ShowSignedNum(uint8_t Line, uint8_t Column, int32_t Number, uint8_t Length);
void OLED_ShowHexNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length);
void OLED_ShowBinNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length);
void OLED_Clear_Part(uint8_t Line, uint8_t start, uint8_t end);

#endif

Recommended Reading- High Cost-Performance and Cheap VPS/Cloud Server Recommendations: https://blog.vpszj.cn/archives/41.html