Go Down

Topic: Logical Operators (Read 3528 times) previous topic - next topic

P18F4550

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: [Select]
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

BKnight760

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.

P18F4550

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

Mike Murdock

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

P18F4550

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: [Select]
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

Mike Murdock

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: [Select]

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

Andy Brown

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);

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

P18F4550

#7
Dec 04, 2010, 04:24 pm Last Edit: Dec 04, 2010, 04:59 pm by Funky_Gibbon Reason: 1
Thanks again Mike, in order for this
Code: [Select]
PB7 = PB7 | on;to compile i had to change it to this
Code: [Select]
PB7 == PB7 | on; It said Lvalue required

Thanks Andy
Code: [Select]
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: [Select]
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 videohttp://www.youtube.com/watch?v=5SaWc10VuXk, my display was writing at the same speed, but using this direct port code it's updating superfast now

Mike Murdock

P18F4550,

The line:
Code: [Select]
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

P18F4550

#9
Dec 04, 2010, 10:56 pm Last Edit: Dec 04, 2010, 11:13 pm by Funky_Gibbon Reason: 1
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

Go Up