需要习惯并学会看手册
请参考STC-ISP里的范例程序里的STC8系列的 "PCA/CCP口切换"
我给你来个程序试试
- #include "reg51.h"
- #include "intrins.h"
- //测试工作频率为11.0592MHz
- #define T38K4HZ (11059200L / 2 / 38400)
- sfr CCON = 0xd8;
- sbit CF = CCON^7;
- sbit CR = CCON^6;
- sbit CCF3 = CCON^3;
- sbit CCF2 = CCON^2;
- sbit CCF1 = CCON^1;
- sbit CCF0 = CCON^0;
- sfr CMOD = 0xd9;
- sfr CL = 0xe9;
- sfr CH = 0xf9;
- sfr CCAPM0 = 0xda;
- sfr CCAP0L = 0xea;
- sfr CCAP0H = 0xfa;
- sfr PCA_PWM0 = 0xf2;
- sfr CCAPM1 = 0xdb;
- sfr CCAP1L = 0xeb;
- sfr CCAP1H = 0xfb;
- sfr PCA_PWM1 = 0xf3;
- sfr CCAPM2 = 0xdc;
- sfr CCAP2L = 0xec;
- sfr CCAP2H = 0xfc;
- sfr PCA_PWM2 = 0xf4;
- sfr CCAPM3 = 0xdd;
- sfr CCAP3L = 0xed;
- sfr CCAP3H = 0xfd;
- sfr PCA_PWM3 = 0xf5;
- sfr P_SW1 = 0xa2;
- unsigned int value;
- void PCA_Isr() interrupt 7
- {
- CCF0 = 0; //清中断标志
- CCAP0L = value;
- CCAP0H = value >> 8; //更新比较值
- value += T38K4HZ;
- }
- void main()
- {
- CCON = 0x00; //初始化PCA控制寄存器
- CMOD = 0x08; //PCA时钟为系统时钟
- CL = 0x00; //复位PCA寄存器
- CH = 0x00;
- CCAPM0 = 0x4d; //PCA模块0为16位定时器模式并使能脉冲输出
- value = T38K4HZ;
- CCAP0L = value; //CCP0 P1.7输出38.4KHz方波
- CCAP0H = value >> 8; //初始化PCA模块0
- value += T38K4HZ;
- P_SW1 = 0x00; //ECI/P1.2, CCP0/P1.7, CCP1/P1.6, CCP2/P1.5,CCP3/P1.4
- // P_SW1 = 0x10; //ECI_2/P2.2, CCP0_2/P2.3, CCP1_2/P2.4, CCP2_2/P2.5,CCP3_2/P2.6
- // P_SW1 = 0x20; //ECI_3/P7.4, CCP0_3/P7.0, CCP1_3/P7.1, CCP2_3/P7.2,CCP3_3/P7.3
- // P_SW1 = 0x30; //ECI_4/P3.5, CCP0_4/P3.3, CCP1_4/P3.2, CCP2_4/P3.1,CCP3_4/P3.0
- CR = 1; //启动PCA计时器
- EA = 1;
- while (1);
- }
复制代码 |