#include <hidef.h>
#include "derivative.h"
#define LEDCPU PORTB_PB1 //定义连接发光二级管的PORTB_PB1口数据寄存
//为LED,写'0'亮,写'1' 灭
#define LEDCPU_dir DDRB_DDRB1 //定义连接发光二级管的PORTB_PB1口方向寄存器
//为LEDCPU_dir,写'0'做输入口,写'1'做输出口
void INIT_PLL(void) //busclock 32M
{
CLKSEL_PLLSEL=0; //总线时钟来源于晶振
PLLCTL_PLLON=0; //先关闭PLL
SYNR=0x40 | 0x03;
REFDV=0x80 | 0x01;
POSTDIV=0x00; //PLL配置为64MHz
PLLCTL_PLLON=1; //打开PLL
_asm(nop);
_asm(nop);
while(!(CRG**_LOCK==1)); //确定PLL是否稳定LOCK=1 稳定,=0 不稳定
CLKSEL_PLLSEL =1; //总线时钟来源于PLL
}
void init_pwm(void)
{
PWMCTL_CON01= 1; //级联0号和1号通道形成16位PWM通道号,由PWM1输出
PWMPOL_PPOL1= 1; //通道01的极性为高电平有效
PWMPRCLK = 0x55; //A时钟和B时钟的分频系数为32,频率为1MHz
PWMSCLA = 25; //SA时钟设置寄存器
//CLOCK SA=CLOCK A/(2*PWMSCLA)
//CLOCK SA=1M/2*25=20KHz
PWMCLK =0x02; //通道01用SA时钟作为时钟源
PWMPER01 = 200; //通道01的周期为100Hz
PWMDTY01 = 0; //通道01的占空比为0%
PWME_PWME1 = 1; //使能通道01
}
void delay(void)
{
unsigned int i,j;
for(i=0;i<20;i++)
for(j=0;j<50000;j++);
}
void main(void) {
DisableInterrupts; //禁止所有中断打开
INIT_PLL(); //初始化PLL模块
init_pwm(); //初始化pwm模块
LEDCPU_dir=1; //设置连接发光二级管的PORTB_PB0口为输出口
LEDCPU=1; //初始化发光二极管为亮
EnableInterrupts; //允许所有中断打开
for(;;)
{
delay();
PWMDTY01=20; //占空比为10%
delay();
PWMDTY01=40; //占空比为20%
delay();
PWMDTY01=60; //占空比为30%
delay();
PWMDTY01=80; //占空比为40%
delay();
PWMDTY01=100; //占空比为50%
delay();
PWMDTY01=120; //占空比为60%
delay();
PWMDTY01=140; //占空比为70%
delay();
PWMDTY01=160; //占空比为80%
delay();
PWMDTY01=180; //占空比为90%
delay();
PWMDTY01=200; //占空比为100%
delay();
PWMDTY01=180; //占空比为90%
delay();
PWMDTY01=160; //占空比为80%
delay();
PWMDTY01=140; //占空比为70%
delay();
PWMDTY01=120; //占空比为60%
delay();
PWMDTY01=100; //占空比为50%
delay();
PWMDTY01=80; //占空比为40%
delay();
PWMDTY01=60; //占空比为30%
delay();
PWMDTY01=40; //占空比为20%
}
}
|