PIC18F Trainer - MC74HC595 SPI 8-Bit Serial Shift Register.

0.Contents

PIC18F Trainer : PIC18F2620 @8MHz Internal oscillator.

1.MC74HC595 Chase.

// 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.

// MC74HC595 - SPI 8-Bit Serial Shift Register.

// PIC18F Trainer Jumpers.
// JP1 - SCL Open.
// JP2 - SDA Open.
// JP3 - VEE Open.
// JP4 - BCKL Not use.

// Definitions.
// SPI Port.
#define SPI_CS					LATAbits.LATA5
#define SPI_CS_TRIS				TRISAbits.TRISA5
#define SPI_SCK_TRIS			TRISCbits.TRISC3
#define SPI_SDI_TRIS			TRISCbits.TRISC5
// MC74HC595 Port.
#define MC74HC595_OE			LATAbits.LATA2
#define MC74HC595_OE_TRIS		TRISAbits.TRISA2
//
#define DELAY					200

// Function prototypes.
void mc74hc595_initialize();
void mc74hc595_writeData(uint8_t u8Data);
void mcu_initialize(void);
void spi_initializeMaster(void);

// Main.
int main(void)
{
	mcu_initialize();
	spi_initializeMaster();
	mc74hc595_initialize();

	MC74HC595_OE = 0;
	while(1){
		mc74hc595_writeData(0x01);
		__delay_ms(DELAY);
		mc74hc595_writeData(0x02);
		__delay_ms(DELAY);
		mc74hc595_writeData(0x04);
		__delay_ms(DELAY);
		mc74hc595_writeData(0x08);
		__delay_ms(DELAY);
		mc74hc595_writeData(0x10);
		__delay_ms(DELAY);
		mc74hc595_writeData(0x20);
		__delay_ms(DELAY);
		mc74hc595_writeData(0x40);
		__delay_ms(DELAY);
		mc74hc595_writeData(0x80);
		__delay_ms(DELAY);
		mc74hc595_writeData(0x40);
		__delay_ms(DELAY);
		mc74hc595_writeData(0x20);
		__delay_ms(DELAY);
		mc74hc595_writeData(0x10);
		__delay_ms(DELAY);
		mc74hc595_writeData(0x08);
		__delay_ms(DELAY);
		mc74hc595_writeData(0x04);
		__delay_ms(DELAY);
		mc74hc595_writeData(0x02);
		__delay_ms(DELAY);
	}
	return(0);
}

// Functions.
void mc74hc595_initialize()
{
	uint8_t dummy;
	
	MC74HC595_OE = 0;
	SPI_CS = 0;
	SSPBUF = 0x00;
	while(!SSPSTATbits.BF);
	dummy = SSPBUF;
	SPI_CS = 1;
	MC74HC595_OE = 1;
}

void mc74hc595_writeData(uint8_t u8Data)
{
	uint8_t dummy;
	
	SPI_CS = 0;
	SSPBUF = u8Data;
	while(!SSPSTATbits.BF);
	dummy = SSPBUF;
	SPI_CS = 1;
}

void mcu_initialize(void)
{
	// Internal Oscillator 8MHz.
	OSCCONbits.IRCF2  = 1;
	OSCCONbits.IRCF1  = 1;
	OSCCONbits.IRCF0  = 1;
	OSCTUNEbits.PLLEN = 0;
	while(!OSCCONbits.IOFS);
	// SPI Port.
	SPI_CS = 1;
	SPI_CS_TRIS  = 0;
	SPI_SCK_TRIS = 0;
	SPI_SDI_TRIS = 0;
	// MC74HC595 Outputs.
	MC74HC595_OE = 0;
	MC74HC595_OE_TRIS = 0;
}

void spi_initializeMaster(void)
{
	// SPI - Master, Mode 0, MSB First.
	// SPI Clock frequency - 8000000 / 4 = 2MHz.
	SSPSTAT = 0x40;
	SSPCON1 = 0x20;
}
Size : 520bytes.

01.2018