stc12c2052ad单片机pwm输出程序
答案:1 悬赏:0 手机版
解决时间 2021-02-18 08:16
- 提问者网友:浪荡绅士
- 2021-02-17 16:44
stc12c2052ad单片机pwm输出程序
最佳答案
- 五星知识达人网友:woshuo
- 2021-02-17 17:34
PWM0 3.7
PWM1 3.5
#include "reg51.h"
#include "intrins.h"
#define FOSC 18432000L
typedef unsigned char BYTE;
typedef unsigned int WORD;
sfr CCON = 0xD8; //PCA control register
sbit CCF0 = CCON^0; //PCA module-0 interrupt flag
sbit CCF1 = CCON^1; //PCA module-1 interrupt flag
sbit CR = CCON^6; //PCA timer run control bit
sbit CF = CCON^7; //PCA timer overflow flag
sfr CMOD = 0xD9; //PCA mode register
sfr CL = 0xE9; //PCA base timer LOW
sfr CH = 0xF9; //PCA base timer HIGH
sfr CCAPM0 = 0xDA; //PCA module-0 mode register
sfr CCAP0L = 0xEA; //PCA module-0 capture register LOW
sfr CCAP0H = 0xFA; //PCA module-0 capture register HIGH
sfr CCAPM1 = 0xDB; //PCA module-1 mode register
sfr CCAP1L = 0xEB; //PCA module-1 capture register LOW
sfr CCAP1H = 0xFB; //PCA module-1 capture register HIGH
sfr CCAPM2 = 0xDC; //PCA module-2 mode register
sfr CCAP2L = 0xEC; //PCA module-2 capture register LOW
sfr CCAP2H = 0xFC; //PCA module-2 capture register HIGH
sfr CCAPM3 = 0xDD; //PCA module-3 mode register
sfr CCAP3L = 0xED; //PCA module-3 capture register LOW
sfr CCAP3H = 0xFD; //PCA module-3 capture register HIGH
sfr PCAPWM0 = 0xF2;
sfr PCAPWM1 = 0xF3;
sfr PCAPWM2 = 0xF4;
sfr PCAPWM3 = 0xF5;
void main()
{
CCON = 0; //Initial PCA control register
//PCA timer stop running
//Clear CF flag
//Clear all module interrupt flag
CL = 0; //Reset PCA base timer
CH = 0;
CMOD = 0x02; //Set PCA timer clock source as Fosc/2
//Disable PCA timer overflow interrupt
CCAP0H = CCAP0L = 0x80; //PWM0 port output 50% duty cycle square wave
CCAPM0 = 0x42; //PCA module-0 work in 8-bit PWM mode and no PCA interrupt
CCAP1H = CCAP1L = 0xff; //PWM1 port output 0% duty cycle square wave
PCAPWM1 = 0x03;
CCAPM1 = 0x42; //PCA module-1 work in 8-bit PWM mode and no PCA interrupt
CR = 1; //PCA timer start run
while (1);
}
PWM1 3.5
#include "reg51.h"
#include "intrins.h"
#define FOSC 18432000L
typedef unsigned char BYTE;
typedef unsigned int WORD;
sfr CCON = 0xD8; //PCA control register
sbit CCF0 = CCON^0; //PCA module-0 interrupt flag
sbit CCF1 = CCON^1; //PCA module-1 interrupt flag
sbit CR = CCON^6; //PCA timer run control bit
sbit CF = CCON^7; //PCA timer overflow flag
sfr CMOD = 0xD9; //PCA mode register
sfr CL = 0xE9; //PCA base timer LOW
sfr CH = 0xF9; //PCA base timer HIGH
sfr CCAPM0 = 0xDA; //PCA module-0 mode register
sfr CCAP0L = 0xEA; //PCA module-0 capture register LOW
sfr CCAP0H = 0xFA; //PCA module-0 capture register HIGH
sfr CCAPM1 = 0xDB; //PCA module-1 mode register
sfr CCAP1L = 0xEB; //PCA module-1 capture register LOW
sfr CCAP1H = 0xFB; //PCA module-1 capture register HIGH
sfr CCAPM2 = 0xDC; //PCA module-2 mode register
sfr CCAP2L = 0xEC; //PCA module-2 capture register LOW
sfr CCAP2H = 0xFC; //PCA module-2 capture register HIGH
sfr CCAPM3 = 0xDD; //PCA module-3 mode register
sfr CCAP3L = 0xED; //PCA module-3 capture register LOW
sfr CCAP3H = 0xFD; //PCA module-3 capture register HIGH
sfr PCAPWM0 = 0xF2;
sfr PCAPWM1 = 0xF3;
sfr PCAPWM2 = 0xF4;
sfr PCAPWM3 = 0xF5;
void main()
{
CCON = 0; //Initial PCA control register
//PCA timer stop running
//Clear CF flag
//Clear all module interrupt flag
CL = 0; //Reset PCA base timer
CH = 0;
CMOD = 0x02; //Set PCA timer clock source as Fosc/2
//Disable PCA timer overflow interrupt
CCAP0H = CCAP0L = 0x80; //PWM0 port output 50% duty cycle square wave
CCAPM0 = 0x42; //PCA module-0 work in 8-bit PWM mode and no PCA interrupt
CCAP1H = CCAP1L = 0xff; //PWM1 port output 0% duty cycle square wave
PCAPWM1 = 0x03;
CCAPM1 = 0x42; //PCA module-1 work in 8-bit PWM mode and no PCA interrupt
CR = 1; //PCA timer start run
while (1);
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯