#include <stdio.h>
#include "NUC1xx.h"
#include "Driver\DrvGPIO.h"
#include "Driver\DrvSYS.h"
#include "Driver\DrvUART.h"
#include "Driver\DrvTIMER.h"
#include "Driver\DrvPWM.h"
volatile uint8_t PWM0_Duty_Cycle = 3; //PWM占空比
void UART_INT_HANDLE(uint32_t u32IntStatus);
void Init_System(void)
{
UNLOCKREG(x);
DrvSYS_SetOscCtrl(E_SYS_XTL12M, 1);
DrvSYS_Delay(5000);
LOCKREG(x);
}
void Init_PWM0(void)
{
S_DRVPWM_TIME_DATA_T sPt;
/* PWM Timer property */
sPt.u8Mode = DRVPWM_AUTO_RELOAD_MODE; /*自动重载模式 DRVPWM_ONE_SHOT_MODE为单触发模式*/
sPt.u32Frequency = 50; /*PWM 频率 为400HZ即2500us为一周期*/
sPt.u8HighPulseRatio = PWM0_Duty_Cycle; /* 高脉冲宽度 : 总脉冲宽度 = 1 : 100 */
sPt.i32Inverter = 0; /*反向关闭*/
/* Enable PWM clock */
DrvPWM_Open();
/* Select PWM engine clock */
DrvPWM_SelectClockSource(DRVPWM_TIMER0, DRVPWM_EXT_12M);
/* Set PWM Timer0 Configuration */
DrvPWM_SetTimerClk(DRVPWM_TIMER0, &sPt);
/* Enable Output for PWM Timer0 */
DrvPWM_SetTimerIO(DRVPWM_TIMER0, 1);
/* Set PWM pins */
DrvGPIO_InitFunction(E_FUNC_PWM01);
/* Enable the PWM Timer 0 */
DrvPWM_Enable(DRVPWM_TIMER0, 1);
}
int main (void)
{
uint8_t PWM0_Duty_Cycle_tmp = 3;
uint16_t PWM_CNR0_tmp = 0;
uint16_t tmp;
Init_System();
Init_PWM0();
while(1)
{
if (0==DrvGPIO_GetBit(E_GPB,15)) //利用助学板上的key1和key2来调节舵机的运动角度
{
DrvSYS_Delay(5000);
if(PWM0_Duty_Cycle < 4 ) PWM0_Duty_Cycle=3;
else PWM0_Duty_Cycle--;
}
if (0==DrvGPIO_GetBit(E_GPB,14))
{
DrvSYS_Delay(5000);
if(PWM0_Duty_Cycle >11 ) PWM0_Duty_Cycle=12;
else PWM0_Duty_Cycle++;
}
if (PWM0_Duty_Cycle_tmp!=PWM0_Duty_Cycle)
{
PWM0_Duty_Cycle_tmp = PWM0_Duty_Cycle;
if (0==PWM0_Duty_Cycle)
{
PWMA->CMR0 = 0;
}
else
{
tmp = (PWM_CNR0_tmp+1)*PWM0_Duty_Cycle/100-1;
PWMA->CMR0 = tmp;
}
}
}
}
pwm.zip
(1.76 MB)
|