呼吸灯就是慢慢的亮,慢慢的灭,如此循环像出气一样。。。。
这里用库函数说,就是要设置PWM,然后启动它,定时的修改占空比。
#include"NuMicro.h"
#include<stdio.h>
void SYS_Init(void)
{
/*---------------------------------------------------------------------------------------------------------*/
/* Init System Clock */
/*---------------------------------------------------------------------------------------------------------*/
/* Unlock protected registers */
SYS_UnlockReg();
/* Set XT1_OUT(PF.2) and XT1_IN(PF.3) to input mode */
PF->MODE &= ~(GPIO_MODE_MODE2_Msk | GPIO_MODE_MODE3_Msk);
/* Enable HIRC clock (Internal RC 48 MHz) */
CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);
/* Wait for HIRC clock ready */
CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk);
/* Set core clock as 96MHz from PLL */
CLK_SetCoreClock(FREQ_96MHZ);
/* Set PCLK0/PCLK1 to HCLK/2 */
CLK->PCLKDIV = (CLK_PCLKDIV_APB0DIV_DIV2 | CLK_PCLKDIV_APB1DIV_DIV2);
/* Enable UART clock */
CLK_EnableModuleClock(UART0_MODULE);
/* Enable EPWM1 module clock */
CLK_EnableModuleClock(EPWM1_MODULE);
/* Select UART clock source from HIRC */
CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART0SEL_HIRC, CLK_CLKDIV0_UART0(1));
/*---------------------------------------------------------------------------------------------------------*/
/* EPWM clock frequency configuration */
/*---------------------------------------------------------------------------------------------------------*/
CLK_SetModuleClock(EPWM1_MODULE, CLK_CLKSEL2_EPWM1SEL_PCLK1, 0);
CLK_SetSysTickClockSrc(CLK_CLKSEL0_STCLKSEL_HIRC_DIV2);
/* Update System Core Clock */
/* User can use SystemCoreClockUpdate() to calculate SystemCoreClock. */
SystemCoreClockUpdate();
/* Set GPB multi-function pins for UART0 RXD and TXD */
SYS->GPB_MFPH = (SYS->GPB_MFPH & ~(SYS_GPB_MFPH_PB12MFP_Msk | SYS_GPB_MFPH_PB13MFP_Msk)) | \
(SYS_GPB_MFPH_PB12MFP_UART0_RXD | SYS_GPB_MFPH_PB13MFP_UART0_TXD);
SYS->GPB_MFPH=(SYS->GPB_MFPH&(~SYS_GPB_MFPH_PB14MFP_Msk))|SYS_GPB_MFPH_PB14MFP_EPWM1_CH1;
/* Lock protected registers */
SYS_LockReg();
}
int main(void)
{
int i=0;
SYS_Init();
UART_Open(UART0, 115200);
printf("Hello World\n");
/* Enable output of BPWM1 channel 0~5 */
EPWM_EnableOutput(EPWM1, BIT1);
/* Start BPWM1 counter */
EPWM_Start(EPWM1, BIT1);
while(1)
{
for(i=0;i<100;i++)
{
EPWM_ConfigOutputChannel(EPWM1, 1, 50000,i);
CLK_SysTickDelay(10000);
}
for(i=100;i>0;i--)
{
EPWM_ConfigOutputChannel(EPWM1, 1, 50000, i);
CLK_SysTickDelay(10000);
}
}
}
|