AVR (atmega16) LCD interfacing problem

Thread Starter

electron_prince

Joined Sep 19, 2012
96
Hello guys,

I've a program which contains this code

Rich (BB code):
#define rs PB0
#define rw PB1
#define en PB2
and later it is used as

Rich (BB code):
void lcd_cmd(int x)
{
PORTB=x;
PORTB &= ~(1<<rs);
PORTB &= ~(1<<rw);
PORTB |= (1<<en);
_delay_ms(2);
PORTB &= ~(1<<en);
}
My question is why we used PB0, PB1 and PB2 instead of 0, 1 and 2 ??

doesn't the below code make more sense?

Rich (BB code):
void lcd_cmd(int x)
{
PORTB=x;
PORTB &= ~(1<<0);
PORTB &= ~(1<<1);
PORTB |= (1<<2);
_delay_ms(2);
PORTB &= ~(1<<2);
}

Please help, it's very urgent.

Thanks.
 

MrChips

Joined Oct 2, 2009
30,807
Why would someone write
Rich (BB code):
PORTB |= (1<<2);
when it is easier to write
Rich (BB code):
PORTB |= 0x04;
The reason one would choose to write PB0, PB1, and PB2 is that one could easily change the port assignments to different pins, for example PB5, PB6, PB7.
 

Thread Starter

electron_prince

Joined Sep 19, 2012
96
Why would someone write
Rich (BB code):
PORTB |= (1<<2);
when it is easier to write
Rich (BB code):
PORTB |= 0x04;
The reason one would choose to write PB0, PB1, and PB2 is that one could easily change the port assignments to different pins, for example PB5, PB6, PB7.
sorry I didn't mean that. What I wanted to say is why not use this code

Rich (BB code):
#define rs 0 
#define rw 1
 #define en 2
which will produce

Rich (BB code):
void lcd_cmd(int x)
{
PORTB=x;
PORTB &= ~(1<<0);
PORTB &= ~(1<<1);
PORTB |= (1<<2);
_delay_ms(2);
PORTB &= ~(1<<2);
}
why we are writing PB0 instead of 0 in #define
 

MrChips

Joined Oct 2, 2009
30,807
Statements such as
Rich (BB code):
#define rs PB0
belong in a .h header file, not in the main code or module.

This allows one to reuse the same code even though the hardware assignment has changed.

This is simply sound systems design practice.

Better still, one could use
Rich (BB code):
#define LCD_PORT PORTB
#define rs 0
#define rw 1
#define en 2
This would allow you to choose any port and pin.
 

ErnieM

Joined Apr 24, 2011
8,377
why we are writing PB0 instead of 0 in #define
Because people are not computers.

People will at least have a feeling as to what PB0 means, as opposed to "0".

What does "0" mean? That is extremely dependent on the contest.

PB0, given the general context of doing a program, has but one meaning: the lowest pin of a port.

A very wise man once said "Never put a magic number into your code."

"0" is a magic number. "PB0" is well on it's way to being a self-documenting symbol.
 

ErnieM

Joined Apr 24, 2011
8,377
Better still, one could use
Rich (BB code):
#define LCD_PORT PORTB
#define rs 0
#define rw 1
#define en 2
This would allow you to choose any port and pin.
That is NOT better. You are using magic numbers.

While you state WHERE they are used you do not state WHY they are used.
Rich (BB code):
#define rs PB0
#define rw PB1
#define en PB2
Shows everyone the port pin rs uses, while the former is quite obscure.
 

MrChips

Joined Oct 2, 2009
30,807
This will work because PB0, PB1 and PB2 are defined as 0, 1, 2.

Ernie is correct because if you wanted to change to PORTE, for example:


Rich (BB code):
#define LCD_PORT PORTE
#define rs PE5
#define rw PE7
#define en PE2
would also work.

Edit: Of course you do have a problem if rs is PB0 and rw is PE7, for example.
 
Last edited:
Top