Pages: [1]   Go Down
Author Topic: Logical Operators  (Read 3064 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Sr. Member
****
Karma: 7
Posts: 476
what?
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, I would like to speed this code up by talking to the ports direct, it's been a while since i've done this and i've forgotten whether to use
|
&
&&
Code:
int LCD_RS = 49;
int LCD_WR = 48;
int LCD_RD = 47;
int LCD_CS  = 46;
int LCD_REST = 45;

void main_Write_COM( int  DH)
{      
  digitalWrite(LCD_RS, LOW);
  digitalWrite(LCD_CS, LOW);            
  PORTC=DH>>8;      
  PORTA=DH;      
  digitalWrite(LCD_WR, LOW);
  digitalWrite(LCD_WR, HIGH);
  digitalWrite(LCD_CS, HIGH);

}
the result i'd like to end up with is something along the lines of

PORTL && LCD_WR to make pin 48 high
or
PORTL && !LCD_WR to make pin 48 low
or
PORTL & LCD_WR = 1;
im confused

Thanks in advance
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 92
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Pin 48?  I assume you're using the Arduino Mega then. Since the microcontroller differs on some of these boards, it'd be best if you posted the link to the datasheet for the microcontroller on your board.
Logged

0
Offline Offline
Sr. Member
****
Karma: 7
Posts: 476
what?
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes i am using a Mega, my question isn't controller specific though, im using portL for lcd control i know how to send a value to the port, i would like to know how to BSF and BCF specific bits on the port instead of using digitalwrite.
BTW it's an atmega 1280
Logged

Huntsville, Alabama, USA
Offline Offline
Sr. Member
****
Karma: 2
Posts: 327
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

P18F4550,

You want to use:

&   AND
|    OR
~   NOT
^   XOR

To set a bit:

  value | bit

To clear a bit:

  value & ~bit

To toggle a bit:

  value ^ bit

Regards,

-Mike
Logged

0
Offline Offline
Sr. Member
****
Karma: 7
Posts: 476
what?
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks mike, at risk of looking like a moron i've just made the most basic code to set and unset pin 13 LED on the mega, no suprises it doesn't work
Code:
bool on = 1;

void setup()
{
  DDRB = 255;
}

void loop()
{
  PB7 | on;  
  delay(500);
  PB7 | ~on;
  delay(500);
}
what is a suprise is that if i change the PB7's to PB5's for Duemilanove and select Duemilanove from tools menu this code will not compile, I got PB7 from pins_arduino.c in the cores folder
Logged

Huntsville, Alabama, USA
Offline Offline
Sr. Member
****
Karma: 2
Posts: 327
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

P18F4550,

One of the pitfalls of the C/C++ programming language is that it will let you have an expression that does nothing.  Your statements don't change the port.  Here's one way to do it:
Code:
void loop()
{
  PB7 = PB7 | on;
  delay(500);
  PB7 = PB7 & ~on;
  delay(500);
}
Also, note that the AND operator is used to clear bits, not the OR operator.

Regards,

-Mike
Logged

Essex, UK
Offline Offline
Full Member
***
Karma: 4
Posts: 150
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You've got the port macros wrong as well. If you want to set bit 7 in PORTB then you do this:

PORTB |= _BV(PB7);

and to clear it:

PORTB &=~ _BV(PB7);

Logged

Home of the Nokia QVGA TFT LCD hacks: http://andybrown.me.uk

0
Offline Offline
Sr. Member
****
Karma: 7
Posts: 476
what?
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks again Mike, in order for this
Code:
PB7 = PB7 | on;
to compile i had to change it to this
Code:
PB7 == PB7 | on;
It said Lvalue required

Thanks Andy
Code:
void loop()
{
  PORTB |= _BV(PB7);
  delay(500);
  PORTB &=~ _BV(PB7);
  delay(500);
}
Bingo. this flashes Pin 13 Led, this is not the purpose of the code though, i have a 320x240 rgb lcd and using digitalWrite is painfully slow for the purpose of setting control bits and clocking 10x slower http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1279055639/7

Thanks for your help guys

**** Update ****

Thanks again PPL, the display is updating superfast now.
Code:
void cld_write_color(char hh,char ll)
{
  PORTL |= _BV(PL0);  //  digitalWrite(LCD_RS, HIGH);
  PORTL &=~ _BV(PL3);//  digitalWrite(LCD_CS, LOW);                                
  PORTC=hh;      
  PORTA=ll;                                
  PORTL &=~ _BV(PL1);//  digitalWrite(LCD_WR, LOW);
  PORTL |= _BV(PL1);  //  digitalWrite(LCD_WR, HIGH);
  PORTL |= _BV(PL3);  //  digitalWrite(LCD_CS, HIGH);      
}
This is the Display im using http://cgi.ebay.co.uk/ws/eBayISAPI.dll?ViewItem&item=320606628830&ssPageName=STRK:MEWNX:IT#ht_500wt_716
This is a link to the sellers youtube demo video , my display was writing at the same speed, but using this direct port code it's updating superfast now
« Last Edit: December 04, 2010, 10:59:04 am by Funky_Gibbon » Logged

Huntsville, Alabama, USA
Offline Offline
Sr. Member
****
Karma: 2
Posts: 327
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

P18F4550,

The line:
Code:
PB7 == PB7 | on;
does not change any bits.  The "==" operator is used to compare two values.  The '=' operator is used to change a variable.

The reason why the version with the '=' got a compile error is that PB7 is a constant.  I gave the example without checking to see if PB7 was the appropriate term.  As someone else pointed out, it is not.

Glad to hear you got everything working.

Regards,

-Mike
Logged

0
Offline Offline
Sr. Member
****
Karma: 7
Posts: 476
what?
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It's been a while since i've done any logical operations, the last thing i did was a 8ch logic analiser in borland visual c, im suprised to see how some opperation differ between versons of c/c++.
I know this code can go faster by using inline ASM but for the moment im just glad it's working
« Last Edit: December 04, 2010, 05:13:01 pm by Funky_Gibbon » Logged

Pages: [1]   Go Up
Jump to: