Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

#include <xc.

h>
#define _XTAL_FREQ 12000000

/*********************Definition of Ports********************************/

#define RS PORTDbits.RD2 /*PIN 0 of PORTD is assigned for register select Pin of


LCD*/
#define EN PORTDbits.RD3 /*PIN 1 of PORTD is assigned for enable Pin of LCD */
#define ldata PORTD /*PORTD(PD4-PD7) is assigned for LCD Data Output*/
#define LCD_Port TRISD /*define macros for PORTD Direction Register*/

/****************************Functions********************************/

void LCD_Command(unsigned char cmd )


{
ldata = (ldata & 0x0f) |(0xF0 & cmd); /*Send higher nibble of command first
to PORT*/
RS = 0; /*Command Register is selected i.e.RS=0*/
EN = 1; /*High-to-low pulse on Enable pin to latch data*/
NOP();
EN = 0;
__delay_ms(1);
ldata = (ldata & 0x0f) | (cmd<<4); /*Send lower nibble of command to PORT */
EN = 1;
NOP();
EN = 0;
__delay_ms(3);
}

void LCD_Init()
{
LCD_Port = 0; /*PORT as Output Port*/
__delay_ms(15); /*15ms,16x2 LCD Power on delay*/
LCD_Command(0x02); /*send for initialization of LCD
for nibble (4-bit) mode */
LCD_Command(0x28); /*use 2 line and
initialize 5*8 matrix in (4-bit mode)*/
LCD_Command(0x01); /*clear display screen*/
LCD_Command(0x0c); /*display on cursor off*/
LCD_Command(0x06); /*increment cursor (shift cursor to right)*/
}

void LCD_Char(unsigned char dat)


{
ldata = (ldata & 0x0f) | (0xF0 & dat); /*Send higher nibble of data first to
PORT*/
RS = 1; /*Data Register is selected*/
EN = 1; /*High-to-low pulse on Enable pin to latch data*/
NOP();
EN = 0;
__delay_ms(1);
ldata = (ldata & 0x0f) | (dat<<4); /*Send lower nibble of data to PORT*/
EN = 1; /*High-to-low pulse on Enable pin to latch data*/
NOP();
EN = 0;
__delay_ms(3);
}
void LCD_String(const char *msg)
{
while((*msg)!=0)
{
LCD_Char(*msg);
msg++;
}
}

char SPI_Read()
{
while ( !SSPSTATbits.BF );
return(SSPBUF);
}

unsigned SPI_Ready2Read()
{
if (SSPSTAT & 0b00000001)
return 1;
else
return 0;
}

int main(void)
{
LCD_Init();
LCD_Command(0x81);
LCD_String("HUME:");
LCD_Command(0xC1);
LCD_String("TEMP:");
TRISC2 = 1;
TRISC3 = 1;
TRISC4 = 1;
TRISC5 = 0;

unsigned char DATA;


int i = 0;
int y = 0;

SSPSTAT = 0b00000000;
SSPCON = 0b00100101;
__delay_ms(50);
while(1)
{
if (SPI_Ready2Read())
{
DATA = SPI_Read();

if (i<5)
{
LCD_Command(0x87+i);
LCD_Char(DATA);
}
if (i<10 && i>4)
{
LCD_Command(0xC7+y);
LCD_Char(DATA);
y = y+1;
}
i=i+1;
if (i == 10)
{
y = 0;
i = 0;
}

}
}

You might also like