[菜农助学交流] 第五批--学习之USB_PWM调灯

[复制链接]
 楼主| yy2bbs 发表于 2011-12-20 23:58 | 显示全部楼层 |阅读模式
下位机代码:
  1. /*---------------------------------------------------------------------------------------------------------*/
  2. /*                                                                                                         */
  3. /* Copyright(c) 2009 Nuvoton Technology Corp. All rights reserved.                                         */
  4. /*                                                                                                         */
  5. /*---------------------------------------------------------------------------------------------------------*/
  6. #include <stdio.h>
  7. #include "Driver\DrvGPIO.h"
  8. #include "Driver\DrvPWM.h"

  9. static S_DRVPWM_TIME_DATA_T sPt,sPtCAS;
  10. static uint8_t is_run=0;
  11. void DRVPWM_PwmIRQHandler()
  12. {
  13.         //周期之后执行
  14. }

  15. void drvpwm_timer_run()
  16. {
  17.         /* Enable PWM clock */
  18.         DrvPWM_Open();       
  19.         /* Set PWM pins */
  20.         DrvGPIO_InitFunction(E_FUNC_PWM01);
  21.         /* PWM Timer property */
  22.         sPt.u8Mode = DRVPWM_AUTO_RELOAD_MODE;
  23.         sPt.u32Frequency = 1;        //PWM频率
  24.         sPt.u8HighPulseRatio = 1;        /* High Pulse peroid : Total Pulse peroid = 1 : 100 */
  25.         sPt.i32Inverter = 0;
  26.           /* Select PWM engine clock */
  27.         DrvPWM_SelectClockSource(DRVPWM_TIMER0, DRVPWM_HCLK);
  28.                                
  29.         /* Set PWM Timer0 Configuration */
  30.         DrvPWM_SetTimerClk(DRVPWM_TIMER0, &sPt);
  31.         /* Enable Output for PWM Timer0 */
  32.         DrvPWM_SetTimerIO(DRVPWM_TIMER0, 1);
  33.         /* Enable Interrupt Sources of PWM Timer0 and install call back function */
  34.         DrvPWM_EnableInt(DRVPWM_TIMER0, 0, DRVPWM_PwmIRQHandler);
  35.         /* Enable the PWM Timer 0 */
  36.         DrvPWM_Enable(DRVPWM_TIMER0, 1);
  37. }

  38. void DRVPWM_CAPS()
  39. {
  40.         /* Enable PWM clock */
  41.         DrvPWM_Open();       
  42.         /* Set PWM pins */
  43.         DrvGPIO_InitFunction(E_FUNC_PWM23);

  44.         /* PWM Timer property for Capture */
  45.         sPtCAS.u8Mode = DRVPWM_AUTO_RELOAD_MODE;
  46.         sPtCAS.u32Frequency = 100;                        /* Set the proper frequency to capture data (Less than the input data)*/
  47.         sPtCAS.u8HighPulseRatio = 50;                /* High Pulse peroid : Total Pulse peroid = 50 : 100 (Set a non-zero value) */
  48.         sPtCAS.u32Duty = 0x10000;                        /* Set the counter to the maximum value */
  49.         sPtCAS.i32Inverter = 0;
  50.         //u8CapTimer = DRVPWM_CAP3;

  51.         /* Select PWM engine clock */
  52.                                 DrvPWM_SelectClockSource(DRVPWM_CAP3, DRVPWM_HCLK);
  53.                                
  54.                                 /* Set PWM Timer 3 for Capture */
  55.                                 DrvPWM_SetTimerClk(DRVPWM_CAP3, &sPt);
  56.                                
  57.                                 /* Enable Interrupt Sources of PWM Capture3 */
  58.                                 DrvPWM_EnableInt(DRVPWM_CAP3, DRVPWM_CAP_FALLING_INT, NULL);
  59.                                
  60.                                 /* Enable Input function for PWM Capture 3 */
  61.                                 DrvPWM_SetTimerIO(DRVPWM_CAP3, 1);
  62.                                
  63.                                 /* Enable the PWM Capture3 */
  64.                                 DrvPWM_Enable(DRVPWM_CAP3, 1);
  65.                
  66.                                 /* Capture the Input Waveform Data */
  67.                                 //CalPeriodTime(DRVPWM_CAP3);                                                       

  68.                                 /*--------------------------------------------------------------------------------------*/
  69.                                 /* Stop PWM Timer 1 (Recommended procedure method 1)                                            */
  70.                                 /* Set PWM Timer counter as 0, When PWM internal counter reaches to 0, disable PWM Timer*/                                    
  71.                                 /*--------------------------------------------------------------------------------------*/                                       
  72.                                                                        
  73.                                 /* Set PWM Timer 1 counter as 0 */
  74.                                 //DrvPWM_SetTimerCounter(u8Timer, 0);

  75.         /* Disable PWM Timer 1 */       
  76.                                 //DrvPWM_Enable(u8Timer, 0);
  77.                                
  78.                                 /* Disable Output function for PWM Timer 1 */
  79.                                 //DrvPWM_SetTimerIO(u8Timer, 0);
  80.                                
  81.                                 /*--------------------------------------------------------------------------------------*/
  82.                                 /* Stop PWM Timer 3 (Recommended procedure method 1)                                            */
  83.                                 /* Set PWM Timer counter as 0, When PWM internal counter reaches to 0, disable PWM Timer*/                                    
  84.                                 /*--------------------------------------------------------------------------------------*/                                                               
  85.                                
  86.                                 /* Set PWM Capture 3 counter as 0 */
  87.                                 DrvPWM_SetTimerCounter(DRVPWM_CAP3, 0);

  88.         /* Clear the PWM Capture 3 Interrupt */
  89.                         //        DrvPWM_ClearInt(DRVPWM_CAP3);
  90.                                                                                        
  91.                                 /* Disable PWM Capture 3 */       
  92.                 //                DrvPWM_Enable(DRVPWM_CAP3, 0);
  93.        
  94.                                 /* Disable Input function for PWM Capture 3 */
  95.                 //                DrvPWM_SetTimerIO(DRVPWM_CAP3, 0);

  96. }

  97. void set_pwm_u8HighPulseRatio(const uint8_t * buf)
  98. {
  99.         if(is_run==0)
  100.         {
  101.                 is_run=1;
  102.                 drvpwm_timer_run();
  103.         }

  104.         sPt.u8HighPulseRatio = buf[4];
  105.         if(buf[2]==1)
  106.                 DrvPWM_SetTimerClk(DRVPWM_TIMER0, &sPt);
  107.         else
  108.                 DrvPWM_SetTimerClk(DRVPWM_CAP3, &sPt);
  109. }               
  110. void set_pwm_u32Frequency(const uint8_t * buf)
  111. {
  112.         if(is_run==0)
  113.         {
  114.                 is_run=1;
  115.                 drvpwm_timer_run();
  116.         }
  117.         sPt.u32Frequency = buf[7]*256*256*256+buf[6]*256*256+buf[5]*256+buf[4];
  118.         if(buf[2]==1)
  119.                 DrvPWM_SetTimerClk(DRVPWM_TIMER0, &sPt);
  120.         else
  121.                 DrvPWM_SetTimerClk(DRVPWM_CAP3, &sPt);
  122. }

  123. void drvpwm_timer_stop()
  124. {
  125.          DrvPWM_Enable(DRVPWM_TIMER0, 0);
  126. }

  127. void drvpwm_timer_clear()
  128. {
  129.          DrvPWM_Enable(DRVPWM_TIMER0, 0);
  130.          DrvPWM_Close();
  131. }

  132. void drvpwm_start(uint8_t * buf)
  133. {
  134.         switch(buf[2])
  135.         {
  136.                 case 1:
  137.                         //DRVPWM_TIMER
  138.                         switch(buf[3])
  139.                         {
  140.                                 case 1:
  141.                                         //设置频率
  142.                                         set_pwm_u32Frequency(buf);
  143.                                         break;
  144.                                 case 2:
  145.                                         //设置高电平比率 x/100  (0-100)
  146.                                         set_pwm_u8HighPulseRatio(buf);
  147.                                         break;
  148.                         }
  149.                         break;
  150.                 case 2:
  151.                         //DRVPWMCAS
  152.                         switch(buf[3])
  153.                         {
  154.                                 case 1:
  155.                                         //设置频率
  156.                                         set_pwm_u8HighPulseRatio(buf);
  157.                                         break;
  158.                                 case 2:
  159.                                         //设置高电平比率 x/100  (0-100)
  160.                                         set_pwm_u8HighPulseRatio(buf);
  161.                                         break;
  162.                         }
  163.                         break;
  164.         }

  165. }


上位机程序:
  1. private void Set_u8HighPulseRatio_Scroll(object sender, EventArgs e)
  2.         {
  3.             this.commandDate[1] = 0x2;
  4.             this.commandDate[2] = 0x3;//PWM实验
  5.             this.commandDate[3] = 0x1;//pwm输出实验
  6.             this.commandDate[4] = 0x2;//调节亮度
  7.             this.commandDate[5] = (byte)Convert.ToByte(100 - this.Set_u8HighPulseRatio.Value);
  8.             this.NUM_u8HighPulseRatio.Text = this.Set_u8HighPulseRatio.Value.ToString();
  9.             this.SendData();
  10.         }

  11.         private void Set_u32Frequency_Scroll(object sender, EventArgs e)
  12.         {
  13.             this.commandDate[1] = 0x2;
  14.             this.commandDate[2] = 0x3;//PWM实验
  15.             this.commandDate[3] = 0x1;//pwm输出实验
  16.             this.commandDate[4] = 0x1;//调节频率
  17.             byte[] temp = System.BitConverter.GetBytes(this.Set_u32Frequency.Value);
  18.             for (int i = 0; i < temp.Length; i++)
  19.             {
  20.                 this.commandDate[i + 5] = temp[i];
  21.             }
  22.             this.NUM_u32Frequency.Text = this.Set_u32Frequency.Value.ToString();
  23.             this.SendData();
  24.         }


界面:


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

上位机+下位机程序:

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×

评分

参与人数 1威望 +10 收起 理由
hotpower + 10

查看全部评分

tao560532 发表于 2011-12-21 04:56 | 显示全部楼层
Swallow_0322 发表于 2011-12-21 07:24 | 显示全部楼层
顶!
电子write_cai 发表于 2011-12-21 08:27 | 显示全部楼层
我要做个乖孩子
consif 发表于 2011-12-21 12:00 | 显示全部楼层
学习下,顶额
yimuyiyang 发表于 2011-12-21 20:03 | 显示全部楼层
973400470 发表于 2011-12-21 21:20 | 显示全部楼层
很好,对我有很大帮助
A2000wps 发表于 2011-12-22 08:40 | 显示全部楼层
看不懂,:(
yanding708811 发表于 2011-12-24 13:44 | 显示全部楼层
顶!
hotpower 发表于 2011-12-25 07:28 | 显示全部楼层
晕,让老头如何回帖???

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


给你一个榔头!!!
crestxa 发表于 2011-12-25 23:45 | 显示全部楼层
谢谢,楼主能不能再坐个USB传AD的
 楼主| yy2bbs 发表于 2011-12-26 16:00 | 显示全部楼层
谢谢,楼主能不能再坐个USB传AD的
crestxa 发表于 2011-12-25 23:45

你说的是AD转换??还是AD采样?能说详细点么?我不大明白呀。
mcsgy 发表于 2011-12-27 11:29 | 显示全部楼层
强 顶
ddllxxrr 发表于 2011-12-28 08:49 | 显示全部楼层
上位机用什么做地
yanding708811 发表于 2012-1-5 23:01 | 显示全部楼层
人才,学习!
plc_avr 发表于 2012-1-6 07:41 | 显示全部楼层
顶!学习...
xiaocanyang 发表于 2012-1-10 12:01 | 显示全部楼层
求LZ的USB的具体数据指令协议。因为也是初学,弄不明白具体怎么工作的。
ichuangj 发表于 2012-1-11 17:14 | 显示全部楼层
dong_abc 发表于 2012-1-12 16:23 | 显示全部楼层
谢谢LZ,这几天就在弄USB的PC端程序。
zhaor 发表于 2012-1-13 09:11 | 显示全部楼层
上位机是VC程序吗?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

0

主题

85

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部