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

getting LCD to work with 16f877a.. kinda stumped.

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








getting LCD to work with 16f877a.. kinda stumped.
PostPosted: Sun Dec 05, 2004 10:39 pm     Reply with quote

Ok so I have to ask. What is the secret to getting the LCD to work?

I have tried everything I can come up with and no luck. I have tried many different configurations. Am I missing something?

I am trying just to teach myself C coding using CCS rather than assembly.

I can get the output waveforms on the pins I have selected but can't get the LCD to work at all. It is really confusing me to no end.. lol

I have been monitoring the pins involved with the LCD (PORT C) with a scope and get only a low on pin 0 and hi's on each other pin. Nothing on the LCD.

I have added in the square wave just to verify that the code has taken and is working in the PIC.

here is the code I am working with right now. Smile TIA

If I can get this to work I will move on to tackling the keyboard. ;)

Code:
#include <16F877A.h>
#fuses HS,NOWDT,NOPUT,NOPROTECT,NOBROWNOUT,NOLVP,NOCPD,NOWRT
#use delay(clock = 20000000)

#define TRISA 0x85
#define TRISB 0x86
#define TRISC 0x87
#define TRISD 0x88
#define   TRISE 0x89
#define   ADCON1 0x9F
//*****////////////////////////////////////////////////////////////*****//
//*****////////////////////    LCD   //////////////////////////////*****//
//*****////////////////////////////////////////////////////////////*****// 
// As defined in the following structure the pin connection is as follows:
//     C0  enable
//     C1  rs
//     C2  rw
//     C4  D4
//     C5  D5
//     C6  D6
//     C7  D7
//**********************************************************************//
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;
#define use_portc_lcd
   #byte lcd = 7                  // on to port C (at address 7)
#define set_tris_lcd(x) set_tris_c(x)

#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

int lcd_read_byte(void);
void lcd_send_nibble( int n );
void lcd_send_byte( int address, int n );
void lcd_init(void);
void lcd_gotoxy( int x, int y);
void lcd_putc( char c);
char lcd_getc( int x, int y);

main()
{
   char c = 'A';
   
   SETUP_ADC(NO_ANALOGS);
   lcd_init();
   while(1)
     {
       output_high(PIN_A0);
      output_d(0xAA);
         delay_us(500);
   
      lcd_gotoxy(1,1);
      lcd_putc(c);
      
      output_low(PIN_A0);
      output_d(0x55);
      delay_us(500);

    }
}

int lcd_read_byte(void) {
      int 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( int n ) {
      lcd.data = n;
      delay_cycles(1);
      lcd.enable = 1;
      delay_us(2);
      lcd.enable = 0;
}
//**********************************************************************//
void lcd_send_byte( int address, int 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(void) {
    int 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( int x, int y) {
   int 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( int x, int y) {
   char value;

    lcd_gotoxy(x,y);
    lcd.rs=1;
    value = lcd_read_byte();
    lcd.rs=0;
    return(value);
}
Guest








PostPosted: Sun Dec 05, 2004 10:40 pm     Reply with quote

oh yes it is a 2x16 LCD.

if you need any more info let me know. Smile
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

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

PostPosted: Sun Dec 05, 2004 10:57 pm     Reply with quote

How did you connect the LCD to the PIC?
Guest








PostPosted: Sun Dec 05, 2004 11:00 pm     Reply with quote

I connected it through PortC using solid core wire on my proto board. Same as I was using when I had the LCD running with my ASM code I had been using. The pinout for the Enable, RW, RS and data lines are as follows.

C0 enable
C1 rs
C2 rw
C4 D4
C5 D5
C6 D6
C7 D7
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

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

PostPosted: Sun Dec 05, 2004 11:40 pm     Reply with quote

Code looks okay to me. Put your asm code in without changing the hardware and see if it still works. Otherwise, check the hardware.
Guest








PostPosted: Mon Dec 06, 2004 12:57 am     Reply with quote

Thanks Mark.

It is flickering now. No solid display tho. I can see most of the lines toggling. only the r/w isn't.

I'll throw in the ASM again and see what comes of it.

Thanks again. Much appreciated.
Haplo



Joined: 06 Sep 2003
Posts: 659
Location: Sydney, Australia

View user's profile Send private message

PostPosted: Mon Dec 06, 2004 2:28 am     Reply with quote

This may be a bit off the mark, but what is the circuitry around the brightness input?
rwyoung



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

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

PostPosted: Mon Dec 06, 2004 9:07 am     Reply with quote

Haplo wrote:
This may be a bit off the mark, but what is the circuitry around the brightness input?


Try grounding the contrast pin. You can get fancy later and add the necessary Vee supply and contrast control.
_________________
Rob Young
The Screw-Up Fairy may just visit you but he has crashed on my couch for the last month!
Guest








PostPosted: Mon Dec 06, 2004 11:31 am     Reply with quote

its a 10k multi turn pot witht eh adjustment pin on the pot to the contrast pinon the LCD. The other two pins on the pot go to 5v and grnd.

The circuitry all works fine with an .asm file to test the LCD. It doesn't work with a CCS C version tho. Which is a bit of a turn off as I'd rather write in C than ASM.
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

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

PostPosted: Mon Dec 06, 2004 11:43 am     Reply with quote

Quote:
It is flickering now.

Not sure what you mean by this but...

Change your main() to this

Code:
main()
{
   char c = 'A';
   
   SETUP_ADC(NO_ANALOGS);
   lcd_init();
   lcd_gotoxy(1,1);
   lcd_putc(c);
   while(1)
     {
      output_high(PIN_A0);
      output_d(0xAA);
      delay_us(500);
      output_low(PIN_A0);
      output_d(0x55);
      delay_us(500);
    }
}

and see what happens.
Guest








PostPosted: Mon Dec 06, 2004 4:38 pm     Reply with quote

Mark wrote:
Quote:
It is flickering now.

Not sure what you mean by this but...

Change your main() to this

Code:
main()
{
   char c = 'A';
   
   SETUP_ADC(NO_ANALOGS);
   lcd_init();
   lcd_gotoxy(1,1);
   lcd_putc(c);
   while(1)
     {
      output_high(PIN_A0);
      output_d(0xAA);
      delay_us(500);
      output_low(PIN_A0);
      output_d(0x55);
      delay_us(500);
    }
}

and see what happens.


What I meant was that I could see the individual dots in the character matrixes flickering faintly very fast.

Thanks for the code. Gave it a shot and still nothing. Instead of seeing some toggling happening on all LCD lines it is locked with no squarewave on the o/p's selected within the code.

I am going to try using my ASM code and place it in C using the #asm.

thanks for the ideas tho.

cheers
Guest








PostPosted: Fri Dec 10, 2004 3:12 pm     Reply with quote

Solved it.

Didn't put

#include "16F877A.H"
#fuses HS,NOWDT,NOPUT,NOPROTECT,NOBROWNOUT,NOLVP,NOCPD,NOWRT
#use delay(clock = 4000000)
#use fast_io(C)
#use fast_io(B)


in there. As soon as it went in code worked like a charm. Well newer cleaner code.

thanks for the help.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Dec 10, 2004 3:18 pm     Reply with quote

Quote:
#use fast_io(C)
#use fast_io(B)

Not to get too picky here, but you didn't show those two
lines in the original post of your program. If they were
in fact in your program and you had posted them, we
would have solved the problem for you on the first reply.
Guest








PostPosted: Sun Dec 12, 2004 6:38 pm     Reply with quote

PCM programmer wrote:
Quote:
#use fast_io(C)
#use fast_io(B)

Not to get too picky here, but you didn't show those two
lines in the original post of your program. If they were
in fact in your program and you had posted them, we
would have solved the problem for you on the first reply.


huh???? Confused Confused

I didn't show those two lines in my code in my initial post as they weren't there..... Do you see them in there anywhere?? They never were until I made my last post saying it was working. Do you think I would edit out code and post partial code here and then ask for assistance????

I only just put them in on the day I made that post saying I had solved the problem..

I am really lost as to what you are trying to imply...

I came here for help. posted my exact code as I was using it. Was told the code was fine and it should work. It never did work. I re-wrote it to follow along with my assembly code I have been using for quite a while that DOES work just fine with my hardware and after fighting for a few more days did some digging and added those two lines in just on the day I made my last post. Upon adding those two lines(they weren't there when I made my initial post) everything started working.

sans assistance here.

CCS is new software to me and I was hoping to get honest assistance so as to learn what I was doing wrong. guess I will refrain from coming back here. hopefully there are better sources out there than what was just shown here.

Mark I do thank you for your assistance. You did make an effort to help me out. I do appreciate that.

cheers
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Dec 12, 2004 6:42 pm     Reply with quote

Never mind... It was a misunderstanding. Don't leave the
forum on account of my post.

Edited to add:
I was taught as a child, that if I don't have something good to say,
then don't say it. Everytime I fail to follow that rule, I regret it.
I apologize.
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