 |
 |
| View previous topic :: View next topic |
| Author |
Message |
rovtech
Joined: 24 Sep 2006 Posts: 288
|
| The code so far |
Posted: Tue Apr 10, 2012 7:44 am |
|
|
Here is the code so far. The functions_2.c is adapted from code I found at
China-Hacker however the link to its origin is broken so I cannot give credit
to the original author, but thank you.
I have noted what works and what doesn't. Hopefully someone out there is interested
in using one of these displays and can contribute some help.
| Code: | //////////////////////////////////////////////////////////////////////////////
// Graph_pb_1.C //
// This program is to test the Digole HJ12864ZW display board that uses an //
// ST7920 controller/driver. The display is 128x64, contains character //
// generators, and is backlit. //
// The PIC16F722 connections are: //
// RC0 RS 0 select instr register (write), or busy flag (read) //
// 1 select data register (write/read) //
// RC1 R/W 0 = write, 1 = read //
// RC2 En 1 = enable trigger //
// RC3 not used //
// RC4 not used //
// RC5 not used //
// RC6 not used //
// RC7 busy flag //
// For now use 8 bits on port B for the data //
// InitLcd must be called before any other function //
// Partly working 10 April 2012 //
//////////////////////////////////////////////////////////////////////////////
// Start by verifying routines used in <functions_2.c> //
//////////////////////////////////////////////////////////////////////////////
#include <16F722.h>
#fuses INTRC_IO, NOWDT, PUT, NOPROTECT, BROWNOUT, MCLR
#use delay (clock = 2 MHZ)
#include <functions_2.c>
// *******************
// Main Function
void main()
{
InitLcd (); // Initialize GLCD
printf (WriteData, "Hello World 123456789abcdefghijklmnop, \n, \f, qrstuvw");
delay_ms (1000);
// Everything works up to here. The string is displayed on line 1, continued
// on line 3, then line 2, then line 4. The \n and \f cause strange characters.
// The next line does nothing but clear the screen after 1 second.
welcome (); // display welcome message
delay_ms (1000);
InitLcd (); // Initialize GLCD, flash chinese characters
printf (WriteData, "Try again"); // OK, so no hang in 'welcome()'
} |
Here is the functions_2.c file:
| Code: | // These work to define the ports and pins on the PIC.
#byte portA = 5
#byte portB = 6
#bit RB0 = PORTB.0
#bit RB1 = PORTB.1
#bit RB2 = PORTB.2
#bit RB3 = PORTB.3
#bit RB4 = PORTB.4
#bit RB5 = PORTB.5
#bit RB6 = PORTB.6
#bit RB7 = PORTB.7
#byte portC = 7
#bit RC0 = PORTC.0
#bit RC1 = PORTC.1
#bit RC2 = PORTC.2
#bit RC3 = PORTC.3
#bit RC4 = PORTC.4
#bit RC5 = PORTC.5
#bit RC6 = PORTC.6
#bit RC7 = PORTC.7
#define DATA portB // data I/O port
#define BUSY RB7 // busy flag
#define RS RC0 // register select, 0=instr, 1=data
#define RW RC1 // 0=write, 1=read
#define EN RC2 // enable trig on -ve edge
#define RST RC3 // reset
// I did not use all of these but they should be correct by now
#define SET_NC 0x02 //set the cursor to start but no clear the screen
#define SET_C 0x01 //set the addres to start and clear the screen
#define CLE 0x00 //clear the screen
#define CUR_ON 0x0f //turn on the cursor to flash
#define CUR_OFF 0x0c //turn off the cursor
// This may be a problem. It seems to be a pointer to addresses in CGROM
// 0x80 is where a welcome message is located (Sitronix ST7920).
// 0x88 is blank, I don't know about the others.
// I can't find type 'rom char' documented anywhere.
rom char *position_lcd = {0x80, 0x90, 0x88, 0x98};
// Wait for busy flag to clear
// working
void wait_for_busy(void)
{
set_tris_b (0xff); // all inputs
set_tris_c (0x00); // all outputs
RS = 0;
RW = 1;
EN = 1;
while(BUSY); // wait while busy set
// delay_us (70); // not needed
EN = 0;
set_tris_b (0x00);
}
// write a command to the instruction register RS=0
// working
void WriteCmd(char ch)
{
wait_for_busy();
delay_ms(2);
EN = 0;
RS = 0; // instruction register
RW = 0; // set to write
delay_us (70);
EN = 1;
DATA = ch; // place on portB
delay_us (70);
EN = 0; // this loads the instruction register
}
// writes to the data register RS=1
// working
void WriteData(char ch)
{
wait_for_busy();
delay_ms(2);
EN = 0;
RS = 1; // data register
RW = 0; // set to write
delay_us (70);
EN = 1;
DATA = ch; // place on portB
delay_us (70);
EN = 0; // this loads the data register
RS = 0;
}
// Initialize GLCD
// working
void InitLcd(void)
{
set_tris_c (0x00);
RST=0; // reset low, pin 17
delay_ms(5); // for 5 ms
RST=1; // done reset
delay_ms(10); // allow to settle
WriteCmd(0x30); // function 8 bit basic
delay_us (120); // spec > 100 us
WriteCmd(0x30); // twice
delay_us (50); // spec > 37 us
WriteCmd(0x0f); // display on, cursor on & blink
delay_us (120); // spec > 100 us
WriteCmd(0x01); // display clear
delay_ms (15); // spec > 10 ms
WriteCmd(0x02); // return home
delay_us (100); // spec > 72 us
WriteCmd(0x80); // DDRAM = 0
delay_ms (10); // spec > 72 us
// tried this entry mode but last digit is one flashing, not next space ??
WriteCmd (0x06); // entry mode, shift cursor right & increment AC
delay_us (100); // spec > 72 us,
}
// I don't know if this works. What is DBC?
// function: set the address for DBC
// e.g: 1,1 1,2 1,7 1,8
// 2,1 2,6 2,8
// 3,1 3,3 3,8
// 4,1 4,4 4,5 4,8
void set_address (char row, char column)
{
WriteCmd(position_lcd[row]+column);
}
// I don't know if this works or the intent.
// Where is 'ch' ?
// function: write ch on the postion of row and column
void write_dbc(char row, char column, char *ch)
{
char c,pi=0;
WriteCmd(position_lcd[row]+column);
while(1)
{
c = ch[pi++];
if ( !c )
break;
WriteData(c);
}
}
// Im not sure if this works
// function: set the address for SBC
// e.g: 1,1 1,2 1,7 1,8
// 2,1 2,6 2,8
// 3,1 3,3 3,8
// 4,1 4,4 4,5 4,8
void display(char x_add, char y_add, char dat1, char dat2)
{
WriteCmd(position_lcd[x_add]+y_add);
WriteData(dat1);
WriteData(dat2);
}
// This does not work
// function: display the welcome message
void welcome(void)
{
char i,j;
WriteCmd(CLE);
// clear_wdt();
delay_ms(5);
for(i=1; i<5; i++)
{
for(j=7; j>i; j--)
{
display(1,j+1,0xD7,0xa0);//clear
switch(i)
{
case 1:
display(1,j,0xba,0xcf);//?
break;
case 2:
display(1,j,0xb3,0xc9);//?
break;
case 3:
display(1,j,0xb5,0xe7);//?
break;
case 4:
display(1,j,0xc6,0xf7);//?
break;
default:
// next line gives error: attempt to create a pointer to a constant
// write_dbc(1,3,"erro!!!");
}
delay_ms(2);
}
}
for(i=0; i<2; i++)
{
delay_ms(4);
// clear_wdt();
}
WriteCmd(CLE);
}
// I'm not sure if this works
// function:write ch on the postion of row and column
void write_dbc_l(char row, char column, char len, char *ch)
{
char c,pi=0;
WriteCmd(position_lcd[row]+column);
while(len--)
{
c = ch[pi++];
WriteData(c);
}
} |
|
|
 |
jeremiah
Joined: 20 Jul 2010 Posts: 1426
|
|
Posted: Tue Apr 10, 2012 9:02 am |
|
|
try a couple things:
Change
| Code: |
// next line gives error: attempt to create a pointer to a constant
// write_dbc(1,3,"erro!!!");
|
to
| Code: |
write_dbc(1,3,g_dbc_error);
|
and add a global variable:
| Code: |
char g_dbc_error[] = "error!!!";
|
That should fix the error message.
Also, try changing:
| Code: |
// This may be a problem. It seems to be a pointer to addresses in CGROM
// 0x80 is where a welcome message is located (Sitronix ST7920).
// 0x88 is blank, I don't know about the others.
// I can't find type 'rom char' documented anywhere.
rom char *position_lcd = {0x80, 0x90, 0x88, 0x98};
|
To
| Code: |
rom char position_lcd[] = {0x80, 0x90, 0x88, 0x98};
|
But also check those values and make sure they correspond to the values you send for the start of each row. |
|
 |
rovtech
Joined: 24 Sep 2006 Posts: 288
|
| Progress |
Posted: Wed Apr 11, 2012 7:28 am |
|
|
Thank you jeremiah, that fixed the error message.
Now that I can write characters to the screen some other things became evident. The ‘WriteCmd(0x80)’ command near the end of the ‘void InitLcd(void)’ function sets the address counter to the start of DDRAM. I changed this to see where charactters were placed on the screen and suddenly the light came on!
There are two LINES on the display, the first is from address 0x80 to 0x8F and from 0x80 to 0x87 is displayed on ROW 1 and from 0x88 to 0x8F is continued and displayed on ROW 3. The second LINE is from address 0x90 to 0x9F and from 0x90 to 0x97 is displayed on ROW 2 and from 0x98 to 0x9F is continued and displayed on ROW 4. Notice that 0x80, 0x90, 0x88, 0x98 in that troublesome pointer statement are the starting address of ROWS 1, 2, 3, 4. Suddenly a lot of the code makes sense.
For anyone who is getting one of these displays working you can experiment with the values in the following and see what happens:
| Code: | // Main Function
void main()
{
InitLcd (); // Initialize GLCD
printf (WriteData, "HABCDJEYTS123456789abZcdefghijklmnop, \n, \f, qrstuvw");
WriteData (0x40);
WriteData (0x41);
WriteData (0x42);
WriteData (0x43);
WriteData (0x61);
delay_ms (3000);
// Everything works up to here. The string is displayed on line 1, continued
// on line 3, then line 2, then line 4. The \n and \f cause strange characters.
WriteCmd (0x01); // clear display
WriteCmd (0x02); // return home
WriteCmd (0x82); // start 2 columns in on row 1
write_dbc(1,3,g_dbc_error); // displays "error!!!" after 3 seconds
delay_ms(2000); // wait 2 seconds
WriteCmd(0x88); // DDRAM, start of 3rd line
printf (WriteData, "Try again");
} |
Note that half characters are being displayed and there are 16 to a row, or 32 to a line. Also they are in pairs and the cursor highlights two characters so when we change the starting column (WriteCmd (0x82); // start 2 columns in on row 1) we actually start 4 positions in.
I think the data sheet is a bit misleading in that it describes the capability of the ST7920, not what it actually does in this application. Experience will tell.
When I get function ‘write_dbc()’ working properly I will post the corrections. |
|
 |
rovtech
Joined: 24 Sep 2006 Posts: 288
|
| Corrections |
Posted: Wed Apr 11, 2012 9:30 am |
|
|
Here are some corrections:
Make this correction:
| Code: | // This is OK. It points to the start of each ROW
// 0x80 is ROW 0, 0x90 is ROW 1, 0x88 is ROW 2, 0x98 is ROW 3
// columns start at 0 and go to 7
// I can't find type 'rom char' documented anywhere.
rom char position_lcd[] = {0x80, 0x90, 0x88, 0x98};
// error message
char g_dbc_error[] = "error!!!"; |
Replace the two functions below with these corrections:
| Code: | // Function set_address(row, column)
// Working. This sets the start position on the screen
// row is from 0 to 3, column is from 0 to 7 e.g. 0,0 1,7
void set_address (char row, char column)
{
WriteCmd(position_lcd[row]+column);
}
// Function write_dbc(row, column, *ch)
// Working. Writes character pointed to at position
// of row & column. Pointer can be to start of string
void write_dbc(char row, char column, char *ch)
{
char c,pi=0;
WriteCmd(position_lcd[row]+column);
while(1)
{
c = ch[pi++];
if ( !c )
break;
WriteData(c);
}
} |
Now you can try these and see where they are displayed:
| Code: | // Main Function
void main()
{
char test[] = "Testing";
InitLcd (); // Initialize GLCD
printf (WriteData, "HABCDJEYTS123456789abZcdefghijklmnop, \n, \f, qrstuvw");
WriteData (0x40);
delay_ms (3000);
WriteCmd (SET_C); // reset, screen cleared
write_dbc(1,1,g_dbc_error); // displays "error!!!" after 3 seconds
delay_ms (3000);
write_dbc(0,0,test); // Testing at 0,0
set_address(2, 7); // DDRAM, start of 3rd row
printf (WriteData, "T2"); // T2 at end of 3rd row (row 2)
// delay_ms (3000);
// welcome (); // display welcome message, not working
} |
Welcome still does not work so I have to figure what the last three functions are doing. Everything else is working. |
|
 |
jeremiah
Joined: 20 Jul 2010 Posts: 1426
|
|
Posted: Wed Apr 11, 2012 10:49 am |
|
|
I don't see what corrections were made to set_address() and write_dbc(). They look identical to what you posted earlier. Or was it just comment changes?
Just a thought on the welcome function: it appears to clear the screen 8 ms after it finishes writing to it. That might be too fast to see? |
|
 |
rovtech
Joined: 24 Sep 2006 Posts: 288
|
| More corrections |
Posted: Wed Apr 11, 2012 3:39 pm |
|
|
Jeremiah, Mostly comment changes. The examples were incorrect and would take you off the screen in some cases. I also marked them as checked.
The welcome was a real mess and seemed designed to scroll multiple Chinese characters that were erased quicker than could be seen, as you thought. I re-wrote it to show what was intended, I think. Anyway it was a good excercise in moving characters into the screen from right to left. See the comments:
The last three functions are working and are commented to show what they do.
Here are the corrections:
| Code: | // function: display(row, column, dat1, dat2)
// Working. Set the address for SBC?
// adds two characters starting at the position specified
// e.g: 0,0 1,0 rows 0 to 3, cols 0 to 7
void display(char x_add, char y_add, char dat1, char dat2)
{
WriteCmd(position_lcd[x_add]+y_add);
WriteData(dat1);
WriteData(dat2);
}
// function: display the welcome message
// Working. This welcome message is in Chinese
// The display from right to left, 16x16
// However since 1955 writing is commonly L to R in China
// the second welcome_E is is 16x16 large text in English
void welcome_C(void)
{
char i=7;
WriteCmd (0x04); // entry mode, shift cursor left & decrement AC
delay_ms(5);
display(0,i--,0xba,0xcf);
display(0,i--,0xb3,0xc9);
display(0,i--,0xb5,0xe7);
display(0,i--,0xc6,0xf7);
WriteCmd (0x06); // entry mode, shift cursor right & increment AC
}
void welcome_E(void)
{
char i=0;
display(1,i++,0xa3,0xc2);
display(1,i++,0xa3,0xc9);
display(1,i++,0xa3,0xc7);
display(1,i++,0xa3,0xa0);
display(1,i++,0xa3,0xd4);
display(1,i++,0xa3,0xc5);
display(1,i++,0xa3,0xd8);
display(1,i++,0xa3,0xad4);
}
// function: write_dbc_l(row,col,length,*ch)
// writes character or string pointed to at position
// Note: that is the letter l not a one (l not 1) in name!
// length is # of characters starting at 1 (not 0)
void write_dbc_l(char row, char column, char len, char *ch)
{
char c,pi=0;
WriteCmd(position_lcd[row]+column);
while(len--)
{
c = ch[pi++];
WriteData(c);
}
} |
The following will test them:
| Code: | WriteCmd(CLE);
welcome_C (); // display welcome message in Chinese
delay_ms (3000);
welcome_E (); // and English
|
To summarize: I have demonstrated half and full character display from DDRAM, CGROM, and HCGROM.
CGRAM (user defined characters) and GDRAM (graphics display RAM) is next on the list but may be a few days as I take a break for other projects.
Hopefully someone else will connect one of these and contribute some functions. I think I will need a PIC with more RAM than the 16F722 to be able to map the full screen. Then I can take a look at the CCS graphics routines.
Have you any idea what SBC and DBC stand for?
Does anyone have some input on how the GDRAM is organized? The data sheet is somewhat confusing but will probably become clear as I tinker. |
|
 |
jeremiah
Joined: 20 Jul 2010 Posts: 1426
|
|
Posted: Wed Apr 11, 2012 5:27 pm |
|
|
If I had to guess, it's single vs double with sbc vs dbc. Maybe single byte character / double byte character? I don't know, just pulling that out of the rear.
As far as GDRAM is concerned. See if this guy's driver uses similar GDRAM to yours:
http://www.ccsinfo.com/forum/viewtopic.php?p=92727
Look for glcd_update() and glcd_init() to see what he does for GDRAM. I just ran across it doing a search on the forums. I don't know how much of it is similar to what you want, but might give you some ideas of how to tackle the GDRAM. |
|
 |
rovtech
Joined: 24 Sep 2006 Posts: 288
|
|
Posted: Thu Apr 12, 2012 4:42 pm |
|
|
| I had tried to compile that driver but my 16F722 does not have enough RAM. Now that I have some idea of what I am doing I have ordered some PIC16F1527 chips. This will test my PCB fabrication as it is a 64 pin surface mount device. |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|