用STC15F2K61S2单片机定时器0做时钟源 产生一个20ms占空比可调的PWM照着例程 配置不出来,求高手指点,下面贴出我的代码,这个程序是可以产生一个20ms的PWM但是PWM的占空比不懂调制。
#include<stc15f2k60s2.h>
#include "intrins.h"
#define CCP_S0 0x10 //P_SW1.4
#define CCP_S1 0x20 //P_SW1.5
void Timer0Init(void); //20??@11.0592MHz
void main()
{
ACC = P_SW1;
ACC &= ~(CCP_S0 | CCP_S1); //CCP_S0=0 CCP_S1=0
P_SW1 = ACC; //(P1.2/ECI, P1.1/CCP0, P1.0/CCP1, P3.7/CCP2)
// ACC = P_SW1;
// ACC &= ~(CCP_S0 | CCP_S1); //CCP_S0=1 CCP_S1=0
// ACC |= CCP_S0; //(P3.4/ECI_2, P3.5/CCP0_2, P3.6/CCP1_2, P3.7/CCP2_2)
// P_SW1 = ACC;
//
// ACC = P_SW1;
// ACC &= ~(CCP_S0 | CCP_S1); //CCP_S0=0 CCP_S1=1
// ACC |= CCP_S1; //(P2.4/ECI_3, P2.5/CCP0_3, P2.6/CCP1_3, P2.7/CCP2_3)
// P_SW1 = ACC;
CCON = 0; //???PCA?????
//PCA?????
//??CF??
//????????
CL = 0; //??PCA???
CH = 0;
CMOD = 0x40; //Ñ¡Ôñ¶¨Ê±Æ÷Áã×÷ΪʱÖÓÔ´
//??PCA???????
PCA_PWM0 = 0x00; //PCA¹¤×÷ÔÚ8λPWM
CCAP0H = CCAP0L =0x0F; //PWM0?????87.5% ((100H-20H)/100H)
CCAPM0 = 0x42; //PCA??0?8?PWM??
PCA_PWM1 = 0x40; //PCA??1???7?PWM
CCAP1H = CCAP1L = 0x0a; //PWM1?????75% ((80H-20H)/80H)
CCAPM1 = 0x42; //PCA??1?7?PWM??
PCA_PWM2 = 0x80; //PCA??2???6?PWM
CCAP2H = CCAP2L = 0x0a; //PWM2?????50% ((40H-20H)/40H)
CCAPM2 = 0x42; //PCA??2?6?PWM??
//CR = 1; //PCA???????
Timer0Init();
EA=1;
ET0=1;//´ò¿ª¶¨Ê±Æ÷0ÖжÏ
while (1);
}
void Timer0Init(void) //20??@11.0592MHz
{
AUXR &= 0x7F; //?????12T??
TMOD &= 0xF0; //???????
TL0 = 0x00; //??????
TH0 = 0xB8; //??????
TF0 = 0; //??TF0??
TR0 = 1; //???0????
}
#include<stc15f2k60s2.h>
#include "intrins.h"
#define CCP_S0 0x10 //P_SW1.4
#define CCP_S1 0x20 //P_SW1.5
void Timer0Init(void); //20??@11.0592MHz
void main()
{
ACC = P_SW1;
ACC &= ~(CCP_S0 | CCP_S1); //CCP_S0=0 CCP_S1=0
P_SW1 = ACC; //(P1.2/ECI, P1.1/CCP0, P1.0/CCP1, P3.7/CCP2)
// ACC = P_SW1;
// ACC &= ~(CCP_S0 | CCP_S1); //CCP_S0=1 CCP_S1=0
// ACC |= CCP_S0; //(P3.4/ECI_2, P3.5/CCP0_2, P3.6/CCP1_2, P3.7/CCP2_2)
// P_SW1 = ACC;
//
// ACC = P_SW1;
// ACC &= ~(CCP_S0 | CCP_S1); //CCP_S0=0 CCP_S1=1
// ACC |= CCP_S1; //(P2.4/ECI_3, P2.5/CCP0_3, P2.6/CCP1_3, P2.7/CCP2_3)
// P_SW1 = ACC;
CCON = 0; //???PCA?????
//PCA?????
//??CF??
//????????
CL = 0; //??PCA???
CH = 0;
CMOD = 0x40; //Ñ¡Ôñ¶¨Ê±Æ÷Áã×÷ΪʱÖÓÔ´
//??PCA???????
PCA_PWM0 = 0x00; //PCA¹¤×÷ÔÚ8λPWM
CCAP0H = CCAP0L =0x0F; //PWM0?????87.5% ((100H-20H)/100H)
CCAPM0 = 0x42; //PCA??0?8?PWM??
PCA_PWM1 = 0x40; //PCA??1???7?PWM
CCAP1H = CCAP1L = 0x0a; //PWM1?????75% ((80H-20H)/80H)
CCAPM1 = 0x42; //PCA??1?7?PWM??
PCA_PWM2 = 0x80; //PCA??2???6?PWM
CCAP2H = CCAP2L = 0x0a; //PWM2?????50% ((40H-20H)/40H)
CCAPM2 = 0x42; //PCA??2?6?PWM??
//CR = 1; //PCA???????
Timer0Init();
EA=1;
ET0=1;//´ò¿ª¶¨Ê±Æ÷0ÖжÏ
while (1);
}
void Timer0Init(void) //20??@11.0592MHz
{
AUXR &= 0x7F; //?????12T??
TMOD &= 0xF0; //???????
TL0 = 0x00; //??????
TH0 = 0xB8; //??????
TF0 = 0; //??TF0??
TR0 = 1; //???0????
}
回复
有奖活动 | |
---|---|
【有奖活动】分享技术经验,兑换京东卡 | |
话不多说,快进群! | |
请大声喊出:我要开发板! | |
【有奖活动】EEPW网站征稿正在进行时,欢迎踊跃投稿啦 | |
奖!发布技术笔记,技术评测贴换取您心仪的礼品 | |
打赏了!打赏了!打赏了! |
打赏帖 | |
---|---|
与电子爱好者谈读图四被打赏50分 | |
与电子爱好者谈读图二被打赏50分 | |
【FRDM-MCXN947评测】Core1适配运行FreeRtos被打赏50分 | |
【FRDM-MCXN947评测】双核调试被打赏50分 | |
【CPKCORRA8D1B评测】---移植CoreMark被打赏50分 | |
【CPKCORRA8D1B评测】---打开硬件定时器被打赏50分 | |
【FRDM-MCXA156评测】4、CAN loopback模式测试被打赏50分 | |
【CPKcorRA8D1评测】--搭建初始环境被打赏50分 | |
【FRDM-MCXA156评测】3、使用FlexIO模拟UART被打赏50分 | |
【FRDM-MCXA156评测】2、rt-thread MCXA156 BSP制作被打赏50分 |