/**************************************************************************//**
* [url=home.php?mod=space&uid=288409]@file[/url] main.c
* [url=home.php?mod=space&uid=895143]@version[/url] V3.00
* $Revision: 2 $
* $Date: 14/08/12 6:35p $
* [url=home.php?mod=space&uid=247401]@brief[/url] Demonstrate how to set GPIO pin mode and use pin data input/output control.
* @note
* Copyright (C) 2013 Nuvoton Technology Corp. All rights reserved.
*
******************************************************************************/
#include "stdio.h"
#include "M451Series.h"
uint32_t Volume;
/*---------------------------------------------------------------------------------------------------------*/
/* Main Function */
/*---------------------------------------------------------------------------------------------------------*/
int32_t main(void)
{
SYS_UnlockReg();
/* Enable HIRC clock (Internal RC 22.1184MHz) */
CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);
/* Wait for HIRC clock ready */
CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk);
/* Select HCLK clock source as HIRC and and HCLK clock divider as 1 */
CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_HIRC, CLK_CLKDIV0_HCLK(1));
SYS_LockReg();
GPIO_SetMode(PC, BIT2, GPIO_MODE_OUTPUT); //LED4
GPIO_SetMode(PC, BIT7, GPIO_MODE_OUTPUT); //LED7
//初始化ADC
CLK_EnableModuleClock(EADC_MODULE);
SYS->GPB_MFPH &= ~(SYS_GPB_MFPH_PB9MFP_Msk | SYS_GPB_MFPH_PB10MFP_Msk);//配置ADC管脚
GPIO_DISABLE_DIGITAL_PATH(PB, BIT9); //关闭ADC引脚数字输入通道
EADC->CTL |= EADC_CTL_ADCEN_Msk; //使能ADC
EADC->CTL &= (~EADC_CTL_DIFFEN_Msk); //设置ADC转换为单端输入模式
EADC_ConfigSampleModule(EADC, 0, EADC_SOFTWARE_TRIGGER, 6); //ADC模块0选择通道6
EADC_ENABLE_SAMPLE_MODULE_INT(EADC, 0, 0x1);//Enable sample module 0 interrupt.
//初始化PWM
SYS->GPC_MFPL &= ~SYS_GPC_MFPL_PC2MFP_Msk;
SYS->GPC_MFPL |= SYS_GPC_MFPL_PC2MFP_PWM0_CH2;//配置PWM管脚
/* Enable PWM module clock */
CLK_EnableModuleClock(PWM0_MODULE);
/* Select PWM module clock source */
CLK_SetModuleClock(PWM0_MODULE, CLK_CLKSEL2_PWM0SEL_PCLK0, 0);//选择PWM时钟源为系统时钟
/* Reset PWM0 channel 0~5 */
SYS_ResetModule(PWM0_RST);
PWM_ConfigOutputChannel(PWM0, 2, 100, 50); //PWM0_CH2频率100Hz,占空比50%
/* Enable PWM Output path for PWM0 channel 2 */
PWM_EnableOutput(PWM0, 0x04);
// Start
PWM_Start(PWM0, 0x04);
while(1)
{
PC->DOUT^=BIT7; //反转IO电平,LED闪烁
EADC_START_CONV(EADC, 0x1); //AD开始转换
while(EADC_GET_INT_FLAG(EADC, 0x1) == 0); //等待转换结束
Volume = EADC_GET_CONV_DATA(EADC, 0); //取出AD转换结果
EADC_CLR_INT_FLAG(EADC, 0x1); //清AD中断标志
PWM_ConfigOutputChannel(PWM0,2,100,(Volume*100/4096));//根据AD值调节PWM占空比调节LED亮度
for(Volume=0;Volume<100000;Volume++);
}
}
|