hclmcu

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

没废话,真接贴代码:(如需转载,请注明出处)

#include "stdio.h"
#include "xparameters.h"
#include "xbasic_types.h"

#define PWM1_addr  XPAR_PWM_BASEADDR //添加的ip core名字为pwm

void PWM_x(unsigned long BaseAddr, double period, double Duty)

 //==============定义相关寄存器的地址=============================
 #define TCSR0 (*(volatile unsigned long *)(BaseAddr+0x00))
 #define TLR0 (*(volatile unsigned long *)(BaseAddr+0x04))
 #define TCR0 (*(volatile unsigned long *)(BaseAddr+0x08))
 #define TCSR1 (*(volatile unsigned long *)(BaseAddr+0x10))
 #define TLR1 (*(volatile unsigned long *)(BaseAddr+0x14))
 #define TCR1 (*(volatile unsigned long *)(BaseAddr+0x18))
 //===========================================================
 double clk = 50000000;  
 double clk_width = 1/clk;
 //=====一个周期时间、高电平持续时间================================
 unsigned long PWM_period = period/clk_width;
 unsigned long PWM_high = Duty * PWM_period; 
 //====装载Load Register=======================================
 TLR0 = PWM_period;
 TLR1 = PWM_high;
 //====装载Control/Status Register=============================
 TCSR0=0x606;
 TCSR1=0x606;
}

 
 //====================main===================================
 int main()
 {
  //unsigned long BaseAddr, double period, double Duty
  //period is in seconds, and duty cycle is in decimal,
  //not percentage
  PWM_x(PWM1_addr, 0.01, 0.5);
 }

 相关资料文档可以在xps软件下system assembly view下双击自己添加的pwm ip core查看其datasheet.

因为还不会将pdf文档传上来,所以,直接到按上述方法查看其资料。

posted on 2010-09-02 17:19  hclmcu  阅读(360)  评论(0编辑  收藏  举报