#include "tempo.h"
// Definition des ports A & B
#define DIR_DATA TRISC
#define DATA PORTC
#define DIR_CTRL TRISB
#define CTRL PORTB
#define WRITE RB0
#define READ RB1
#define CHIP_ENABLED RB2
#define DATA_or_CODE RB3
#define RESET RB4
#define BACKLIGHT RB5
#define ON 1
#define OFF 0
// Definition des fonctions elementaires
#define NOP asm("nop()");
// [------------------- Pointer Set -------------------]
#define Cursor_Pointer_Set PutCode(0x21);
#define Offset_Register_Set PutCode(0x22);
#define Address_Pointer_Set PutCode(0x24);
// [------------------- Control Word -------------------]
#define Text_Home_Address_Set PutCode(0x40);
#define Text_Home_Address_High 0x20
#define Text_Home_Address_Low 0x00
#define Text_Area_Set PutCode(0x41);
#define Text_Area_High 0x00
#define Text_Area_Low 0x14
#define Graphic_Home_Address_Set PutCode(0x42);
#define Graphic_Home_Address_High 0x00
#define Graphic_Home_Address_Low 0x00
#define Graphic_Area_Set PutCode(0x43);
#define Graphic_Area_High 0x00
#define Graphic_Area_Low 0x14
// [------------------- Mode Set -------------------]
#define OR_Rom PutCode(0x80);
#define OR_Ram PutCode(0x88);
#define EXOR_Rom PutCode(0x81);
#define EXOR_Ram PutCode(0x89);
#define AND_Rom PutCode(0x83);
#define AND_Ram PutCode(0x8B);
#define Text_Rom PutCode(0x84);
#define Text_Ram PutCode(0x8C);
// [------------------- Lecture Ecran -------------------]
#define Screen_Peeking PutCode(0xE0);
#define Screen_Copy PutCode(0xE8);
#define DataWriteIncrement PutCode(0xC0);
#define DataReadIncrement PutCode(0xC1);
#define DataWriteDecrement PutCode(0xC2);
#define DataReadDecrement PutCode(0xC3);
#define DataWriteUnchange PutCode(0xC4);
#define DataReadUnchange PutCode(0xC5);
// Definition des variables
int BufferRead;
int Immobile, SensDown, Read;
void Short_Delay (void)
{
char count;
count = 3;
while (--count); /* delay */
}
void LcdReset(void)
{
RESET = 0;
Tempo_ms(50);
RESET = 1;
}
void GetData(char Val)
{
RA0=1;
DIR_DATA = 0xFF;
DATA_or_CODE = 0;
WRITE = 1; READ = 0;
CHIP_ENABLED = 0;
Val = DATA;
Tempo_ms(1);
CHIP_ENABLED = 1;
WRITE = 1; READ = 1;
RA0=0;
}
int LcdBusy(void)
{
RA0=1;
DIR_DATA = 0xFF;
DATA_or_CODE = 1;
WRITE = 1; READ = 0;
CHIP_ENABLED = 0;
BufferRead = DATA;
Tempo_ms(1);
CHIP_ENABLED = 1;
WRITE = 1; READ = 1;
RA0=0;
return BufferRead;
}
void PutCode(unsigned char Val)
{
while((0x03 & LcdBusy()) != 0x03);
RA2=1;
DIR_DATA = 0x00;
DATA = Val;
DATA_or_CODE = 1;
WRITE = 0; READ = 1;
CHIP_ENABLED = 0;
Tempo_ms(1);
CHIP_ENABLED = 1;
WRITE = 1; READ = 1;
RA2=0;
}
void PutData(unsigned char Val)
{
while((0x03 & LcdBusy()) != 0x03);
RA2=1;
DIR_DATA = 0x00;
DATA = Val;
DATA_or_CODE = 0;
WRITE = 0; READ = 1;
CHIP_ENABLED = 0;
Tempo_ms(1);
CHIP_ENABLED = 1;
WRITE = 1; READ = 1;
RA2=0;
}
void MoveData(int Immo, int SDown, int ReadAct)
{
Immobile = Immo;
SensDown = SDown;
Read = ReadAct;
}
void DisplayModes(int Graphics, int Text, int Cursor, int CursorBlink)
{
PutCode(0x90 + Graphics * 8 + Text * 4 + Cursor * 2 + CursorBlink);
}
void CursorPattern(int NbrLigne)
{
PutCode(0xA0 + NbrLigne - 1);
}
void ChangeBit(int Set, int Position)
{
PutCode(0xF0 + Set * 8 + Position);
}
void SendCar(char Car)
{
char CarT6963C;
CarT6963C = Car - 0x20;
PutData(CarT6963C);
DataWriteIncrement;
}
void SetAtHomeAddressMemory(void)
{
PutData(0x00);
PutData(0x00);
Address_Pointer_Set;
}
void SetAtHomeAddressTxt(void)
{
PutData(Text_Home_Address_Low) ;
PutData(Text_Home_Address_High );
Address_Pointer_Set;
}
void SetAtHomeAddressGraph(void)
{
PutData(Graphic_Home_Address_L ow);
PutData(Graphic_Home_Address_H igh);
Address_Pointer_Set;
}
void SetAtHomeCursorMemory(void)
{
PutData(0x00);
PutData(0x00);
Cursor_Pointer_Set;
}
void SetAtHomeCursorTxt(void)
{
PutData(Text_Home_Address_Low) ;
PutData(Text_Home_Address_High );
Cursor_Pointer_Set;
}
void SetAtHomeCursorGraph(void)
{
PutData(Graphic_Home_Address_L ow);
PutData(Graphic_Home_Address_H igh);
Cursor_Pointer_Set;
}
void ClearScreen(void)
{
int i;
SetAtHomeAddressMemory();
PutCode(0xB0);
for(i = 0; i < 0x2000; i++) PutData(0xFF);
PutCode(0xB2);
SetAtHomeAddressMemory();
}
void LcdInit(void)
{
DIR_DATA = 0x00;
DATA = 0;
DIR_CTRL = 0x00;
RESET = 1;
WRITE = 1;
READ = 1;
CHIP_ENABLED = 1;
BACKLIGHT = OFF;
LcdReset();
MoveData(0,0,0);
AND_Rom;
PutData(Graphic_Home_Address_L ow); // Graphic Home address
PutData(Graphic_Home_Address_H igh);
Graphic_Home_Address_Set; // 0x42
PutData(Graphic_Area_Low); // Graphic Area set
PutData(Graphic_Area_High);
Graphic_Area_Set; // 0x43
PutData(Text_Home_Address_Low) ; // Text Home address
PutData(Text_Home_Address_High );
Text_Home_Address_Set; // 0x40
PutData(Text_Area_Low); // Text Area set
PutData(Text_Area_High);
Text_Area_Set; // 0x41
PutData(Text_Home_Address_Low) ;
PutData(Text_Home_Address_High );
Address_Pointer_Set;
PutData(Text_Home_Address_Low) ;
PutData(Text_Home_Address_High );
Cursor_Pointer_Set;
}