#pragma config FOSC = LP // Oscillator Selection->LP Oscillator, Low-power crystal connected between OSC1 and OSC2 pins
#pragma config WDTE = OFF // Watchdog Timer Enable->WDT disabled
#pragma config PWRTE = OFF // Power-up Timer Enable->PWRT disabled
#pragma config MCLRE = OFF // MCLR Pin Function Select->MCLR/VPP pin function is digital input
#pragma config CP = OFF // Flash Program Memory Code Protection->Program memory code protection is disabled
#pragma config CPD = OFF // Data Memory Code Protection->Data memory code protection is disabled
#pragma config BOREN = OFF // Brown-out Reset Enable->Brown-out Reset disabled
#pragma config CLKOUTEN = OFF // Clock Out Enable->CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin
#pragma config IESO = OFF // Internal/External Switchover->Internal/External Switchover mode is disabled
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable->Fail-Safe Clock Monitor is disabled
// CONFIG2
#pragma config WRT = OFF // Flash Memory Self-Write Protection->Write protection off
#pragma config PLLEN = OFF // PLL Enable->4x PLL disabled
#pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable->Stack Overflow or Underflow will cause a Reset
#pragma config BORV = LO // Brown-out Reset Voltage Selection->Brown-out Reset Voltage (Vbor), low trip point selected.
#pragma config LVP = OFF
void OSCILLATOR_Initialize(void)
{
OSCCON = 0x6A;
// TUN 0;
OSCTUNE = 0x00;
// Set the secondary oscillator
TRISA4 = 1; // timer-1 osc
TRISA5 = 1; // timer-1 osc
TMR1H = 0x00; // initially timer to count 00
TMR1L = 0x00;
T1CON = 0x00;
T1GCON = 0; // Timer 1 Gate function disabled
TMR1IF = 0; // Clear any pending Timer 1 interrupt indication TMR1IE = 1;
TMR1ON = 0;
PEIE=1; //Enable Peripheral Interrupt
}
void TMR1_Initialize(void)
{
//Set the Timer to the options selected in the GUI
//T1CKPS 1:8; T1OSCEN enabled; nT1SYNC do_not_synchronize; TMR1CS External; TMR1ON off;
T1CON = 0x89;
//T1GSS T1G; TMR1GE disabled; T1GTM disabled; T1GPOL low; T1GGO done; T1GSPM disabled;
T1GCON = 0x00;
TMR1H = 0x80; // 1 second
TMR1L = 0x00;
// Load the TMR value to reload variable
timer1ReloadVal=(TMR1H << 8) | TMR1L;
T1GCON = 0; // Timer 1 Gate function disabled
TMR1IF = 0; // Clear any pending Timer 1 interrupt indication
TMR1IE = 1; // Enable Timer 1 interrupt
// Clearing IF flag before enabling the interrupt.
PIR1bits.TMR1IF = 0;
// Enabling TMR1 interrupt.
PIE1bits.TMR1IE = 1;
// Set Default Interrupt Handler
// TMR1_SetInterruptHandler(TMR1_DefaultInterruptHandler);
}
void PIN_MANAGER_Initialize(void)
{
/**
LATx registers
*/
LATA = 0x00;
LATC = 0x00;
/**
TRISx registers
*/
TRISA = 0x3E;
TRISC = 0x1F;
/**
ANSELx registers
*/
ANSELC = 0x00;
ANSELA = 0x14;
/**
WPUx registers
*/
WPUA = 0x04;
WPUC = 0x00;
OPTION_REGbits.nWPUEN = 0;
/**
APFCONx registers
*/
APFCON1 = 0x00;
APFCON0 = 0x00;
}
void WDT_Initialize(void)
{
// WDTPS 1:65536; SWDTEN OFF;
WDTCON = 0x16;
}
void SYSTEM_Initialize(void)
{
PIN_MANAGER_Initialize();
OSCILLATOR_Initialize();
WDT_Initialize();
TMR1_Initialize();
}
#pragma config FOSC = LP // Oscillator Selection->LP Oscillator, Low-power crystal connected between OSC1 and OSC2 pins
// CONFIG1
#pragma config FOSC = INTOSC // Oscillator Selection->INTOSC oscillator: I/O function on CLKIN pin
#pragma config WDTE = OFF // Watchdog Timer Enable->WDT disabled
#pragma config PWRTE = ON // Power-up Timer Enable->PWRT disabled
#pragma config MCLRE = OFF // MCLR Pin Function Select->MCLR/VPP pin function is digital input
#pragma config CP = OFF // Flash Program Memory Code Protection->Program memory code protection is disabled
#pragma config CPD = OFF // Data Memory Code Protection->Data memory code protection is disabled
#pragma config BOREN = OFF // Brown-out Reset Enable->Brown-out Reset disabled
#pragma config CLKOUTEN = ON // Clock Out Enable->CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin
#pragma config IESO = OFF // Internal/External Switchover->Internal/External Switchover mode is disabled
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable->Fail-Safe Clock Monitor is disabled
// CONFIG2
#pragma config WRT = OFF // Flash Memory Self-Write Protection->Write protection off
#pragma config PLLEN = OFF // PLL Enable->4x PLL disabled
#pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable->Stack Overflow or Underflow will cause a Reset
#pragma config BORV = LO // Brown-out Reset Voltage Selection->Brown-out Reset Voltage (Vbor), low trip point selected.
#pragma config LVP = OFF // Low-Voltage Programming Enable->High-voltage on MCLR/VPP must be used for programming
void PIN_MANAGER_Initialize(void)
{
/**
LATx registers
*/
LATA = 0x00;
LATC = 0x00;
/**
TRISx registers
*/
TRISA = 0x3E;
TRISC = 0x1F;
/**
ANSELx registers
*/
ANSELC = 0x00;
ANSELA = 0x04;
/**
WPUx registers
*/
WPUA = 0x04;
WPUC = 0x00;
OPTION_REGbits.nWPUEN = 0;
/**
APFCONx registers
*/
APFCON1 = 0x00;
APFCON0 = 0x00;
/**
IOCx registers
*/
// interrupt on change for group IOCAF - flag
IOCAFbits.IOCAF4 = 0;
// interrupt on change for group IOCAN - negative
IOCANbits.IOCAN4 = 0;
// interrupt on change for group IOCAP - positive
IOCAPbits.IOCAP4 = 0;
// register default IOC callback functions at runtime; use these methods to register a custom function
IOCAF4_SetInterruptHandler(IOCAF4_DefaultInterruptHandler);
} void SYSTEM_Initialize(void)
{
PIN_MANAGER_Initialize();
OSCILLATOR_Initialize();
WDT_Initialize();
TMR1_Initialize();
}
void OSCILLATOR_Initialize(void)
{
OSCCON = 0x6A; // 4Mhz IOSC
while(!HFIOFL); // T1OSCR bit, oscilador TMR1 estable?
// Set the secondary oscillator
T1CON = 0x8C; // Activo oscilador
//OSCSTATbits_t.HFIOFL
while(!T1OSCR);
}
void TMR1_Initialize(void)
{
//Set the Timer to the options selected in the GUI
//T1CKPS 1:1; T1OSCEN enabled; nT1SYNC synchronize; TMR1CS External; TMR1ON off;
T1CONbits.T1CKPS = 0b11;
T1CONbits.TMR1CS = 0b10; // OSC externo
//T1GSS T1G; TMR1GE disabled; T1GTM disabled; T1GPOL low; T1GGO done; T1GSPM disabled;
T1GCON = 0x00;
TMR1H = 0x00;
TMR1L = 0x00;
// Load the TMR value to reload variable
timer1ReloadVal=(TMR1H << 8) | TMR1L;
// Clearing IF flag before enabling the interrupt.
PIR1bits.TMR1IF = 0;
// Enabling TMR1 interrupt.
PIE1bits.TMR1IE = 1;
// Set Default Interrupt Handler
TMR1_SetInterruptHandler(TMR1_DefaultInterruptHandler);
// Start TMR1
TMR1_StartTimer();
}
void TMR1_StartTimer(void)
{
// Start the Timer by writing to TMRxON bit
T1CONbits.TMR1ON = 1;
}
OSCTUNE = 0x1F; //The default value of the OSCTUNE register is ‘0’. The
//value is a 6-bit two’s complement number. A value of
//1Fh will provide an adjustment to the maximum
//frequency. A value of 20h will provide an adjustment to
//the minimum frequency
Era mas facil programar todo esto con CCS que con MPLABX...
A veces XC8 puede ser un poco desesperante cuando inicias algún modulo que no has tocado nunca o las configuraciones, al menos para mi, aunque después el resultado es mas gratificante porque sabes exactamente que has configurado y como lo has hecho y en CCS no siempre es así.