__attribute__((always_inline)) __INLINE void lcd_wrdata8(uint32_t data)
{
  //GPIO_SETPIN(LCD_PORT, RS_PIN); //data

  GPIO_CLRPIN(LCD_PORT, WR_PIN);
  DATA_WR(data);
  GPIO_SETPIN(LCD_PORT, WR_PIN);

  return;
}
__attribute__((always_inline)) __INLINE void lcd_wrcmd8(uint32_t cmd)
{
  GPIO_CLRPIN(LCD_PORT, RS_PIN); //cmd

  GPIO_CLRPIN(LCD_PORT, WR_PIN);
  DATA_WR(cmd);
  GPIO_SETPIN(LCD_PORT, WR_PIN);

  GPIO_SETPIN(LCD_PORT, RS_PIN); //data

  return;
}
__attribute__((always_inline)) __INLINE void lcd_wrdata16(uint_least16_t data)
{
  //GPIO_SETPIN(LCD_PORT, RS_PIN); //data

  GPIO_CLRPIN(LCD_PORT, WR_PIN);
  DATA_WR(data>>8);
  GPIO_SETPIN(LCD_PORT, WR_PIN);

  //NOP();

  GPIO_CLRPIN(LCD_PORT, WR_PIN);
  DATA_WR(data);
  GPIO_SETPIN(LCD_PORT, WR_PIN);

  return;
}
void PORTD_IRQHandler(void)
{
	if (PORTD->ISFR & 0x00000010) {
		delay(200);
		PTC->PDOR=0xC0;//Second Row
				CMD_WR();

		PTC->PDOR=0x45;//Writes to LCD
				DATA_WR();
		PTC->PDOR=0x4E;//Writes to LCD
				DATA_WR();
		PTC->PDOR=0x54;//Writes to LCD
				DATA_WR();
		PTC->PDOR=0x45;//Writes to LCD
				DATA_WR();
		PTC->PDOR=0x52;//Writes to LCD
				DATA_WR();
		PTC->PDOR=0xA0;//Writes to LCD
				DATA_WR();
		PTC->PDOR=0x54;//Writes to LCD
				DATA_WR();
		PTC->PDOR=0x45;//Writes to LCD
				DATA_WR();
		PTC->PDOR=0x4D;//Writes to LCD
				DATA_WR();
		PTC->PDOR=0x50;//Writes to LCD
				DATA_WR();
		PTC->PDOR=0x3A;//Writes to LCD
				DATA_WR();
		PTC->PDOR=0xA0;//Writes to LCD
				DATA_WR();
				
				
		num1=HexPad();
		num1C=num1|0x30;
		//PTC->PDOR=num1C;//Writes to LCD
			//	DATA_WR();
			delay(500);
		num2=HexPad();
		num2C=num2|0x30;
		//PTC->PDOR=num2C;//Writes to LCD
			//	DATA_WR();
		
		delay(1000);
		PTC->PDOR=0x01;//clear display
				CMD_WR();
		//PTC->PDOR=0x80;//line 1 space 1
			//	CMD_WR();
		
		
		
		PORTD->ISFR = 0x0010; /* clear interrupt flag */
}
}
	int HexPad()
{
	int row=4;
		int col=4;
		int B=500;
		int num;
		PTB->PDDR= 0xFFFFFFFF;
		PTE->PDDR= 0xFFFFFFF0;

		//PTC->PDOR=0x00; //Need then DATA_WR() called to show numbers
		
	//Row
	
		do
		{
			if(PTE->PDIR==0x07)
			{
				row=0;
			}
			else if (PTE->PDIR==0x0B)
			{
				row=1;
			}
			else if(PTE->PDIR==0x0D)
			{
				row=2;
			}
			else if (PTE->PDIR==0x0E)
			{
				row=3;
			}
		}while(row==4);
		
		
		PTB->PDDR= 0xFFFFFFF0;
		PTE->PDDR= 0xFFFFFFFF;
		//Get Columb
		
		
		do
		{
			if(PTB->PDIR==0x07)
			{
				col=0;
			}
			else if (PTB->PDIR==0x0B)
			{
				col=1;
			}
			else if(PTB->PDIR==0x0D)
			{
				col=2;
			}
			else if (PTB->PDIR==0x0E)
			{
				col=3;
			}
		}while(col==4);
		
		
		if(row==0 && col==0)
		{
			PTC->PDOR=0x31;
			DATA_WR();
			num=1;
		}
		else if(row==1 && col==0)
		{
			PTC->PDOR=0x32;
			DATA_WR();
			num=2;
		}
		else if(row==2 && col==0)
		{
			PTC->PDOR=0x33;
			DATA_WR();
			num=3;
		}
		else if(row==3 && col==0)
		{
			PTC->PDOR=0x34;
			DATA_WR();
			num=15;
		}
																	//Row 1
		else if(row==0 && col==1)
		{
			PTC->PDOR=0x34;
			DATA_WR();
			num=4;
		}
		else if(row==1 && col==1)
		{
			PTC->PDOR=0x35;
			DATA_WR();
			num=5;
		}
		else if(row==2 && col==1)
		{
			PTC->PDOR=0x36;
			DATA_WR();
			num=6;
		}
		else if(row==3 && col==1)
		{
			PTC->PDOR=0x45;
			DATA_WR();
			num=14;
		}
																	//Row 2
		else if(row==0 && col==2)
		{
			PTC->PDOR=0x37;
			DATA_WR();
			num=7;
		}
		else if(row==1 && col==2)
		{
			PTC->PDOR=0x38;
			DATA_WR();
			num=8;
		}
		else if(row==2 && col==2)
		{
			PTC->PDOR=0x39;
			DATA_WR();
			num=9;
		}
		else if(row==3 && col==2)
		{
			PTC->PDOR=0x44;
			DATA_WR();
			num=13;
		}
																		//Row3
		else if(row==0 && col==3)
		{
			PTC->PDOR=0x41;
			DATA_WR();
			num=10;
		}
		else if(row==1 && col==3)
		{
			PTC->PDOR=0x30;
			DATA_WR();
			num=0;
		}
		else if(row==2 && col==3)
		{
			PTC->PDOR=0x42;
			DATA_WR();
			num=11;
		}
		else if(row==3 && col==3)
		{
			PTC->PDOR=0x43;
			DATA_WR();
			num=12;
		}

	return num;
}
int main(void){
		int num3,num4, num;
		//char c1, c2;
		Ports_init(); //Do I need to initialize the ports everytime??
		initializer();
		Port_inter();
		while(1)
		{
					//Transmitting
				num=num1*10;
			num=num+num2;
			
				UART0_initT();
				while(!(UART0->S1 & 0x80)) 
					{}
				UART0->D = num; //* send a char *
						while(!(UART0->S1 & 0x80)) 
					{}
			num=0;
						//Recieving
				UART0_initR();
				while(!(UART0->S1 & 0x20)) 
					{} //* wait for receive buffer full *
				num = UART0->D;
						
						//Converting number to ASCII
			/*	if(num>=10)
				{
					num=num+1;
					num1=num%10;
					num=num|0x30;
				}
				else
				{
					num=num|0x30;
				}*/
						num4=num%10;
						num3=(num-num4)/10;
						num3=num3|0x30;
						num4=num4|0x30;
				PTC->PDOR=0x43;//Writes to LCD
				DATA_WR();
				PTC->PDOR=0x3A;//Writes to LCD
				DATA_WR();
				PTC->PDOR=num3;//Writes to LCD
				DATA_WR();
				PTC->PDOR=num4;//Writes to LCD
				DATA_WR();
				PTC->PDOR=0xA0;//Writes to LCD
				DATA_WR();
				PTC->PDOR=0x53;//Writes to LCD
				DATA_WR();
				PTC->PDOR=0x3A;//Writes to LCD
				DATA_WR();
				PTC->PDOR=num1C;//Writes to LCD
				DATA_WR();
				PTC->PDOR=num2C;//Writes to LCD
				DATA_WR();
				delay(200);
				PTC->PDOR=0x01;//clear display
				CMD_WR();
				PTC->PDOR=0x80;//line 1 space 1
				CMD_WR();
		}
	}