Ir ao conteúdo
  • Cadastre-se

PIC PWM do PIC16F887 não funciona


Posts recomendados

Ola, o PWM do meu pic16f887 não está funcionando. 

 

Codigo: 

/*
 * File:   PWM.c
 * Author: Gregorio Leão
 *
 * Created on 12 de Janeiro de 2022, 23:36
 */
// PIC16F887 Configuration Bit Settings

// 'C' source line config statements

// CONFIG1
#pragma config FOSC = HS        // Oscillator Selection bits (HS oscillator: High-speed crystal/resonator on RA6/OSC2/CLKOUT and RA7/OSC1/CLKIN)
#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled and can be enabled by SWDTEN bit of the WDTCON register)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = OFF      // RE3/MCLR pin function select bit (RE3/MCLR pin function is digital input, MCLR internally tied to VDD)
#pragma config CP = OFF         // Code Protection bit (Program memory code protection is disabled)
#pragma config CPD = OFF        // Data Code Protection bit (Data memory code protection is disabled)
#pragma config BOREN = OFF      // Brown Out Reset Selection bits (BOR disabled)
#pragma config IESO = OFF       // Internal External Switchover bit (Internal/External Switchover mode is disabled)
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is disabled)
#pragma config LVP = OFF        // Low Voltage Programming Enable bit (RB3 pin has digital I/O, HV on MCLR must be used for programming)

// CONFIG2
#pragma config BOR4V = BOR40V   // Brown-out Reset Selection bit (Brown-out Reset set to 4.0V)
#pragma config WRT = OFF        // Flash Program Memory Self Write Enable bits (Write protection off)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

#include <xc.h>
#include <stdio.h>
#include <stdlib.h>

#define _XTAL_FREQ 20000000

void Inicia_PWM(){
    INTCONbits.GIE = 1;
    INTCONbits.PEIE = 1;
    PIE1bits.TMR2IE = 1;
    TRISCbits.TRISC2 = 1;
    PR2 = 255;
    CCP1CONbits.CCP1M0 = 0;
    CCP1CONbits.CCP1M1 = 0;
    CCP1CONbits.CCP1M3 = 1;
    CCP1CONbits.CCP1M2 = 1;
    CCP1CONbits.P1M0 = 0;
    CCP1CONbits.P1M1 = 0;
    CCP1CONbits.DC1B0 = 0;
    CCP1CONbits.DC1B1 = 0;
    CCPR1L = 0x0;
    PIR1bits.TMR2IF = 0;
    T2CONbits.T2CKPS0 = 0;
    T2CONbits.T2CKPS1 = 0;
    T2CONbits.TMR2ON = 1;
    while(!PIR1bits.TMR2IF);
    TRISCbits.TRISC2 = 0;
    PIR1bits.TMR2IF = 0;
}

void Gera_PWM(int DutyCicle){
    CCPR1L = DutyCicle >> 2;
    CCP1CONbits.DC1B1 = DutyCicle >> 1;
    CCP1CONbits.DC1B0 = DutyCicle;
}

void Blink(){
    TRISDbits.TRISD2 = 0;
    while(1){
        PORTDbits.RD2 = 1;
        __delay_ms(1000);
        PORTDbits.RD2 = 0;
        __delay_ms(1000);
    }
}

void main(void) {
    Inicia_PWM();
    while(1){
        for(int i = 0; i < 1023; i++){
            Gera_PWM(i);
        }
    } 
}

 

Estou seguindo o que informa o datasheet do produto, entretanto, mesmo assim, não funciona. Gostaria de ajuda.

 

 

Captura de Tela (4).png

Link para o comentário
Compartilhar em outros sites

  • 2 semanas depois...

Crie uma conta ou entre para comentar

Você precisa ser um usuário para fazer um comentário

Criar uma conta

Crie uma nova conta em nossa comunidade. É fácil!

Crie uma nova conta

Entrar

Já tem uma conta? Faça o login.

Entrar agora

Sobre o Clube do Hardware

No ar desde 1996, o Clube do Hardware é uma das maiores, mais antigas e mais respeitadas comunidades sobre tecnologia do Brasil. Leia mais

Direitos autorais

Não permitimos a cópia ou reprodução do conteúdo do nosso site, fórum, newsletters e redes sociais, mesmo citando-se a fonte. Leia mais

×
×
  • Criar novo...

 

GRÁTIS: ebook Redes Wi-Fi – 2ª Edição

EBOOK GRÁTIS!

CLIQUE AQUI E BAIXE AGORA MESMO!