📄 main.c
字号:
/*****************头文件****************************************/
#include <hidef.h> /* for EnableInterrupts macro */
#include "derivative.h" /* include peripheral declarations */
/*********************变量和函数定义****************************/
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 = 0x90;
EnableInterrupts; //PWM占空比为0
while(1){
__RESET_WATCHDOG();
/*if(Duty<Period) //判断脉宽是否达到周期的长度(占空比为100%)
Duty=Duty+1; //PWM占空比加1
else{
Delay_ms(1000);
Duty=0; //占空比达到100%,设置占空比为0%
}*/
TPM1SC=0X10;
TPM1C0VH = (Duty>>8); ////设置PWM脉宽,设置高字节
TPM1C0VL = Duty; //设置低字节
TPM1SC=0X10;
}
}
/************************PWM初始化设置**************************/
void PWMInit(unsigned int Period)
{
unsigned int Temp=Period;
TPM1SC=0X10; //不允许溢出中断,不启动计数,分频因子=4
TPM1MODH = (Temp>>8); //设置PWM周期
TPM1MODL = Period;
TPM1C0VH = 0x00;//初始化设置PWM脉宽为0
TPM1C0VL = 0x00;
TPM1C0SC=0X28; //设为PWM模式,不允许输出比较中断,输出低电平,不带缓冲
}
/************************延时函数********************************/
void Delay_ms(int i) {
int k;
for(k=i;k<i;k--) {
;}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -