为什么这个简单的 PWM 在 xc8 中不起作用
Why this simple PWM doesn't work in xc8
我知道互联网上有很多示例,但是需要这段代码才能工作吗?
频率振荡器 = 4mhz
周期 = 0.25us
duty_cicle = 250
预分频 = 16
PR2 = 124
#include <xc.h>
#include <stdio.h>
#include <stdlib.h>
#include <pic16f88.h>
#pragma config FOSC = HS // Oscillator Selection bits (INTOSC oscillator: I/O function on RA6/OSC2/CLKOUT pin, I/O function on RA7/OSC1/CLKIN)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = OFF // RA5/MCLR/VPP Pin Function Select bit (RA5/MCLR/VPP pin function is digital input, MCLR internally tied to VDD)
#pragma config BOREN = ON // Brown-out Detect Enable bit (BOD enabled)
#pragma config LVP = OFF // Low-Voltage Programming Enable bit (RB4/PGM pin has digital I/O function, HV on MCLR must be used for programming)
#pragma config CPD = OFF // Data EE Memory Code Protection bit (Data memory code protection off)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
void main ()
{
while (1)
{
CCP1CON = 0x2C; /*activate PWM mode*/
PR2 = 0x7C; /*124 (DECIMAL)*/
T2CON = 0X06; /*prescale 16 */
CCPR1L = 0X3E;
}
}
我想看:
PWM 周期 = 2ms
占空比 = 1ms
真诚的
宁
数据表指出:
In Pulse-Width Modulation (PWM) mode, the CCP1 pin
produces up to a 10-bit resolution PWM output. Since
the CCP1 pin is multiplexed with the PORTB data latch,
the TRISB bit must be cleared to make the CCP1
pin an output.
所以CCP1管脚必须设置TRIS位才能输出:
TRISB &= ~(1 << 3); //Clear bit RB3/CCP1 in TRISB, makes PORTB3/CCP1 output.
这假定配置字中的 CCPMX: CCP1 Pin Selection bit
是明确的。如果设置,则 CCP1 在 RB0 而不是 RB3 上,但由于我在您的配置编译指示中没有看到 CCPMX 的提及,我假设它已被清除。
第一个题外话:
不包含 pic16f88.h,它被 xc.h.
包含
有点跑题了:
如果您使用更现代的部分(例如 PIC16f1619), you can use the MPLAB Code Configurator to generate the TMR2 and CCP code for you. It'll also cost less and have more flash/ram. That device is on the curiosity board ($20).
关于主题:
您的第一站是 datasheet.
PWM部分有setup for PWM operation.
第一步:
timer 2 将 Fosc/4 作为输入,在您的情况下为 1mhz。
目标频率为 500Hz。 1e6/500 = 2k。
我建议使用 16 的预分频器和 125 的 pr 值。这将为您提供准确的 500Hz。
第二步:
我们想要 50% 的占空比。 CCP1L floor(125/2) = 62.CCP1X:CCP1Y = 0.5 * 4 = 2.
第 3 步:
清除 tris 位。
第四步和第五步:
开启它
// Step 1
TMR2ON = 0;
TOUTPS = 0;
T2CKPS = 2;
PR2 = 250U;
// Step 2
CCP1L = 62U;
CCP1X = 1;
CCP1Y = 0;
// Step 3
TRISB3 = 0;
// Step 4
TMR2ON = 1;
// Step 5
CCP1M = 0xC;
希望对您有所帮助。
我知道互联网上有很多示例,但是需要这段代码才能工作吗?
频率振荡器 = 4mhz
周期 = 0.25us
duty_cicle = 250
预分频 = 16
PR2 = 124
#include <xc.h>
#include <stdio.h>
#include <stdlib.h>
#include <pic16f88.h>
#pragma config FOSC = HS // Oscillator Selection bits (INTOSC oscillator: I/O function on RA6/OSC2/CLKOUT pin, I/O function on RA7/OSC1/CLKIN)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = OFF // RA5/MCLR/VPP Pin Function Select bit (RA5/MCLR/VPP pin function is digital input, MCLR internally tied to VDD)
#pragma config BOREN = ON // Brown-out Detect Enable bit (BOD enabled)
#pragma config LVP = OFF // Low-Voltage Programming Enable bit (RB4/PGM pin has digital I/O function, HV on MCLR must be used for programming)
#pragma config CPD = OFF // Data EE Memory Code Protection bit (Data memory code protection off)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
void main ()
{
while (1)
{
CCP1CON = 0x2C; /*activate PWM mode*/
PR2 = 0x7C; /*124 (DECIMAL)*/
T2CON = 0X06; /*prescale 16 */
CCPR1L = 0X3E;
}
}
我想看:
PWM 周期 = 2ms
占空比 = 1ms
真诚的 宁
数据表指出:
In Pulse-Width Modulation (PWM) mode, the CCP1 pin produces up to a 10-bit resolution PWM output. Since the CCP1 pin is multiplexed with the PORTB data latch, the TRISB bit must be cleared to make the CCP1 pin an output.
所以CCP1管脚必须设置TRIS位才能输出:
TRISB &= ~(1 << 3); //Clear bit RB3/CCP1 in TRISB, makes PORTB3/CCP1 output.
这假定配置字中的 CCPMX: CCP1 Pin Selection bit
是明确的。如果设置,则 CCP1 在 RB0 而不是 RB3 上,但由于我在您的配置编译指示中没有看到 CCPMX 的提及,我假设它已被清除。
第一个题外话: 不包含 pic16f88.h,它被 xc.h.
包含有点跑题了: 如果您使用更现代的部分(例如 PIC16f1619), you can use the MPLAB Code Configurator to generate the TMR2 and CCP code for you. It'll also cost less and have more flash/ram. That device is on the curiosity board ($20).
关于主题: 您的第一站是 datasheet.
PWM部分有setup for PWM operation.
第一步: timer 2 将 Fosc/4 作为输入,在您的情况下为 1mhz。 目标频率为 500Hz。 1e6/500 = 2k。 我建议使用 16 的预分频器和 125 的 pr 值。这将为您提供准确的 500Hz。
第二步: 我们想要 50% 的占空比。 CCP1L floor(125/2) = 62.CCP1X:CCP1Y = 0.5 * 4 = 2.
第 3 步: 清除 tris 位。
第四步和第五步: 开启它
// Step 1
TMR2ON = 0;
TOUTPS = 0;
T2CKPS = 2;
PR2 = 250U;
// Step 2
CCP1L = 62U;
CCP1X = 1;
CCP1Y = 0;
// Step 3
TRISB3 = 0;
// Step 4
TMR2ON = 1;
// Step 5
CCP1M = 0xC;
希望对您有所帮助。