中级会员
- 积分
- 461
- 威望
- 93
- 贡献
- 42
- 兑换币
- 45
- 注册时间
- 2016-3-1
- 在线时间
- 163 小时
- 毕业学校
- 河海大学
|
#include <hidef.h> /* common defines and macros */
#include "derivative.h" /* derivative-specific definitions */
void delay(int x) {
int i=60000;
while(x--)
while(i--);
}
void SetBusCLK_16M(void)
{
CLKSEL=0X00; // disengage PLL to system
PLLCTL_PLLON=1; // turn on PLL
SYNR=0x00 | 0x01; // VCOFRQ[7:6];SYNDIV[5:0]
// fVCO= 2*fOSC*(SYNDIV + 1)/(REFDIV + 1)
// fPLL= fVCO/(2 × POSTDIV)
// fBUS= fPLL/2
// VCOCLK Frequency Ranges VCOFRQ[7:6]
// 32MHz <= fVCO <= 48MHz 00
// 48MHz < fVCO <= 80MHz 01
// Reserved 10
// 80MHz < fVCO <= 120MHz 11
REFDV=0x80 | 0x01; // REFFRQ[7:6];REFDIV[5:0]
// fREF=fOSC/(REFDIV + 1)
// REFCLK Frequency Ranges REFFRQ[7:6]
// 1MHz <= fREF <= 2MHz 00
// 2MHz < fREF <= 6MHz 01
// 6MHz < fREF <= 12MHz 10
// fREF > 12MHz 11
// pllclock=2*osc*(1+SYNR)/(1+REFDV)=32MHz;
POSTDIV=0x00; // 4:0, fPLL= fVCO/(2xPOSTDIV)
// If POSTDIV = $00 then fPLL is identical to fVCO (divide by one).
_asm(nop); // BUS CLOCK=16M
_asm(nop);
while(!(CRGFLG_LOCK==1)); //when pll is steady ,then use it;
CLKSEL_PLLSEL =1; //engage PLL to system;
}
static void PWM_Init(void)
{
PWMCTL_CON23=1; //2和3联合成16位PWM;
PWMCAE_CAE3=0; //选择输出模式为左对齐输出模式
PWMCNT23 = 0; //计数器清零;
PWMPOL_PPOL3=1; //先输出高电平,计数到DTY时,反转电平
PWMPRCLK = 0X00; //clockB 不分频,
PWMSCLB = 0x08; //对clock SB 16分频,pwm clock=clockB/16=1MHz;
PWMCLK_PCLK3 = 1; //选择clock SB做时钟源
PWMPER23 = 20000; //周期20ms; 50Hz;(可以使用的范围:50-200hz)
PWMDTY23 = 1510; //高电平时间为1.5ms;
PWME_PWME3 = 1;
}
void main(void) {
/* put your own code here */
DDRP=0XFF;
SetBusCLK_16M();
PWM_Init();
delay(10);
}
|
|