/*


   _____         _               __ ____                _
  / ___/ ____   (_)_____ ____ _ / // __ ) _____ ____ _ (_)____
  \__ \ / __ \ / // ___// __ `// // __  |/ ___// __ `// // __ \
 ___/ // /_/ // // /   / /_/ // // /_/ // /   / /_/ // // / / /
/____// .___//_//_/    \__,_//_//_____//_/    \__,_//_//_/ /_/
     /_/

PIC 18f1320 Serial RGB LED controller
http://sunbizhosting.com/~spiral/pixel/

File: pixel.c
last revision: 09 Oct 2009 18:19

*/

#include <p18f1320.h> /* Register and bit declarations */ 
#include <delays.h> 
#include <timers.h>  
#include <usart.h>
#include <stdlib.h>
#include <string.h>
#include <adc.h>
#include <pwm.h> 



#pragma config WDT = OFF
#pragma config OSC = INTIO2
#pragma config MCLRE = OFF
#pragma config PWRT = ON
#pragma config DEBUG= OFF
#pragma config LVP = OFF
typedef unsigned char BYTE; 
//// Constant declarations 

#define CLOCK 8	//clock in MHz

#define red 	PORTBbits.RB5
#define green 	PORTBbits.RB2
#define blue 	PORTBbits.RB3

unsigned int r,g,b,c;
char inpbuf[8];	
unsigned char udata;


void main(void);
void InterruptHandlerHigh(void);
void init (void);
void process_command(void);
		
		// High priority interrupt vector 
		#pragma code InterruptVectorHigh = 0x08 
		void InterruptVectorHigh (void) {
					_asm 
					goto InterruptHandlerHigh //jump to interrupt routine endasm 
					_endasm
				}
		// High priority interrupt routine 
		#pragma code 
		#pragma interrupt InterruptHandlerHigh 
		void InterruptHandlerHigh () { 
		
					 if(PIR1bits.TMR2IF)
					  { PIR1bits.TMR2IF = 0;
							if(c>r)
								red=0;
							
							if(c>g)
								green=0;
						
							if(c>b)
								blue=0;
							
							if(c>50){
								c=0;
								if(r>0)
									red=1;
								if(g>0)
									green=1;
								if(b>0)
									blue=1;
								}else
									c++;
					  }
									}
		
		
		void main (void) {
		 
		
		unsigned char i;
		
			
			init();
		
		
			putrsUSART("\r\nMain program....now entering infinite loop...\r\n");
		
			r=50;
			g=5;
			b=50;
		
			while(1) {
		
			if(PIR1bits.RCIF)									// Check for USART interrupt
			   	{
			   	udata = RCREG;
				inpbuf[i] = udata;		// get received char 
				                  i++;							// increment buffer index     
					if( udata == '$' ){
												process_command();
											    memset(inpbuf,0,8);	// buffer
				                     			i = 0;
									  }
			    }       									
					}
		 				}
		
		//initialise system variables. 
		void init (void) {

					TRISB = 0b00000000; 	// set port B to all outputs
				   	INTCON=0; 
					RCON=0; 
					RCONbits.IPEN=1;		//enable priority levels
					INTCONbits.GIE=1;
					INTCONbits.PEIE=1; 
		
					OpenTimer2(T2_PS_1_1 & T2_POST_1_1 & TIMER_INT_ON);
		
		
					OpenUSART( USART_TX_INT_OFF &USART_RX_INT_OFF &USART_ASYNCH_MODE&USART_EIGHT_BIT &USART_CONT_RX &USART_BRGH_HIGH,25 );
					// 8Mhz clock, open USART 19200 baud 8/n/1
					putcUSART(12);		// clear all
					putrsUSART("\r\nPIC Booting...\r\n");
					return;
						}
		// Process the received command
		void process_command(void) {
				char j,k[5];
				int co;
				memset(k,0,5);
				for(j=0;j<5;j++){
				if(inpbuf[j+3]=='$')
				break;
				else
				k[j]=inpbuf[j+3];
				}
				
				co=atoi(k);
				
				if ( inpbuf[0] == 'R' && inpbuf[1] == 'D' ){
					r = co;
				}
				
				if ( inpbuf[0] == 'B' && inpbuf[1] == 'L' ){
					b = co;
				}
				
				if ( inpbuf[0] == 'G' && inpbuf[1] == 'R' ){
					g = co;
				}
		
		
	}
