|
|
View previous topic :: View next topic |
Author |
Message |
Sid2286
Joined: 12 Aug 2010 Posts: 119
|
LCD not working! |
Posted: Fri Dec 27, 2013 5:42 am |
|
|
Hi,
I'm using flex_lcd.c with connection as follows:
Code: |
#define LCD_DB4 PIN_D0
#define LCD_DB5 PIN_D1
#define LCD_DB6 PIN_D2
#define LCD_DB7 PIN_D3
#define LCD_E PIN_D6
#define LCD_RS PIN_D4
#define LCD_RW PIN_D5
|
I have designed a circuit with above pin connections,
now when I write the following code:
Code: |
#include <18F4520.h>
#device ICD=TRUE ADC=12
#fuses HS, NOLVP, NOWDT
#use delay (clock=8M)
#include <flex_lcd.c>
#include <stdlib.h>
void main()
{
lcd_init();
delay_us(30);
while(true)
{
lcd_putc("\fHello World\n");
delay_ms(1000);
output_high(Pin_b4);
delay_ms(1000);
output_low(Pin_B4);
delay_ms(1000);
}
} |
my pin B4 is not blinking as it should, nothing comes on LCD, it goes completely blank.
Whereas when I write only the blinking code by commenting the LCD init and LCD_putc, the pin B4 toggles properly.
Not sure what is going wrong.
Please help.
Sid |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9241 Location: Greensville,Ontario
|
|
Posted: Fri Dec 27, 2013 6:46 am |
|
|
comments:
1) you should have a delay_ms(1000) before the lcd_init();. This allows the LCD module to 'startup and configure itself' before you(the PIC) tries to access it. All LCD modules are similar,needing a small time before you use them. The datasheet will have this spec.Some are faster ,some as slower,but over the years I've settle on 1 second.If you only need to write to the LCD you can save a pin and get rid of the 'am I ready' code.See the driver's comments at the top of the file.
2) I'm assuming those defines are in the flex driver?If so, that's kinda bad practise.Normally you put them in your main() program, just before the #include flex_lcd.c line.Also 'bad' is modifying the original driver.I've always copied the original, calling the copy 'my_flex_lcd.c',make changes in it.That way the original is STILL original!.Also see <>around it, as I use "".The brackets tell the compiler where to find the file,so I wonder if it's finding the original and not the local copy? If it's using the original,then your program would stop and not work correctly. A dump of the listing will quickly tell you what's going on.
3)Some LCD module require contrast to be set with a pot on pins 1,2,3.Do you get black rectangles where the character should be?
As the program doesn't work right( no LCD, no blinking) my guess is either incorrect wiring or corrupted driver and the PIC program is 'hanging' while trying to see if the LCD module is ready.
If you can, post the 'program.lst' file here as it will tell us what's going on.
hth
jay |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19535
|
|
Posted: Fri Dec 27, 2013 7:49 am |
|
|
One thing that would cause a complete hang, would be no power to the LCD.
It'd never go 'ready' so the code would sit waiting for ever.
Also, triple check that you are connecting to DB4 to 7 on the LCD, not DB0 to 3. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Dec 27, 2013 9:57 am |
|
|
Quote: |
2) I'm assuming those defines are in the flex driver?If so, that's kinda bad
practise.Normally you put them in your main() program, just before the \
#include flex_lcd.c line.Also 'bad' is modifying the original driver
|
Temtronic,
The original flex driver, as posted, does require editing the driver file to
set the #define statements for the pins:
http://www.ccsinfo.com/forum/viewtopic.php?t=24661
Later, a forum member added a post showing how he generalizes the
driver to allow it to be used the way you describe:
http://www.ccsinfo.com/forum/viewtopic.php?t=24661&start=97
I myself have done something similar for years, but I've done it in a
more simple way. I edit the flex_lcd.c driver and add a #ifndef/#endif
pair of statements for one of the pin #define statements:
Code: |
#ifndef LCD_DB4
#define LCD_DB4 PIN_D0
#define LCD_DB5 PIN_D1
#define LCD_DB6 PIN_D2
#define LCD_DB7 PIN_D3
#define LCD_E PIN_A1
#define LCD_RS PIN_A3
#define LCD_RW PIN_A2
#endif
|
Then, the user can #include the flex_lcd.c file and put the pin #define
statements above it. Example:
Code: |
#define LCD_DB4 PIN_C0
#define LCD_DB5 PIN_C1
#define LCD_DB6 PIN_C2
#define LCD_DB7 PIN_C3
#define LCD_E PIN_A5
#define LCD_RS PIN_A2
#define LCD_RW PIN_A3
#include "flex_lcd.c"
|
This is all similar to the way CCS does it in their driver files. |
|
|
Sid2286
Joined: 12 Aug 2010 Posts: 119
|
|
Posted: Sun Dec 29, 2013 9:44 pm |
|
|
I did check the connections again and they seems to be fine. I also checked the program on proteus and its working ok, so the connections are ok.
My .lst file is as follows:
Code: |
CCS PCH C Compiler, Version 4.124, 58401 30-Dec-13 09:06
Filename: D:\LCD TEST\lcd_test.lst
ROM used: 624 bytes (2%)
Largest free fragment is 31312
RAM used: 11 (1%) at main() level
22 (1%) worst case
Stack: 6 locations
*
0000: GOTO 01FE
.................... #include <18F4520.h>
.................... //////// Standard Header file for the PIC18F4520 device ////////////////
.................... #device PIC18F4520
.................... #list
....................
.................... #device ICD=TRUE ADC=12
.................... #fuses HS, NOLVP, NOWDT
.................... #use delay (clock=8M)
*
0026: CLRF FEA
0028: MOVLW 0D
002A: MOVWF FE9
002C: MOVF FEF,W
002E: BZ 004A
0030: MOVLW 02
0032: MOVWF 01
0034: CLRF 00
0036: DECFSZ 00,F
0038: BRA 0036
003A: DECFSZ 01,F
003C: BRA 0034
003E: MOVLW 97
0040: MOVWF 00
0042: DECFSZ 00,F
0044: BRA 0042
0046: DECFSZ FEF,F
0048: BRA 0030
004A: RETURN 0
.................... #include "D:\LCD TEST\flex_lcd.c"
.................... // 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_D0 // PIN_D0 b3
.................... #define LCD_DB5 PIN_D1 // PIN_D1 b4
.................... #define LCD_DB6 PIN_D2 // PIN_D2 b5
.................... #define LCD_DB7 PIN_D3 //PIN_D3 b6
....................
.................... #define LCD_E PIN_D6 //A1
.................... #define LCD_RS PIN_D4 //A3
.................... #define LCD_RW PIN_D5
....................
.................... // 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));
004C: BTFSC 14.0
004E: BRA 0054
0050: BCF F8C.0
0052: BRA 0056
0054: BSF F8C.0
0056: BCF F95.0
.................... output_bit(LCD_DB5, !!(nibble & 2));
0058: BTFSC 14.1
005A: BRA 0060
005C: BCF F8C.1
005E: BRA 0062
0060: BSF F8C.1
0062: BCF F95.1
.................... output_bit(LCD_DB6, !!(nibble & 4));
0064: BTFSC 14.2
0066: BRA 006C
0068: BCF F8C.2
006A: BRA 006E
006C: BSF F8C.2
006E: BCF F95.2
.................... output_bit(LCD_DB7, !!(nibble & 8));
0070: BTFSC 14.3
0072: BRA 0078
0074: BCF F8C.3
0076: BRA 007A
0078: BSF F8C.3
007A: BCF F95.3
....................
.................... delay_cycles(1);
007C: NOP
.................... output_high(LCD_E);
007E: BCF F95.6
0080: BSF F8C.6
.................... delay_us(2);
0082: BRA 0084
0084: BRA 0086
.................... output_low(LCD_E);
0086: BCF F95.6
0088: BCF F8C.6
.................... }
008A: RETURN 0
....................
.................... //-----------------------------------
.................... // 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;
008C: CLRF 15
....................
.................... output_high(LCD_E);
008E: BCF F95.6
0090: BSF F8C.6
.................... delay_cycles(1);
0092: NOP
....................
.................... retval_0 = input(LCD_DB4);
0094: BSF F95.0
0096: BCF 15.0
0098: BTFSC F83.0
009A: BSF 15.0
.................... retval_1 = input(LCD_DB5);
009C: BSF F95.1
009E: BCF 15.1
00A0: BTFSC F83.1
00A2: BSF 15.1
.................... retval_2 = input(LCD_DB6);
00A4: BSF F95.2
00A6: BCF 15.2
00A8: BTFSC F83.2
00AA: BSF 15.2
.................... retval_3 = input(LCD_DB7);
00AC: BSF F95.3
00AE: BCF 15.3
00B0: BTFSC F83.3
00B2: BSF 15.3
....................
.................... output_low(LCD_E);
00B4: BCF F95.6
00B6: BCF F8C.6
....................
.................... return(retval);
00B8: MOVFF 15,01
.................... }
00BC: RETURN 0
.................... #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);
00BE: BCF F95.5
00C0: BSF F8C.5
.................... delay_cycles(1);
00C2: NOP
....................
.................... high = lcd_read_nibble();
00C4: RCALL 008C
00C6: MOVFF 01,14
....................
.................... low = lcd_read_nibble();
00CA: RCALL 008C
00CC: MOVFF 01,13
....................
.................... return( (high<<4) | low);
00D0: SWAPF 14,W
00D2: MOVWF 00
00D4: MOVLW F0
00D6: ANDWF 00,F
00D8: MOVF 00,W
00DA: IORWF 13,W
00DC: MOVWF 01
.................... }
00DE: GOTO 00E8 (RETURN)
.................... #endif
....................
.................... //----------------------------------------
.................... // Send a byte to the LCD.
.................... void lcd_send_byte(int8 address, int8 n)
.................... {
.................... output_low(LCD_RS);
00E2: BCF F95.4
00E4: BCF F8C.4
....................
.................... #ifdef USE_LCD_RW
.................... while(bit_test(lcd_read_byte(),7)) ;
00E6: BRA 00BE
00E8: MOVFF 01,13
00EC: BTFSC 01.7
00EE: BRA 00E6
.................... #else
.................... delay_us(60);
.................... #endif
....................
.................... if(address)
00F0: MOVF 11,F
00F2: BZ 00FA
.................... output_high(LCD_RS);
00F4: BCF F95.4
00F6: BSF F8C.4
.................... else
00F8: BRA 00FE
.................... output_low(LCD_RS);
00FA: BCF F95.4
00FC: BCF F8C.4
....................
.................... delay_cycles(1);
00FE: NOP
....................
.................... #ifdef USE_LCD_RW
.................... output_low(LCD_RW);
0100: BCF F95.5
0102: BCF F8C.5
.................... delay_cycles(1);
0104: NOP
.................... #endif
....................
.................... output_low(LCD_E);
0106: BCF F95.6
0108: BCF F8C.6
....................
.................... lcd_send_nibble(n >> 4);
010A: SWAPF 12,W
010C: MOVWF 13
010E: MOVLW 0F
0110: ANDWF 13,F
0112: MOVFF 13,14
0116: RCALL 004C
.................... lcd_send_nibble(n & 0xf);
0118: MOVF 12,W
011A: ANDLW 0F
011C: MOVWF 13
011E: MOVWF 14
0120: RCALL 004C
.................... }
0122: RETURN 0
....................
.................... //----------------------------
.................... void lcd_init(void)
.................... {
.................... int8 i;
....................
.................... output_low(LCD_RS);
0124: BCF F95.4
0126: BCF F8C.4
....................
.................... #ifdef USE_LCD_RW
.................... output_low(LCD_RW);
0128: BCF F95.5
012A: BCF F8C.5
.................... #endif
....................
.................... output_low(LCD_E);
012C: BCF F95.6
012E: BCF F8C.6
....................
.................... delay_ms(15);
0130: MOVLW 0F
0132: MOVWF 0D
0134: RCALL 0026
....................
.................... for(i=0 ;i < 3; i++)
0136: CLRF 0A
0138: MOVF 0A,W
013A: SUBLW 02
013C: BNC 014E
.................... {
.................... lcd_send_nibble(0x03);
013E: MOVLW 03
0140: MOVWF 14
0142: RCALL 004C
.................... delay_ms(5);
0144: MOVLW 05
0146: MOVWF 0D
0148: RCALL 0026
.................... }
014A: INCF 0A,F
014C: BRA 0138
....................
.................... lcd_send_nibble(0x02);
014E: MOVLW 02
0150: MOVWF 14
0152: RCALL 004C
....................
.................... for(i=0; i < sizeof(LCD_INIT_STRING); i++)
0154: CLRF 0A
0156: MOVF 0A,W
0158: SUBLW 03
015A: BNC 016E
.................... {
.................... lcd_send_byte(0, LCD_INIT_STRING[i]);
015C: CLRF 03
015E: MOVF 0A,W
0160: RCALL 0004
0162: MOVWF 0B
0164: CLRF 11
0166: MOVWF 12
0168: RCALL 00E2
....................
.................... // 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
.................... }
016A: INCF 0A,F
016C: BRA 0156
....................
.................... }
016E: GOTO 0226 (RETURN)
....................
.................... //----------------------------
....................
.................... void lcd_gotoxy(int8 x, int8 y)
.................... {
.................... int8 address;
....................
.................... if(y != 1)
0172: DECFSZ 0E,W
0174: BRA 0178
0176: BRA 017E
.................... address = lcd_line_two;
0178: MOVLW 40
017A: MOVWF 0F
.................... else
017C: BRA 0180
.................... address=0;
017E: CLRF 0F
....................
.................... address += x-1;
0180: MOVLW 01
0182: SUBWF 0D,W
0184: ADDWF 0F,F
.................... lcd_send_byte(0, 0x80 | address);
0186: MOVF 0F,W
0188: IORLW 80
018A: MOVWF 10
018C: CLRF 11
018E: MOVWF 12
0190: RCALL 00E2
.................... }
0192: GOTO 01D8 (RETURN)
....................
.................... //-----------------------------
.................... void lcd_putc(char c)
.................... {
.................... switch(c)
.................... {
0196: MOVF 0C,W
0198: XORLW 0C
019A: BZ 01A6
019C: XORLW 06
019E: BZ 01B6
01A0: XORLW 02
01A2: BZ 01C2
01A4: BRA 01CC
.................... case '\f':
.................... lcd_send_byte(0,1);
01A6: CLRF 11
01A8: MOVLW 01
01AA: MOVWF 12
01AC: RCALL 00E2
.................... delay_ms(2);
01AE: MOVLW 02
01B0: MOVWF 0D
01B2: RCALL 0026
.................... break;
01B4: BRA 01D8
....................
.................... case '\n':
.................... lcd_gotoxy(1,2);
01B6: MOVLW 01
01B8: MOVWF 0D
01BA: MOVLW 02
01BC: MOVWF 0E
01BE: BRA 0172
.................... break;
01C0: BRA 01D8
....................
.................... case '\b':
.................... lcd_send_byte(0,0x10);
01C2: CLRF 11
01C4: MOVLW 10
01C6: MOVWF 12
01C8: RCALL 00E2
.................... break;
01CA: BRA 01D8
....................
.................... default:
.................... lcd_send_byte(1,c);
01CC: MOVLW 01
01CE: MOVWF 11
01D0: MOVFF 0C,12
01D4: RCALL 00E2
.................... break;
01D6: BRA 01D8
.................... }
.................... }
01D8: GOTO 01F0 (RETURN)
....................
.................... //------------------------------
.................... #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
....................
.................... #include <stdlib.h>
|
|
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
|
Posted: Mon Dec 30, 2013 12:15 am |
|
|
Whoa whoa whoa...
Is this running on proteus or real hardware!?!
You did read this at the top of the forum list:
http://www.ccsinfo.com/forum/viewtopic.php?t=47549
Yes? _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
Sid2286
Joined: 12 Aug 2010 Posts: 119
|
Resovled |
Posted: Mon Dec 30, 2013 4:21 am |
|
|
Yeah I know proteus is not an idea way to test circuits, but just to confirm if the connections were proper I had to resolve to it.
Anyways the problem is resolved, Just the way temtronic suggested.,
delay before the initialization made it work.
Thanks
Sid |
|
|
gpsmikey
Joined: 16 Nov 2010 Posts: 588 Location: Kirkland, WA
|
|
Posted: Mon Dec 30, 2013 10:19 am |
|
|
I think everyone has been bitten by the "delay needed before init" feature at some point. Good that it is working now.
mikey _________________ mikey
-- you can't have too many gadgets or too much disk space !
old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3 |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19535
|
|
Posted: Mon Dec 30, 2013 11:51 am |
|
|
Yes, very true.
Key I think is that in many cases the delay needs to be longer than the data sheet may suggest.
The PIC in most cases starts to wake at perhaps only 3v. Depending on the rise time of the supply, this can be several mSec before the LCD reaches the point where it starts to wake.
So you can have a display that says in it's data sheet that it needs 300mSec after the supply comes on, before the first command, which refuses to work, till the delay at the start of the code is set to perhaps 400mSec....
I'd suggest using a figure perhaps of 1.5* the data sheet figure to give good reliability.
Have a good New Year. |
|
|
|
|
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
|