//lcd.c

#include <xc.h>
#include <stdint.h>
#include "lcd.h"

void clkLcd()
{
    __delay_us(5);
    E = 1;
    __delay_us(85);
    E = 0;
    __delay_us(85);
}
void setLcd (uint8_t data_Lcd)
{
    RS = 0;
    RW = 0;
    DATA = data_Lcd;
    clkLcd(); 
    __delay_ms(1);
}

void initLcd()
{
    TRISC &= 0b10001111;
    TRISD &= 0b00000000;
    __delay_ms(50);
    //Function set
    setLcd(0b00111001);
    //Function set
    setLcd(0b00111001);
    //Function set
    setLcd(0b00111001);
    //Internal OSC frequency
    setLcd(0b00011101);
    //Contrast set
    setLcd(0b01111100);
    //Function set
    setLcd(0b00111001);
    //Power/ICON/Contrast control
    setLcd(0b01010000);
    //Follower control
    setLcd(0b01101100);
    //Display ON/OFF control
    setLcd(0b00001100);
    //Clear Display
    setLcd(0b00000001);
    //Entry mode set
    setLcd(0b00000110);
}


void datLcd (char znak)
{
    RS = 1;
    RW = 0;
    DATA = znak;
    clkLcd(); 
}

void posLcd (uint8_t row, uint8_t column)
{
    //Set DDRAM adress
    setLcd (111 + (row << 4) + column);
}

void writeLcd (char *string)
{
  uint8_t i = 0;
  for(i = 0; i<=15 && string[i] != '\0'; i++)
  {
      datLcd (string[i]);
  }
}

void clearLcdRow (uint8_t num)
{
   posLcd (num, 1);
   writeLcd ("                ");
}

void clearLcd ()
{
   clearLcdRow (1);
   clearLcdRow (2);
   clearLcdRow (3);
}

