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

Need Help for HD44780 Programming with PIC16F876A
Goto page 1, 2, 3, 4  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
kevin5k



Joined: 08 Jan 2007
Posts: 41

View user's profile Send private message

Need Help for HD44780 Programming with PIC16F876A
PostPosted: Mon Jan 08, 2007 10:06 am     Reply with quote

I'm currently a student and I require help for my Project. I am currently stuck at making the LCD Display to initialise. Any gurus out there please help...I'm currently using PCW to compile the codes an MPLAB IDE v7.5 to transfer software into Microcontroller. The Display Panel is still as per default when u power it on everytime (i.e the 1st line is all blacked out with backlight on)

My code for setting up the LCD are as follows:

===========================================
#define LCD_RW PIN_A2
#define LCD_RS PIN_A3
#define LCD_ENA PIN_A5
#define LCD_DATA_0 PIN_B0 /* LCD BIT 0 = DB0 */
#define LCD_DATA_1 PIN_B1 /* LCD BIT 1 = DB1 */
#define LCD_DATA_2 PIN_B2 /* LCD BIT 2 = DB2 */
#define LCD_DATA_3 PIN_B3 /* LCD BIT 3 = DB3 */
#define LCD_DATA_4 PIN_B4 /* LCD BIT 4 = DB4 */
#define LCD_DATA_5 PIN_B5 /* LCD BIT 5 = DB5 */
#define LCD_DATA_6 PIN_B6 /* LCD BIT 6 = DB6 */
#define LCD_DATA_7 PIN_B7 /* LCD BIT 7 = DB7 */
#define clear_lcd 0x01 /* Clear Display */
#define return_home 0x02 /* Cursor to Home position */
#define entry_mode 0x06 /* Normal entry mode */
#define function_set 0x38 /* 8 bit data mode 2 line ( 5x7 font ) */
#define set_dd_line1 0x80 /* Line 1 position 1 */
#define set_dd_line2 0xC0 /* Line 2 position 1 */
#define set_dd_ram 0x80 /* Line 1 position 1 */
#define write_data 0x01 /* With rs = 1 */
#define no_write 0x00 /* With rs = 0 */
#define cursorblink_on 0x0D /* Switch Cursor ON */
#define cursorblink_off 0x0E /* Switch Cursor OFF */
#define lcd_start_on 0x0F /* Display, Cursor, Blink Option ON */
#define port_b_allout 0x00 // Set RB0-7 = LCD D0-7 [Output]

void LCDSetup(void)
{
delay_ms(70);
set_tris_b(port_b_allout);

// Function Set Starts here
output_low(LCD_RS);
delay_ms(1);
output_high(LCD_ENA);
delay_ms(1);
output_b(function_set);
delay_ms(1);
output_low(LCD_ENA);
output_high(LCD_RS);
delay_ms(5);
// Function Set Ends here

// Sets Display on/off control
output_low(LCD_RS);
delay_ms(1);
output_high(LCD_ENA);
delay_ms(1);
output_b(lcd_start_on);
delay_ms(1);
output_low(LCD_ENA);
output_high(LCD_RS);
delay_ms(2);
/* Display settings ends here */

// Clear Display
output_low(LCD_RS);
delay_ms(1);
output_high(LCD_ENA);
delay_ms(1);
output_b(clear_lcd);
delay_ms(2);
output_low(LCD_ENA);
output_high(LCD_RS);
delay_ms(2);
// Clear Display ends here

/* Entry Mode Set
Clears all DRAM data, shifts cursor to left edge.*/
output_low(LCD_RS);
delay_ms(1);
output_high(LCD_ENA);
delay_ms(1);
output_b(entry_mode);
delay_ms(1);
output_low(LCD_ENA);
output_high(LCD_RS);
delay_ms(2);
// Entry Mode Set ends here

// Initialisation End
output_b(port_b_allout); //Using only the address to reset output_b signals to LOW*/
}
===============================================


The main C Program codes are as follows:
===============================================
#include <picsettings.h>
#include <LCD44780.h>
#include <LCDChar.h>
#include <math.h>
//#include <16F876A.h> // PIC Wizard has already included the file

/* PIC PORT defined here ------------------------------*/

#define PortA_Config1 0x2 /* Configure port A ( 1 = input, 0 = output )
RA0 = Speaker [Output]
RA1 = Vo value from Jig & Student Board[Input]
RA2 = R/W Port [Output]
RA3 = Register Select [Output]
RA4 = NA [Output]
RA5 = Enable Line [Output]
*/
/* PIC PORT definition ends here ----------------------*/

void main()
{
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_spi(FALSE);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
set_tris_a(PortA_Config1);
output_a(0x00);
LCDSetup();
}
===============================================

Please advise me where did I go wrong.... Question
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Jan 08, 2007 11:20 am     Reply with quote

Don't write your own LCD driver initially. Use one that is known to work:
http://www.ccsinfo.com/forum/viewtopic.php?t=24661
kevin5k



Joined: 08 Jan 2007
Posts: 41

View user's profile Send private message

PostPosted: Mon Jan 08, 2007 1:23 pm     Reply with quote

Ok. I'm assuming the program works with the HD44780 (2x16) model. Nothing happens, the LCD just remains in its power-up state.

PCM programmer wrote:
Don't write your own LCD driver initially. Use one that is known to work:
http://www.ccsinfo.com/forum/viewtopic.php?t=24661
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Jan 08, 2007 1:41 pm     Reply with quote

Look at the LCD schematic on the PicDem2-Plus board.
http://www.cis.upenn.edu/~lee/06cse480/data/picdem2.pdf
It's in the upper left corner of page 16 (page 20 in the Acrobat Reader).

Do you have your LCD wired correctly, as shown in that schematic ?

The schematic shows two ways to set the contrast. You can either
use a 10K trimpot (and set it near the low end of its range), or you
can use two resistors (10K and 300 ohms). In other words, the
contrast voltage should be somewhere between 0 and 0.5 volts.
The proper setting will depend upon your brand of LCD.

Also, make sure that you're using pins DB4-DB7 on the LCD, as the
data bus pins. In 4-bit mode, it's the upper 4 bits of the LCD data
bus that are used. The drivers in the code library use 4-bit mode.
The schematic linked above also uses 4-bit mode.

One more thing. Make sure you call the lcd_init() function at the start
of main().
kevin5k



Joined: 08 Jan 2007
Posts: 41

View user's profile Send private message

PostPosted: Mon Jan 08, 2007 2:20 pm     Reply with quote

For the Vee, I have just connected it to Gnd for maximum contrast. (Does it affect?)

Pins DB4-7 have already been wired. I have also called the lcd_init();

My redefined main C program file has the following:
===============================================
#include <picsettings.h>
#include <LCDChar.h>
#include <math.h>

#define PortA_Config1 0x2

#define LCD_DB4 PIN_B4
#define LCD_DB5 PIN_B5
#define LCD_DB6 PIN_B6
#define LCD_DB7 PIN_B7

#define LCD_E PIN_A5
#define LCD_RS PIN_A3
#define LCD_RW PIN_A2
.
.
.
.
.
.
void main()
{
set_tris_a(PortA_Config1);
output_a(0x00);
lcd_init();

}
===============================================


Just for you to note I have included these in the <picsettings.h> file

===============================================
#use delay(clock=4000000,RESTART_WDT)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
===============================================

For pins DB4-7, I have connected it to pins RB4-7 of the PIC. Please advise...Thanks for answering my queries so far...


PCM programmer wrote:
Look at the LCD schematic on the PicDem2-Plus board.

http://www.cis.upenn.edu/~lee/06cse480/data/picdem2.pdf
It's in the upper left corner of page 16 (page 20 in the Acrobat Reader).

Do you have your LCD wired correctly, as shown in that schematic ?

The schematic shows two ways to set the contrast. You can either
use a 10K trimpot (and set it near the low end of its range), or you
can use two resistors (10K and 300 ohms). In other words, the
contrast voltage should be somewhere between 0 and 0.5 volts.
The proper setting will depend upon your brand of LCD.

Also, make sure that you're using pins DB4-DB7 on the LCD, as the
data bus pins. In 4-bit mode, it's the upper 4 bits of the LCD data
bus that are used. The drivers in the code library use 4-bit mode.
The schematic linked above also uses 4-bit mode.

One more thing. Make sure you call the lcd_init() function at the start
of main().
kevin5k



Joined: 08 Jan 2007
Posts: 41

View user's profile Send private message

PostPosted: Mon Jan 08, 2007 3:26 pm     Reply with quote

By the way PCM Programmer, is there a way to check if the PIC is sending out the correct signals? I am thinking of linking it up to an oscilloscope. I welcome any suggestions...
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Jan 08, 2007 4:08 pm     Reply with quote

Are you using #fast_io mode ? You shouldn't do that with this driver.
Don't set the TRIS on port A. Don't output to Port A. Just do the
call to lcd_init(). Also don't let the code fall off the end of main().

Use a test program similar to this. Notice how simple it is.
Code:

#include <16F876A.H>
#fuses HS, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 20000000)

#include "flex_lcd.c"
   
//==============================
void main()
{
lcd_init();

printf(lcd_putc, "\fHello World\nLine 2");

while(1);
}


Also, you're using pins B6 and B7 for the LCD. Keep in mind that
your debugger or programmer (ICD, ICD2, etc.) uses those pins.
Notice the #fuses settings. Make sure you have the NOLVP fuse.
kevin5k



Joined: 08 Jan 2007
Posts: 41

View user's profile Send private message

PostPosted: Mon Jan 08, 2007 4:46 pm     Reply with quote

I did as advised. But still, there isn't anything else showing up.

I have reconfigured the pins to:
====================
#define LCD_DB4 PIN_B0
#define LCD_DB5 PIN_B1
#define LCD_DB6 PIN_B2
#define LCD_DB7 PIN_B3
====================

So my main file will have the following codes:
====================
#include <16F876A>
#fuses HS, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 4000000) // 4MHz resonator used
#include "flex_lcd.c"

void main()
{
lcd_init();
printf(lcd_putc, "\fHello World\nLine2");
while (1);
}
====================

Errors during compilation included the following
-------------------------------------
Error 128 "C:\....\flex_lcd.c" Line 27(1,42): A #DEVICE required before this line
-------------------------------------

I know for sure the LCD driver is working as it has the usual blacked out boxes on the first line.

Am really puzzled why. I hope i it isn't the microcontroller that needs replacing...

Listening attentively to your answers....

PCM programmer wrote:
Are you using #fast_io mode ? You shouldn't do that with this driver.
Don't set the TRIS on port A. Don't output to Port A. Just do the
call to lcd_init(). Also don't let the code fall off the end of main().

Use a test program similar to this. Notice how simple it is.
Code:

#include <16F876A>
#fuses HS, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 20000000)

#include "flex_lcd.c"
   
//==============================
void main()
{
lcd_init();

printf(lcd_putc, "\fHello World\nLine 2");

while(1);
}


Also, you're using pins B6 and B7 for the LCD. Keep in mind that
your debugger or programmer (ICD, ICD2, etc.) uses those pins.
Notice the #fuses settings. Make sure you have the NOLVP fuse.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Jan 08, 2007 5:00 pm     Reply with quote

Quote:
Errors during compilation included the following
-------------------------------------
Error 128 "C:\....\flex_lcd.c" Line 27(1,42): A #DEVICE required before this line

You will get that error if you "added" the Flex_lcd.c file to the list
of source files in MPLAB.
You should only have the main source file listed in the Source Files list.
You can add the Flex_lcd.c file to the "Other Files" section in the Project
files window.


Regarding the black boxes, read this thread:
http://www.ccsinfo.com/forum/viewtopic.php?t=27246

Post your compiler version. It's a number such as 3.191, 3.249, 4.018,
etc. You can find it at the top of your .LST file, which is in your project
directly. The "x.xxx" is the complete version number. You don't need
to post any additional numbers that follow after it in the .LST file.
kevin5k



Joined: 08 Jan 2007
Posts: 41

View user's profile Send private message

PostPosted: Mon Jan 08, 2007 5:08 pm     Reply with quote

Ok, I have corrected the file.

My compiler version is 4.016. I just found some updates for the compiler. So i'm gonna update it now....

I'll check out the link u gave me.

PCM programmer wrote:
Quote:
Errors during compilation included the following
-------------------------------------
Error 128 "C:\....\flex_lcd.c" Line 27(1,42): A #DEVICE required before this line

You will get that error if you "added" the Flex_lcd.c file to the list
of source files in MPLAB.
You should only have the main source file listed in the Source Files list.
You can add the Flex_lcd.c file to the "Other Files" section in the Project
files window.


Regarding the black boxes, read this thread:
http://www.ccsinfo.com/forum/viewtopic.php?t=27246

Post your compiler version. It's a number such as 3.191, 3.249, 4.018,
etc. You can find it at the top of your .LST file, which is in your project
directly. The "x.xxx" is the complete version number. You don't need
to post any additional numbers that follow after it in the .LST file.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Jan 08, 2007 5:21 pm     Reply with quote

I have tested the code on a PicDem2-Plus board with PCM vs. 4.016
and it works.
kevin5k



Joined: 08 Jan 2007
Posts: 41

View user's profile Send private message

PostPosted: Mon Jan 08, 2007 5:25 pm     Reply with quote

Tks...I'm doing my own breadboarding now...Erm, i somehow get the 4 identical error messages

*** Error 12 "C:\...\flex_lcd.c" Line 48(2,10): Undefined identifier delay_ms

Any way to resolve it?

PCM programmer wrote:
I have tested the code on a PicDem2-Plus board with PCM vs. 4.016
and it works.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Jan 08, 2007 5:33 pm     Reply with quote

You need to follow my example. Everything in the example is important.
The order of the statements above main() is important.

The #include files must go below the #use delay() statement.
That statement invokes the CCS delay library code. Any #include
files that use delay statements must occur after the #use delay() line.
Put all #include files below that line. (except for the main .H file for
the PIC, which goes as the first line in the file.) Example:
Code:

#include <16F876A.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 4000000)

#include "flex_lcd.c"
kevin5k



Joined: 08 Jan 2007
Posts: 41

View user's profile Send private message

PostPosted: Mon Jan 08, 2007 5:44 pm     Reply with quote

Ok. I have corrected my programming errors and reconnected all the LCD lines to prevent hardware error....

Just plugged in the PIC to the board and fired it up..........*holds breath*

Nothing's happening...I guess it just might be the microcontroller which has probably died... Just curious, should the resonator not function properly, would it cause the PIC not to work?

I have connected the R/W line to Gnd.

PCM programmer wrote:
You need to follow my example. Everything in the example is important.
The order of the statements above main() is important.

The #include files must go below the #use delay() statement.
That statement invokes the CCS delay library code. Any #include
files that use delay statements must occur after the #use delay() line.
Put all #include files below that line. (except for the main .H file for
the PIC, which goes as the first line in the file.) Example:
Code:

#include <16F876A>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 4000000)

#include "flex_lcd.c"
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Jan 08, 2007 5:49 pm     Reply with quote

Quote:
I have connected the R/W line to Gnd.

Did you comment out the #define statement which enables that line ?
You need to do that if you connect the LCD's R/W line to ground.

You should comment it out like this. Then re-compile and re-program
the PIC:
Code:
//  #define USE_RW_PIN   1
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, 3, 4  Next
Page 1 of 4

 
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