Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
#include<xc.h>
#define _XTAL_FREQ 4000000
#define SDATA GPIO2
#define SCLK GPIO1
#define SCS GPIO0
#pragma config FOSC = INTRCIO
#pragma config MCLRE = OFF
#pragma config WDTE = OFF
unsigned char dataout;
void shiftout(unsigned char ch)
{
char x;
unsigned char dummy = ch;
for(x=0;x<8;x++)
{
SDATA = 0;
if(dummy & 0x80) SDATA = 1;
SCLK = 1;
NOP();
SCLK = 0;
dummy<<=1;
}
SCS = 1;
NOP();
SCS = 0;
}
void LCDtoggle(void)
{
dataout ^= 0x10;
shiftout(dataout);
dataout ^= 0x10;
shiftout(dataout);
}
void LCDnibbles(unsigned char ch)
{
dataout &= 0xF0;
dataout += (ch>>4) & 0xF;
shiftout(dataout);
LCDtoggle();
dataout &= 0xF0;
dataout += ch & 0xF;
shiftout(dataout);
LCDtoggle();
}
void LCDcmd(unsigned char cmd)
{
dataout = 0;
LCDnibbles(cmd);
__delay_us(100);
}
void LCDdata(unsigned char data)
{
dataout = 0x20;
LCDnibbles(data);
__delay_us(100);
}
void LCDinit(void)
{
TRISIO = 0;
__delay_ms(15);
dataout = 2;
LCDtoggle();
__delay_ms(5);
LCDtoggle();
__delay_ms(5);
LCDtoggle();
__delay_ms(5);
LCDcmd(0x28);
__delay_ms(5);
LCDcmd(0x28);
LCDcmd(0xC);
LCDcmd(0x6);
LCDcmd(0x1);
}
void LCDprint(const char* str)
{
while(*str != 0)
LCDdata(*str++);
}
void LCDgoto(char x, char y)
{
char addr = 0x80;
if(y==2) addr = 0xC0;
if(y==3) addr = 0x94;
if(y==4) addr = 0xD4;
addr+=x;
LCDcmd(addr);
}
void main(void)
{
ANSEL = 0;
LCDinit();
__delay_ms(10);
LCDprint("Hello World");
LCDgoto(0,2);
LCDprint("Serial LCD driver");
LCDgoto(4,3);
LCDprint("For minimum");
LCDgoto(6,4);
LCDprint("pin counts");
while(1);
}