hola, buenas tardes,
estoy usando el MPLAB X IDE con el XC8 v2.40 para programar un PIC16F887 a 8mHz que voy a usar para contar los pulsos de un encoder rotativo de 600 ppm y mostrarlos en un LCD de 16x2, las rutinas para controlar el LCD usan __delay_us() y delay_ms(), para retardo_us() uso asm("NOP") pero para retardo_ms quiero utilizar el TIMER0 y noto un comportamiento muy extraño según donde lo coloque,
hay una versión muy simple que funciona pero la otra, con la configuracion de todos los perifericos que necesito del pic, no funciona,
esta es la versión que funciona
#include "config_bits.h"
#include <xc.h>
#include <pic16f887.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
#include <math.h>
#include "retardos.h"
//------------------------------------------------------------------------------
#define LED PORTBbits.RB7
//------------------------------------------------------------------------------
void main(void)
{
TRISBbits.TRISB7 = 0;
LED = 0;
OPTION_REG = 0b00000011
TMR0=TMR0_VALOR;
while (1)
{
LED = !LED;
retardo_ms(500);
}
return;
}
config_bits tiene esto
#pragma config FOSC = INTRC_NOCLKOUT
#pragma config WDTE = OFF
#pragma config PWRTE = OFF
#pragma config MCLRE = OFF
#pragma config CP = ON
#pragma config CPD = ON
#pragma config BOREN = ON
#pragma config IESO = OFF
#pragma config FCMEN = ON
#pragma config LVP = OFF
#pragma config BOR4V = BOR40V
#pragma config WRT = OFF
#define _XTAL_FREQ 8000000
y retardos tiene esto
#include "includes.h"
//------------------------------------------------------------------------------
void retardo_ms(uint16_t retardo)
{
uint16_t idx=0;
TMR0 = TMR0_VALOR;
while (idx < retardo)
{
if (TMR0 == 255)
{
idx++;
TMR0 = TMR0_VALOR;
}
}
}
la otra versión que no funciona (enciende el led y nunca lo apaga) es esta
#include "includes.h"
//------------------------------------------------------------------------------
#define LED PORTBbits.RB7
//------------------------------------------------------------------------------
void main(void)
{
configPIC();
LED = 0;
while (1)
{
LED = !LED;
retardo_ms(500);
}
return;
}
config_pic tiene esto
#include "config.h"
//------------------------------------------------------------------------------
void config_clock(void)
{
OSCCONbits.IRCF2 = 1;
OSCCONbits.IRCF1 = 1;
OSCCONbits.IRCF0 = 1;
}
//------------------------------------------------------------------------------
void config_puertos(void)
{
ANSEL = 0b00000000;
ANSELH = 0b00000000;
//######## PORTA ########
TRISAbits.TRISA0 = 0;
TRISAbits.TRISA1 = 0;
TRISAbits.TRISA2 = 0;
TRISAbits.TRISA3 = 0;
TRISAbits.TRISA4 = 0;
TRISAbits.TRISA5 = 0;
TRISAbits.TRISA6 = 0;
TRISAbits.TRISA7 = 0;
//######## PORTB ########
TRISBbits.TRISB0 = 0;
TRISBbits.TRISB1 = 0;
TRISBbits.TRISB2 = 0;
TRISBbits.TRISB3 = 0;
TRISBbits.TRISB4 = 0;
TRISBbits.TRISB5 = 0;
TRISBbits.TRISB6 = 0;
TRISBbits.TRISB7 = 0;
// OPTION_REGbits.nRBPU = 1;
WPUBbits.WPUB0 = 0;
WPUBbits.WPUB1 = 0;
WPUBbits.WPUB2 = 0;
WPUBbits.WPUB3 = 0;
WPUBbits.WPUB4 = 0;
WPUBbits.WPUB5 = 0;
WPUBbits.WPUB6 = 0;
WPUBbits.WPUB7 = 0;
//######## PORTC ########
CCP1CONbits.CCP1M = 0b0000;
CCP2CONbits.CCP2M = 0b0000;
TRISCbits.TRISC0 = 0;
TRISCbits.TRISC1 = 0;
TRISCbits.TRISC2 = 0;
TRISCbits.TRISC3 = 0;
TRISCbits.TRISC4 = 0;
TRISCbits.TRISC5 = 0;
TRISCbits.TRISC6 = 0;
TRISCbits.TRISC7 = 0;
//######## PORTD ########
TRISDbits.TRISD0 = 0;
TRISDbits.TRISD1 = 0;
TRISDbits.TRISD2 = 0;
TRISDbits.TRISD3 = 0;
TRISDbits.TRISD4 = 0;
TRISDbits.TRISD5 = 0;
TRISDbits.TRISD6 = 0;
TRISDbits.TRISD7 = 0;
//######## PORTE ########
TRISEbits.TRISE0 = 0;
TRISEbits.TRISE1 = 0;
TRISEbits.TRISE2 = 0;
TRISEbits.TRISE3 = 0;
PORTA = 0x00;
PORTB = 0x00;
PORTC = 0x00;
PORTD = 0x00;
PORTE = 0x00;
}
//------------------------------------------------------------------------------
void config_TIMER0(void)
{
OPTION_REG = 0b00000011;
TMR0 = TMR0_VALOR;
}
//------------------------------------------------------------------------------
void config_TIMER1(void)
{
T1CONbits.T1CKPS1 = 1;
T1CONbits.T1CKPS0 = 1;
T1CONbits.T1OSCEN = 0;
T1CONbits.T1SYNC = 1;
T1CONbits.TMR1CS = 0;
T1CONbits.TMR1ON = 1;
TMR1H = 11;
TMR1L = 220;
}
//------------------------------------------------------------------------------
void config_interrupciones(void)
{
INTCON = 0b00000000;
PIR1bits.TMR1IF = 0;
PIE1bits.TMR1IE = 1;
INTCONbits.GIE = 1;
INTCONbits.PEIE = 1;
INTCONbits.INTE = 1;
IOCBbits.IOCB0 = 0;
IOCBbits.IOCB1 = 0;
IOCBbits.IOCB2 = 0;
IOCBbits.IOCB3 = 0;
IOCBbits.IOCB4 = 0;
IOCBbits.IOCB5 = 0;
IOCBbits.IOCB6 = 0;
IOCBbits.IOCB7 = 0;
INTCONbits.RBIE = 0;
}
//------------------------------------------------------------------------------
void configPIC(void)
{
config_clock();
config_puertos();
config_TIMER0();
config_TIMER1();
config_interrupciones();
config_TIMER0();
}
lo demas es igual a la primer version,
no entiendo por que no funciona, seguramente hay algo en config_pic que no le gusta,
agradezco cualquier ayuda,
saludos