//PIC16F690,MPLABX,XC8
#include "config.h" //belső órajel
#define byte unsigned char

void delay(unsigned int);
void LcdParancs(byte);
void LcdAdat(byte);
void LcdInit();

int main()
{
    ANSEL = ANSELH = 0; //digitális adat
    TRISC = 0; //RC0 kimenet (LED)
    TRISA = 0; //PORTC és PORTA az LCD vezérléshez
    TRISB7 = 1; //RB7 bemenet (nyomógomb)
    LcdInit();
    while(1)
    {
        LcdAdat('a');
        while(RB7); //lenyomásig itt várakozik
        delay(983); //15ms
        RC0 = !RC0;
        while(!RB7); //felengedésig itt várakozik
        delay(983); //15ms
    }
}
void delay(unsigned int varakozasIdo)
{
  for (unsigned int counter = 0; counter < varakozasIdo; counter++)
  {
      ;
  }
}
void LcdParancs(byte adat)
{
RC1 = 0; //RS
RC0 = 0; //R/W (utasítást írunk)
if (adat & 0x80) PORTA |= 0x20; else PORTA &=~0x20; //D7
if (adat & 0x40) PORTA |= 0x10; else PORTA &=~0x10; //D6
if (adat & 0x20) PORTC |= 0x40; else PORTC &=~0x40; //D5
if (adat & 0x10) PORTC |= 0x20; else PORTC &=~0x20; //D4
if (adat & 0x08) PORTC |= 0x10; else PORTC &=~0x10; //D3
if (adat & 0x04) PORTC |= 0x08; else PORTC &=~0x08; //D2
if (adat & 0x02) PORTA |= 0x01; else PORTA &=~0x01; //D1
if (adat & 0x01) PORTA |= 0x02; else PORTA &=~0x02; //D0

delay(1); // Data setup time min. 195 ns
RA2 = 1;
delay(1); // E pulse width min. 450 ns
RA2 = 0;
delay(7); // 50 us // Address, Data hold time, Cicle time, execution time
}
void LcdAdat(byte adat)
{
RC1 = 1; //RS
RC0 = 0; //R/W (adatot írunk)
if (adat & 0x80) PORTA |= 0x20; else PORTA &=~0x20; //D7
if (adat & 0x40) PORTA |= 0x10; else PORTA &=~0x10; //D6
if (adat & 0x20) PORTC |= 0x40; else PORTC &=~0x40; //D5
if (adat & 0x10) PORTC |= 0x20; else PORTC &=~0x20; //D4
if (adat & 0x08) PORTC |= 0x10; else PORTC &=~0x10; //D3
if (adat & 0x04) PORTC |= 0x08; else PORTC &=~0x08; //D2
if (adat & 0x02) PORTA |= 0x01; else PORTA &=~0x01; //D1
if (adat & 0x01) PORTA |= 0x02; else PORTA &=~0x02; //D0

delay(1); // Data setup time min. 195 ns
RA2 = 1;
delay(1); // E pulse width min. 450 ns
RA2 = 0;
delay(7); // 50 us // Address, Data hold time, Cicle time, execution time
}
void LcdInit()
{
    delay(5000);
    LcdParancs(0b00111000); //funkció beállítás
    delay(3);
    LcdParancs(0b00111000); //funkció beállítás
    delay(3);
    LcdParancs(0b00001100); //kijelző beállítás
    delay(3);
    LcdParancs(0b00000001); //kijelző törlés
    delay(100);
    LcdParancs(0b00000110); //entry mód beállítás
    delay(100);
}
