/*
* File: graphics_LCD.c
* Author: thunai
*
* Created on October 22, 2023, 2:37 PM
*/
#include <stdio.h>
#include <stdlib.h>
#include <xc.h>
#include "glcd.h"
// CONFIG
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = OFF // Brown-out Reset Enable bit (BOR disabled)
#pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
#ifndef _XTAL_FREQ
#define _XTAL_FREQ 20000000
#endif
#define GlcdDataBus PORTB
#define GlcdControlBus PORTD
#define GlcdDataBusDirnReg TRISB
#define GlcdCtrlBusDirnReg TRISD
#define RS 0
#define RW 1
#define EN 2
#define CS1 3
#define CS2 4
/* 5x7 Font including 1 space to display HELLO WORLD */
char H[]={0x7F, 0x08, 0x08, 0x08, 0x7F, 0x00};
char E[]={0x7F, 0x49, 0x49, 0x49, 0x41, 0x00};
char L[]={0x7F, 0x40, 0x40, 0x40, 0x40, 0x00};
char O[]={0x3E, 0x41, 0x41, 0x41, 0x3E, 0x00};
char W[]={0x3F, 0x40, 0x38, 0x40, 0x3F, 0x00};
char R[]={0x7F, 0x09, 0x19, 0x29, 0x46, 0x00};
char D[]={0x7F, 0x41, 0x41, 0x22, 0x1C, 0x00};
/* local function to generate delay */
void delay(int cnt)
{
int i;
for(i=0;i<cnt;i++);
}
void Glcd_SelectPage0() // CS1=1, CS2=0
{
GlcdControlBus |= (1<<CS1);
GlcdControlBus &= ~(1<<CS2);
}
void Glcd_SelectPage1() // CS1=0, CS1=1
{
GlcdControlBus &= ~(1<<CS1);
GlcdControlBus |= (1<<CS2);
}
/* Function to send the command to LCD */
void Glcd_CmdWrite(char cmd)
{
GlcdDataBus = cmd; //Send the Command nibble
GlcdControlBus &= ~(1<<RS); // Send LOW pulse on RS pin for selecting Command register
GlcdControlBus &= ~(1<<RW); // Send LOW pulse on RW pin for Write operation
GlcdControlBus |= (1<<EN); // Generate a High-to-low pulse on EN pin
delay(100);
GlcdControlBus &= ~(1<<EN);
delay(5000);
}
/* Function to send the Data to LCD */
void Glcd_DataWrite(char dat)
{
GlcdDataBus = dat; //Send the data on DataBus nibble
GlcdControlBus |= (1<<RS); // Send HIGH pulse on RS pin for selecting data register
GlcdControlBus &= ~(1<<RW); // Send LOW pulse on RW pin for Write operation
GlcdControlBus |= (1<<EN); // Generate a High-to-low pulse on EN pin
delay(100);
GlcdControlBus &= ~(1<<EN);
delay(5000);
}
void Glcd_DisplayChar(char *ptr_array)
{
int i;
for(i=0;i<6;i++) // 5x7 font, 5 chars + 1 blankspace
Glcd_DataWrite(ptr_array);
}
int main()
{
GlcdDataBusDirnReg = 0x00; // Configure all the LCD pins as output
GlcdCtrlBusDirnReg = 0x00; // Configure the Ctrl pins as output
/* Select the Page0/Page1 and Turn on the GLCD */
Glcd_SelectPage0();
Glcd_CmdWrite(0x3f);
Glcd_SelectPage1();
Glcd_CmdWrite(0x3f);
delay(1000);
/* Select the Page0/Page1 and Enable the GLCD */
Glcd_SelectPage0();
Glcd_CmdWrite(0xc0);
Glcd_SelectPage1();
Glcd_CmdWrite(0xc0);
delay(1000);
Glcd_SelectPage0(); // Display HELLO on Page0, Line1
delay(1000);
Glcd_CmdWrite(0xb8);
delay(1000);
Glcd_DisplayChar(H);
Glcd_DisplayChar(E);
Glcd_DisplayChar(L);
Glcd_DisplayChar(L);
Glcd_DisplayChar(O);
Glcd_SelectPage1(); // Display WORLD on Page1, Last line
delay(1000);
Glcd_CmdWrite(0xbF);
delay(1000);
Glcd_DisplayChar(W);
Glcd_DisplayChar(O);
Glcd_DisplayChar(R);
Glcd_DisplayChar(L);
Glcd_DisplayChar(D);
while(1);
}
//#include "delay.h"
//#define DELAY_TIME 3
//void main()
//{
// int num = 1234;
// GLCD_Init();
//
// while (1)
// {
// GLCD_Clear();
// GLCD_DisplayLogo(LogoBitMap);
// //DELAY_sec(DELAY_TIME);
// __delay_ms(5000);
// GLCD_Clear();
// GLCD_Printf("Dec:%d \nHex:%x \nBin:%b \nFloat:%f", num, num, num, 4567.89);
// //DELAY_sec(DELAY_TIME);
//
// GLCD_Clear();
// GLCD_HorizontalGraph(0, 45);
// GLCD_HorizontalGraph(1, 50);
// GLCD_HorizontalGraph(2, 82);
// GLCD_HorizontalGraph(3, 74);
// //DELAY_sec(DELAY_TIME);
// __delay_ms(5000);
// GLCD_Clear();
// GLCD_VerticalGraph(0, 45);
// GLCD_VerticalGraph(1, 50);
// GLCD_VerticalGraph(2, 82);
// GLCD_VerticalGraph(3, 74);
// // DELAY_sec(DELAY_TIME);
// __delay_ms(5000);
// }
//}