找回密码
 立即注册
查看: 607|回复: 0

关于stc15系列硬件pwm库函数的疑惑,求解答

[复制链接]

该用户从未签到

2

主题

1

回帖

17

积分

新手上路

积分
17
发表于 2023-3-5 13:03:19 | 显示全部楼层 |阅读模式
本帖最后由 zjb12702881 于 2023-3-5 13:04 编辑

PCA.H

PCA.H




/*------------------------------------------------------------------*/
/* --- 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        "PCA.h"

bit                B_Capture0,B_Capture1,B_Capture2;
u8                PCA0_mode,PCA1_mode,PCA2_mode;
u16                CCAP0_tmp,PCA_Timer0;
u16                CCAP1_tmp,PCA_Timer1;
u16                CCAP2_tmp,PCA_Timer2;

/*************        功能说明        **************


******************************************/




//========================================================================
// 函数: UpdatePwm(u8 PCA_id, u8 pwm_value)
// 描述: 更新PWM值.
// 参数: PCA_id: PCA序号. 取值 PCA0,PCA1,PCA2,PCA_Counter
//                 pwm_value: pwm值, 这个值是输出低电平的时间.
// 返回: none.
// 版本: V1.0, 2012-11-22
//========================================================================
void        UpdatePwm(u8 PCA_id, u8 pwm_value)
{
        if(PCA_id == PCA0)                CCAP0H = pwm_value;
        else if(PCA_id == PCA1)        CCAP1H = pwm_value;
        else if(PCA_id == PCA2)        CCAP2H = pwm_value;
}

//========================================================================
// 函数: void        PCA_Init(PCA_id, PCA_InitTypeDef *PCAx)
// 描述: PCA初始化程序.
// 参数: PCA_id: PCA序号. 取值 PCA0,PCA1,PCA2,PCA_Counter
//                 PCAx: 结构参数,请参考PCA.h里的定义.
// 返回: none.
// 版本: V1.0, 2012-11-22
//========================================================================
void        PCA_Init(u8 PCA_id, PCA_InitTypeDef *PCAx)
{
        if(PCA_id > PCA_Counter)        return;                //id错误

        if(PCA_id == PCA_Counter)                        //设置公用Counter
        {
                CR = 0;
                CH = 0;
                CL = 0;
               
                /**************A =  &~(B<<C);将A从第C位开始的B位数清零*************/
                /**************A =   |(B<<C);将A从第C位开始的B位数置1*************/
                AUXR1 = (AUXR1 & ~(3<<4)) | PCAx->PCA_IoUse;                        //切换IO口,将AUXR1的第四位开始连续的两位先置零然后或上PCAx->PCA_IoUse
               
                CMOD  = (CMOD  & ~(7<<1)) | PCAx->PCA_Clock;                        //选择时钟源,将COMD的第一位开始连续三位先置零然后或上PCAx->PCA_Clock
                CMOD  = (CMOD  & ~1) | (PCAx->PCA_Interrupt_Mode & 1);        //ECF
                if(PCAx->PCA_Polity == PolityHigh)                PPCA = 1;                //高优先级中断
                else                                                                        PPCA = 0;                //低优先级中断
                if(PCAx->PCA_RUN == ENABLE)        CR = 1;
                return;
        }

        PCAx->PCA_Interrupt_Mode &= (3<<4) + 1;
        if(PCAx->PCA_Mode >= PCA_Mode_SoftTimer)        PCAx->PCA_Interrupt_Mode &= ~(3<<4);

        if(PCA_id == PCA0)
        {
                CCAPM0    = PCAx->PCA_Mode | PCAx->PCA_Interrupt_Mode;        //工作模式, 中断模式
                PCA_PWM0  = (PCA_PWM0 & ~(3<<6)) | PCAx->PCA_PWM_Wide;        //PWM宽度

                PCA_Timer0 = PCAx->PCA_Value;
                B_Capture0 = 0;
                PCA0_mode = PCAx->PCA_Mode;
                CCAP0_tmp = PCA_Timer0;
                CCAP0L = (u8)CCAP0_tmp;                        //将影射寄存器写入捕获寄存器,先写CCAP0L
                CCAP0H = (u8)(CCAP0_tmp >> 8);        //后写CCAP0H
        }
        if(PCA_id == PCA1)
        {
                CCAPM1    = PCAx->PCA_Mode | PCAx->PCA_Interrupt_Mode;
                PCA_PWM1  = (PCA_PWM1 & ~(3<<6)) | PCAx->PCA_PWM_Wide;

                PCA_Timer1 = PCAx->PCA_Value;
                B_Capture1 = 0;
                PCA1_mode = PCAx->PCA_Mode;
                CCAP1_tmp = PCA_Timer1;
                CCAP1L = (u8)CCAP1_tmp;                        //将影射寄存器写入捕获寄存器,先写CCAP0L
                CCAP1H = (u8)(CCAP1_tmp >> 8);        //后写CCAP0H
        }
        if(PCA_id == PCA2)
        {
                CCAPM2    = PCAx->PCA_Mode | PCAx->PCA_Interrupt_Mode;
                PCA_PWM2  = (PCA_PWM2 & ~(3<<6)) | PCAx->PCA_PWM_Wide;

                PCA_Timer2 = PCAx->PCA_Value;
                B_Capture2 = 0;
                PCA2_mode = PCAx->PCA_Mode;
                CCAP2_tmp = PCA_Timer2;
                CCAP2L = (u8)CCAP2_tmp;                        //将影射寄存器写入捕获寄存器,先写CCAP0L
                CCAP2H = (u8)(CCAP2_tmp >> 8);        //后写CCAP0H
        }
}


//========================================================================
// 函数: void        PCA_Handler (void) interrupt PCA_VECTOR
// 描述: PCA中断处理程序.
// 参数: None
// 返回: none.
// 版本: V1.0, 2012-11-22
//========================================================================
void        PCA_Handler (void) interrupt PCA_VECTOR
{
        if(CCF0)                        //PCA模块0中断
        {
                CCF0 = 0;                //清PCA模块0中断标志
                if(PCA0_mode >= PCA_Mode_SoftTimer)                //PCA_Mode_SoftTimer and PCA_Mode_HighPulseOutput
                {
                        CCAP0_tmp += PCA_Timer0;
                        CCAP0L = (u8)CCAP0_tmp;                        //将影射寄存器写入捕获寄存器,先写CCAP0L
                        CCAP0H = (u8)(CCAP0_tmp >> 8);        //后写CCAP0H
                }
                else if(PCA0_mode == PCA_Mode_Capture)
                {
                        CCAP0_tmp = CCAP0H;        //读CCAP0H
                        CCAP0_tmp = (CCAP0_tmp << 8) + CCAP0L;
                        B_Capture0 = 1;
                }
        }

        if(CCF1)        //PCA模块1中断
        {
                CCF1 = 0;                //清PCA模块1中断标志
                if(PCA1_mode >= PCA_Mode_SoftTimer)                //PCA_Mode_SoftTimer and PCA_Mode_HighPulseOutput
                {
                        CCAP1_tmp += PCA_Timer1;
                        CCAP1L = (u8)CCAP1_tmp;                        //将影射寄存器写入捕获寄存器,先写CCAP0L
                        CCAP1H = (u8)(CCAP1_tmp >> 8);        //后写CCAP0H
                }
                else if(PCA1_mode == PCA_Mode_Capture)
                {
                        CCAP1_tmp = CCAP1H;        //读CCAP1H
                        CCAP1_tmp = (CCAP1_tmp << 8) + CCAP1L;
                        B_Capture1 = 1;
                }
        }

        if(CCF2)        //PCA模块2中断
        {
                CCF2 = 0;                //清PCA模块1中断标志
                if(PCA2_mode >= PCA_Mode_SoftTimer)                //PCA_Mode_SoftTimer and PCA_Mode_HighPulseOutput
                {
                        CCAP2_tmp += PCA_Timer2;
                        CCAP2L = (u8)CCAP2_tmp;                        //将影射寄存器写入捕获寄存器,先写CCAP0L
                        CCAP2H = (u8)(CCAP2_tmp >> 8);        //后写CCAP0H
                }
                else if(PCA2_mode == PCA_Mode_Capture)
                {
                        CCAP2_tmp = CCAP2H;        //读CCAP2H
                        CCAP2_tmp = (CCAP2_tmp << 8) + CCAP2L;
                        B_Capture2 = 1;
                }
        }

        if(CF)        //PCA溢出中断
        {
                CF = 0;                        //清PCA溢出中断标志
        }

}

PCA上面的变量bit B_Capture0,B_Capture1,B_Capture2;在pca.c中也就是上面的代码区出现了,三个变量是什么变量为什么在中断里在置 1 ,在void        PCA_Init(u8 PCA_id, PCA_InitTypeDef *PCAx)初始化函数里置 0,这个变量表达的是什么意思那?这个变量在结构体指针变量PCA_InitTypeDef *PCAx里面也没有为何在初始化中置 0,中断里面置1?
回复 送花

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|深圳国芯人工智能有限公司 ( 粤ICP备2022108929号-2 )

GMT+8, 2024-4-26 19:46 , Processed in 0.066605 second(s), 31 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表