PIC Tutorial: 4-Digit 7-Segment LED

by Miguel on February 21, 2014

in PIC

PIC18F252 and 4-digit 7-segment LED

PIC18F252 and 4-digit 7-segment LED

The Code

#include <p18f252.h>
#include <delays.h>

#pragma config OSC=HS
#pragma config WDT=OFF

void main()
{
	unsigned int count=0;
	// set output ports

	TRISC=0x00;
	TRISB=0x00;



	PORTC=0xff;

	PORTBbits.RB0=0;
	PORTCbits.RC0=0;
	PORTCbits.RC1=0;
	PORTCbits.RC2=0;

	// turn off dots
	PORTCbits.RC3=0;



	while(1)
	{
		// one
		PORTCbits.RC4=0;
		PORTCbits.RC7=0;

		PORTCbits.RC0=1;
		Delay1KTCYx(1);
		PORTCbits.RC0=0;

		PORTCbits.RC4=1;
		PORTCbits.RC7=1;

		// two
		PORTCbits.RC5=0;
		PORTCbits.RC6=0; 
		PORTCbits.RC7=0;
		PORTBbits.RB4=0;
		PORTBbits.RB3=0;

		PORTCbits.RC1=1;
		Delay1KTCYx(1);
		PORTCbits.RC1=0;

		PORTCbits.RC5=1;
		PORTCbits.RC6=1; 
		PORTCbits.RC7=1;
		PORTBbits.RB4=1;
		PORTBbits.RB3=1;


		// three
		PORTCbits.RC4=0;
		PORTCbits.RC5=0;
		PORTCbits.RC6=0;
		PORTCbits.RC7=0;
		PORTBbits.RB4=0;
		

		PORTCbits.RC3=1;
		Delay1KTCYx(1);
		PORTCbits.RC3=0;

		PORTCbits.RC4=1;
		PORTCbits.RC5=1;
		PORTCbits.RC6=1;
		PORTCbits.RC7=1;
		PORTBbits.RB4=1;

		// four
		PORTCbits.RC4=0;
		PORTCbits.RC7=0;
		PORTCbits.RC6=0;
		PORTBbits.RB2=0;

		PORTBbits.RB0=1;
		Delay1KTCYx(1);
		PORTBbits.RB0=0;

		PORTCbits.RC4=1;
		PORTCbits.RC7=1;
		PORTCbits.RC6=1;
		PORTBbits.RB2=1;

	}
}

Previous post:

Next post: