PIC12F683 ... ¿Error con los bits de configuración?

0

Esta es la primera vez que trato de usar las funciones ADC y PWM del 12f683. Tomé ayuda de este hilo para escribir mi código: No se puede hacer que PWM funcione en un PIC12F683 con MPLAB XC8

Creo que hay algún conflicto en la forma en que los relojes ADC y PWM se configuran porque obtengo un PWM de muy baja frecuencia como salida (no tengo un alcance para medirlo). Estaría extremadamente agradecido si alguien pudiera señalar lo que estoy haciendo mal. He añadido el código y el diagrama del circuito.

Gracias.

#include <xc.h>
#include <pic12f683.h>

#define _XTAL_FREQ 8000000

#pragma config FOSC = INTOSCIO  // Oscillator Selection bits (INTOSCIO   oscillator: I/O function on RA4/OSC2/CLKOUT pin, I/O function on RA5/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      // MCLR Pin Function Select bit (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 Detect (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)

void PWM_setup(void){
    // Setup PWM on GP2
    TRISIO &= 0b11111011;       // Make sure GP2 is OUTPUT
    PR2 = 0x65;
    CCP1CON = 0b00001100;       // Active high PWM
    PIR1bits.TMR2IF = 0;
    T2CONbits.T2CKPS = 0b00;    // Set prescaler to 1
    T2CONbits.TMR2ON = 1;       // Enable Timer 2 and therefore PWM
}

void PWM_set_duty(int duty_cycle){
    // Sets the PWM duty cycle, 10 bit resolution
    CCP1CONbits.DC1B = duty_cycle;  // Setting the 2 LSB's in DCB
    CCPR1L = duty_cycle >> 2;       // Setting the 8 MSB's in CCPR1L
}

unsigned int read_volt(){
    // Reads and returns the voltage at AN0
    ADCON0 = 0b10000001;        // Enable ADC
    __delay_ms(2);              // Acquisition time to charge hold capacitor
    ADCON0 = 0b10000011;        // GO
    while (GO_nDONE){
                                // Wait
    }
    unsigned int voltage = ADRESL | (ADRESH << 8); // 10 bit ADC result
    return voltage;
}

void main(void) {
    OSCCON = 0b01110001;        // Select 8Mhz internal clock
    TRISIO = 0b00001001;        // Configure GP0 and GP3 as analog inputs
    ANSEL = 0b00000011;         // Set clock = 1 MHz and analog configure
    INTCONbits.PEIE = 1;
    INTCONbits.GIE = 1;
    INTCONbits.T0IE = 1;
    PIE1bits.CCP1IE = 1;
    PIE1bits.TMR2IE = 1;
    PWM_setup();
    PWM_set_duty(0);
    while (1){
        if(read_volt()<600){    // 600 = 18 Deg C
            PWM_set_duty(102);  // 102 = 25% duty cycle
        }else{
            PWM_set_duty(408);  // 408 = 100% duty cycle
        }
    }
    return;
}

void interrupt ISR(){
    // Timer2 overflow starts a new PWM cycle
    if(PIR1bits.TMR2IF == 1){
      PIR1bits.TMR2IF = 0;
      TRISIObits.TRISIO2 = 0;
    }
}
    
pregunta HGP

0 respuestas

Lea otras preguntas en las etiquetas