PIC18F Trainer - Blink LED.

0.Contents

PIC18F Trainer : PIC18F2620 @8MHz Internal oscillator.

1.Blink One LED with Delay.

// Configuration register.
#pragma config IESO = OFF, FCMEN = OFF, OSC = INTIO7
#pragma config BORV = 3, BOREN = OFF, PWRT = OFF
#pragma config WDTPS = 32768, WDT = OFF
#pragma config MCLRE = ON, LPT1OSC = OFF, PBADEN = OFF, CCP2MX = PORTC
#pragma config DEBUG = OFF, XINST = OFF, LVP = OFF, STVREN = ON
#pragma config CP3 = OFF, CP2 = OFF, CP1 = OFF, CP0 = OFF
#pragma config CPD = OFF, CPB = OFF
#pragma config WRT3 = OFF, WRT2 = OFF, WRT1 = OFF, WRT0 = OFF
#pragma config WRTD = OFF, WRTB = OFF, WRTC = OFF
#pragma config EBTR3 = OFF, EBTR2 = OFF, EBTR1 = OFF, EBTR0 = OFF
#pragma config EBTRB = OFF

#include <xc.h>
#define _XTAL_FREQ 8000000
// PIC18F2620 - Compile with XC8(v1.44).
// PIC18F2620 - @8MHz Internal oscillator.

// Blink One LED with Delay.

// MCU.RA0 -> LED.01.

// Definitions.
#define ADCON			ADCON1
#define LED_PORT		PORTAbits.RA0
#define LED_TRIS		TRISAbits.TRISA0
#define LED				LATAbits.LATA0

// Function prototypes.
void mcu_initialize(void);

// Main.
int main(void)
{
	mcu_initialize();
	
	while(1){
		LED = 1;
		__delay_ms(100);
		LED = 0;
		__delay_ms(100);
		LED = 1;
		__delay_ms(100);
		LED = 0;
		__delay_ms(400);
	}
	return(0);
}

// Functions.
void mcu_initialize(void)
{
	// Internal Oscillator 8MHz.
	OSCCONbits.IRCF2  = 1;
	OSCCONbits.IRCF1  = 1;
	OSCCONbits.IRCF0  = 1;
	OSCTUNEbits.PLLEN = 0;
	while(!OSCCONbits.IOFS);
	// All Port as Digital I/O.
	ADCON = 0x0f;
	// Output LED.
	LED_PORT  = 0;
	LED_TRIS  = 0;
}
Size : 138bytes.
Blink One LED with Delay - No Interrupt.

2.Blink One LED with Software Interrupt.

// Configuration register.
#pragma config IESO = OFF, FCMEN = OFF, OSC = INTIO7
#pragma config BORV = 3, BOREN = OFF, PWRT = OFF
#pragma config WDTPS = 32768, WDT = OFF
#pragma config MCLRE = ON, LPT1OSC = OFF, PBADEN = OFF, CCP2MX = PORTC
#pragma config DEBUG = OFF, XINST = OFF, LVP = OFF, STVREN = ON
#pragma config CP3 = OFF, CP2 = OFF, CP1 = OFF, CP0 = OFF
#pragma config CPD = OFF, CPB = OFF
#pragma config WRT3 = OFF, WRT2 = OFF, WRT1 = OFF, WRT0 = OFF
#pragma config WRTD = OFF, WRTB = OFF, WRTC = OFF
#pragma config EBTR3 = OFF, EBTR2 = OFF, EBTR1 = OFF, EBTR0 = OFF
#pragma config EBTRB = OFF

#include <xc.h>
#include <stdint.h>
#define _XTAL_FREQ 8000000
// PIC18F2620 - Compile with XC8(v1.44).
// PIC18F2620 - @8MHz Internal oscillator.

// Blink One LED with Software Interrupt.

// MCU.RA0 -> LED.01.

// Definitions.
#define ADCON			ADCON1
#define LED_PORT		PORTAbits.RA0
#define LED_TRIS		TRISAbits.TRISA0
#define LED				LATAbits.LATA0

// Function prototypes.
void mcu_initialize(void);
void timer0_initialize(void);

// Global variable.
volatile uint8_t counter = 0;

// Main.
int main(void)
{
	mcu_initialize();
	timer0_initialize();
	
	INTCONbits.GIE = 1;
	while(1){
	}
	return(0);
}

// Interrupt Service Routine.
void interrupt ISR(void)
{
	counter++;
	if(counter == 2)
	{
		LED ^= 1;
		counter = 0;
	}
	INTCONbits.TMR0IF = 0;
}

// Functions.
void mcu_initialize(void)
{
	// Internal Oscillator 8MHz.
	OSCCONbits.IRCF2  = 1;
	OSCCONbits.IRCF1  = 1;
	OSCCONbits.IRCF0  = 1;
	OSCTUNEbits.PLLEN = 0;
	while(!OSCCONbits.IOFS);
	// All Port as Digital I/O.
	ADCON = 0x0f;
	// Output LED.
	LED_PORT  = 0;
	LED_TRIS  = 0;
	// Interrupt Priority Disable.
	RCONbits.IPEN = 0;
}

void timer0_initialize(void)
{
	// Timer0 8-Bit mode.
	T0CONbits.T08BIT = 1;
	// Timer0 Internal CLKO.
	T0CONbits.T0CS = 0;
	// Timer0 Prescaler is assigned.
	T0CONbits.PSA = 0;
	// Timer0 Prescaler Select bits.
	// T0PS2.0 = 1 1 1 - 1:256 Prescale value.
	T0CONbits.T0PS2 = 1;
	T0CONbits.T0PS1 = 1;
	T0CONbits.T0PS0 = 1;

	// Timer0 Start.
	T0CONbits.TMR0ON = 1;

	// Enable Timer Overflow Interrupt.
	INTCONbits.TMR0IE = 1;
}
Size : 248bytes.
Blink One LED with Software Interrupt.

3.Blink Two LED One with Delay - One with Software Interrupt.

// Configuration register.
#pragma config IESO = OFF, FCMEN = OFF, OSC = INTIO7
#pragma config BORV = 3, BOREN = OFF, PWRT = OFF
#pragma config WDTPS = 32768, WDT = OFF
#pragma config MCLRE = ON, LPT1OSC = OFF, PBADEN = OFF, CCP2MX = PORTC
#pragma config DEBUG = OFF, XINST = OFF, LVP = OFF, STVREN = ON
#pragma config CP3 = OFF, CP2 = OFF, CP1 = OFF, CP0 = OFF
#pragma config CPD = OFF, CPB = OFF
#pragma config WRT3 = OFF, WRT2 = OFF, WRT1 = OFF, WRT0 = OFF
#pragma config WRTD = OFF, WRTB = OFF, WRTC = OFF
#pragma config EBTR3 = OFF, EBTR2 = OFF, EBTR1 = OFF, EBTR0 = OFF
#pragma config EBTRB = OFF

#include <xc.h>
#include <stdint.h>
#define _XTAL_FREQ 8000000
// PIC18F2620 - Compile with XC8(v1.44).
// PIC18F2620 - @8MHz Internal oscillator.

// Blink Two LED - One with Delay, One with Software Interrupt.

// MCU.RA0 -> LED.01.
// MCU.RA1 -> LED.02.

// Definitions.
#define ADCON			ADCON1
#define LED_PORT_0		PORTAbits.RA0
#define LED_PORT_1		PORTAbits.RA1
#define LED_TRIS_0		TRISAbits.TRISA0
#define LED_TRIS_1		TRISAbits.TRISA1
#define LED_0			LATAbits.LATA0
#define LED_1			LATAbits.LATA1

// Function prototypes.
void mcu_initialize(void);
void timer0_initialize(void);

// Global variable.
volatile uint8_t counter = 0;

// Main.
int main(void)
{
	mcu_initialize();
	timer0_initialize();
	
	INTCONbits.GIE = 1;
	while(1){
		LED_0 = 1;
		__delay_ms(100);
		LED_0 = 0;
		__delay_ms(100);
		LED_0 = 1;
		__delay_ms(100);
		LED_0 = 0;
		__delay_ms(400);
	}
	return(0);
}

// Interrupt Service Routine.
void interrupt ISR(void)
{
	counter++;
	if(counter == 2)
	{
		LED_1 ^= 1;
		counter = 0;
	}
	INTCONbits.TMR0IF = 0;
}

// Functions.
void mcu_initialize(void)
{
	// Internal Oscillator 8MHz.
	OSCCONbits.IRCF2  = 1;
	OSCCONbits.IRCF1  = 1;
	OSCCONbits.IRCF0  = 1;
	OSCTUNEbits.PLLEN = 0;
	while(!OSCCONbits.IOFS);
	// All Port as Digital I/O.
	ADCON = 0x0f;
	// Output LED.
	LED_PORT_0 = 0;
	LED_PORT_1 = 0;
	LED_TRIS_0 = 0;
	LED_TRIS_1 = 0;
	// Interrupt Priority Disable.
	RCONbits.IPEN = 0;
}

void timer0_initialize(void)
{
	// Timer0 8-Bit mode.
	T0CONbits.T08BIT = 1;
	// Timer0 Internal CLKO.
	T0CONbits.T0CS = 0;
	// Timer0 Prescaler is assigned.
	T0CONbits.PSA = 0;
	// Timer0 Prescaler Select bits.
	// T0PS2.0 = 1 1 1 - 1:256 Prescale value.
	T0CONbits.T0PS2 = 1;
	T0CONbits.T0PS1 = 1;
	T0CONbits.T0PS0 = 1;

	// Timer0 Start.
	T0CONbits.TMR0ON = 1;

	// Enable Timer Overflow Interrupt.
	INTCONbits.TMR0IE = 1;
}
Size : 356bytes.
Blink Two LED One with Delay - One with Software Interrupt.

01.2018