gpt4 book ai didi

PIC16F877 (PICC) 无法使 PWM 工作

转载 作者:行者123 更新时间:2023-11-30 17:57:34 27 4
gpt4 key购买 nike

我一直在尝试在 PIC16F877 MCU 上使用 PWM 模块,但我得到的只是 CCP1/CCP2 引脚上的平坦低电平。

配置和启动 PWM 模块的代码如下。

// Configure PWM
// Timer 2 (PWM timebase)
TMR2 = 0; //Clear timer
TOUTPS0 = 0;
TOUTPS1 = 0;
TOUTPS2 = 0;
TOUTPS3 = 1; //Postscaler -> 8 (previously set to 0)
T2CKPS0 = 0;
T2CKPS1 = 1; //Prescaler -> 16
TMR2IF = 0;
TMR2IE = 1; //Interrupt
PR2 = 233; //~2.5ms

//PWM1 config
CCPR1L = 0x0F;
CCP1X = 0;
CCP1Y = 0; //PWM1 duty cycle
TRISB2 = 0; //CCP1 pin is output (Error is here, see below)
TMR2ON = 1; //Enable timer
CCP1CON = 0x0c; //CPP1 is a PWM

任何可以在 pic16 器件上成功启动 PWM 的代码 (PICC) 都会很有用。

最佳答案

CCP1 引脚设置不正确。 'TRISB2 = 0' 应为'TRISC2 = 0'

// Timer 2 (PWM timebase)
TMR2 = 0; //Clear timer
TOUTPS0 = 0;
TOUTPS1 = 0;
TOUTPS2 = 0;
TOUTPS3 = 1; //Postscaler -> 8
T2CKPS0 = 0;
T2CKPS1 = 1; //Prescaler -> 16
TMR2IF = 0;
TMR2IE = 1; //Interrupt
PR2 = 233; //~2.5ms

//PWM1 config
CCPR1L = 0xFF;
CCP1X = 1;
CCP1Y = 1; //PWM1 duty cycle
TRISC2 = 0; //Previously was TRISB2
TMR2ON = 1;
CCP1CON = 0x0c; //CPP1 is a PWM

关于PIC16F877 (PICC) 无法使 PWM 工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12719203/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com