打印
[菜农助学交流]

第五批--学习之USB_PWM调灯

[复制链接]
5482|29
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
yy2bbs|  楼主 | 2011-12-20 23:58 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
下位机代码:
/*---------------------------------------------------------------------------------------------------------*/
/*                                                                                                         */
/* 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)
评分
参与人数 1威望 +10 收起 理由
hotpower + 10

相关帖子

沙发
tao560532| | 2011-12-21 04:56 | 只看该作者

使用特权

评论回复
板凳
Swallow_0322| | 2011-12-21 07:24 | 只看该作者
顶!

使用特权

评论回复
地板
电子write_cai| | 2011-12-21 08:27 | 只看该作者
我要做个乖孩子

使用特权

评论回复
5
consif| | 2011-12-21 12:00 | 只看该作者
学习下,顶额

使用特权

评论回复
6
yimuyiyang| | 2011-12-21 20:03 | 只看该作者

使用特权

评论回复
7
973400470| | 2011-12-21 21:20 | 只看该作者
很好,对我有很大帮助

使用特权

评论回复
8
A2000wps| | 2011-12-22 08:40 | 只看该作者
看不懂,:(

使用特权

评论回复
9
yanding708811| | 2011-12-24 13:44 | 只看该作者
顶!

使用特权

评论回复
10
hotpower| | 2011-12-25 07:28 | 只看该作者
晕,让老头如何回帖???

看贴要回贴才是乖孩子!!


给你一个榔头!!!

使用特权

评论回复
11
crestxa| | 2011-12-25 23:45 | 只看该作者
谢谢,楼主能不能再坐个USB传AD的

使用特权

评论回复
12
yy2bbs|  楼主 | 2011-12-26 16:00 | 只看该作者
谢谢,楼主能不能再坐个USB传AD的
crestxa 发表于 2011-12-25 23:45

你说的是AD转换??还是AD采样?能说详细点么?我不大明白呀。

使用特权

评论回复
13
mcsgy| | 2011-12-27 11:29 | 只看该作者
强 顶

使用特权

评论回复
14
ddllxxrr| | 2011-12-28 08:49 | 只看该作者
上位机用什么做地

使用特权

评论回复
15
yanding708811| | 2012-1-5 23:01 | 只看该作者
人才,学习!

使用特权

评论回复
16
plc_avr| | 2012-1-6 07:41 | 只看该作者
顶!学习...

使用特权

评论回复
17
xiaocanyang| | 2012-1-10 12:01 | 只看该作者
求LZ的USB的具体数据指令协议。因为也是初学,弄不明白具体怎么工作的。

使用特权

评论回复
18
ichuangj| | 2012-1-11 17:14 | 只看该作者
OK

使用特权

评论回复
19
dong_abc| | 2012-1-12 16:23 | 只看该作者
谢谢LZ,这几天就在弄USB的PC端程序。

使用特权

评论回复
20
zhaor| | 2012-1-13 09:11 | 只看该作者
上位机是VC程序吗?

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

0

主题

85

帖子

0

粉丝