本帖最后由 xianghou 于 2011-10-27 22:22 编辑
初始化代码:#include "NUC1xx.h"
#include "drvsys.h"
#include "drvgpio.h"
#include "drvpwm.h"
static void ClockInit( void )
{
UNLOCKREG();
DrvSYS_SetOscCtrl( E_SYS_XTL12M, ENABLE );
DrvSYS_SelectPLLSource( E_SYS_EXTERNAL_12M );
DrvSYS_Open(48000000);
DrvSYS_SelectSysTickSource( 3 );
}
static void PWMInit( void )
{
S_DRVPWM_TIME_DATA_T sPt;
sPt.u8Mode = DRVPWM_AUTO_RELOAD_MODE;
sPt.u8HighPulseRatio = 1; /* High Pulse peroid : Total Pulse peroid = 20 : 100 */
sPt.i32Inverter = 0;
sPt.u32Frequency = 1000; // Set 1KHz to PWM timer output frequency
/* Select PWM engine clock */
DrvPWM_SelectClockSource(DRVPWM_TIMER7, DRVPWM_EXT_12M);
DrvPWM_Open();
DrvGPIO_InitFunction(E_FUNC_PWM0);
/* Set PWM Timer0 Configuration */
DrvPWM_SetTimerClk(DRVPWM_TIMER0, &sPt);
DrvPWM_SetTimerIO(DRVPWM_TIMER0, 1);
DrvPWM_Enable(DRVPWM_TIMER0, 1);
}
void SystemInit( void )
{
ClockInit();
PWMInit();
}
程序代码
#include "NUC1xx.h"
#include <rtl.h>
#include "drvgpio.h"
#include "bsp.h"
#include "drvpwm.h"
__task void ledtask( void )
{
for( ;; )
{
S_DRVPWM_TIME_DATA_T sPt;
static unsigned char sPulseRatio, sDir;
if(sDir == 0)
{
sPulseRatio++;
}
else
{
sPulseRatio--;
}
sPt.u8Mode = DRVPWM_AUTO_RELOAD_MODE;
sPt.u8HighPulseRatio = sPulseRatio;
sPt.i32Inverter = 0;
sPt.u32Frequency = 1000; // Set 1KHz to PWM timer output frequency
DrvPWM_SetTimerClk(DRVPWM_TIMER0, &sPt);
if(sPulseRatio == 100)
{
sDir = 1;
os_dly_wait( 200 );
}
else if(sPulseRatio == 1)
{
sDir = 0;
}
os_dly_wait( 5 );
}
}
__task void taskinit( void )
{
os_tsk_create(ledtask, 1); //创建任务
os_tsk_delete_self(); //删除自己
for( ;; )
{
}
}
int main( void )
{
os_sys_init( taskinit ); //启动任务调度器
while( 1 )
{
}
}
|