飞思卡尔8位单片机HCS08核的PWM例程
0赞
发表于 2011/11/23 22:22:30
阅读(2503)
实验介绍:
本程序是PWM模块的例程,所用资源为单片机内部TPM模块
实验现象:
LED的闪烁频率逐渐减小,直到熄灭,然后再次点亮,如此循环熄灭点亮!
LED的闪烁频率逐渐减小,直到熄灭,然后再次点亮,如此循环熄灭点亮!
/*****************头文件****************************************/
#include /* for EnableInterrupts macro */
#include "derivative.h" /* include peripheral declarations */
#include
#include "derivative.h" /* include peripheral declarations */
/*********************变量和函数定义****************************/
void PWMInit(unsigned int i);
void Delay_ms(int i);
void PWMInit(unsigned int i);
void Delay_ms(int i);
/*********************主函数************************************/
void main(){
unsigned int Period; //PWM周期
unsigned int Duty; //PWM占空比
Period =0x120;//变量初始化
PWMInit(Period);//PWM初始化
Duty = 0;
EnableInterrupts; //PWM占空比为0
while(1){
__RESET_WATCHDOG();
if(DutyDuty=Duty+1; //PWM占空比加1
else{
Delay_ms(1000);
Duty=0; //占空比达到100%,设置占空比为0%
}
TPMSC=0X12;
TPMC0VH = (Duty>>8); ////设置PWM脉宽,设置高字节
TPMC0VL = Duty; //设置低字节
TPMSC=0X12;
}
}
void main(){
unsigned int Period; //PWM周期
unsigned int Duty; //PWM占空比
Period =0x120;//变量初始化
PWMInit(Period);//PWM初始化
Duty = 0;
EnableInterrupts; //PWM占空比为0
while(1){
__RESET_WATCHDOG();
if(Duty
else{
Delay_ms(1000);
Duty=0; //占空比达到100%,设置占空比为0%
}
TPMSC=0X12;
TPMC0VH = (Duty>>8); ////设置PWM脉宽,设置高字节
TPMC0VL = Duty; //设置低字节
TPMSC=0X12;
}
}
/************************PWM初始化设置**************************/
void PWMInit(unsigned int Period)
{
unsigned int Temp=Period;
TPMSC=0X12; //不允许溢出中断,不启动计数,分频因子=4
TPMMODH = (Temp>>8); //设置PWM周期
TPMMODL = Period;
TPMC0VH = 0x00;//初始化设置PWM脉宽为0
TPMC0VL = 0x00;
TPMC0SC=0X28; //设为PWM模式,不允许输出比较中断,输出低电平,不带缓冲
}
{
unsigned int Temp=Period;
TPMSC=0X12; //不允许溢出中断,不启动计数,分频因子=4
TPMMODH = (Temp>>8); //设置PWM周期
TPMMODL = Period;
TPMC0VH = 0x00;//初始化设置PWM脉宽为0
TPMC0VL = 0x00;
TPMC0SC=0X28; //设为PWM模式,不允许输出比较中断,输出低电平,不带缓冲
}
/************************延时函数********************************/
void Delay_ms(int i) {
void Delay_ms(int i) {
int k;
for(k=i;k;}
}
for(k=i;k;}
}