#include<avr/io.h>
#include<util/delay.h>
#include <avr/pgmspace.h>
//GLCD Module Connections
#define GLCD_CPRT PORTC
#define GLCD_CDDR DDRC
#define GLCD_CPIN PINC
#define GLCD_APIN PINA
#define GLCD_APRT PORTA
#define GLCD_ADDR DDRA
#define GLCD_BPRT PORTB
#define GLCD_BDDR DDRB
#define GLCD_BPIN PINB
//Command Port Bits
#define GLCD_CSEL1 0
#define GLCD_CSEL2 1
#define GLCD_RST 2
#define GLCD_RS 5
#define GLCD_RW 6
#define GLCD_EN 7
void delay_us(unsigned int d)
{
_delay_us(d);
}
void delay_ms(unsigned int d)
{
_delay_ms(d);
}
void Enable_Pulse (void)
{
GLCD_APRT |= (1<<GLCD_EN); //E pin high
delay_us(5);
GLCD_APRT &= ~(1<<GLCD_EN); //E pin low
delay_us(5);
}
void GLCD_ON()
{
GLCD_CDDR = 0xFF;
GLCD_APRT = 0;
GLCD_ADDR = 0xFF;
GLCD_BPRT = 0;
GLCD_BDDR = 0xFF;
GLCD_BPRT |= (1<<GLCD_CSEL1);
GLCD_BPRT |= (1<<GLCD_CSEL2);
GLCD_APRT &= ~(1<<GLCD_RS);
GLCD_APRT &= ~(1<<GLCD_RW);
GLCD_CPRT = 0x3F; //ON command
Enable_Pulse();
}
void Set_Start_Line(unsigned short line)
{
GLCD_CDDR = 0xFF;
GLCD_APRT = 0;
GLCD_ADDR = 0xFF;
GLCD_BPRT = 0;
GLCD_BDDR = 0xFF;
GLCD_APRT &= ~(1<<GLCD_RS);
GLCD_APRT &= ~(1<<GLCD_RW);
//Activate both chips
GLCD_BPRT |= (1<<GLCD_CSEL1);
GLCD_BPRT |= (1<<GLCD_CSEL2);
GLCD_CPRT = 0xC0 | line; //Set Address
Enable_Pulse();
}
void GOTO_COL(unsigned int x)
{
unsigned short Col_Data;
GLCD_CDDR = 0xFF;
GLCD_APRT = 0;
GLCD_ADDR = 0xFF;
GLCD_BPRT = 0;
GLCD_BDDR = 0xFF;
GLCD_APRT &= ~(1<<GLCD_RS);
GLCD_APRT &= ~(1<<GLCD_RW);
if(x<64) //Left Half
{
GLCD_BPRT |= (1<<GLCD_CSEL1); //Select Chip1
GLCD_BPRT &= ~(1<<GLCD_CSEL2); //Deselect Chip2
Col_Data = x;
}
else
{
GLCD_BPRT |= (1<<GLCD_CSEL2); //Select Chip2
GLCD_BPRT &= ~(1<<GLCD_CSEL1); //Deselect Chip1
Col_Data = x-64; //Put column address on data port
}
Col_Data = (Col_Data | 0x40) & 0x7F; //Command format
GLCD_CPRT = Col_Data;
Enable_Pulse();
}
void GOTO_ROW(unsigned int y)
{
unsigned short Row_Data;
GLCD_CDDR = 0xFF;
GLCD_APRT = 0;
GLCD_ADDR = 0xFF;
GLCD_BPRT = 0;
GLCD_BDDR = 0xFF;
GLCD_APRT &= ~(1<<GLCD_RS);
GLCD_APRT &= ~(1<<GLCD_RW);
Row_Data = (y | 0xB8) & 0xBF; //Put row address on data port
GLCD_CPRT = Row_Data;
Enable_Pulse();
}
void GOTO_XY(unsigned int x,unsigned int y)
{
GOTO_COL(x);
GOTO_ROW(y);
}
void GLCD_Write(unsigned short b)
{
GLCD_CDDR = 0xFF;
GLCD_APRT = 0;
GLCD_ADDR = 0xFF;
GLCD_BPRT = 0;
GLCD_BDDR = 0xFF;
GLCD_APRT |= (1<<GLCD_RS);
GLCD_APRT &= ~(1<<GLCD_RW);
GLCD_CPRT = b;
delay_us(1);
Enable_Pulse();
}
unsigned short GLCD_Read(unsigned short column)
{
unsigned short read_data = 0; //Read data here
GLCD_CDDR = 0x00;
GLCD_APRT = 0;
GLCD_ADDR = 0xFF;
GLCD_BPRT = 0;
GLCD_BDDR = 0xFF;
GLCD_APRT |= (1<<GLCD_RS);
GLCD_APRT |= (1<<GLCD_RW);
if(column>63)
GLCD_BPRT = (1<<GLCD_CSEL1);
else
GLCD_BPRT = (1<<GLCD_CSEL2); //Disable/Enable CSEL2
delay_us(1);
GLCD_APRT |= (1<<GLCD_EN); //Latch RAM data into ouput register
delay_us(1);
//Dummy read to fetch data from the display RAM
GLCD_APRT &= ~(1<<GLCD_EN); //Low Enable
delay_us(5);
GLCD_APRT |= (1<<GLCD_EN); //latch data from output register to data bus
delay_us(1);
read_data = GLCD_CPRT; //Input data
GLCD_APRT &= ~(1<<GLCD_EN); //Low Enable to remove data from the bus
delay_us(1);
GLCD_CDDR = 0xFF; //PORTC is Output again
return read_data;
}
void GLCD_ClrIn(unsigned short In)
{
int i;
GOTO_XY(0,In); //At start of line of left side
GOTO_XY(64,In); //At start of line of right side
GLCD_BPRT |= (1<<GLCD_CSEL1);
for(i=0;i<65;i++)
GLCD_Write(0);
}
void GLCD_CLR()
{
unsigned short m;
for(m=0;m<8;m++)
{
GLCD_ClrIn(m);
}
}
void Draw_Point(unsigned short x,unsigned short y, unsigned short color)
{
unsigned short Col_Data;;
GOTO_XY(x,(y/8));
switch (color)
{
case 0: //Light spot
Col_Data = ~(1<<(y%8)) & GLCD_Read(x);
break;
case 1: //Dark spot
Col_Data = (1<<(y%8)) | GLCD_Read(x);
break;
}
GOTO_XY(x,(y/8));
GLCD_Write(Col_Data);
}
int main(void)
{
unsigned short u, v;
GLCD_CDDR = 0x00;
GLCD_BDDR = 0x00;
GLCD_ADDR = 0x00;
GLCD_BPRT = 0x00;
GLCD_APRT = 0x00;
GLCD_CPRT = 0x00;
GLCD_BPRT &= ~(1<<GLCD_CSEL1); // De-Activate both chips
GLCD_BPRT &= ~(1<<GLCD_CSEL2);
GLCD_BPRT |= (1<<GLCD_RST);
GLCD_ON();
GLCD_CLR();
Set_Start_Line(0);
do
{
for(u=0; u<64; u+=6)
for(v=0; v<128; v+=2)
Draw_Point(v, u, 1);
delay_ms(1000);
GLCD_CLR();
delay_ms(1000);
}
while(1);
}