/********************************************************************
* FileName:        Lab Exam Problem 2.c
* Processor:       PIC18F4520
* Compiler:        MPLAB C18 v.3.06 
*
* This file displays a random number in a random location
*                                                                     
*
* Author               Date        Comment
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
// (Your name here) 


/** Processor Header Files ****************************************/     
#include <p18f4520.h> 
#include "PICDEM 2 LCD module.h"
#include <stdlib.h>
#include <stdio.h>
#include <timers.h>
#include <pwm.h>

/** Define Constants Here ******************************************/
#define sample 100

/** Local Function Prototypes **************************************/
void low_isr(void);
void high_isr(void);
void sampleFunction(void);

// ============================================================
// Configuration Bits 
// ============================================================
#pragma config OSC = INTIO67  // External 4MHz crystal
#pragma config WDT = OFF
#pragma config LVP = OFF
#pragma config BOREN = OFF

/** Declare Interrupt Vector Sections ****************************/
#pragma code high_vector=0x08
void interrupt_at_high_vector(void)
{
   _asm goto high_isr _endasm
}

#pragma code low_vector=0x18
void interrupt_at_low_vector(void)
{
   _asm goto low_isr _endasm
}

/** Global Variables *********************************************/
	int sampleVariable;
	int randNum,randSpaces,randLine;
	char i;
	char numString[5];
		
/*****************************************************************
* Function:        void main(void)
******************************************************************/
#pragma code
void main (void)
{
	OSCCONbits.IRCF2 = 1;
	OSCCONbits.IRCF1 = 1;
	OSCCONbits.IRCF0 = 0;
	
	// To do: write code
	LCDInit();
	DisplayClr();
	ADCON1 = 0x0F;
	TRISB = 0x00;
	OpenTimer2(TIMER_INT_OFF & T2_PS_1_16);
	OpenPWM1(207);
	// 1/300 = (period + 1) * 4 * 1/4000000 * 16
	// period
	SetDCPWM1(0);
	
	for(i=12;i>0;i--)
	{
		PORTB = i;
		
		Delay10KTCYx(50);
		// 0.000 001 second per instruction
		//   500 000 instructions needed)
	}
	PORTB = 0;
	
	SetDCPWM1(200);
	Delay10KTCYx(50);
	Delay10KTCYx(50);
	Delay10KTCYx(50);
	Delay10KTCYx(50);
	Delay10KTCYx(50);
	Delay10KTCYx(50);
	SetDCPWM1(0);
	

	while (1)
    {
		
		randLine=rand()/7%2;
		randSpaces=rand()/7%12;
		randNum=rand();
		
		DisplayClr();
		// Randomize the Line
		if(randLine==0)
			LCDLine1();
		else
			LCDLine2();
		
		// Randomize the number of spaces
		for(i=0;i<randSpaces;i++)
			LCDwrite(' ');
		
		// Display the random number
		sprintf(numString,"%u",randNum);
		text_display(numString);
		
		Delay10KTCYx(100);
    }
}

/*****************************************************************
* Local functions go after main but before the interrupts if needed
******************************************************************/

/*****************************************************************
* Function:		void sample(void)
* Input:
* Output:
* Overview:		You change this template of a function if needed
******************************************************************/
void sampleFunction(){
}	


/*****************************************************************
* Function:        void high_isr(void)
* Input:
* Output:
* Overview:
******************************************************************/
#pragma interrupt high_isr			// declare function as high priority isr
void high_isr(void)
{
}

/******************************************************************
* Function:        void low_isr(void)
* Input:
* Output:
* Overview:
********************************************************************/
#pragma interruptlow low_isr		// declare function as low priority isr
void low_isr(void)
{
}