/*
 * This is a Demo Code to show simple functionality associated with PIC18F4550 in MPLAB-IDE
 */

// References : 
// https://www.microchip.com/stellent/groups/sitecomm_sg/documents/devicedoc/en542701.pdf
// https://openlabpro.com/guide/timer-delay-implementation-in-pic18f4550/

#include <p18f4550.h>   

#define B0 PORTBbits.RB0 	// Providing Alias of RB0 and RB7
#define bit7 PORTBbits.RB7  // One can now access these bits either by their PORT bits or by their Alias

int count,x,y; 				// Define the variable as global, if intented to be observed on watch window of the Debugger
char mask_val,num;

/*
 * The following lines of code perform interrupt vector relocation to work with the USB bootloader. 
 * These must be used with every application program to run as a USB application.
 */

extern void _startup (void);
#pragma code _RESET_INTERRUPT_VECTOR = 0x1000

void _reset (void)
{
	_asm goto _startup _endasm
}

#pragma code
#pragma code _HIGH_INTERRUPT_VECTOR = 0x1008
void high_ISR (void)
{

}

#pragma code
#pragma code _LOW_INTERRUPT_VECTOR = 0x1018
void low_ISR (void)
{
}
#pragma code

/*End of interrupt vector relocation*/


void main()
{

///////////////////////////////////////////////////////////////////////////////////
/*
 * To access the PORT pins byte wise
 */

//  TRISB = 0x00; //make portB as Output.
////  TRISB = 0xFF; //make portB as Input.
//	PORTB = 0xFF; //assigning value to PORTB, when it's declared as an output
//
// Masking ->  right shift, left shift 
///////////////////////////////////////////////////////////////////////////////////
/*
 * To access the PORT pins bit wise
 */

	//// Data Direction can be set using TRIS as well as DDR 
//	INTCON2bits.NOT_RBPU=0;  // Enabling pull-ups for Switches
//
//	 TRISBbits.TRISB0 = 0;
//	 TRISBbits.TRISB1 = 0;
//	 TRISBbits.TRISB2 = 0;
//	 TRISBbits.TRISB3 = 0;
//
//	 DDRBbits.RB0 = 0;
//	 DDRBbits.RB1 = 0;
//	 DDRBbits.RB2 = 0;
//	 DDRBbits.RB3 = 0;
//   DDRBbits.RB7 = 1;
//
// 	 B0 = 0;
// 	 PORTBbits.RB1 = 1;
// 	 PORTBbits.RB2 = 1;
// 	 PORTBbits.RB3 = 0;
// 	 bit7 = 1;

//
//
	// while(1)
	// {
	// 	count = 93;	
	// 	if (bit7 = 1)
	// 		count = 100; 
	// 	else 
	// 		count = 50;
	// }

	// count = 100;
	// while(1)
	// {
		
	// 	if (bit7 == 1)
	// 		count = count >> 1; 
	// 	else 
	// 		count = count << 1;
	// }

///////////////////////////////////////////////////////////////////////////////////
/* 
 * Masking of bits, used in case of extracting particular bit value, nibble value etc 
 */
//	 
//		mask_val = 0x13;
//		num = 0x56;
//		num = num & mask_val;

///////////////////////////////////////////////////////////////////////////////////
/* 
 * Delay of 1ms by calculating from Oscillator frequency (fosc/4 by default) and observing it on stop watch 
 */

// count = 798;
// for(x=1;x<=count;x++);


///////////////////////////////////////////////////////////////////////////////////
/*
 * Program to toggle LED with 1s 'on' and 'off' period i.e. duty cycle 50%, Time period 2 s
 */
 // count = 798;
 // DDRBbits.RB3 = 0;
	
 // while(1)
 // {	
 // 	PORTBbits.RB3=1;
	// for(y=1;y<=1000;y++)
 // 		for(x=1;x<=count;x++);

 // 	PORTBbits.RB3=0;
	// for(y=1;y<=1000;y++)
 // 		for(x=1;x<=count;x++);

 // }


///////////////////////////////////////////////////////////////////////////////////
/* 
 * To show on debugger the effect of Stimulus and response on watch window 
 */
//	INTCON2bits.NOT_RBPU=0;
//	TRISB = 0xFF;
//	count = PORTB;
//	x = 0;
//	 while(1)
//	 {	
//	 	if(count == PORTB)
//	 		x=x+1;
//	 }  
//
//
//	TRISC = 0xFF;
//	count = PORTC;
//	x = 0;
//	 while(1)
//	 {	
//	 	if(count == PORTC)
//	 		x=x+1;
//	 }         
//
///////////////////////////////////////////////////////////////////////////////////

	// while(1);     // This while is to stay at the end of program forever
	//   			  // If not given then the program counter may reset and the entire program will run over and over
		
}
