STC15F2K60S2的 出不了波形 谁能帮我看看 谢谢各位了
#include"pwm.h"
/*******************PCA模块捕获初始化*******************************/
void fn_PCA_Init(unsigned char CCAPMn,unsigned char PCA_CCAPnL,unsigned char PCA_CCAPnH,unsigned char PCA_CLK)
{
CCON = 0x00; //PCA控制寄存器
AUXR1 |= 0x30;
CL = 0x00; //清除PCA计数器
CH = 0x00;
CMOD = 0x80|PCA_CLK; //空闲模式下PCA停止工作|PCA,PWM时钟源选择输入方式
PCA_PWM0 = 0x00; //在PWM模式下CCAPL0九位数 CCAPH0九位数
P1M0 |= 0x02;
P1M1 &= 0xff;
CCAPM0 = CCAPMn; //模块比较寄存器,输出口选择
CCAP0L = PCA_CCAPnL; // 用于控制输出占空比
CCAP0H = PCA_CCAPnH;
// step0 = ((PCA_CCAPnH&0x00ff)<<8)|PCA_CCAPnL;
CF = 0;
// IP = 0x80; //中断优先级
// EA = 1; //总中断
CR = 1; //启动PCA计数器
}
/********************初始化定时器0,作为PCA时钟源*********************/
void fn_Timer0_Init()
{
TMOD|= 0x02; // 采用定时器0方式2 8位自动重载
AUXR|= 0x80; // 定时器0工作1T
TH0 = 0;
TL0 = 0;
// TR0 = 1;
}
/*功能描述:定时器0作为PCA时钟输入源,设置频率
*参数选择:Fre要设置的频率 184Hz~45KHz
*/
void Set_PWM0_Fre(unsigned int Fre)
{
TH0 = Fre;
TL0 = Fre;
TR0=1;
}
/*功能描述:设置PWM占空比
*参数选择:duty 占空比0~255的范围
*/
void Set_PWM0(unsigned char duty)
{
CCAP0L = duty;
CCAP0H = duty;
}
/****************PWM。h文件
#ifndef _PWM_H
#define _PWM_H
#include "STC15F2K60S2.H"
#include "intrins.h"
#define FOSE 12000000 //系统时钟
//PCA时钟源
#define CLK0 0x00 //Fosc/12
#define CLK1 0x02 //Fosc/2
#define CLK2 0x04 //定时器0的溢出率
#define CLK3 0x06 //ECI外部时钟
#define CLK4 0x08 //Fosc/1
#define CLK5 0x0a //Fosc/4
#define CLK6 0x0c //Fosc/6
#define CLK7 0x0e //Fosc/8
//捕获
#define RISE 0x20 //CCAPM0 上升沿捕获
#define ECCF 0x01 // PCA 使能中断
//PWM可调模式
//PWM频率=时钟源/256
// 占空比=(CCAPnL/256)*100%
#define PWM 0x42 // 允许比较器功能 PWM可调
//高速输出模式(8Hz~64Mhz)
//CCAP0H CCAP0L=INT (PCA时钟源/(2*f)+0.5)
#define HIGH_SPEED_OUT 0x4C
//定时器模式
//CCAPnH,CCAPnL=t/(1/PCA时钟源)
#define Timer 0x48
#define ECCF 0x01
extern void fn_PCA_Init(unsigned char CCAPMn,unsigned char PCA_CCAPnL,unsigned char PCA_CCAPnH,unsigned char PCA_CLCK);
extern void fn_Timer0_Init();
extern void Set_PWM0_Fre(unsigned int Fre);
extern void Set_PWM0(unsigned char duty);
extern void delay(unsigned int z);
#endif
|