CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Trouble with LCD VK2220

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
lacolo



Joined: 10 Mar 2006
Posts: 15

View user's profile Send private message

Trouble with LCD VK2220
PostPosted: Fri Mar 10, 2006 10:31 pm     Reply with quote

I'm using PIC18F452 with LCD VK2220 (http://www.wrighthobbies.net/datasheets/VK2220.pdf
I have looked up most posts on the forum regarding LCDs. Some helpful tips, but I still have had no luck. My code is all compiling, and LED blinking OK, but the LCD panel is just displaying a line of black squares on the first line (it's a 16x2.)

Here's my code:

Code:
// LCD text display

////  lcd_init()   Must be called before any other function.           ////
////                                                                   ////
////  lcd_putc(c)  Will display c on the next position of the LCD.     ////
////                     The following have special meaning:           ////
////                      \f  Clear display                            ////
////                      \n  Go to start of second line               ////
////                      \b  Move back one position                   ////
////                                                                   ////
////  lcd_gotoxy(x,y) Set write position on LCD (upper left is 1,1)    ////
////                                                                   ////
////  lcd_getc(x,y)   Returns character at position x,y on LCD 

// As defined in the following structure the pin connection is as follows:
//     D0  enable
//     D1  rs
//     D2  rw
//     D4  D4
//     D5  D5
//     D6  D6
//     D7  D7
//
//   LCD pins D0-D3 are not used and PIC D3 is not used.
//    (could use D3 for backlight?)

#include <18F452.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)

#include <lcd.c>

int var;

void main() {
   output_high(PIN_A1);
   delay_ms(100);
   output_low(PIN_A1);
   delay_ms(100);
   output_high(PIN_A1);
   delay_ms(100);
   output_low(PIN_A1);
   delay_ms(100);
   // arbitrary value
   var = 16;      
   lcd_init();
//   delay_ms(10);
   lcd_putc("\fReady...\n");
   //printf(lcd_putc,"\fTest Variable: %U\nOkay?", var);
   while (true) {
      output_high(PIN_A0);
      delay_ms(100);
      output_low(PIN_A0);
      delay_ms(1000);      
   }
}


and
Code:
///////////////////////////////////////////////////////////////////////////
////                             LCDD.C                              ////
////                 Driver for common LCD modules      ////
////                                                                 ////
////  lcd_init()   Must be called before any other function.         
////
////                                                                 
////
////  lcd_putc(c)  Will display c on the next position of the LCD.   
////
////                     The following have special meaning:         
////
////                      \f  Clear display                         
////
////                      \n  Go to start of second line             
////
////                      \b  Move back one position                 
////
////                                                                 
////
////  lcd_gotoxy(x,y) Set write position on LCD (upper left is 1,1) 
////
////                                                                 

  ////
////  lcd_getc(x,y)   Returns character at position x,y on LCD       

  ////
////                                                                 

  ////
////////////////////////////////////////////////////////////////////

///////
////        (C) Copyright 1996,2003 Custom Computer Services         

  ////
//// This source code may only be used by licensed users of the CCS
C  ////
//// compiler.  This source code may only be distributed to other   
////
//// licensed users of the CCS C compiler.  No other use, reproduction ////
//// or distribution is permitted without written permission.        ////
//// Derivative programs created using this software in object code  ////
//// form are not restricted in any way.                             ////
////////////////////////////////////////////////////////////////////

///////

// As defined in the following structure the pin connection is as

follows:
//     D0  enable
//     D1  rs
//     D2  rw
//     D4  D4
//     D5  D5
//     D6  D6
//     D7  D7
//
//   LCD pins D0-D3 are not used and PIC D3 is not used.

// Un-comment the following define to use port B
// #define use_portb_lcd TRUE


struct lcd_pin_map {                 // This structure is overlayed
           BOOLEAN enable;           // on to an I/O port to gain
           BOOLEAN rs;               // access to the LCD pins.
           BOOLEAN rw;               // The bits are allocated from
           BOOLEAN unused;           // low order up.  ENABLE will
           int     data : 4;         // be pin B0.
        } lcd;


#if defined(__PCH__)
#if defined use_portb_lcd
   #byte lcd = 0xF81                   // This puts the entire

structure
#else
   #byte lcd = 0xF83                   // This puts the entire

structure
#endif
#else
#if defined use_portb_lcd
   #byte lcd = 6                  // on to port B (at address 6)
#else
   #byte lcd = 8                 // on to port D (at address 8)
#endif
#endif

#if defined use_portb_lcd
   #define set_tris_lcd(x) set_tris_b(x)
#else
   #define set_tris_lcd(x) set_tris_d(x)
#endif


#define lcd_type 2           // 0=5x7, 1=5x10, 2=2 lines
#define lcd_line_two 0x40    // LCD RAM address for the second line


BYTE const LCD_INIT_STRING[4] = {0x20 | (lcd_type << 2), 0xc, 1, 6};
                             // These bytes need to be sent to the LCD
                             // to start it up.
                             // The following are used for setting
                             // the I/O port direction register.
struct lcd_pin_map const LCD_WRITE = {0,0,0,0,0}; // For write mode all pins are out

struct lcd_pin_map const LCD_READ = {0,0,0,0,15}; // For read mode data pins are in

BYTE lcd_read_byte() {
      BYTE low,high;
      set_tris_lcd(LCD_READ);
      lcd.rw = 1;
      delay_cycles(1);
      lcd.enable = 1;
      delay_cycles(1);
      high = lcd.data;
      lcd.enable = 0;
      delay_cycles(1);
      lcd.enable = 1;
      delay_us(1);
      low = lcd.data;
      lcd.enable = 0;
      set_tris_lcd(LCD_WRITE);
      return( (high<<4) | low);
}


void lcd_send_nibble( BYTE n ) {
      lcd.data = n;
      delay_cycles(1);
      lcd.enable = 1;
      delay_us(2);
      lcd.enable = 0;
}


void lcd_send_byte( BYTE address, BYTE n ) {

      lcd.rs = 0;
      while ( bit_test(lcd_read_byte(),7) ) ;
      lcd.rs = address;
      delay_cycles(1);
      lcd.rw = 0;
      delay_cycles(1);
      lcd.enable = 0;
      lcd_send_nibble(n >> 4);
      lcd_send_nibble(n & 0xf);
}


void lcd_init() {
    BYTE i;
    set_tris_lcd(LCD_WRITE);
    lcd.rs = 0;
    lcd.rw = 0;
    lcd.enable = 0;
    delay_ms(15);
    for(i=1;i<=3;++i) {
       lcd_send_nibble(3);
       delay_ms(5);
    }
    lcd_send_nibble(2);
    for(i=0;i<=3;++i)
       lcd_send_byte(0,LCD_INIT_STRING[i]);
}


void lcd_gotoxy( BYTE x, BYTE y) {
   BYTE address;

   if(y!=1)
     address=lcd_line_two;
   else
     address=0;
   address+=x-1;
   lcd_send_byte(0,0x80|address);
}

void lcd_putc( char c) {
   switch (c) {
     case '\f'   : lcd_send_byte(0,1);
                   delay_ms(2);
                                           break;
     case '\n'   : lcd_gotoxy(1,2);        break;
     case '\b'   : lcd_send_byte(0,0x10);  break;
     default     : lcd_send_byte(1,c);     break;
   }
}

char lcd_getc( BYTE x, BYTE y) {
   char value;

    lcd_gotoxy(x,y);
    while ( bit_test(lcd_read_byte(),7) ); // wait until busy flag

is low
    lcd.rs=1;
    value = lcd_read_byte();
    lcd.rs=0;
    return(value);
}


I have double-checked my wiring as well. Any ideas on what may be wrong? Is a one-line of black squares a usual indicator of a particular problem?
Markdem



Joined: 24 Jun 2005
Posts: 206

View user's profile Send private message Send e-mail

PostPosted: Fri Mar 10, 2006 10:45 pm     Reply with quote

Hi lacolo, if the black squares are only coming up on the line you are tring to print on, it sounds like the contrast is set to high. Try playing the with contrast pot.

Mark
lacolo



Joined: 10 Mar 2006
Posts: 15

View user's profile Send private message

PostPosted: Sat Mar 11, 2006 10:11 am     Reply with quote

I have a potentiometer controlling the contrast, so I can easily adjust it. I tried that, and no luck. Any other ideas?

Thank you,
Cynthia
Charlie U



Joined: 09 Sep 2003
Posts: 183
Location: Somewhere under water in the Great Lakes

View user's profile Send private message

PostPosted: Sat Mar 11, 2006 12:21 pm     Reply with quote

lacolo,

In general, if the first line of your LCD module is just dark squares, this indicates that the LCD module has not been initialized correctly. If this is your complete program, it appears that you have not defined __PCH__ as required by the driver to work with the 18F452. Add a line at the beginning of the file that contains your main() function: #define __PCH__ This will force the compiler to use the port addresses for the 18F452.
lacolo



Joined: 10 Mar 2006
Posts: 15

View user's profile Send private message

PostPosted: Sat Mar 11, 2006 3:50 pm     Reply with quote

Thanks, but that didn't work either. I tried inserting it, received a duplication error, and then realized it is already in the LCD.C code as #if defined(__PCH__)

Any other thoughts?

//Cynthia
Ttelmah
Guest







PostPosted: Sat Mar 11, 2006 4:03 pm     Reply with quote

No. The 'point' is that the LCD code, tests 'if' it is defined. The poster was suggesting that _you_ define it, which will then force the test in the LCD code to use the PCH defines.
However this should be unnecessary, since using the PCH compiler ought to automatically define this, which is why you then get the 'duplicate definition' error.
Though you say you have checked your wiring, I'd go back and do it again. Look particularly, for something silly, like two of the wires being shorted, either to ueach other, or a 'whisker' to Vss/Vdd (rather than just for a failure in the connections). It is sounding more and more like a hardware error.

Best Wishes
lacolo



Joined: 10 Mar 2006
Posts: 15

View user's profile Send private message

PostPosted: Sat Mar 11, 2006 5:13 pm     Reply with quote

Well, I just soldered a different LCD Panel (same model, VK2220), to ensure no stray wires, etc, and I'm getting the same results. 1st line of black squares.

Any other thoughts?

//Cynthia
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Mar 12, 2006 2:04 am     Reply with quote

Post a list of the connections that you have made between the LCD pins
and the PIC pins.
lacolo



Joined: 10 Mar 2006
Posts: 15

View user's profile Send private message

PostPosted: Sun Mar 12, 2006 3:04 pm     Reply with quote

LCD pins
1 --- GND
2 --- +5V
3 --- 10K Potentiometer b/w GND & +5V
4 --- PIC pin 20 (D1)
5 --- PIC pin 21 (D2)
6 --- PIC pin 19 (D0)
7 --- 10K to GND
8 --- 10K to GND
9 --- 10K to GND
10--- 10K to GND
11--- PIC pin 27 (D4)
12--- PIC pin 28 (D5)
13--- PIC pin 29 (D6)
14--- PIC pin 30 (D7)
15--- GND
16--- +5V
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Mar 12, 2006 3:46 pm     Reply with quote

Your connections look normal except for the 10K resistors to ground.
The LCD controller has built-in pull-up resistors on the data lines,
so you shouldn't need to put any resistors on DB0-DB3.
See page 54 of the data sheet:
http://www.sparkfun.com/datasheets/LCD/HD44780.pdf

The LCD interface starts out in 8-bit mode upon power-up, and then is
put into 4-bit mode by the CCS driver. See page 46 of the data sheet.
I haven't tested this, but it's possible that by putting pull-down resistors
on pins DB0-DB3, you may be causing a problem.
lacolo



Joined: 10 Mar 2006
Posts: 15

View user's profile Send private message

PostPosted: Sun Mar 12, 2006 4:41 pm     Reply with quote

Hi everyone,

Thanks again for all the help - it is finally working! I reworked all of the electronics onto a new breadboard, and that did it.

No idea, really, what was going on, but now, I'm all ready to go!

//Cynthia
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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