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

16X4 Lcd displaying unreadable characters with Pic18f4550

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



Joined: 21 Aug 2014
Posts: 39
Location: India

View user's profile Send private message

16X4 Lcd displaying unreadable characters with Pic18f4550
PostPosted: Thu Aug 21, 2014 1:51 am     Reply with quote

Hey GUYS........
I'm new to pic programming and to this forum. I'm trying to program a pic18f4550 which receives data from virtual terminal(isis) and display it on a 16x4 lcd display....But it displays unreadable characters like α α α α

HERE is my prgm:
Code:


#include <18f4550.h>
#fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN,NOPBADEN
#use delay(clock=48000000)
#use rs232(baud=56000, xmit=PIN_C6,parity=N,rcv=PIN_C7,ERRORS,bits=8)           
#include <flex_lcd416.c>
void main()                                       
{
for(;;)
  {
    lcd_init();                 
    lcd_setcursor_vb(1,1);   
    lcd_putc("Receving Data\n");
    delay_ms(1000);                                                                       
 
    if(kbhit())
    {   
    printf(LCD_PUTC,"%c",getch());
    delay_ms(500);
    output_high(PIN_A0);
    delay_ms(500);
    }                               
    else                                                       
    {                 
    output_low(PIN_A0);
    delay_ms(100);
    }
}
                                           
}     


Here is my LCD Driver:
Code:

             // flex_lcd.c

// These pins are for the Microchip PicDem2-Plus board,
// which is what I used to test the driver.  Change these
// pins to fit your own board.

#define LCD_DB4   PIN_B4                                   
#define LCD_DB5   PIN_B5
#define LCD_DB6   PIN_B6
#define LCD_DB7   PIN_B7
#define LCD_RS    PIN_C0
#define LCD_RW    PIN_C1                       
#define LCD_E     PIN_C2
                                                 
// If you only want a 6-pin interface to your LCD, then
// connect the R/W pin on the LCD to ground, and comment
// out the following line.

#define USE_LCD_RW   1

//========================================

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


int8 const LCD_INIT_STRING[4] =
{
 0x20 | (lcd_type << 2), // Func set: 4-bit, 2 lines, 5x8 dots
 0xc,                    // Display on
 1,                      // Clear display
 6                       // Increment cursor
 };


//-------------------------------------
void lcd_send_nibble(int8 nibble)
{
// Note:  !! converts an integer expression
// to a boolean (1 or 0).
 output_bit(LCD_DB4, !!(nibble & 1));
 output_bit(LCD_DB5, !!(nibble & 2));
 output_bit(LCD_DB6, !!(nibble & 4));
 output_bit(LCD_DB7, !!(nibble & 8));

 delay_cycles(1);
 output_high(LCD_E);
 delay_us(2);
 output_low(LCD_E);
}

//-----------------------------------
// This sub-routine is only called by lcd_read_byte().
// It's not a stand-alone routine.  For example, the
// R/W signal is set high by lcd_read_byte() before
// this routine is called.

#ifdef USE_LCD_RW
int8 lcd_read_nibble(void)
{
int8 retval;
// Create bit variables so that we can easily set
// individual bits in the retval variable.
#bit retval_0 = retval.0
#bit retval_1 = retval.1
#bit retval_2 = retval.2
#bit retval_3 = retval.3

retval = 0;

output_high(LCD_E);
delay_cycles(1);

retval_0 = input(LCD_DB4);
retval_1 = input(LCD_DB5);
retval_2 = input(LCD_DB6);
retval_3 = input(LCD_DB7);

output_low(LCD_E);

return(retval);
}
#endif

//---------------------------------------
// Read a byte from the LCD and return it.

#ifdef USE_LCD_RW
int8 lcd_read_byte(void)
{
int8 low;
int8 high;

output_high(LCD_RW);
delay_cycles(1);

high = lcd_read_nibble();

low = lcd_read_nibble();

return( (high<<4) | low);
}
#endif

//----------------------------------------
// Send a byte to the LCD.
void lcd_send_byte(int8 address, int8 n)
{
output_low(LCD_RS);

#ifdef USE_LCD_RW
while(bit_test(lcd_read_byte(),7)) ;
#else
delay_us(60);
#endif

if(address)
   output_high(LCD_RS);
else
   output_low(LCD_RS);

 delay_cycles(1);

#ifdef USE_LCD_RW
output_low(LCD_RW);
delay_cycles(1);
#endif

output_low(LCD_E);

lcd_send_nibble(n >> 4);
lcd_send_nibble(n & 0xf);
}

//----------------------------
void lcd_init(void)
{
int8 i;

output_low(LCD_RS);

#ifdef USE_LCD_RW
output_low(LCD_RW);
#endif

output_low(LCD_E);

delay_ms(15);

for(i=0 ;i < 3; i++)
   {
    lcd_send_nibble(0x03);
    delay_ms(5);
   }

lcd_send_nibble(0x02);

for(i=0; i < sizeof(LCD_INIT_STRING); i++)
   {
    lcd_send_byte(0, LCD_INIT_STRING[i]);

    // If the R/W signal is not used, then
    // the busy bit can't be polled.  One of
    // the init commands takes longer than
    // the hard-coded delay of 60 us, so in
    // that case, lets just do a 5 ms delay
    // after all four of them.
    #ifndef USE_LCD_RW
    delay_ms(5);
    #endif
   }

}

//----------------------------

void lcd_gotoxy(int8 x, int8 y)
{
int8 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;
   }
}

//------------------------------
#ifdef USE_LCD_RW
char lcd_getc(int8 x, int8 y)
{
char value;

lcd_gotoxy(x,y);

// Wait until busy flag is low.
while(bit_test(lcd_read_byte(),7));

output_high(LCD_RS);
value = lcd_read_byte();
output_low(lcd_RS);

return(value);
}
#endif

void lcd_setcursor_vb(short visible, short blink) {
  lcd_send_byte(0, 0xC|(visible<<1)|blink);
}



here is the output:





PLZZZ HELP ME GUYS.....
_________________
Krazzy7434
temtronic



Joined: 01 Jul 2010
Posts: 9229
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu Aug 21, 2014 5:29 am     Reply with quote

a few things, not all....

1) is this REAL hardware or just an ISIS 'simulation'?
2) 56000 baud is non standard,PC may not be able to generate it withn spec
3) have you correctly wired a MAX232 or equal from PIC to PC?
4)you _must_ add a delay_ms(500); BEFORE the LCD_INIT() function in main()
5)lcd driver is 2 line NOT 4 line
6)schematic doesn't show power or contrast properly connected
7) LCD_init(); should NOT be in the forever loop

recode/recompile/retest/report back

jay
Krazzy7434



Joined: 21 Aug 2014
Posts: 39
Location: India

View user's profile Send private message

PostPosted: Thu Aug 21, 2014 9:29 am     Reply with quote

temtronic wrote:
a few things, not all....

1) is this REAL hardware or just an ISIS 'simulation'?
2) 56000 baud is non standard,PC may not be able to generate it withn spec
3) have you correctly wired a MAX232 or equal from PIC to PC?
4)you _must_ add a delay_ms(500); BEFORE the LCD_INIT() function in main()
5)lcd driver is 2 line NOT 4 line
6)schematic doesn't show power or contrast properly connected
7) LCD_init(); should NOT be in the forever loop

recode/recompile/retest/report back

jay


1)First of all very very thanks for the reply...... Very Happy
2)secondly its an isis simulation for the time being..not tested on a hardware..but soon i have the hardware..
3)i did exactly as you have said...but the output is same....
4)Could you please suggest a good lcd driver for 16x4 lcd...

Thank you....
_________________
Krazzy7434
temtronic



Joined: 01 Jul 2010
Posts: 9229
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu Aug 21, 2014 9:47 am     Reply with quote

there is a 4x20 driver in the 'code library' here, courtesy of PCM programmer or Mr. Ttelmah. It should work fine though may need modifying as you say you have a 16 x 4 LCD.

You need to KNOW that ISIS is full of bugs, errors, and faulty DRCs and CANNOT be trusted as a working, functional 'simulator'. There are dozens of 'threads' here about it's 'quirks' and how programs 'work' in ISIS but NOT in the real world. No one here has the time to rewrite ISIS to be correct and frankly you are far better off getting real PIC and hardware and working on the bench and not in the virtual world.
while I've only got about 20 years with PICs I've never found a 'simulator' that actually works properly.EVERY one fails simple tests I toss at it.

Once you get your real PIC and test, come on back.

hth
jay
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Aug 21, 2014 3:49 pm     Reply with quote

I tested it in hardware (with a 20x4 lcd) and it does work at 48 MHz.
But I suggest adding these two fuses for a more reliable PIC start-up:
Code:
 BORV45, PUT

I used the 20x4 lcd driver (not the 16x2, as you are doing):
http://www.ccsinfo.com/forum/viewtopic.php?t=28268
Krazzy7434



Joined: 21 Aug 2014
Posts: 39
Location: India

View user's profile Send private message

PostPosted: Fri Aug 22, 2014 12:32 am     Reply with quote

PCM programmer wrote:
I tested it in hardware (with a 20x4 lcd) and it does work at 48 MHz.
But I suggest adding these two fuses for a more reliable PIC start-up:
Code:
 BORV45, PUT

I used the 20x4 lcd driver (not the 16x2, as you are doing):
http://www.ccsinfo.com/forum/viewtopic.php?t=28268


Thanks for the reply PIC Programmer... Very Happy
I tried the 4x16 lcd driver and test program for lcd written by you. The test program worked perfectly..... Very Happy ...But when i used getc() the lcd is not displaying anything....but without getc() it works perfectly....

This is what i added.....

Code:

char ch;
ch=getc();
if (kbhit())
{
printf(lcd_putc,"%c",ch);
}


Specifically when getch() is used lcd wont work....!!! Confused Confused
_________________
Krazzy7434
Ttelmah



Joined: 11 Mar 2010
Posts: 19520

View user's profile Send private message

PostPosted: Fri Aug 22, 2014 1:09 am     Reply with quote

The code you have is backwards.....

Code:

   char ch;
   //Do put this at the start of the code section, not inline


   ch=getc(); //This will wait for a character to be available, then get it
   if (kbhit()) //This will therefore always be 'false', since the previous
   //line has read the character, so one is no longer waiting.....
   {
      printf(lcd_putc,"%c",ch);
   }
Krazzy7434



Joined: 21 Aug 2014
Posts: 39
Location: India

View user's profile Send private message

PostPosted: Fri Aug 22, 2014 1:48 am     Reply with quote

Ttelmah wrote:
The code you have is backwards.....

Code:

   char ch;
   //Do put this at the start of the code section, not inline


   ch=getc(); //This will wait for a character to be available, then get it
   if (kbhit()) //This will therefore always be 'false', since the previous
   //line has read the character, so one is no longer waiting.....
   {
      printf(lcd_putc,"%c",ch);
   }


Thanks for the reply...
okay i got it...i changed it to......
Code:

   if (kbhit())
   {
      ch=getc();
      printf(lcd_putc,"%c",ch);
   }


still not working....
i did find my problem with my lcd.
my lcd wont working with following statement.............

1)#use rs232(baud=56000, xmit=PIN_C6,parity=N,rcv=PIN_C7,ERRORS,bits=8)

2)getc();

Any IDEA.... Idea Idea Idea[/code]
_________________
Krazzy7434
temtronic



Joined: 01 Jul 2010
Posts: 9229
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Fri Aug 22, 2014 5:51 am     Reply with quote

As I said in my original post 56000 baud is NOT a standard baudrate ! When the UART is configured,the speed is either 1 or 3% out of spec.Assuming the PC comport is similar, you can easily be 6% out of spec for timing and it won't communicate properly if at all. This is all based on using REAL hardware though I think you're still 'simulating' in ISIS.
Not knowing your actual hardware configuration makes it difficult to debug.
Everyone here _knows_ ISIS is not reliable though.

hth
jay
Krazzy7434



Joined: 21 Aug 2014
Posts: 39
Location: India

View user's profile Send private message

PostPosted: Fri Aug 22, 2014 7:06 am     Reply with quote

temtronic wrote:
As I said in my original post 56000 baud is NOT a standard baudrate ! When the UART is configured,the speed is either 1 or 3% out of spec.Assuming the PC comport is similar, you can easily be 6% out of spec for timing and it won't communicate properly if at all. This is all based on using REAL hardware though I think you're still 'simulating' in ISIS.
Not knowing your actual hardware configuration makes it difficult to debug.
Everyone here _knows_ ISIS is not reliable though.

hth
jay


Thanks for the reply temtronic......
Actually i have ordered a pic18f4550...including dev board and all necessary items. It is on its way....it takes couple of days to reach me. Right now i dont want to waste my time.. thats why I'm using isis..


Thank you....
_________________
Krazzy7434
Ttelmah



Joined: 11 Mar 2010
Posts: 19520

View user's profile Send private message

PostPosted: Fri Aug 22, 2014 8:53 am     Reply with quote

So, have you tried using one of the standard baud rates?. 57600.
Remember that the virtual terminal needs a MAX232 to drive the PIC. Just like a 'real' PC.
Krazzy7434



Joined: 21 Aug 2014
Posts: 39
Location: India

View user's profile Send private message

PostPosted: Fri Aug 22, 2014 9:34 am     Reply with quote

Ttelmah wrote:
So, have you tried using one of the standard baud rates?. 57600.
Remember that the virtual terminal needs a MAX232 to drive the PIC. Just like a 'real' PC.


I dont know how to enable serial communication with Pin C6
and Pin C7. Do serial communication possible with with other pins other than pin C6 and pin C7??
_________________
Krazzy7434
Krazzy7434



Joined: 21 Aug 2014
Posts: 39
Location: India

View user's profile Send private message

PostPosted: Fri Aug 22, 2014 10:27 am     Reply with quote

Hey Guys Finally it worked ....................
I removed my lcd from port C to Port D and removed UART1 from #use 232() Very Happy Very Happy Very Happy Very Happy Very Happy Very Happy Very Happy Very Happy

Here is my final program::

Code:


#include <18f4550.h>
#fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN,NOPBADEN,BORV45,PUT
#use delay(clock=48000000)
#use rs232(baud=9600,xmit=PIN_C6,parity=N,rcv=PIN_C7,ERRORS,bits=8)
#include <flex_lcd416.c>
#include <kbd.c>

void main()
{
int p;
 lcd_init();                 
 lcd_setcursor_vb(1,1);   
 lcd_putc("Receving Data\n");                     
 delay_ms(1000);
 for(;;)
 { 
     output_high(PIN_A0);
     p=getc();
     lcd_putc(p);
     delay_ms(100);
     printf("Data Send");
     delay_ms(100);
     }
  }
   
 


i m Thankful to All the Guys who help me.....thank u very much guys... Very Happy Very Happy
_________________
Krazzy7434
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