/*
Matt Hobson
ELEN 599
Spring 2006
File Name: SCI1_Init.c
File Purpose: Initialize the registers related to SCI1 for operation in 
              flash memory.  SCI1 will be used to communicate with the 
			  barcode scanner aboard the 2006 IEEE Robot.
			  
			  *NOTE: ASCII: 1 = 0x31
			                2 = 0x32
							3 = 0x33
			  
*/

//********************************** INCLUDE ***********************************

#include <mc9s12dp256.h>

//********************************** DEFINE ************************************
//@ 24MHz bus speed:
#define BAUD 156		//SCI baud rate 9600

//@ 8MHz bus speed:
//#define BAUD 52   //SCI baud rate 9600
//#define BAUD 13    //SCI baud rate 38400
//#define BAUD 4    //SCI baud rate 115200
#define TE 0x08   //Transmitter Enable Bit
#define RE 0x04   //Receiver Enable Bit

unsigned char barcode = 0;

//***************************** SCI0_INIT FUNCTION *****************************

void SCI1_Init (void) //Initialize SCI 1 used with barcode scanner
{
   SCI1BD = BAUD;     //Set Baud Rate for 9600
   SCI1CR1 = 0;		  //Set character format; 1 start, 8-bits, 1 stop
   SCI1CR2 = TE | RE; //Enable receiver and transmitter
}

//*************************** getBARCODE FUNCTION ******************************

void getBARCODE (void)   //Function to read and return the barcode value
{   
   while(!(SCI1SR1 & 0x20));  //Polling method to get the SCI data (RDRF flag)
   barcode = SCI1DRL;           //Stores the barcode value
   if(barcode == 0x31)
     PTH = 0x06;  //Display "1"
   if(barcode == 0x32)
   	 PTH = 0x5B;  //Display "2"
   if(barcode == 0x33)
     PTH = 0x4F;  //Display "3"
}

//******************************************************************************
