Skip to content

Instantly share code, notes, and snippets.

@bsantraigi
Last active August 29, 2015 14:02
Show Gist options
  • Save bsantraigi/737ee5d1ec4b474b9ee2 to your computer and use it in GitHub Desktop.
Save bsantraigi/737ee5d1ec4b474b9ee2 to your computer and use it in GitHub Desktop.
/*
* GLCD.c
*
* Created: 5/8/2014 10:41:05 AM
* Author: Bishal Santra
*/
#define F_CPU 1000000UL
#include <avr/io.h>
#include <stdio.h>
#include <stdlib.h>
#include <util/delay.h>
#include <stdbool.h>
#include <avr/pgmspace.h>
#include <string.h>
#include "GLCD.h"
#include "character_map.h"
#define RS PORTD4
#define RW PORTD5
#define EN PORTD6
#define CS1 PORTD1
#define CS2 PORTD2
#define RESET PORTD0
#define GLCD_DATA PORTB
#define CONTROL_PORT PORTD
#define DDR_GLCD_DATA DDRB
#define DDR_CONTROL DDRD
volatile uint8_t CURRENT_X = 0;
volatile uint8_t CURRENT_Y = 0;
volatile uint8_t CURRENT_Z = 0;
void Enable_signal(){
_delay_us(10);
CONTROL_PORT |= (1<<EN);
_delay_us(10);
CONTROL_PORT &= ~(1<<EN);
_delay_us(100);
}
void init_GLCD()
{
DDR_GLCD_DATA = 255;
DDR_CONTROL = 255;
//display on
unsigned char cmd = (0<<RS)|(0<<RW)|(1<<CS1)|(1<<CS2);
CONTROL_PORT = cmd;
unsigned char data = (0<<7)|(0<<6)|(1<<5)|(1<<4)|(1<<3)|(1<<2)|(1<<1)|(1<<0);
GLCD_DATA = data;
Enable_signal();
}
void select_chip(int _CS){
switch(_CS){
case 1:
CONTROL_PORT |= (1<<CS1);
CONTROL_PORT &= ~(1<<CS2);
break;
case 2:
CONTROL_PORT |= (1<<CS2);
CONTROL_PORT &= ~(1<<CS1);
break;
}
}
void turn_off_GLCD(){
GLCD_send_cmd(0x3E);
Enable_signal();
}
void set_top_display_line(int _CS,int disp_line){
unsigned char cmd;
select_chip(_CS);
if(disp_line<64){
//start line
cmd = (1<<PORTB7)|(1<<PORTB6)|(disp_line);
GLCD_send_cmd(cmd);
Enable_signal();
}
}
void goto_line(int x){
unsigned char cmd;
if (x<8)
{
//set row
cmd = (1<<PORTB7)|(0<<PORTB6)|(1<<PORTB5)|(1<<PORTB4)|(1<<PORTB3)|(x);
GLCD_send_cmd(cmd);
Enable_signal();
set_position_GLCD(0);
CURRENT_X = x;
}else{
cmd = (1<<PORTB7)|(0<<PORTB6)|(1<<PORTB5)|(1<<PORTB4)|(1<<PORTB3)|(0);
CURRENT_X = 0;
GLCD_send_cmd(cmd);
Enable_signal();
set_position_GLCD(0);
}
}
void goto_next_line(){
CURRENT_X++;
CURRENT_Y = 0;
goto_line(CURRENT_X);
}
void set_position_GLCD(int y){
// Moves the cursor to position y horizontally
unsigned char cmd;
if (y<64)
{
select_chip(1);
//set col
cmd = (0<<PORTB7)|(1<<PORTB6)|(y);
GLCD_send_cmd(cmd);
Enable_signal();
}
else if (y>=64 && y<128)
{
select_chip(2);
//set col
cmd = (0<<PORTB7)|(1<<PORTB6)|(y-64);
GLCD_send_cmd(cmd);
Enable_signal();
}
return;
}
void GLCD_print_string(char string[]){
int length = strlen(string);
int count = 0;
while(count<length){
GLCD_print_char((unsigned char)string[count++]);
}
}
void GLCD_print_byte(unsigned char data_byte){
GLCD_DATA = data_byte;
CONTROL_PORT |= (1<<RS);
CONTROL_PORT &= ~(1<<RW);
Enable_signal();
return;
}
void write_on_chip2()
{
CURRENT_Y = 64;
select_chip(2);
goto_line(CURRENT_X);
set_position_GLCD(CURRENT_Y);
}
void write_on_chip1()
{
CURRENT_Y = 0;
select_chip(1);
goto_line(++CURRENT_X);
set_position_GLCD(CURRENT_Y);
}
void GLCD_print_char(unsigned char data){
int address = ((uint8_t)data) - 32;
//for the subtraction constant refer to character_map.h, it is the ascii code
//of the first character present in the character list
if ((CURRENT_Y+5)<=63)
{
GLCD_print_byte(pgm_read_byte(&Arial12[address][0]));
GLCD_print_byte(pgm_read_byte(&Arial12[address][1]));
GLCD_print_byte(pgm_read_byte(&Arial12[address][2]));
GLCD_print_byte(pgm_read_byte(&Arial12[address][3]));
GLCD_print_byte(pgm_read_byte(&Arial12[address][4]));
GLCD_print_byte(0);
CURRENT_Y += 6;
}else if ((CURRENT_Y+5)>63 && CURRENT_Y<=63)
{
int available_bytes = 63-CURRENT_Y+1;
switch(available_bytes){
case 5:
GLCD_print_byte(pgm_read_byte(&Arial12[address][0]));
GLCD_print_byte(pgm_read_byte(&Arial12[address][1]));
GLCD_print_byte(pgm_read_byte(&Arial12[address][2]));
GLCD_print_byte(pgm_read_byte(&Arial12[address][3]));
GLCD_print_byte(pgm_read_byte(&Arial12[address][4]));
write_on_chip2();
GLCD_print_byte(0);
CURRENT_Y+=1;
break;
case 4:
GLCD_print_byte(pgm_read_byte(&Arial12[address][0]));
GLCD_print_byte(pgm_read_byte(&Arial12[address][1]));
GLCD_print_byte(pgm_read_byte(&Arial12[address][2]));
GLCD_print_byte(pgm_read_byte(&Arial12[address][3]));
write_on_chip2();
GLCD_print_byte(pgm_read_byte(&Arial12[address][4]));
GLCD_print_byte(0);
CURRENT_Y+=2;
break;
case 3:
GLCD_print_byte(pgm_read_byte(&Arial12[address][0]));
GLCD_print_byte(pgm_read_byte(&Arial12[address][1]));
GLCD_print_byte(pgm_read_byte(&Arial12[address][2]));
write_on_chip2();
GLCD_print_byte(pgm_read_byte(&Arial12[address][3]));
GLCD_print_byte(pgm_read_byte(&Arial12[address][4]));
GLCD_print_byte(0);
CURRENT_Y+=3;
break;
case 2:
GLCD_print_byte(pgm_read_byte(&Arial12[address][0]));
GLCD_print_byte(pgm_read_byte(&Arial12[address][1]));
write_on_chip2();
GLCD_print_byte(pgm_read_byte(&Arial12[address][2]));
GLCD_print_byte(pgm_read_byte(&Arial12[address][3]));
GLCD_print_byte(pgm_read_byte(&Arial12[address][4]));
GLCD_print_byte(0);
CURRENT_Y+=4;
break;
case 1:
GLCD_print_byte(pgm_read_byte(&Arial12[address][0]));
write_on_chip2();
GLCD_print_byte(pgm_read_byte(&Arial12[address][1]));
GLCD_print_byte(pgm_read_byte(&Arial12[address][2]));
GLCD_print_byte(pgm_read_byte(&Arial12[address][3]));
GLCD_print_byte(pgm_read_byte(&Arial12[address][4]));
GLCD_print_byte(0);
CURRENT_Y+=5;
break;
}
}else if ((CURRENT_Y+5)<=127)
{
GLCD_print_byte(pgm_read_byte(&Arial12[address][0]));
GLCD_print_byte(pgm_read_byte(&Arial12[address][1]));
GLCD_print_byte(pgm_read_byte(&Arial12[address][2]));
GLCD_print_byte(pgm_read_byte(&Arial12[address][3]));
GLCD_print_byte(pgm_read_byte(&Arial12[address][4]));
GLCD_print_byte(0);
CURRENT_Y += 6;
}else if ((CURRENT_Y+5)>127 && CURRENT_Y<=127)
{
int available_bytes = 127-CURRENT_Y+1;
switch(available_bytes){
case 5:
GLCD_print_byte(pgm_read_byte(&Arial12[address][0]));
GLCD_print_byte(pgm_read_byte(&Arial12[address][1]));
GLCD_print_byte(pgm_read_byte(&Arial12[address][2]));
GLCD_print_byte(pgm_read_byte(&Arial12[address][3]));
GLCD_print_byte(pgm_read_byte(&Arial12[address][4]));
write_on_chip1();
GLCD_print_byte(0);
CURRENT_Y+=1;
break;
default:
write_on_chip1();
GLCD_print_byte(pgm_read_byte(&Arial12[address][0]));
GLCD_print_byte(pgm_read_byte(&Arial12[address][1]));
GLCD_print_byte(pgm_read_byte(&Arial12[address][2]));
GLCD_print_byte(pgm_read_byte(&Arial12[address][3]));
GLCD_print_byte(pgm_read_byte(&Arial12[address][4]));
GLCD_print_byte(0);
CURRENT_Y += 6;
}
}
}
void GLCD_put_space(){
GLCD_print_byte(0);
GLCD_print_byte(0);
GLCD_print_byte(0);
GLCD_print_byte(0);
}
void GLCD_send_cmd(unsigned char cmd){
CONTROL_PORT &= ~((1<<RS)|(1<<RW));
GLCD_DATA = cmd;
Enable_signal();
return;
}
void GLCD_clear_screen(){
set_position_GLCD(0);
goto_line(0);
for (int i=0;i<512;i++)
{
GLCD_print_byte(0);
}
set_position_GLCD(64);
goto_line(0);
for (int i=0;i<512;i++)
{
GLCD_print_byte(0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment