下位机代码:
/*---------------------------------------------------------------------------------------------------------*/
/* */
/* Copyright(c) 2009 Nuvoton Technology Corp. All rights reserved. */
/* */
/*---------------------------------------------------------------------------------------------------------*/
#include <stdio.h>
#include "Driver\DrvGPIO.h"
#include "Driver\DrvPWM.h"
static S_DRVPWM_TIME_DATA_T sPt,sPtCAS;
static uint8_t is_run=0;
void DRVPWM_PwmIRQHandler()
{
//周期之后执行
}
void drvpwm_timer_run()
{
/* Enable PWM clock */
DrvPWM_Open();
/* Set PWM pins */
DrvGPIO_InitFunction(E_FUNC_PWM01);
/* PWM Timer property */
sPt.u8Mode = DRVPWM_AUTO_RELOAD_MODE;
sPt.u32Frequency = 1; //PWM频率
sPt.u8HighPulseRatio = 1; /* High Pulse peroid : Total Pulse peroid = 1 : 100 */
sPt.i32Inverter = 0;
/* Select PWM engine clock */
DrvPWM_SelectClockSource(DRVPWM_TIMER0, DRVPWM_HCLK);
/* Set PWM Timer0 Configuration */
DrvPWM_SetTimerClk(DRVPWM_TIMER0, &sPt);
/* Enable Output for PWM Timer0 */
DrvPWM_SetTimerIO(DRVPWM_TIMER0, 1);
/* Enable Interrupt Sources of PWM Timer0 and install call back function */
DrvPWM_EnableInt(DRVPWM_TIMER0, 0, DRVPWM_PwmIRQHandler);
/* Enable the PWM Timer 0 */
DrvPWM_Enable(DRVPWM_TIMER0, 1);
}
void DRVPWM_CAPS()
{
/* Enable PWM clock */
DrvPWM_Open();
/* Set PWM pins */
DrvGPIO_InitFunction(E_FUNC_PWM23);
/* PWM Timer property for Capture */
sPtCAS.u8Mode = DRVPWM_AUTO_RELOAD_MODE;
sPtCAS.u32Frequency = 100; /* Set the proper frequency to capture data (Less than the input data)*/
sPtCAS.u8HighPulseRatio = 50; /* High Pulse peroid : Total Pulse peroid = 50 : 100 (Set a non-zero value) */
sPtCAS.u32Duty = 0x10000; /* Set the counter to the maximum value */
sPtCAS.i32Inverter = 0;
//u8CapTimer = DRVPWM_CAP3;
/* Select PWM engine clock */
DrvPWM_SelectClockSource(DRVPWM_CAP3, DRVPWM_HCLK);
/* Set PWM Timer 3 for Capture */
DrvPWM_SetTimerClk(DRVPWM_CAP3, &sPt);
/* Enable Interrupt Sources of PWM Capture3 */
DrvPWM_EnableInt(DRVPWM_CAP3, DRVPWM_CAP_FALLING_INT, NULL);
/* Enable Input function for PWM Capture 3 */
DrvPWM_SetTimerIO(DRVPWM_CAP3, 1);
/* Enable the PWM Capture3 */
DrvPWM_Enable(DRVPWM_CAP3, 1);
/* Capture the Input Waveform Data */
//CalPeriodTime(DRVPWM_CAP3);
/*--------------------------------------------------------------------------------------*/
/* Stop PWM Timer 1 (Recommended procedure method 1) */
/* Set PWM Timer counter as 0, When PWM internal counter reaches to 0, disable PWM Timer*/
/*--------------------------------------------------------------------------------------*/
/* Set PWM Timer 1 counter as 0 */
//DrvPWM_SetTimerCounter(u8Timer, 0);
/* Disable PWM Timer 1 */
//DrvPWM_Enable(u8Timer, 0);
/* Disable Output function for PWM Timer 1 */
//DrvPWM_SetTimerIO(u8Timer, 0);
/*--------------------------------------------------------------------------------------*/
/* Stop PWM Timer 3 (Recommended procedure method 1) */
/* Set PWM Timer counter as 0, When PWM internal counter reaches to 0, disable PWM Timer*/
/*--------------------------------------------------------------------------------------*/
/* Set PWM Capture 3 counter as 0 */
DrvPWM_SetTimerCounter(DRVPWM_CAP3, 0);
/* Clear the PWM Capture 3 Interrupt */
// DrvPWM_ClearInt(DRVPWM_CAP3);
/* Disable PWM Capture 3 */
// DrvPWM_Enable(DRVPWM_CAP3, 0);
/* Disable Input function for PWM Capture 3 */
// DrvPWM_SetTimerIO(DRVPWM_CAP3, 0);
}
void set_pwm_u8HighPulseRatio(const uint8_t * buf)
{
if(is_run==0)
{
is_run=1;
drvpwm_timer_run();
}
sPt.u8HighPulseRatio = buf[4];
if(buf[2]==1)
DrvPWM_SetTimerClk(DRVPWM_TIMER0, &sPt);
else
DrvPWM_SetTimerClk(DRVPWM_CAP3, &sPt);
}
void set_pwm_u32Frequency(const uint8_t * buf)
{
if(is_run==0)
{
is_run=1;
drvpwm_timer_run();
}
sPt.u32Frequency = buf[7]*256*256*256+buf[6]*256*256+buf[5]*256+buf[4];
if(buf[2]==1)
DrvPWM_SetTimerClk(DRVPWM_TIMER0, &sPt);
else
DrvPWM_SetTimerClk(DRVPWM_CAP3, &sPt);
}
void drvpwm_timer_stop()
{
DrvPWM_Enable(DRVPWM_TIMER0, 0);
}
void drvpwm_timer_clear()
{
DrvPWM_Enable(DRVPWM_TIMER0, 0);
DrvPWM_Close();
}
void drvpwm_start(uint8_t * buf)
{
switch(buf[2])
{
case 1:
//DRVPWM_TIMER
switch(buf[3])
{
case 1:
//设置频率
set_pwm_u32Frequency(buf);
break;
case 2:
//设置高电平比率 x/100 (0-100)
set_pwm_u8HighPulseRatio(buf);
break;
}
break;
case 2:
//DRVPWMCAS
switch(buf[3])
{
case 1:
//设置频率
set_pwm_u8HighPulseRatio(buf);
break;
case 2:
//设置高电平比率 x/100 (0-100)
set_pwm_u8HighPulseRatio(buf);
break;
}
break;
}
}
上位机程序:
private void Set_u8HighPulseRatio_Scroll(object sender, EventArgs e)
{
this.commandDate[1] = 0x2;
this.commandDate[2] = 0x3;//PWM实验
this.commandDate[3] = 0x1;//pwm输出实验
this.commandDate[4] = 0x2;//调节亮度
this.commandDate[5] = (byte)Convert.ToByte(100 - this.Set_u8HighPulseRatio.Value);
this.NUM_u8HighPulseRatio.Text = this.Set_u8HighPulseRatio.Value.ToString();
this.SendData();
}
private void Set_u32Frequency_Scroll(object sender, EventArgs e)
{
this.commandDate[1] = 0x2;
this.commandDate[2] = 0x3;//PWM实验
this.commandDate[3] = 0x1;//pwm输出实验
this.commandDate[4] = 0x1;//调节频率
byte[] temp = System.BitConverter.GetBytes(this.Set_u32Frequency.Value);
for (int i = 0; i < temp.Length; i++)
{
this.commandDate[i + 5] = temp[i];
}
this.NUM_u32Frequency.Text = this.Set_u32Frequency.Value.ToString();
this.SendData();
}
界面:
看贴要回贴才是乖孩子!!
上位机+下位机程序:
yy2bbs_USB_PWM调灯.rar
(1.04 MB)
|