
#include "mcf5213.h"

void init_clock_config (void);
void init_pto_ua(void);
void init_uart_0(void);
void output_s(char caracter);
unsigned char input_s();

 int main (void) 
	{
		unsigned char ascii; 	

		init_clock_config ();		
		init_pto_ua();
		init_uart_0();		
			
		while (1)
			{									       		
	       	    	ascii = input_s();
				if (ascii=='0') 
					output_s('A');
				else				
					output_s(ascii);				
					
			}
		return 0;
	}


void init_clock_config (void)
	{
		// Clock source is 7.3728 MHz external oscillator
	    	// Clock mode: External clock mode
		// Processor/Bus clock frequency = 7.37 MHz

	    	MCF_CLOCK_SYNCR = 0;
	}


void init_pto_ua(void)
	{
    		MCF_GPIO_DDRUA  = 0;
		MCF_GPIO_PUAPAR = MCF_GPIO_PUAPAR_PUAPAR1(0x1) |
            			MCF_GPIO_PUAPAR_PUAPAR0(0x1);
 	}


void init_uart_0(void)
	{
    		// Configure UART 0 (first UART channel)
    		// Baud Rate = 9600
    		// 8 data bits, 1 stop bit, No parity
    		// /CTS input has no effect on the transmitter
    		// /RTS output is not affected by receiver or transmitter
    		// UART 0: Reset transmitter, receiver and mode register pointer

    		
		MCF_UART0_UCR = 	MCF_UART_UCR_MISC(0x3);		//Reset Transmisión
    		MCF_UART0_UCR = 	MCF_UART_UCR_MISC(0x2);		//Reset Recepción
    		MCF_UART0_UCR = 	MCF_UART_UCR_MISC(0x1);		//Apunta a UMR1
    		MCF_UART0_UMR = 	MCF_UART_UMR_PM(0x2) |		//UMR1 Modo no paridad
                    		MCF_UART_UMR_BC(0x3);		//UMR1 Bits por caracter = 8
    		MCF_UART0_UMR = 	MCF_UART_UMR_SB(0x7);		//UMR2 Bit de Stop = 1
    		MCF_UART0_UCSR =	MCF_UART_UCSR_RCS(0xd) |	//Clock de Recepcion = interno
                     		MCF_UART_UCSR_TCS(0xd);		//Clock de Transmision = interno
    		MCF_UART0_UBG2 = 	0x17;					//Baudios = 9600    		
    		MCF_UART0_UCR = 	MCF_UART_UCR_TXC(0x1);		//Habilita transmisión
    		MCF_UART0_UCR = 	MCF_UART_UCR_RXC(0x1);		//Habilita recepción

	}




void output_s(char caracter) 
	{				
		char val_TXRDY;
		char val_TXEMP;			
		MCF_UART0_USR = MCF_UART_USR(0);						
		val_TXRDY = (MCF_UART0_USR & MCF_UART_USR_TXRDY);
		val_TXEMP = (MCF_UART0_USR & MCF_UART_USR_TXEMP);					
		while (val_TXRDY ==0 && val_TXEMP ==0)	
			{
				MCF_UART0_USR = MCF_UART_USR(0);						
				val_TXRDY = (MCF_UART0_USR & MCF_UART_USR_TXRDY  );
				val_TXEMP = (MCF_UART0_USR & MCF_UART_USR_TXEMP  );
			}	
		MCF_UART0_UTB = caracter;
	}


unsigned char input_s()
	{	
		char val_RXRDY;
		char val_FFULL;

		MCF_UART0_USR = MCF_UART_USR(0);						
		val_RXRDY = (MCF_UART0_USR & MCF_UART_USR_RXRDY);		
		val_FFULL = (MCF_UART0_USR & MCF_UART_USR_FFULL);				
		while ( val_RXRDY == 0 && val_FFULL == 0 ) 		
			{
				MCF_UART0_USR = MCF_UART_USR(0);						
				val_RXRDY = (MCF_UART0_USR & MCF_UART_USR_RXRDY);
				val_FFULL = (MCF_UART0_USR & MCF_UART_USR_FFULL);			
			}		
		return MCF_UART0_URB;
	}




