

#include "mcf5213.h" 


void init_pto_an(void);
void init_clock_config (void);
void init_adc(void);
unsigned int  convertir(void);
void init_pto_ua(void);
void init_interrupt_controller (void);
void init_uart_0(void);
void output_s(char caracter);
void time(void);

int main (void)  


	{	
		unsigned int  	valor;

		unsigned int  	aux;
		unsigned char  	ochobits; 		
		

		init_pto_ua();
		init_interrupt_controller();
		init_uart_0();
		asm_set_ipl(0);	

		init_pto_an();
		init_clock_config();		
		init_adc();

		

		while(1)					// ciclo infinito 
		{

			valor = convertir();		// Sdddddddddddd000 --> S=signo d=dato 12 bits 
			valor >>= 3;			// corrimiento a la derecha 3 bits
			aux= (valor & 0xff00);		// clear 8 bits LSB
			aux >>= 8;				// corrimiento de 8 bits a la derecha	
			
			ochobits = aux;
			output_s(ochobits );		// envia por UART0 8 bits MSB

			aux= (valor & 0x00ff);		// clear 8 bits MSB
			ochobits = aux;

			output_s(ochobits );		// envia por UART0 8 bits LSB

			time();				// espera un tiempo
			

		}
		return 0;
	}


void init_pto_an(void)
	{
		MCF_GPIO_PANPAR = MCF_GPIO_PANPAR_PANPAR0;	// GPIO Entrada Analógica
   		MCF_GPIO_DDRAN = 0;					// Dirección: Entrada
	}


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;  // usa el clock externo     		
	}


void init_adc(void)
	{
		MCF_ADC_ADHLMT0 = MCF_ADC_ADHLMT_HLMT(0xfff);		//Limite superior = Vdd
											//para la Entrada 0  
    		
    		MCF_ADC_CTRL1 = 	MCF_ADC_CTRL1_STOP0 |			//Pone en modo Stop 
                    		MCF_ADC_CTRL1_SMODE(0x3)|		//Ciclo parlelo ADC_A ADC_B
					MCF_ADC_CTRL1_SYNC0 |			//Inicia por software START0
					MCF_ADC_CTRL1_CHNCFG(0x00);		//Single ended modo	

    		MCF_ADC_CTRL2 = 	MCF_ADC_CTRL2_STOP1 |			//Detiene  ADC_B
                    		MCF_ADC_CTRL2_DIV(0x2);			//Clock Div select = CLK/4
    		MCF_ADC_POWER = 	MCF_ADC_POWER_PUDELAY(0xd) |
                    		MCF_ADC_POWER_PD2;

		MCF_ADC_ADSDIS=	MCF_ADC_ADSDIS_DS1;			//solo scan input 0
	
		MCF_ADC_ADOFS_OFFSET(0x0000);					// =0 resultado sin signo

		MCF_ADC_ADLLMT_LLMT(0x0000);
		MCF_ADC_ADHLMT_HLMT(0x0FFF);

    		while (MCF_ADC_POWER & MCF_ADC_POWER_PSTS0)
        	;
    		while (MCF_ADC_POWER & MCF_ADC_POWER_PSTS1)
        	;
	}

unsigned int  convertir(void)
	{
		unsigned int result;

		MCF_ADC_CTRL2 |= MCF_ADC_CTRL2_SIMULT;			
		MCF_ADC_CTRL1 &= ~MCF_ADC_CTRL1_STOP0; 			//Desactiva Stop
		MCF_ADC_CTRL1 |= MCF_ADC_CTRL1_START0;       		//Inicia la conversión
		

		result = MCF_ADC_ADSTAT & MCF_ADC_ADSTAT_RDY0;		// LA MUESTRA ESTA LISTA?


		while ( result == 0)
			{
				result = MCF_ADC_ADSTAT & MCF_ADC_ADSTAT_RDY0;
			}
		
		
		MCF_ADC_CTRL1 &= ~MCF_ADC_CTRL1_START0;       		//Desac. la conversión		
		return MCF_ADC_ADRSLT0;						//retorna el resultado 
	}



void init_pto_ua(void)
	{
    		
    MCF_GPIO_DDRUA = 0;
    MCF_GPIO_PUAPAR = MCF_GPIO_PUAPAR_PUAPAR1(0x1) |   //habilita los pines UA para Tx y Rx
                      MCF_GPIO_PUAPAR_PUAPAR0(0x1);
   
}


void init_interrupt_controller (void)
{
	
    MCF_INTC_ICR13 = MCF_INTC_ICR_IL(0x6) |	// Nivel
                     MCF_INTC_ICR_IP(0x4);	// Prioridad

    MCF_INTC_IMRL = MCF_INTC_IMRL_MASK31 |	// Enmascara todas las fuentes de Interrupción
                    MCF_INTC_IMRL_MASK30 |	// excepto la fuente No 13
                    MCF_INTC_IMRL_MASK29 |
                    MCF_INTC_IMRL_MASK28 |
                    MCF_INTC_IMRL_MASK27 |
                    MCF_INTC_IMRL_MASK26 |
                    MCF_INTC_IMRL_MASK25 |
                    MCF_INTC_IMRL_MASK24 |
                    MCF_INTC_IMRL_MASK23 |
                    MCF_INTC_IMRL_MASK22 |
                    MCF_INTC_IMRL_MASK21 |
                    MCF_INTC_IMRL_MASK20 |
                    MCF_INTC_IMRL_MASK19 |
                    MCF_INTC_IMRL_MASK18 |
                    MCF_INTC_IMRL_MASK17 |
                    MCF_INTC_IMRL_MASK16 |
                    MCF_INTC_IMRL_MASK15 |
	              MCF_INTC_IMRL_MASK14 |
								//EL 13 CORRESPONDE A UART0
                    MCF_INTC_IMRL_MASK12 |
                    MCF_INTC_IMRL_MASK11 |
                    MCF_INTC_IMRL_MASK10 |
                    MCF_INTC_IMRL_MASK9  |
                    MCF_INTC_IMRL_MASK8  |
                    MCF_INTC_IMRL_MASK7  |
                    MCF_INTC_IMRL_MASK6  |
                    MCF_INTC_IMRL_MASK5  |
                    MCF_INTC_IMRL_MASK4  |
                    MCF_INTC_IMRL_MASK3  |
                    MCF_INTC_IMRL_MASK2  |
                    MCF_INTC_IMRL_MASK1; 			
}

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_UIMR = 	MCF_UART_UIMR_RXRDY_FU;		//Interrupcion Rx = 1 caracter
    		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;
	}


void time(void)
	{
       	unsigned long  contador;	  
     
     	  	contador=0;       
      	while (contador < 566400) 
			{
              		contador++;
       		}       
	}

