本帖最后由 coslight 于 2016-4-6 21:24 编辑
看了一下应用手册和开发板原理图发现可以利用开发板上的LED中的2个做呼吸灯效果。通过开发板硬件手册可以看到,LD5和LD6两个LED占用PD12和PD13引脚。通过GD32用户手册我们可以看到,TIME4的通道1和通道2通过引脚映射正好也占用了PD12和PD13,因此产生了利用TIME4的通道1和通道2产生PWM可调占空比输出驱动LED灯,产生呼吸灯效果。
程序已GD32的例程为基础,添加TIME4的初始化,PWM输出初始化,输出占空比动态调整函数接口。
/*************************************************************************************************
* ¹¦ÄÜ£º³õʼ»¯Óû§LedÉ豸 *
* ²ÎÊý£ºÎÞ *
* ·µ»Ø£ºÎÞ *
* ˵Ã÷£º *
*************************************************************************************************/
void EvbLed1Config(void)
{
GPIO_InitPara GPIO_InitStructure;
/* -----------------------------------------------------------------------
TIMER2 Configuration: generate 3 PWM signals with 3 different duty cycles:
TIMER2CLK = SystemCoreClock / 120 = 1MHz
TIMER2 frequency = 10000/1M = 10ms
TIMER2 Channel2 duty cycle = (2500/ 10000)* 100 = 25%
TIMER2 Channel3 duty cycle = (5000/ 10000)* 100 = 50%
TIMER2 Channel4 duty cycle = (7500/ 10000)* 100 = 75%
----------------------------------------------------------------------- */
TIMER_BaseInitPara TIMER_TimeBaseStructure;
TIMER_OCInitPara TIMER_OCInitStructure;
RCC_APB2PeriphClock_Enable(RCC_APB2PERIPH_GPIOD,ENABLE);
GPIO_PinRemapConfig(GPIO_REMAP_TIMER4, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_PIN_12|GPIO_PIN_13;
GPIO_InitStructure.GPIO_Mode = GPIO_MODE_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_SPEED_50MHZ;
GPIO_Init(GPIOD,&GPIO_InitStructure);
/* TIMER2 clock enable */
RCC_APB1PeriphClock_Enable(RCC_APB1PERIPH_TIMER4,ENABLE);
/* TIMER2 configuration */
TIMER_DeInit(TIMER4);
TIMER_TimeBaseStructure.TIMER_Prescaler = 119;
TIMER_TimeBaseStructure.TIMER_CounterMode = TIMER_COUNTER_UP;
TIMER_TimeBaseStructure.TIMER_Period = 9999;
TIMER_TimeBaseStructure.TIMER_ClockDivision = TIMER_CDIV_DIV1;
TIMER_BaseInit(TIMER4,&TIMER_TimeBaseStructure);
/* CH2,CH3 and CH4 Configuration in PWM mode */
TIMER_OCInitStructure.TIMER_OCMode = TIMER_OC_MODE_PWM1;
TIMER_OCInitStructure.TIMER_OCPolarity = TIMER_OC_POLARITY_HIGH;
TIMER_OCInitStructure.TIMER_OutputState = TIMER_OUTPUT_STATE_ENABLE;
TIMER_OCInitStructure.TIMER_OCIdleState = TIMER_OC_IDLE_STATE_RESET;
TIMER_OCInitStructure.TIMER_Pulse = 0;
TIMER_OC1_Init(TIMER4, &TIMER_OCInitStructure);
TIMER_OC1_Preload(TIMER4,TIMER_OC_PRELOAD_DISABLE);
TIMER_OCInitStructure.TIMER_Pulse = 9999;
TIMER_OC2_Init(TIMER4, &TIMER_OCInitStructure);
TIMER_OC2_Preload(TIMER4,TIMER_OC_PRELOAD_DISABLE);
/* Auto-reload preload enable */
TIMER_CARLPreloadConfig(TIMER4,ENABLE);
/* TIMER enable counter*/
TIMER_Enable( TIMER4, ENABLE );
}
/*************************************************************************************************
* ¹¦ÄÜ£º¿ØÖÆLedµÄµãÁÁºÍϨÃð *
* ²ÎÊý£º(1) index LedµÆ±àºÅ *
* (2) cmd LedµÆµãÁÁ»òÕßϨÃðµÄÃüÁî *
* ·µ»Ø£ºÎÞ *
* ˵Ã÷£º *
*************************************************************************************************/
void EvbLed1Control(int index, int cmd)
{
TIMER_OCInitPara TIMER_OCInitStructure;
TIMER_OCInitStructure.TIMER_OCMode = TIMER_OC_MODE_PWM1;
TIMER_OCInitStructure.TIMER_OCPolarity = TIMER_OC_POLARITY_HIGH;
TIMER_OCInitStructure.TIMER_OutputState = TIMER_OUTPUT_STATE_ENABLE;
TIMER_OCInitStructure.TIMER_OCIdleState = TIMER_OC_IDLE_STATE_RESET;
TIMER_OCInitStructure.TIMER_Pulse = cmd%10000;
if(index==1)
{
TIMER_OC1_Init(TIMER4, &TIMER_OCInitStructure);
TIMER_OC1_Preload(TIMER4,TIMER_OC_PRELOAD_DISABLE);
}
else
{
TIMER_OC2_Init(TIMER4, &TIMER_OCInitStructure);
TIMER_OC2_Preload(TIMER4,TIMER_OC_PRELOAD_DISABLE);
}
}
主程序测试代码
#include "example.h"
#if (EVB_EXAMPLE == LED1_EXAMPLE)
/* Óû§Ó¦ÓóÌÐòÈë¿Úº¯Êý */
static int count;
static char flag=0;
int main(void)
{
int i;
/* ÅäÖÃʹÄÜÆÀ¹À°åÉϵÄÉ豸 */
EvbSetupBoard();
count = 0;
PRINTF("¸ÐлʹÓÃColibri¿ª·¢°å\r\n");
PRINTF("\r\n");
PRINTF("##### LED ʵÑé #####\r\n");
PRINTF("\r\n");
while(1)
{
for (i = 0; i< 0x2ffff; i++);
EvbLed1Control(1,count);
EvbLed1Control(0,9999-count);
if(flag ==0)
{
count+=100;
if(count >= 9999)
{
flag = 1;
count = 9999;
}
}
else
{
count-=100;
if(count <= 0)
{
flag = 0;
count = 0;
}
}
}
}
#endif
源代码就不上传整个工程了,把修改的部分上传上来。
time4-pwm-led.rar
(1.4 KB)
最后附上视频显示执行效果
http://v.youku.com/v_show/id_XMTUyNTczMjg4OA==.html
|