// EN: Enable
// DI: Data/Instruction
// CS: Chip Select
// RS: Reset
// RW: Read/Write
#ifndef _GLCD_CONFIG_H_
#define _GLCD_CONFIG_H_
// EN bit is complemented on the LCD side
//#define GLCD_CFG_EN_COMPLEMENTED
// DI bit is complemented on the LCD side
// By default, D is active high and I is active low
// Uncomment this line if your LCD has different pinout configuration
//#define GLCD_CFG_DI_COMPLEMENTED
// CSx bits are complemented on the LCD side
//#define GLCD_CFG_CS_COMPLEMENTED
// RS bit is complemented on the LCD side
#define GLCD_CFG_RS_COMPLEMENTED
// RW bit is complemented on the LCD side
// By default, R is active high and W is active low
// Uncomment this line if your LCD has different pinout configuration
//#define GLCD_CFG_RW_COMPLEMENTED
// EN bit
#ifdef GLCD_CFG_EN_COMPLEMENTED
#define GLCD_EN_HIGH 0
#define GLCD_EN_LOW 1
#else
#define GLCD_EN_HIGH 1
#define GLCD_EN_LOW 0
#endif
// DI bit
#ifdef GLCD_CFG_DI_COMPLEMENTED
#define GLCD_DI_HIGH 0
#define GLCD_DI_LOW 1
#else
#define GLCD_DI_HIGH 1
#define GLCD_DI_LOW 0
#endif
// CSx bits
#ifdef GLCD_CFG_CS_COMPLEMENTED
#define GLCD_CS_HIGH 0
#define GLCD_CS_LOW 1
#else
#define GLCD_CS_HIGH 1
#define GLCD_CS_LOW 0
#endif
// RS bit
#ifdef GLCD_CFG_RS_COMPLEMENTED
#define GLCD_RS_HIGH 0
#define GLCD_RS_LOW 1
#else
#define GLCD_RS_HIGH 1
#define GLCD_RS_LOW 0
#endif
// RW bit
#ifdef GLCD_CFG_RW_COMPLEMENTED
#define GLCD_RW_HIGH 0
#define GLCD_RW_LOW 1
#else
#define GLCD_RW_HIGH 1
#define GLCD_RW_LOW 0
#endif
#endif // #ifndef _GLCD_CONFIG_H_
My problem is that my program does't get trought init routine. It fall down at part clearscreen when it do this: GLCD_Write_Cmd(0x40) it go to wait_not_busy and there is fall down. i measured lcd and the values I write before. I don't know how to repair it.
I try something and I found part where is the problem. This is it:
unsigned char GLCD_Read(void){
b_GLCD_E=1;Delay();
Delay();
Wa=GLCD_Data;Delay();
blinkLed(3);
b_GLCD_E=0;Delay();
return Wa;
}
two times it go trought right but when it go third it ends in blinkLed(3); and led blink and blink and blink. Any ideas?
#include <p18f4525.h>
#include "glcd.h"
const rom unsigned char Font[96][7];
unsigned char i,XPos,YPos,Wa;
void blikLed(unsigned char led);
void plot(unsigned char x,unsigned char y){
unsigned char d;
if(x>63){
b_GLCD_GCS1=0;
b_GLCD_GCS2=1;
x-=64;
}
else
{
b_GLCD_GCS1=1;
b_GLCD_GCS2=0;
}
GLCD_Write_Cmd(0x40+x); //write column address
GLCD_Write_Cmd(0xb8+(y>>3)); //write row address
d=GLCD_Read_Data(); //dummy read
d=GLCD_Read_Data();
GLCD_Write_Cmd(0x40+x); //write column address again
d=d&(0xff-(1<<(y&7)));
GLCD_Write_Data(d);
}
void hline(unsigned char x,unsigned char y1,unsigned char y2){
for(i=y1;i<y2;i++)
plot(x,i);
}
void vline(unsigned char x1,unsigned char x2,unsigned char y){
for(i=x1;i<x2;i++)
plot(i,y);
}
void box(unsigned char x1,unsigned char y1,
unsigned char x2,unsigned char y2){
vline(x1,x2,y1);
vline(x1,x2,y2);
hline(x1,y1,y2);
hline(x2,y1,y2);
}
void Delay(void){
_asm NOP _endasm
_asm NOP _endasm
_asm NOP _endasm
_asm NOP _endasm
_asm NOP _endasm
_asm NOP _endasm
_asm NOP _endasm
_asm NOP _endasm
_asm NOP _endasm
_asm NOP _endasm
}
void Wait_Not_Busy(void){
TRIS_Data=0xff;
b_GLCD_RS=0;
b_GLCD_RW=1;
if (b_GLCD_GCS1==1 && b_GLCD_GCS2==1){
b_GLCD_GCS1=0;
while (GLCD_Read()&0x80);
b_GLCD_GCS1=1;
b_GLCD_GCS2=0;
while (GLCD_Read()&0x80);
b_GLCD_GCS2=1;
}
else{
while (GLCD_Read()&0x80);
}
TRIS_Data=0x00;
}
unsigned char GLCD_Read(void){
blikLed(3);
Delay10KTCYx(0);
Delay10KTCYx(0);
Delay10KTCYx(0);
b_GLCD_E=1;
Delay();
Wa=GLCD_Data;
b_GLCD_E=0;Delay();
return Wa;
}
void GLCD_Write_Cmd(unsigned char data){
Wait_Not_Busy();
GLCD_Data=data;
b_GLCD_RS=0;
b_GLCD_RW=0;
b_GLCD_E=1;
Delay();
Delay();
b_GLCD_E=0;Delay();
}
void GLCD_Write_Data (unsigned char data){
Wait_Not_Busy();
GLCD_Data=data;
b_GLCD_RS=1;
b_GLCD_RW=0;
b_GLCD_E=1;Delay();
Delay();Delay();
b_GLCD_E=0;
}
void MoveRight(void){
if(++XPos==64){
WritePosition();
}
if(XPos==128){
XPos=0;
YPos+=8;
YPos=YPos&0x3f;
WritePosition();
}
}
void WritePosition(void){
if(XPos>63){
b_GLCD_GCS1=0;
b_GLCD_GCS2=1;
}
else{
b_GLCD_GCS1=1;
b_GLCD_GCS2=0;
}
GLCD_Write_Cmd(0x40+(XPos&0x3f)); //column=0
GLCD_Write_Cmd(0xb8+((YPos&0x3f)>>3)); //row=0
}
unsigned char GLCD_Read_Data(void){
Wait_Not_Busy();
TRIS_Data=0xff;
b_GLCD_RS=1;
b_GLCD_RW=1;
b_GLCD_E=1;
Delay();
Wa=GLCD_Data;
b_GLCD_E=0;
TRIS_Data=0x00;
return Wa;
}
void ClearScreen(void){
unsigned char m,n;
b_GLCD_GCS1=1;
b_GLCD_GCS2=1;
for(m=0;m<0x08;m++){
GLCD_Write_Cmd(0x40);Delay(); //y=0
GLCD_Write_Cmd(0xb8+m);Delay(); //x=0
for(n=0;n<0x40;n++){
GLCD_Write_Data(0x00);Delay();
}
}
SetPos(0,0);Delay();
}
void Init_GLCD(void){
b_TRIS_GCS1=0;
b_TRIS_GCS2=0;
b_TRIS_RS=0;
b_TRIS_RW=0;
b_TRIS_E=0;
b_TRIS_On=0;
b_GLCD_On=1;
b_GLCD_GCS1=1;
b_GLCD_GCS2=1;
GLCD_Write_Cmd(0x3f); //display on
blikLed(1);
GLCD_Write_Cmd(0xc0); //z=0
blikLed(2);
ClearScreen();
}
void PutChar(unsigned char data){
unsigned char i,d;
if(data<32){
switch(data){
case 13:
XPos=0;
case 10:
XPos=0;
YPos+=8;
YPos=YPos&63;
}
WritePosition();
}
else{
for(i=0;i<7;i++){
d=Font[data-32];
if(d!=0x55){
GLCD_Write_Data(d);
MoveRight();
}
}
GLCD_Write_Data(0xff);
MoveRight();
}
}
void PutMessage(static char rom *Message){
while(*Message!=0)
if(*Message==0x16){
*Message++;
XPos=*Message++;
YPos=*Message++;
WritePosition();
}
else
PutChar(*Message++);
}
void PutLogo(static char rom *logo){
unsigned char w,h,bitcount,Byte;
w=*logo++;
h=*logo++;
bitcount=0;
do{
for(i=0;i<w;i++){
if(bitcount==0){
bitcount=8;
Byte=*logo++;
}
if(Byte&1) plot(XPos,YPos);
XPos++;
Byte/=2;
bitcount--;
}
YPos++;
XPos-=w;
}while(--h);
}
void SetPos(unsigned char x,unsigned char y){
XPos=x;
YPos=y;
WritePosition();
}
void blikLed(unsigned char led)
{
int j; for (j=0; j<2; j++)
{
switch (led)
{
case 0:
LATAbits.LATA0 = 1;
Delay10KTCYx(0);
LATAbits.LATA0 = 0;
Delay10KTCYx(0);
break;
case 1:
LATAbits.LATA1 = 1;
Delay10KTCYx(0);
LATAbits.LATA1 = 0;
Delay10KTCYx(0);
break;
case 2:
LATAbits.LATA2 = 1;
Delay10KTCYx(0);
LATAbits.LATA2 = 0;
Delay10KTCYx(0);
break;
case 3:
LATAbits.LATA3 = 1;
Delay10KTCYx(0);
LATAbits.LATA3 = 0;
Delay10KTCYx(0);
break;
case 4:
LATAbits.LATA4 = 1;
Delay10KTCYx(0);
LATAbits.LATA4 = 0;
Delay10KTCYx(0);
break;
default:
LATAbits.LATA0 = 0;
LATAbits.LATA1 = 0;
LATAbits.LATA2 = 0;
LATAbits.LATA3 = 0;
LATAbits.LATA4 = 0;
break;
}
}
}
void glcd_DrawLine(unsigned char x1, unsigned char y1, unsigned char x2, unsigned char y2)
{
// y = ((y2 - y1) / (x2 - x1)) * (x - x1) + y1
float m = (float) (y2 - y1) / (float) (x2 - x1);
for(glcdI=x1; glcdI<=x2; glcdI++) glcd_Plot(glcdI, (unsigned char) (m * (glcdI - x1) + y1));
}
void glcd_DrawCircle(unsigned char x, unsigned char y, unsigned char r)
{
// y = y0 - sqrt(r^2 - (x0 - x)^2)
// y = y0 + sqrt(r^2 - (x0 - x)^2)
unsigned char rt; // The root term
signed char i, j;
for (i=x-r; i<=x+r; i++)
{
rt = (signed char) sqrt((float) r*r - (float) (x-i)*(x-i));
if ((i < 0) || (i > 127)) continue;
j = y - rt;
if (!((j > 63) || (j < 0))) glcd_Plot((unsigned char) i, (unsigned char) j);
j = y + rt;
if (!((j > 63) || (j < 0))) glcd_Plot((unsigned char) i, (unsigned char) j);
}
}
Sub DrawCircle(CX, CY, R)
x = 0
y = R
p = -R / 2
Plot8 CX, CY, x, y
Do While x < y
x = x + 1
If p < 0 Then
p = p + 2 * x + 1
Else
y = y - 1
p = p + 2 * (x - y) + 1
End If
Plot8 CX, CY, x, y
Loop
End Sub
Sub Plot8(x, y, dx, dy)
PlotPixel x + dx, y + dy, vbRed
PlotPixel x + dx, y - dy, vbRed
PlotPixel x - dx, y + dy, vbRed
PlotPixel x - dx, y - dy, vbRed
PlotPixel x + dy, y + dx, vbRed
PlotPixel x + dy, y - dx, vbRed
PlotPixel x - dy, y + dx, vbRed
PlotPixel x - dy, y - dx, vbRed
End Sub
void Plot8(unsigned char x,unsigned char y,unsigned char dx,unsigned char dy){
plot(x + dx, y + dy);
plot(x + dx, y - dy);
plot(x - dx, y + dy);
plot(x - dx, y - dy);
plot(x + dy, y + dx);
plot(x + dy, y - dx);
plot(x - dy, y + dx);
plot(x - dy, y - dx);
}
void PutCircle(unsigned char CX,unsigned char CY,unsigned char R){
unsigned char y,x;
signed char p;
x=0;
y=R;
p= -(R/2);
Plot8(CX,CY,x,y);
while(x<y){
x++;
if(p<0)
p=p+2*x+1;
else{
y--;
p=p+2*(x-y)+1;
}
Plot8(CX, CY, x, y);
}
}
I added the code to the plot routine to exclude off screen pixels and you can now plot on the edge of the screen. This is what PutCircle(50,50,20); does. I think it looks pretty good for an integer maths circle.
Mike.
void PutMessage(static char rom *Message){
Error - could not find file 'c018i.o'.
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?