4.2 Bare Metal Code
First, the Watchdog Timer has to be disabled and Low-Voltage Programming (LVP) has to be enabled using the following pragma code:
#pragma config WDTE = OFF /* WDT operating mode->WDT Disabled */ #pragma config LVP = ON /* Low voltage programming enabled, RE3 pin is MCLR */
The following function initializes the system clock to have the HFINTOSC oscillator as input clock and to run at 1 MHz:
static void CLK_Initialize(void) { OSCCON1 = 0x60; /* set HFINTOSC as new oscillator source */ OSCFRQ = 0x00; /* set HFFRQ to 1 MHz */ }
The following function initializes the RC2 pin as output pin:
static void PORT_Initialize(void) { TRISCbits.TRISC2 = 0; /* configure RC2 as output */ }
The following function initializes Timer0 in 8-bit mode, sets the prescaler to 1:1, loads the TMR0H and TMR0L registers, clears the Interrupt flag and enables Timer0:
static void TMR0_Initialize(void)
{
T0CON1 = 0x90; /* select LFINTOSC and disable TMR0 sync*/
TMR0H = 0x7B; /* load TMR0H */
TMR0L = 0x00; /* load TMR0L */
PIR0bits.TMR0IF = 0; /* clear the interrupt flag */
T0CON0 = 0x80; /* enable TMR0 */
}
The following function configures the TMR0 output to RC2 in PPS:
static void PPS_Initialize(void) { RC2PPS = 0x13; /* configure RC2 for TMR0 output */ }
The main function calls all the initializing functions and run all the
peripherals in an infinite empty
loop:
void main(void)
{
CLK_Initialize();
PORT_Initialize();
TMR0_Initialize();
PPS_Initialize();
while(1)
{
;
}
}