STC12C5A60S2输出PWM波形示例程序-IO及PCA
1、利用PCA生产PWM代码如下:
/*------------------------------------------------------------------*/
/* --- STC MCU Limited ---------------------------------------------*/
/* --- STC12C5Axx Series MCU PCA module output PWM wave Demo -------*/
/* --- Mobile: (86)139******** -------------------------------------*/
/* --- Fax: 86-0513-********,55012947,55012969 ---------------------*/
/* --- Tel: 86-0513-********,55012929,55012966----------------------*/
/* --- Web: https://www.360docs.net/doc/6f11504169.html, -----------------------------------------*/
/* --- Web: https://www.360docs.net/doc/6f11504169.html, -----------------------------------------*/
/* If you want to use the program or the program referenced in the */
/* article, please specify in which data and procedures from STC */
/*------------------------------------------------------------------*/
#include "reg51.h"
#include "intrins.h"
#define FOSC 11059200L
typedef unsigned char BYTE;
typedef unsigned int WORD;
/*Declare SFR associated with the PCA */
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 PCAPWM0 = 0xf2;
sfr PCAPWM1 = 0xf3;
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);
}
2、利用定时器0,使用IO口生产PWM例程如下,驱动LED灯亮灭
/*****************************
论坛支持:https://www.360docs.net/doc/6f11504169.html,
*******************************/
#include
sbit LED=P0^0; //定义LED端口
/*------------------------------------------------
定时器初始化子程序
------------------------------------------------*/
void Init_Timer0(void)
{
TMOD |= 0x01; //使用模式1,16位定时器,使用"|"符号可以在使用多个定时器时不受影响
TH0=0x00; //给定初值,这里使用定时器最大值从0开始计数一直到65535溢出TL0=0x00;
EA=1; //总中断打开
ET0=1; //定时器中断打开
TR0=1; //定时器开关打开
}
/*------------------------------------------------
主程序
------------------------------------------------*/
void main(void)
{
Init_Timer0();
while(1);
}
/*------------------------------------------------
定时器中断子程序
------------------------------------------------*/
void Timer0_isr(void) interrupt 1
{
TH0=0x00; //重新赋值
TL0=0x00;
LED=~LED; //指示灯反相,可以看到闪烁}