简体   繁体   English

无法使PWM在PIC16F877(PICC)上工作

[英]Can't get PWM to work on PIC16F877 (PICC)

I've been trying to use the PWM module on a PIC16F877 MCU but all I get is a flat low level on both CCP1/CCP2 pins. 我一直在尝试在PIC16F877 MCU上使用PWM模块,但我得到的只是两个CCP1 / CCP2引脚上的电平均为低电平。

The code configuring and starting the PWM module is the following. 下面是配置和启动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

Any code (PICC) which can successfully start the PWM on pic16 devices whould be useful. 可以成功使用pic16器件上的PWM的任何代码(PICC)。

CCP1 Pin was not being correctly set. CCP1引脚未正确设置。 'TRISB2 = 0' should be 'TRISC2 = 0' “ 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM