Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

problem in avr c codes

Status
Not open for further replies.

engineer khan

Member level 3
Member level 3
Joined
Aug 31, 2012
Messages
66
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Activity points
1,833
what does the following means in lcd interfacing program????(plz anybody help me brothers)

Code:
lcd_cmd(unsigned char item)
{
dataport=item;
ctlrport=(0<<rs)|(0<<rw)|(1<<en);//i dont understand this
delay(1);
ctrlport=(0<<rw)|(0<<rw)|(0<<en);//i dont understand this
delay(50);
return;
}



in 8051 v define this function as
Code:
lcd_cmd(unsigned char item)
{
dataport=item;
rs=0;
rw=0;
en=1;
delay(1);
en=0;
return;
}
 
Last edited by a moderator:

As Pretest said both the code does the same thing.

ctlrport=(0<<rs)|(0<<rw)|(1<<en);//i dont understand this

Here en pin is pulled high in (1<<en)

ctrlport=(0<<rw)|(0<<rw)|(0<<en);//i dont understand this
Here en pin is low in (0<<en)
 

I think the problem is with the complexity here, the author of the code tried to make the code clear but it might become more complicated in an inexperienced's eyes. (S)he tried to emphasize 'rs' and 'rw' bits should be 0 when 'en' is clocked:

Code:
ctlrport=(0<<rs)|(0<<rw)|(1<<en); // rs and rw should be zero when
delay(1);
ctrlport=(0<<rw)|(0<<rw)|(0<<en); // en is clocked.

That code is basically the same as the code below:

Code:
ctlrport=(1<<en);//i dont understand this
delay(1);
ctrlport=(0<<en);//i dont understand this

Only difference is it doesn't tell about the state of 'rs' and 'rw' pins.
 

what << ,>>,| stand for???brief these plz,,,,cant v use the same c codes as for 8051 as i have given in previous post????
 

Have you done a search for C operators?
If not you should, you will find numerous web pages explaining these.
 

what << ,>>,| stand for???brief these plz,,,,cant v use the same c codes as for 8051 as i have given in previous post????

<< shifts the bit left and >> shifts the bit right.

If PORTA = 0b00000001; if it is shifted left it becomes 0b00000010; i.e, 1<<PORTA

if shifted right it becomes 0b10000000; 1>>PORTA

8<<PORTA or 8>>PORTA means the value remains the same.

| is a bitwise OR operator.
 
<< shifts the bit left and >> shifts the bit right.

If PORTA = 0b00000001; if it is shifted left it becomes 0b00000010; i.e, 1<<PORTA

if shifted right it becomes 0b10000000; 1>>PORTA

8<<PORTA or 8>>PORTA means the value remains the same.

| is a bitwise OR operator.

That is not correct the behavior you described is rotate not shift.

For a right shift by 1 on a byte gives 0 and a left shift by 8 gives also 0 if the initial value is 1.
 

<< shifts the bit left and >> shifts the bit right.

If PORTA = 0b00000001; if it is shifted left it becomes 0b00000010; i.e, 1<<PORTA

if shifted right it becomes 0b10000000; 1>>PORTA

| is a bitwise OR operator.

You are using the shift operator the wrong way around, 1<<PORTA means shift 1 left by PORTA value bits.

The shift operation is value>>bits or value<<bits where value is the value you want to shift and bits the number of bit places.

PORTA<<1 means shift PORTA content one bit to the left.

8<<PORTA or 8>>PORTA means the value remains the same.

Apart from this being wrong because 8<<PORTA shifts 8 by PORTA bits even if you write it correctly like PORTA<<8 or PORTA>>8 the result is not what you describe, the shift operation is not rotating the content.
 

Hello,

Code:
ctrlport=(0<<en);
ctlrport=(0<<rs)|(0<<rw)|(1<<en);
Are you sure that this will only set the desired 'en' bit to zero. I would expect all bits in ctrlport sat to zero with these two lines.

To set one bit in a variable to zero would take something like this:
Code:
ctrlport &=~(1<<en)
 

There isn't any operation applied to 'ctrlport' in the 1st line, it's just loaded with a new value which a '0' shifted 'en' number of times which is still a '0'. In the 2nd line it is again loaded with a new value but this time 3 values are ORed. While the '(0<<rs)' and '(0<<rw)' being '0', the 3rd however is '1' shifted 'en' number of times which concludes to a number with only its ENth bit is '1'. And when a number is ORed with '0', it's still the same number. So the result of the 2nd line is 'ctrlport' only with its ENth bit is set. Your code however clears the ENth bit of 'ctrlport' instead of loading it with a new value. Its really a matter of usage really as both codes do similar things but not exactly the same.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top