【东软载波ESF0654 PDS开发板活动】ADC及PWM调光测试
今天测试了板子的ADC功能及PWM调光功能,主要实现了根据ADC采集的板载电位器的值来使用PWM功能调节导光板的亮度。
ES32F0654LT集成的支持 1 路模数转换器(ADC0)。ADC(Analog to Digital Convertor)是 12 位分辨率的逐次逼近型模数转换器。它具有多达 19 个复用通道,可测量从管脚输入的 16 个外部信号、3 个内部信号。这些通道的模数转换可在单次、连续、扫描或不连续采样模式下进
行。ADC 的结果存储在一个左对齐或右对齐的 16 位数据寄存器中。
ESF0654 PDS开发板ADC接在PB0接口,PB0为ADC0的14通道。
IO引脚配置如下:
static void adc_pin_init(void)
{
/* Initialize ADC pin */
gpio_init_t x;
/* Initialize PB0 pin */
x.mode = GPIO_MODE_INPUT;
x.odos = GPIO_PUSH_PULL;
x.pupd = GPIO_PUSH_UP;
x.odrv = GPIO_OUT_DRIVE_NORMAL;
x.flt = GPIO_FILTER_DISABLE;
x.type = GPIO_TYPE_TTL;
x.func = GPIO_FUNC_0;
ald_gpio_init(GPIOB, GPIO_PIN_0, &x);
return;
}
ADC0的配置如下:
void adc_init(void)
{
/* clear lpuart_handle_t structure */
memset(&h_adc, 0x0, sizeof(adc_handle_t));
/* clear adc_nch_conf_t structure */
memset(®_config, 0x0, sizeof(adc_nch_conf_t));
adc_pin_init();
/* Initialize adc */
h_adc.perh = ADC0;
h_adc.init.data_align = ADC_DATAALIGN_RIGHT;
h_adc.init.scan_mode = DISABLE;
h_adc.init.cont_mode = DISABLE;
h_adc.init.nch_len = ADC_NCH_LEN_1;
h_adc.init.disc_mode = ADC_ALL_DISABLE;
h_adc.init.disc_nbr = ADC_DISC_NBR_1;
h_adc.init.conv_res = ADC_CONV_RES_10;
h_adc.init.clk_div = ADC_CKDIV_128;
h_adc.init.neg_ref = ADC_NEG_REF_VSS;
h_adc.init.pos_ref = ADC_POS_REF_VDD;
ald_adc_init(&h_adc);
/* Initialize regular convert channel */
reg_config.channel = ADC_CHANNEL_14;
reg_config.rank = ADC_NCH_RANK_1;
reg_config.samp_time = ADC_SAMPLETIME_4;
ald_adc_normal_channel_config(&h_adc, ®_config);
return;
}
ES32F0654LT集成通用定时器(GP16C2T)包含一个 16 位自动重载计数器,该计数器由可配置的预分频器驱动。支持 2 路通用定时器(GP16C2T0,GP16C2T1) 。GP16C2T 包含 2 个独立通道。 通用定时器(GP16C2T) 的用途广泛,可测量信号脉冲长度(输入捕获)或输出脉冲波形 (比较输出、PWM 及带死区时间插入的互补 PWM)。
本程序中使用了TIM3的1通道,对应引脚为PC6。
IO引脚初始化如下,对应IO的功能3:
static void tim3_pin_init(void)
{
gpio_init_t GPIO_InitStructure;
/* Initialize tim0 ch1 pin */
GPIO_InitStructure.mode = GPIO_MODE_OUTPUT;
GPIO_InitStructure.odos = GPIO_PUSH_PULL;
GPIO_InitStructure.pupd = GPIO_PUSH_UP;
GPIO_InitStructure.odrv = GPIO_OUT_DRIVE_NORMAL;
GPIO_InitStructure.flt = GPIO_FILTER_DISABLE;
GPIO_InitStructure.type = GPIO_TYPE_TTL;
GPIO_InitStructure.func = GPIO_FUNC_3;
ald_gpio_init(GPIOC, GPIO_PIN_6, &GPIO_InitStructure);
}
TIM3初始化程序如下:
void tim3_init(timer_handle_t *tim_h, timer_oc_init_t *tim_ocinit, timer_clock_config_t *tim_clock)
{
/* Initialize pin */
tim3_pin_init();
/* Initialize TIM3 */
tim_h->perh = GP16C2T1;
tim_h->init.prescaler = 0;
tim_h->init.mode = TIMER_CNT_MODE_UP;
tim_h->init.period = (uint32_t)(255 - 1);
tim_h->init.clk_div = TIMER_CLOCK_DIV1;
tim_h->init.re_cnt = 0;
ald_timer_pwm_init(tim_h);
/* Initialize clock source */
tim_clock->source = TIMER_SRC_INTER;
ald_timer_config_clock_source(tim_h, tim_clock);
/* Common configuration for all channels */
tim_ocinit->oc_mode = TIMER_OC_MODE_PWM1;
tim_ocinit->oc_polarity = TIMER_OC_POLARITY_HIGH;
tim_ocinit->oc_fast_en = DISABLE;
tim_ocinit->ocn_polarity = TIMER_OCN_POLARITY_HIGH;
tim_ocinit->ocn_idle = TIMER_OCN_IDLE_RESET;
tim_ocinit->oc_idle = TIMER_OC_IDLE_RESET;
/* Set the pulse value for channel 1 */
tim_ocinit->pulse = (uint32_t)(255 * 20 / 100);
ald_timer_oc_config_channel(tim_h, tim_ocinit, TIMER_CHANNEL_1);
/* Set the pulse value for channel 2 */
tim_ocinit->pulse = (uint32_t)(255 * 50 / 100);
ald_timer_oc_config_channel(tim_h, tim_ocinit, TIMER_CHANNEL_2);
/* Start input pwm from tim2/3 channel 1 */
ald_timer_oc_start(tim_h, TIMER_CHANNEL_1);
}
主程序实现了根据ADC采用进行PWM调节控制外部导光板的亮度:
/* ADC测试 */
value = adc_average();
sprintf((char *)tx_buf, "ADC_Value: %d \n", value);
ald_uart_send(&h_uart, tx_buf, sizeof("ADC_Value: %d \n"), 1000);
/* TIM3 PWM测试 */
duty = value/32.72;
if(duty <= 1)
duty = 1;
ald_timer_pwm_set_duty(&tim3.tim_h, TIMER_CHANNEL_1, (uint16_t)duty);
实现现象如下: