1
完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
/**************************************************************************//**
* @file main.c * @version V1.00 * $Revision: 4 $ * $Date: 15/05/22 2:05p $ * @brief 通过P40(24)和P41(PIN36)输出两路PWM信号. * * @note * Copyright (C) 2014 Nuvoton Technology Corp. All rights reserved. * ******************************************************************************/ #include #include "M051Series.h" /*---------------------------------------------------------------------------------------------------------*/ /* Macro, type and constant definitions */ /*---------------------------------------------------------------------------------------------------------*/ #define PLLCON_SETTING CLK_PLLCON_50MHz_HXT #define PLL_CLOCK 50000000 /*---------------------------------------------------------------------------------------------------------*/ /* Global variables */ /*---------------------------------------------------------------------------------------------------------*/ void SYS_Init(void) { /*---------------------------------------------------------------------------------------------------------*/ /* Init System Clock */ /*---------------------------------------------------------------------------------------------------------*/ /* Enable Internal RC clock */ CLK_EnableXtalRC(CLK_PWRCON_OSC22M_EN_Msk); /* Waiting for IRC22M clock ready */ CLK_WaitClockReady(CLK_CLKSTATUS_OSC22M_STB_Msk); /* Switch HCLK clock source to Internal RC and HCLK source divide 1 */ CLK_SetHCLK(CLK_CLKSEL0_HCLK_S_HIRC, CLK_CLKDIV_HCLK(1)); /* Enable external 12MHz XTAL, internal 22.1184MHz */ CLK_EnableXtalRC(CLK_PWRCON_XTL12M_EN_Msk | CLK_PWRCON_OSC22M_EN_Msk); /* Enable PLL and Set PLL frequency */ CLK_SetCoreClock(PLLCON_SETTING); /* Waiting for clock ready */ CLK_WaitClockReady(CLK_CLKSTATUS_PLL_STB_Msk | CLK_CLKSTATUS_XTL12M_STB_Msk | CLK_CLKSTATUS_OSC22M_STB_Msk); /* Switch HCLK clock source to PLL, STCLK to HCLK/2 */ CLK_SetHCLK(CLK_CLKSEL0_HCLK_S_PLL, CLK_CLKDIV_HCLK(2)); /* Enable UART module clock */ CLK_EnableModuleClock(UART0_MODULE); /* Enable PWM module clock */ CLK_EnableModuleClock(PWM01_MODULE); /* Select UART module clock source */ CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART_S_HXT, CLK_CLKDIV_UART(1)); /* Select PWM module clock source */ CLK_SetModuleClock(PWM01_MODULE, CLK_CLKSEL1_PWM01_S_HXT, 0); /* Reset PWMA channel0~channel3 */ SYS_ResetModule(PWM03_RST); /* Update System Core Clock */ /* User can use SystemCoreClockUpdate() to calculate PllClock, SystemCoreClock and CycylesPerUs automatically. */ //SystemCoreClockUpdate(); PllClock = PLL_CLOCK; // PLL SystemCoreClock = PLL_CLOCK / 1; // HCLK CyclesPerUs = PLL_CLOCK / 1000000; // For SYS_SysTickDelay() /*---------------------------------------------------------------------------------------------------------*/ /* Init I/O Multi-function */ /*---------------------------------------------------------------------------------------------------------*/ /* Set P3 multi-function pins for UART0 RXD and TXD */ SYS->P3_MFP &= ~(SYS_MFP_P30_Msk | SYS_MFP_P31_Msk); SYS->P3_MFP |= SYS_MFP_P30_RXD0 | SYS_MFP_P31_TXD0; /* Set P4 multi-function pins for PWMA Channel 0 and 1 */ SYS->P4_MFP &= ~(SYS_MFP_P40_Msk|SYS_MFP_P41_Msk); SYS->P4_MFP |= (SYS_MFP_P40_PWM0|SYS_MFP_P41_PWM1); } void UART0_Init(void) { /*---------------------------------------------------------------------------------------------------------*/ /* Init UART */ /*---------------------------------------------------------------------------------------------------------*/ /* Configure UART0 and set UART0 Baudrate */ UART_Open(UART0, 115200); } /*---------------------------------------------------------------------------------------------------------*/ /* Main Function */ /*---------------------------------------------------------------------------------------------------------*/ int32_t main(void) { uint8_t i=100; /* Unlock protected registers */ SYS_UnlockReg(); /* Init System, IP clock and multi-function I/O */ SYS_Init(); /* Lock protected registers */ SYS_LockReg(); /* Init UART0 for printf */ UART0_Init(); //使能PWM0和PWM1通道 PWM_EnableOutput(PWMA, 0x1|0x2); //设置PWM0通道的频率与占空比配置,因为是互补输出,只需要设置PWM0的即可 PWM_ConfigOutputChannel(PWMA, PWM_CH0, 1000, i); //使能死区功能,死区设置为0,即生成完全互补信号。 PWM_EnableDeadZone(PWMA,PWM_CH0,0); PWM_Start(PWMA, 0x1|0x2); GPIO_SetMode(P3,BIT6,GPIO_PMD_OUTPUT); while(1) { while(i>0) { i=i-1; PWM_ConfigOutputChannel(PWMA, PWM_CH0, 1000, i); CLK_SysTickDelay(10000); } //驱动板载的LED红灯闪烁 P36 ^=1; while(i<100) { i=i+1; PWM_ConfigOutputChannel(PWMA, PWM_CH0, 1000, i); CLK_SysTickDelay(10000); } //驱动板载的LED红灯闪烁 P36 ^=1; } } |
|
相关推荐
1个回答
|
|
#include "NUC131.h"
#define PWM_CH0_PIN 0x40 /* P5.6 */ #define PWM_CH1_PIN 0x80 /* P5.7 */ void SYS_Init(void); void UART0_Init(void); void PWM0_Init(void); void PWM1_Init(void); int main(void) { SYS_UnlockReg(); SYS_Init(); UART0_Init(); PWM0_Init(); PWM1_Init(); while(1); } void SYS_Init(void) { SYS_Init_72MHZ_PLL(); CLK_EnableXtalRC(CLK_PWRCON_XTL12M_EN_Msk); SYS_LockReg(); } void UART0_Init(void) { /* UART0: 115200-8n1 */ UART_Open(UART0, 115200); } void PWM0_Init(void) { /* PWM0 frequency is 100Hz, duty 30%. */ PWM_ConfigOutputChannel(PWM0, 0, 100, 30); PWM_EnableOutput(PWM0, PWM_CH_0_MASK); } void PWM1_Init(void) { /* PWM1 frequency is 500Hz, duty 50%. */ PWM_ConfigOutputChannel(PWM1, 0, 500, 50); PWM_EnableOutput(PWM1, PWM_CH_0_MASK); } |
|
|
|
只有小组成员才能发言,加入小组>>
798 浏览 0 评论
1154 浏览 1 评论
2531 浏览 5 评论
2863 浏览 9 评论
移植了freeRTOS到STMf103之后显示没有定义的原因?
2713 浏览 6 评论
keil5中manage run-time environment怎么是灰色,不可以操作吗?
1086浏览 3评论
195浏览 2评论
461浏览 2评论
374浏览 2评论
M0518 PWM的电压输出只有2V左右,没有3.3V是怎么回事?
455浏览 1评论
小黑屋| 手机版| Archiver| 德赢Vwin官网 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-24 01:00 , Processed in 1.135736 second(s), Total 80, Slave 61 queries .
Powered by 德赢Vwin官网 网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
德赢Vwin官网 观察
版权所有 © 湖南华秋数字科技有限公司
德赢Vwin官网 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号