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 support@ccsinfo.com

LCD help for a noob
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
RatFink



Joined: 01 May 2004
Posts: 49

View user's profile Send private message

LCD help for a noob
PostPosted: Sun May 09, 2004 3:46 pm     Reply with quote

I'm trying to see if I can get an LCD to work with my PIC16F877A. I'm using the lcd.c driver and have one of CrystalFontz 2x8 LCD's with a HD44780compatible controller. right now I'm just seeing if I can get one small thing to print to the LCD. Everything in my code works untill I try to incorperate the LCD, when I do that I get an error message that says "Could not detect target chip"

This is the code up to the only print statement I have;

Code:
#bit Heat = PORTD.4
#bit Fan_Slow = PORTD.5
#bit Fan_Fast = PORTD.6
#bit Vent = PORTD.7


#bit Timed1 = READ.0
#bit Timed2 = READ.1

/* Define Inputs */
#bit PB_Preheat = PORTD.0
#bit PB_Plastic = PORTD.1
#bit PB_Metal = PORTD.2



#use delay(clock=20000000)
//#use rs232(DEBUGGER)
//#use rs232(baud=9600, xmit=PIN_c6,rcv=PIN_c7)
#include <lcd.c>


   void main(void)

{

   lcd_init();
   
   ADCON0 = 0x41;      /* Fosc/8, A/D enabled */
   OPTION = 0x07;      /* TMR0 prescaller, 1:256, PORTB pullups */
   ADCON1 = 0x0E;      /* Left justify, 1 analog channel, VDD and Vss references */





   //TRISA = 0x0f;      /* PORTA required inputs*/
   //TRISB = 0x07;      /* PORTB requred inputs */
   //TRISC = 0;      /* PORTA all outputs */
   TRISD = 0x0F;         /* PORTD Inputs and 0utputs */
   PORTD =0x0F;       /*Sets up PORTD*/
   Timed1 =0;
   Timed2 = 0;

BACK:   do
{;}
   while (PB_Preheat&&PB_Plastic&&PB_Metal == 1);



   if (PB_Preheat == 0)
   {
      Heat = 1;
      Fan_Slow = 1;
      Fan_Fast = 0;
      Vent = 0;
      Timed1 = 0;
      Timed2 = 0;
      printf(lcd_putc, "Warming.");

   }


the lcd.c file is the one that comes with the ccs compiler except that I add this, #device PIC16F877A because the compiler kept yelling for a device statement,

Code:
#device PIC16F877A
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;


all my connections are just as indicated in the driver, and the power is hooked up as;

pin 1 to ground
pin 2 to vcc (5V)
pin 3 tp the wiper on a 10k pot.

What in all of this would make it so I get the "Could not target chip" error?
Humberto



Joined: 08 Sep 2003
Posts: 1215
Location: Buenos Aires, La Reina del Plata

View user's profile Send private message

PostPosted: Sun May 09, 2004 6:29 pm     Reply with quote

Hi RatFink,

I canīt see what is the port reserved to handle the LCD module.
The driver has a predefined structure:
Code:
 
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;


That you must redirect to a port in this fashion:
#byte lcd = 6 // if you assign port B (at address 6)


HTH

Humberto
Humberto



Joined: 08 Sep 2003
Posts: 1215
Location: Buenos Aires, La Reina del Plata

View user's profile Send private message

PostPosted: Sun May 09, 2004 6:53 pm     Reply with quote

Pls take care of this:

//#use rs232(baud=9600, xmit=PIN_c6,rcv=PIN_c7)

//TRISC = 0; /* PORTA all outputs */ ????

Humberto
RatFink



Joined: 01 May 2004
Posts: 49

View user's profile Send private message

PostPosted: Sun May 09, 2004 7:09 pm     Reply with quote

Humberto wrote:
Pls take care of this:

//#use rs232(baud=9600, xmit=PIN_c6,rcv=PIN_c7)

//TRISC = 0; /* PORTA all outputs */ ????

Humberto


Those are there from some other testing I was doing (running, on it's own without the ICD and getting info through Hyper Terminal), that's why it's commented out, in fact there are a few things like that. I will get rid of them when I have it all working.

Quote:
That you must redirect to a port in this fashion:
#byte lcd = 6 // if you assign port B (at address 6)


I think that is done elsewhere in the LCD.C file.

I sent the everything to you through email. Please just have a short look at it, I don't want you wasting your whole evening. I know you have better things to do.

Thank you.
RatFink



Joined: 01 May 2004
Posts: 49

View user's profile Send private message

PostPosted: Sun May 09, 2004 11:02 pm     Reply with quote

OK, now I'm fed up, even just running a simple test code I get all kinds of errors, and they are random, asking for something different every time, quite often in the lcd.c file. I've spent all day on this and I'll I have is a headache.


could sombody write me just a piece of test code, just so it prints "TEST" just so I can see if it's a hardware problem?

I do get the start sceen on the LCD, so it's pawered and working, I just get lots of error messages when I try to program it.

I'm using the lcd.c driver, wired up just like in that file.

Using a PIC16F877A
and a LCD with a HD44780compatible controller
ICD-U40
Neutone



Joined: 08 Sep 2003
Posts: 839
Location: Houston

View user's profile Send private message

PostPosted: Mon May 10, 2004 8:12 am     Reply with quote

RatFink wrote:
OK, now I'm fed up, even just running a simple test code I get all kinds of errors, and they are random, asking for something different every time, quite often in the lcd.c file. I've spent all day on this and I'll I have is a headache.


could sombody write me just a piece of test code, just so it prints "TEST" just so I can see if it's a hardware problem?

I do get the start sceen on the LCD, so it's pawered and working, I just get lots of error messages when I try to program it.

I'm using the lcd.c driver, wired up just like in that file.

Using a PIC16F877A
and a LCD with a HD44780compatible controller
ICD-U40



You should declare the device type first. Not a bunch of variables. Some of those variables are dependent on the device type. Take this line;
#device PIC16F877A
and place it at the top of your main file before any includes.
Humberto



Joined: 08 Sep 2003
Posts: 1215
Location: Buenos Aires, La Reina del Plata

View user's profile Send private message

PostPosted: Mon May 10, 2004 9:23 am     Reply with quote

Hi RatFink,

I didnīt receive anything in my email.

Just to start, tell us your pinouts from PIC to LCD, post the header and includes.

Humberto
RatFink



Joined: 01 May 2004
Posts: 49

View user's profile Send private message

PostPosted: Mon May 10, 2004 11:09 am     Reply with quote

Humberto wrote:
Hi RatFink,

I didnīt receive anything in my email.

Just to start, tell us your pinouts from PIC to LCD, post the header and includes.

Humberto


HMM that's wierd that you didn't get it.

I think the easiest way to start would be if somebody could just write me a small little piece of code that prints out the word "test" complete with whatever header info I need, that way I could know that I'm not having hardware problems, I've tried it and I'm afraid I'm doing it wrong.

Code:
#include <16F877A.h>

#fuses HS, NOWDT, NOPROTECT,PUT,BROWNOUT, NOLVP

#byte PORTC = 0x07
#byte TRISC = 0x87



#use delay(clock=20000000)

#use rs232(baud=9600, xmit=PIN_c6,rcv=PIN_c7, ERRORS, stream=LCD)
#include <lcd.c>



void main()

{
lcd_init();

TRISC = 0x0;

printf(lcd_putc,"TEST"); 



}




Here's the lcd.c file as I got it in in my examples file;

Code:
////////////////////////////////////////////////////////////////////////////
////                             LCD.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,1997 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:
//     B0  enable
//     B1  rs
//     B2  rw
//     B4  D4
//     B5  D5
//     B6  D6
//     B7  D7
//
//   LCD pins D0-D3 are not used and PIC B3 is not used.


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__)
   #byte lcd = 0xF86                   // This puts the entire structure(81)
#else                                  // on to port B (at address 6)
   #byte lcd = 6
#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_b(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_b(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_b(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);
    lcd.rs=1;
    value = lcd_read_byte();
    lcd.rs=0;
    return(value);
}


I used the pins that are stated in the file;
Quote:
// As defined in the following structure the pin connection is as follows:
// B0 enable
// B1 rs
// B2 rw
// B4 D4
// B5 D5
// B6 D6
// B7 D7
//
// LCD pins D0-D3 are not used and PIC B3 is not used.


I also have
pin 1 of the lcd to ground
pin 2 to 5V
pin 3 across the wiper of a 10K pot

Here is the data sheet for my LCD, it's a CrystalFontz 2x8 LCD with a HD44780 compatible controller.

http://www.crystalfontz.com/products/0802a-color/CFAH0802AYMCJP.pdf

I'm using a ccs develoment board with a PIC16F877A.


I've had many different error messages even when trying a simple test code, it almost seems random. some are;

"could not detect target chip"

"Could not start target: The target was not halted after reset. Check the target oscillator and MCLR."

"Identifier is allready used in the scope" in relation to this line in the lcd.c file (in red)
Quote:
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;


and a few others.
Neutone



Joined: 08 Sep 2003
Posts: 839
Location: Houston

View user's profile Send private message

PostPosted: Mon May 10, 2004 11:48 am     Reply with quote

These two are related to the state the PIC is in when you attempt to load code into it. They are not at all related to the LCD.

"could not detect target chip"
"Could not start target: The target was not halted after reset. Check the target oscillator and MCLR."

Generaly you can fix these by disconnecting and reconnecting the ICD.

I thought that development boards had a 4Mhz xtl are you sure yours is 20Mhz.

"Identifier is allready used in the scope" in relation to lcd structure. Are you declaring the structure before or after you specify it's location?
rwyoung



Joined: 12 Nov 2003
Posts: 563
Location: Lawrence, KS USA

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

PostPosted: Mon May 10, 2004 12:46 pm     Reply with quote

If you are mapping the LCD to port B (and I think you are based on what I've read) and you are trying to use the ICSP lines for programming the chip you should also add two resistors to the RB7 and RB6 pins to isolate the lcd from the PIC and ICSP lines.

A little ASCII art so bear with me:


PIC RB7 ----*---[4.7k]---- LCD DB7
PIC RB6 ---*----[4.7k]---- LCD DB6

The places where I've put the asterisk (*) indicate where you would hook up the ICSP data and clock lines. The resistors are there to islolate the LCD from the programmer. I've had some LCD modules that worked OK without the resistors and others that brought everything to a screaching halt so now I just put them in. Since my code doesn't read back from the LCD the value of the resistor isn't terribly important but 4.7K seems to work quite well. If you don't want resistors then you will have to use some jumpers and remove them during programming.

Also don't even bother trying to run the debugger with anything attached to the data and clock lines or RB3 if you are using low voltage programming mode.
_________________
Rob Young
The Screw-Up Fairy may just visit you but he has crashed on my couch for the last month!
Humberto



Joined: 08 Sep 2003
Posts: 1215
Location: Buenos Aires, La Reina del Plata

View user's profile Send private message

PostPosted: Mon May 10, 2004 12:51 pm     Reply with quote

Hi RatFink,
As Neutone pointed, try to separate the ICD warning mssges from those related to code compiling.
Not time to test it but this should work.

Code:


#include <16F877.h>
#fuses HS, NOWDT, NOPROTECT,PUT,BROWNOUT, NOLVP
#use delay(clock=20000000)
#use fast_io(b)

void main()
{
   delay_ms(15);   // Power up
   lcd_init();
   delay_ms(10);   // some modules needs a delay
   lcd_putc("\f Test...\n");
   while (TRUE);
}

#include <lcd.c>



In previous post you wrote that a trimpot is connected between +5V and GND. Be sure that the voltage in the tap or cursor is around +0.350 to +0.500V as a starting contrast set point.

HTH

Humberto
RatFink



Joined: 01 May 2004
Posts: 49

View user's profile Send private message

PostPosted: Mon May 10, 2004 1:56 pm     Reply with quote

Thank you guys for the help, it will be a few hours before I can get home and test it, but I really do appreciate the ideas.

Quote:
try to separate the ICD warning mssges from those related to code compiling


This is why I was asking for someone to write test code for me, the randomness of both harware and code related problem was making it hard for me to pin down problems, if I could be sure the code was ok, then I could just look at hardware.

Neutone wrote:
"Identifier is allready used in the scope" in relation to lcd structure. Are you declaring the structure before or after you specify it's location?


I'm not really sure what you mean, the lcd.c file is the lcd driver that comes with the board. also the dev boards have a 20 Mhz crystal

rwyoung wrote:
...


I'll have to check out your stuff when I get home.
RatFink



Joined: 01 May 2004
Posts: 49

View user's profile Send private message

PostPosted: Tue May 11, 2004 3:04 am     Reply with quote

rwyoung, when I got home I struggled and struggled with the stupid "Can't find target" messages, I then tried programming using the ICD software and had the same problem, finally I looked in the Help file and it mentioned the same pins you did (DUH!) so I remembered your post. I added some resistors and now I don't get that error message.

I'm still having problems with only part of the message printing, for example if I want the word "TEST" to print I'll get "TST" or just "ST" but at least I can program it now.
RatFink



Joined: 01 May 2004
Posts: 49

View user's profile Send private message

PostPosted: Tue May 11, 2004 11:34 am     Reply with quote

edit
rwyoung



Joined: 12 Nov 2003
Posts: 563
Location: Lawrence, KS USA

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

PostPosted: Tue May 11, 2004 11:55 am     Reply with quote

Ratfink,

At least now you can program the chip and get SOMETHING on the display. Now you should check your write timings and signal integrety. Some LCD controllers are very forgiving and others are not.

Borrow an oscilloscope (or if you have been a good boy and Santa Claus brough you one) and check:

1) Clean clock and data signals - by that I mean no overshoot/undershoot of edges. Use good oscilloscope probing technique, don't use the 6" ground clip lead but rather solder a wire to a ground very near the signal you are probing and wrap it around the little ground ring at the end of the probe. Use a x10 probe, it is less of a load on the driving circuit.

2) Clean power and ground. Add at least 1 0.1uF ceramic at the LCD display and 10uF electrolytic or tantalum. Rate the voltages appropriately. Remember that in general it is OK to over rate so a 10V or 16V (or even 50V) electrolytic is OK

3) Verify timing of data versus your enable and write lines from the PIC.

And finally, TAKE GOOD NOTES. If you don't write it down, it didn't happen! This way you have something to refer to if the problem goes away but comes back or when you start your next project. Don't make the same mistakes, make new and better ones.
_________________
Rob Young
The Screw-Up Fairy may just visit you but he has crashed on my couch for the last month!
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
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