STC15单片机PWM实际波形测量
Posted perseverance52
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了STC15单片机PWM实际波形测量相关的知识,希望对你有一定的参考价值。
STC15单片机PWM实际波形测量
本实验跑的官方PWM示例
- 官方示例代码下载位置: https://www.stcmcudata.com/
- 示例8:
主程序注释掉了增量变化
/*------------------------------------------------------------------*/
/* --- STC MCU International Limited -------------------------------*/
/* --- STC 1T Series MCU RC Demo -----------------------------------*/
/* --- Mobile: (86)13922805190 -------------------------------------*/
/* --- Fax: 86-0513-55012956,55012947,55012969 ---------------------*/
/* --- Tel: 86-0513-55012928,55012929,55012966 ---------------------*/
/* --- Web: www.GXWMCU.com -----------------------------------------*/
/* --- QQ: 800003751 ----------------------------------------------*/
/* If you want to use the program or the program referenced in the */
/* article, please specify in which data and procedures from STC */
/*------------------------------------------------------------------*/
#include "config.h"
#include "PCA.h"
#include "delay.h"
/************* 功能说明 **************
输出3路9~16位变化的PWM信号。类似"呼吸灯"的驱动.
PWM频率 = MAIN_Fosc / PWM_DUTY, 假设 MAIN_Fosc = 24MHZ, PWM_DUTY = 6000, 则输出PWM频率为4000HZ.
******************************************/
/************* 本地常量声明 **************/
/************* 本地变量声明 **************/
u16 pwm0,pwm1,pwm2;
/************* 本地函数声明 **************/
/************* 外部函数和变量声明 *****************/
void PCA_config(void)
{
PCA_InitTypeDef PCA_InitStructure;
PCA_InitStructure.PCA_Clock = PCA_Clock_1T; //PCA_Clock_1T, PCA_Clock_2T, PCA_Clock_4T, PCA_Clock_6T, PCA_Clock_8T, PCA_Clock_12T, PCA_Clock_Timer0_OF, PCA_Clock_ECI
PCA_InitStructure.PCA_IoUse = PCA_P12_P11_P10_P37; //PCA_P12_P11_P10_P37, PCA_P34_P35_P36_P37, PCA_P24_P25_P26_P27
PCA_InitStructure.PCA_Interrupt_Mode = DISABLE; //ENABLE, DISABLE
PCA_InitStructure.PCA_Polity = PolityHigh; //优先级设置 PolityHigh,PolityLow
PCA_InitStructure.PCA_RUN = DISABLE; //ENABLE, DISABLE
PCA_Init(PCA_Counter,&PCA_InitStructure);
PCA_InitStructure.PCA_Mode = PCA_Mode_HighPulseOutput; //PCA_Mode_PWM, PCA_Mode_Capture, PCA_Mode_SoftTimer, PCA_Mode_HighPulseOutput
PCA_InitStructure.PCA_PWM_Wide = 0; //PCA_PWM_8bit, PCA_PWM_7bit, PCA_PWM_6bit
PCA_InitStructure.PCA_Interrupt_Mode = ENABLE; //PCA_Rise_Active, PCA_Fall_Active, ENABLE, DISABLE
PCA_InitStructure.PCA_Value = 65535; //对于软件定时, 为匹配比较值
PCA_Init(PCA0,&PCA_InitStructure);
PCA_InitStructure.PCA_Mode = PCA_Mode_HighPulseOutput; //PCA_Mode_PWM, PCA_Mode_Capture, PCA_Mode_SoftTimer, PCA_Mode_HighPulseOutput
PCA_InitStructure.PCA_PWM_Wide = 0; //PCA_PWM_8bit, PCA_PWM_7bit, PCA_PWM_6bit
PCA_InitStructure.PCA_Interrupt_Mode = ENABLE; //PCA_Rise_Active, PCA_Fall_Active, ENABLE, DISABLE
PCA_InitStructure.PCA_Value = 65535; //对于软件定时, 为匹配比较值
PCA_Init(PCA1,&PCA_InitStructure);
PCA_InitStructure.PCA_Mode = PCA_Mode_HighPulseOutput; //PCA_Mode_PWM, PCA_Mode_Capture, PCA_Mode_SoftTimer, PCA_Mode_HighPulseOutput
PCA_InitStructure.PCA_PWM_Wide = 0; //PCA_PWM_8bit, PCA_PWM_7bit, PCA_PWM_6bit
PCA_InitStructure.PCA_Interrupt_Mode = ENABLE; //PCA_Rise_Active, PCA_Fall_Active, ENABLE, DISABLE
PCA_InitStructure.PCA_Value = 65535; //对于软件定时, 为匹配比较值
PCA_Init(PCA2,&PCA_InitStructure);
}
/******************** task A **************************/
void main(void)
{
P2M1 &= ~(0xe0); //P2.7 P2.6 P2.5 设置为推挽输出
P2M0 |= (0xe0);
PCA_config();
pwm0 = (PWM_DUTY / 4 * 1); //给PWM一个初值
pwm1 = (PWM_DUTY / 4 * 2);
pwm2 = (PWM_DUTY / 4 * 3);
PWMn_Update(PCA0,pwm0);
PWMn_Update(PCA1,pwm1);
PWMn_Update(PCA2,pwm2);
EA = 1;
while (1)
{
delay_ms(2);
//if(++pwm0 >= PWM_HIGH_MAX)
pwm0 = PWM_HIGH_MAX;//设置为最大
PWMn_Update(PCA0,pwm0);
//if(++pwm1 >= PWM_HIGH_MAX)
pwm1 = PWM_HIGH_MIN;
PWMn_Update(PCA1,pwm1);
//if(++pwm2 >= PWM_HIGH_MAX)
pwm2 = PWM_HIGH_MIN;
PWMn_Update(PCA2,pwm2);
}
}
- 示波器测得
P1.1
的脉宽波形,如下:
- 示波器测得
P3.7
的脉宽波形,如下:
- 由于
P1.0
IO口与GND相同了,测不到波形。
以上是关于STC15单片机PWM实际波形测量的主要内容,如果未能解决你的问题,请参考以下文章
STC单片机通过ADC外部输入调节PWM占空比输出并串口打印当前脉冲值