EmbDev.net

Forum: ARM programming with GCC/GNU tools HD44780 on Atmega32 with Peter Fleurys library


von Leonhard H. (bitfinder)


Rate this post
useful
not useful
Hi,
I am a complete beginner at programming microcontrollers, I programm 
Java decently though.

I am desperately trying to print strings to a 16x4 LCD display (HD44780) 
using Peter Fleury's library for days, but I am not succeeding.

Since I am using the Atmega32 (8MHz clock frequency), I try to access 
the display with the 4-bit mode. My setup is working for Bascom, so the 
harware should not cause my trouble.


Here is how i connected the LCD:
1
DB7 -> PB0
2
DB6 -> PB1
3
DB5 -> PB2
4
DB4 -> PB3
5
E -> PB4
6
RS -> PB5
7
RW -> not connected to the microcontroller, but to ground
8
(I don't know how to set the variable in the code)

And I have a LED at PD6 (low active).


The configurations in the lcd.h file:
1
/** 
2
 *  @name  Definitions for MCU Clock Frequency
3
 *  Adapt the MCU clock frequency in Hz to your target. 
4
 */
5
#define XTAL 8000000              /**< clock frequency in Hz, used to calculate delay timer */
6
7
8
/**
9
 * @name  Definition for LCD controller type
10
 * Use 0 for HD44780 controller, change to 1 for displays with KS0073 controller.
11
 */
12
#define LCD_CONTROLLER_KS0073 0  /**< Use 0 for HD44780 controller, 1 for KS0073 controller */
13
14
/** 
15
 *  @name  Definitions for Display Size 
16
 *  Change these definitions to adapt setting to your display
17
 */
18
#define LCD_LINES           4     /**< number of visible lines of the display */
19
#define LCD_DISP_LENGTH    16     /**< visibles characters per line of the display */
20
#define LCD_LINE_LENGTH  0x40     /**< internal line length of the display    */
21
#define LCD_START_LINE1  0x00     /**< DDRAM address of first char of line 1 */
22
#define LCD_START_LINE2  0x40     /**< DDRAM address of first char of line 2 */
23
#define LCD_START_LINE3  0x10     /**< DDRAM address of first char of line 3 */
24
#define LCD_START_LINE4  0x50     /**< DDRAM address of first char of line 4 */
25
#define LCD_WRAP_LINES      0     /**< 0: no wrap, 1: wrap at end of visibile line */
26
27
28
#define LCD_IO_MODE      1         /**< 0: memory mapped mode, 1: IO port mode */
29
#if LCD_IO_MODE
30
/**
31
 *  @name Definitions for 4-bit IO mode
32
 *  Change LCD_PORT if you want to use a different port for the LCD pins.
33
 *
34
 *  The four LCD data lines and the three control lines RS, RW, E can be on the 
35
 *  same port or on different ports. 
36
 *  Change LCD_RS_PORT, LCD_RW_PORT, LCD_E_PORT if you want the control lines on
37
 *  different ports. 
38
 *
39
 *  Normally the four data lines should be mapped to bit 0..3 on one port, but it
40
 *  is possible to connect these data lines in different order or even on different
41
 *  ports by adapting the LCD_DATAx_PORT and LCD_DATAx_PIN definitions.
42
 *  
43
 */
44
#define LCD_PORT         PORTB        /**< port for the LCD lines   */
45
#define LCD_DATA0_PORT   LCD_PORT     /**< port for 4bit data bit 0 */
46
#define LCD_DATA1_PORT   LCD_PORT     /**< port for 4bit data bit 1 */
47
#define LCD_DATA2_PORT   LCD_PORT     /**< port for 4bit data bit 2 */
48
#define LCD_DATA3_PORT   LCD_PORT     /**< port for 4bit data bit 3 */
49
#define LCD_DATA0_PIN    3            /**< pin for 4bit data bit 0  */
50
#define LCD_DATA1_PIN    2            /**< pin for 4bit data bit 1  */
51
#define LCD_DATA2_PIN    1            /**< pin for 4bit data bit 2  */
52
#define LCD_DATA3_PIN    0            /**< pin for 4bit data bit 3  */
53
#define LCD_RS_PORT      LCD_PORT     /**< port for RS line         */
54
#define LCD_RS_PIN       5            /**< pin  for RS line         */
55
#define LCD_RW_PORT      LCD_PORT     /**< port for RW line         */
56
#define LCD_RW_PIN       5            /**< pin  for RW line         */
57
#define LCD_E_PORT       LCD_PORT     /**< port for Enable line     */
58
#define LCD_E_PIN        4            /**< pin  for Enable line     */
59
60
#elif defined(__AVR_AT90S4414__) || defined(__AVR_AT90S8515__) || defined(__AVR_ATmega64__) || \
61
      defined(__AVR_ATmega8515__)|| defined(__AVR_ATmega103__) || defined(__AVR_ATmega128__) || \
62
      defined(__AVR_ATmega161__) || defined(__AVR_ATmega162__)
63
/*
64
 *  memory mapped mode is only supported when the device has an external data memory interface
65
 */
66
#define LCD_IO_DATA      0xC000    /* A15=E=1, A14=RS=1                 */
67
#define LCD_IO_FUNCTION  0x8000    /* A15=E=1, A14=RS=0                 */
68
#define LCD_IO_READ      0x0100    /* A8 =R/W=1 (R/W: 1=Read, 0=Write   */
69
#else
70
#error "external data memory interface not available for this device, use 4-bit IO port mode"
71
72
#endif
73
74
75
/**
76
 *  @name Definitions for LCD command instructions
77
 *  The constants define the various LCD controller instructions which can be passed to the 
78
 *  function lcd_command(), see HD44780 data sheet for a complete description.
79
 */
80
81
/* instruction register bit positions, see HD44780U data sheet */
82
#define LCD_CLR               0      /* DB0: clear display                  */
83
#define LCD_HOME              1      /* DB1: return to home position        */
84
#define LCD_ENTRY_MODE        2      /* DB2: set entry mode                 */
85
#define LCD_ENTRY_INC         1      /*   DB1: 1=increment, 0=decrement     */
86
#define LCD_ENTRY_SHIFT       0      /*   DB2: 1=display shift on           */
87
#define LCD_ON                3      /* DB3: turn lcd/cursor on             */
88
#define LCD_ON_DISPLAY        2      /*   DB2: turn display on              */
89
#define LCD_ON_CURSOR         1      /*   DB1: turn cursor on               */
90
#define LCD_ON_BLINK          0      /*     DB0: blinking cursor ?          */
91
#define LCD_MOVE              4      /* DB4: move cursor/display            */
92
#define LCD_MOVE_DISP         3      /*   DB3: move display (0-> cursor) ?  */
93
#define LCD_MOVE_RIGHT        2      /*   DB2: move right (0-> left) ?      */
94
#define LCD_FUNCTION          5      /* DB5: function set                   */
95
#define LCD_FUNCTION_8BIT     4      /*   DB4: set 8BIT mode (0->4BIT mode) */
96
#define LCD_FUNCTION_2LINES   3      /*   DB3: two lines (0->one line)      */
97
#define LCD_FUNCTION_10DOTS   2      /*   DB2: 5x10 font (0->5x7 font)      */
98
#define LCD_CGRAM             6      /* DB6: set CG RAM address             */
99
#define LCD_DDRAM             7      /* DB7: set DD RAM address             */
100
#define LCD_BUSY              7      /* DB7: LCD is busy                    */
101
102
/* set entry mode: display shift on/off, dec/inc cursor move direction */
103
#define LCD_ENTRY_DEC            0x04   /* display shift off, dec cursor move dir */
104
#define LCD_ENTRY_DEC_SHIFT      0x05   /* display shift on,  dec cursor move dir */
105
#define LCD_ENTRY_INC_           0x06   /* display shift off, inc cursor move dir */
106
#define LCD_ENTRY_INC_SHIFT      0x07   /* display shift on,  inc cursor move dir */
107
108
/* display on/off, cursor on/off, blinking char at cursor position */
109
#define LCD_DISP_OFF             0x08   /* display off                            */
110
#define LCD_DISP_ON              0x0C   /* display on, cursor off                 */
111
#define LCD_DISP_ON_BLINK        0x0D   /* display on, cursor off, blink char     */
112
#define LCD_DISP_ON_CURSOR       0x0E   /* display on, cursor on                  */
113
#define LCD_DISP_ON_CURSOR_BLINK 0x0F   /* display on, cursor on, blink char      */
114
115
/* move cursor/shift display */
116
#define LCD_MOVE_CURSOR_LEFT     0x10   /* move cursor left  (decrement)          */
117
#define LCD_MOVE_CURSOR_RIGHT    0x14   /* move cursor right (increment)          */
118
#define LCD_MOVE_DISP_LEFT       0x18   /* shift display left                     */
119
#define LCD_MOVE_DISP_RIGHT      0x1C   /* shift display right                    */
120
121
/* function set: set interface data length and number of display lines */
122
#define LCD_FUNCTION_4BIT_1LINE  0x20   /* 4-bit interface, single line, 5x7 dots */
123
#define LCD_FUNCTION_4BIT_2LINES 0x28   /* 4-bit interface, dual line,   5x7 dots */
124
#define LCD_FUNCTION_8BIT_1LINE  0x30   /* 8-bit interface, single line, 5x7 dots */
125
#define LCD_FUNCTION_8BIT_2LINES 0x38   /* 8-bit interface, dual line,   5x7 dots */
126
127
128
#define LCD_MODE_DEFAULT     ((1<<LCD_ENTRY_MODE) | (1<<LCD_ENTRY_INC) )

I am not sure about what i have to change here :/

I can let this LED blink, but as soon as I call lcd_init(LCD_DISP_ON);, 
my programm seems to freeze.

Hopefully, you can help me, I don't know what to do anymore, I read a 
lot and tried a lot...

Thanks a lot,
Bitfinder

: Edited by User
von isidor (Guest)


Rate this post
useful
not useful
Leonhard Holland wrote:
> I can let this LED blink, but as soon as I call lcd_init(LCD_DISP_ON);,
> my programm seems to freeze.

Any warnings at compile time ?

Do you have set up the correct F_CPU in your project file?
Is your CPU correctly fused regarding the oscillator
frequency an oscillator type?

If the program hangs during LCD Init it could be because the
delay times are not calculated correctly at compile time.

von Leonhard H. (bitfinder)


Rate this post
useful
not useful
When I build, I have no warnings.

The SUT_CKSEL fuse bit is set to INTRCOSC_8MHZ_6CK_64MS.

In the lcd.h file, there is the line
1
#define XTAL 8000000

Additionally, in my programm, there is this linee
1
#define F_CPU 8000000

Frankly, i don't really know where's the difference here.


Here the code of the lcd_init(...):
1
void lcd_init(uint8_t dispAttr)
2
{
3
#if LCD_IO_MODE
4
    /*
5
     *  Initialize LCD to 4 bit I/O mode
6
     */
7
     
8
    if ( ( &LCD_DATA0_PORT == &LCD_DATA1_PORT) && ( &LCD_DATA1_PORT == &LCD_DATA2_PORT ) && ( &LCD_DATA2_PORT == &LCD_DATA3_PORT )
9
      && ( &LCD_RS_PORT == &LCD_DATA0_PORT) && ( &LCD_RW_PORT == &LCD_DATA0_PORT) && (&LCD_E_PORT == &LCD_DATA0_PORT)
10
      && (LCD_DATA0_PIN == 0 ) && (LCD_DATA1_PIN == 1) && (LCD_DATA2_PIN == 2) && (LCD_DATA3_PIN == 3) 
11
      && (LCD_RS_PIN == 4 ) && (LCD_RW_PIN == 5) && (LCD_E_PIN == 6 ) )
12
    {
13
        /* configure all port bits as output (all LCD lines on same port) */
14
        DDR(LCD_DATA0_PORT) |= 0x7F;
15
    }
16
    else if ( ( &LCD_DATA0_PORT == &LCD_DATA1_PORT) && ( &LCD_DATA1_PORT == &LCD_DATA2_PORT ) && ( &LCD_DATA2_PORT == &LCD_DATA3_PORT )
17
           && (LCD_DATA0_PIN == 0 ) && (LCD_DATA1_PIN == 1) && (LCD_DATA2_PIN == 2) && (LCD_DATA3_PIN == 3) )
18
    {
19
        /* configure all port bits as output (all LCD data lines on same port, but control lines on different ports) */
20
        DDR(LCD_DATA0_PORT) |= 0x0F;
21
        DDR(LCD_RS_PORT)    |= _BV(LCD_RS_PIN);
22
        DDR(LCD_RW_PORT)    |= _BV(LCD_RW_PIN);
23
        DDR(LCD_E_PORT)     |= _BV(LCD_E_PIN);
24
    }
25
    else
26
    {
27
        /* configure all port bits as output (LCD data and control lines on different ports */
28
        DDR(LCD_RS_PORT)    |= _BV(LCD_RS_PIN);
29
        DDR(LCD_RW_PORT)    |= _BV(LCD_RW_PIN);
30
        DDR(LCD_E_PORT)     |= _BV(LCD_E_PIN);
31
        DDR(LCD_DATA0_PORT) |= _BV(LCD_DATA0_PIN);
32
        DDR(LCD_DATA1_PORT) |= _BV(LCD_DATA1_PIN);
33
        DDR(LCD_DATA2_PORT) |= _BV(LCD_DATA2_PIN);
34
        DDR(LCD_DATA3_PORT) |= _BV(LCD_DATA3_PIN);
35
    }
36
    delay(16000);        /* wait 16ms or more after power-on       */
37
    
38
    /* initial write to lcd is 8bit */
39
    LCD_DATA1_PORT |= _BV(LCD_DATA1_PIN);  // _BV(LCD_FUNCTION)>>4;
40
    LCD_DATA0_PORT |= _BV(LCD_DATA0_PIN);  // _BV(LCD_FUNCTION_8BIT)>>4;
41
    lcd_e_toggle();
42
    delay(4992);         /* delay, busy flag can't be checked here */
43
   
44
    /* repeat last command */ 
45
    lcd_e_toggle();      
46
    delay(64);           /* delay, busy flag can't be checked here */
47
    
48
    /* repeat last command a third time */
49
    lcd_e_toggle();      
50
    delay(64);           /* delay, busy flag can't be checked here */
51
52
    /* now configure for 4bit mode */
53
    LCD_DATA0_PORT &= ~_BV(LCD_DATA0_PIN);   // LCD_FUNCTION_4BIT_1LINE>>4
54
    lcd_e_toggle();
55
    delay(64);           /* some displays need this additional delay */
56
    
57
    /* from now the LCD only accepts 4 bit I/O, we can use lcd_command() */    
58
#else
59
    /*
60
     * Initialize LCD to 8 bit memory mapped mode
61
     */
62
    
63
    /* enable external SRAM (memory mapped lcd) and one wait state */        
64
    MCUCR = _BV(SRE) | _BV(SRW);
65
66
    /* reset LCD */
67
    delay(16000);                           /* wait 16ms after power-on     */
68
    lcd_write(LCD_FUNCTION_8BIT_1LINE,0);   /* function set: 8bit interface */                   
69
    delay(4992);                            /* wait 5ms                     */
70
    lcd_write(LCD_FUNCTION_8BIT_1LINE,0);   /* function set: 8bit interface */                 
71
    delay(64);                              /* wait 64us                    */
72
    lcd_write(LCD_FUNCTION_8BIT_1LINE,0);   /* function set: 8bit interface */                
73
    delay(64);                              /* wait 64us                    */
74
#endif
75
76
#if KS0073_4LINES_MODE
77
    /* Display with KS0073 controller requires special commands for enabling 4 line mode */
78
  lcd_command(KS0073_EXTENDED_FUNCTION_REGISTER_ON);
79
  lcd_command(KS0073_4LINES_MODE);
80
  lcd_command(KS0073_EXTENDED_FUNCTION_REGISTER_OFF);
81
#else
82
    lcd_command(LCD_FUNCTION_DEFAULT);      /* function set: display lines  */
83
#endif
84
    lcd_command(LCD_DISP_OFF);              /* display off                  */
85
    lcd_clrscr();                           /* display clear                */ 
86
    lcd_command(LCD_MODE_DEFAULT);          /* set entry mode               */
87
    lcd_command(dispAttr);                  /* display/cursor control       */
88
89
}/* lcd_init */

von jurs (Guest)


Rate this post
useful
not useful
1
#define LCD_RS_PORT      LCD_PORT     /**< port for RS line         */
2
#define LCD_RS_PIN       5            /**< pin  for RS line         */
3
#define LCD_RW_PORT      LCD_PORT     /**< port for RW line         */
4
#define LCD_RW_PIN       5            /**< pin  for RW line         */

I don't know the library, but I suppose, that this cannot be the correct 
declaration when not using the RW line.

I think that you must declare some unused port pin for RW, even if it is 
not connected to your controller, or that you must make the library 
ignore RW, but not use the same line for RW and RS.

von Leonhard H. (bitfinder)


Rate this post
useful
not useful
Thanks,

I just tried out something, previously, this line said:
1
#define LCD_RW_PIN       6            /**< pin  for RW line         */

Either way, the result is the same :/

I didn't find a way to tell this library not to use the RW line...

Furthermore, I am not sure whether these settings are correct (because 
the amount of the characters per line was 20 in the original version) 
and I don't even know where to look it up.
1
#define LCD_LINES           4     /**< number of visible lines of the display */
2
#define LCD_DISP_LENGTH    16     /**< visibles characters per line of the display */
3
#define LCD_LINE_LENGTH  0x40     /**< internal line length of the display    */
4
#define LCD_START_LINE1  0x00     /**< DDRAM address of first char of line 1 */
5
#define LCD_START_LINE2  0x40     /**< DDRAM address of first char of line 2 */
6
#define LCD_START_LINE3  0x10     /**< DDRAM address of first char of line 3 */
7
#define LCD_START_LINE4  0x50     /**< DDRAM address of first char of line 4 */


Actually I wouldn't mind using another library. I just want to display 
stuff :D. However, I didn't find any library that worked for me.

von Dieter F. (Guest)


Rate this post
useful
not useful
Use the lib from thiy guy

Beitrag "mal wieder ein LCD am Atmega8"

He changed the original fleury-lib for use without R/W-Line (not 
possible with the original lib)

LCD_READ_REQUIRED

von Leonhard H. (bitfinder)


Attached files:

Rate this post
useful
not useful
Thank you very much!

The Problem was, that the original library could not use the R/W pin 
(because it was not connected). Therefore Peter Fleury's routine could 
not read the busy-bit (and waited forever while initiating).

The edited version posted in the Thread mentioned above can be 
configured, so it does not use the RW line.

I just set LCD_READ_REQUIRED to 0.

In the attachment, you will find the library (for all those who have the 
same problem).

Thank you all (especially Hr. Frohnapfel ;)

Please log in before posting. Registration is free and takes only a minute.
Existing account
Do you have a Google/GoogleMail account? No registration required!
Log in with Google account
No account? Register here.