Help me! Please explain this code for me

Your source for all things Atmel.

Moderator: phalanx

Post Reply
vantusaonho
Posts: 12
Joined: Sun Dec 06, 2009 9:37 am

Help me! Please explain this code for me

Post by vantusaonho » Fri Jan 08, 2010 1:57 am

Code: Select all

void CParallelPortDlg::UpdatePins()
{
     int reg;
     reg=_inp(STATUS);

     if((reg & 0x40)==0) m_pin10=0;     else m_pin10=1;
     if((reg & 0x80)==0) m_pin11=0;     else m_pin11=1;
     if((reg & 0x20)==0) m_pin12=0;     else m_pin12=1;    
     if((reg & 0x10)==0) m_pin13=0;     else m_pin13=1;
     if((reg & 0x08)==0) m_pin15=0;     else m_pin15=1;
     //////////
     reg=_inp(DATA);
    
     if((reg & 0x01)==0) m_pin2=0;     else m_pin2=1;
     if((reg & 0x02)==0) m_pin3=0;     else m_pin3=1;    
     if((reg & 0x04)==0) m_pin4=0;     else m_pin4=1;
     if((reg & 0x08)==0) m_pin5=0;     else m_pin5=1;
     if((reg & 0x10)==0) m_pin6=0;     else m_pin6=1;
     if((reg & 0x20)==0) m_pin7=0;     else m_pin7=2;
     if((reg & 0x40)==0) m_pin8=0;     else m_pin8=1;
     if((reg & 0x80)==0) m_pin9=0;     else m_pin9=1;
     //////
     reg = _inp(CONTROL);

     if((reg & 0x01)==0) m_pin1=0;      else m_pin1=1;
     if((reg & 0x02)==0) m_pin14=0;     else m_pin14=1;
     if((reg & 0x04)==0) m_pin16=0;     else m_pin16=1;
     if((reg & 0x08)==0) m_pin17=0;     else m_pin17=1;

     UpdateData(FALSE);
}

This is code that programming LPT port but i dont understand where

Code: Select all

(reg & 0x10)==0
(reg & 0x20)==0
(reg & 0x40)==0
(reg & 0x80)==0
i think they must be

Code: Select all

(reg & 0x16)==0
(reg & 0x32)==0
(reg & 0x64)==0
(reg & 0x128)==0
pls help me!

frank26080115
Posts: 237
Joined: Thu Jul 05, 2007 8:32 am

Post by frank26080115 » Fri Jan 08, 2010 8:34 am

the prefix "0x" indicates that the number is in base 16 (hexadecimal)
Recently graduated from UW for EE!

stevech
Support Volunteer
Posts: 2907
Joined: Sat Jun 05, 2004 8:51 pm
Location: USA, California

Post by stevech » Sun Jan 10, 2010 11:38 am

Please don't post with "Help me!"
In english, that's an imperative - calling out a critical/dire situation.

Liencouer
Posts: 155
Joined: Fri Mar 28, 2008 7:36 am

Post by Liencouer » Sun Jan 10, 2010 2:25 pm

Code: Select all

if((reg & 0x40)==0) m_pin10=0;     else m_pin10=1; 

could be written like

Code: Select all

if((reg & _BV(4)) == 0){ 
     m_pin10 = 0;
}else{
     m_pin10 = 1;
}
its a whole bunch of 'if bit n is set in A, set bit m in B'

your snippet looks like something from the parallel lcd routines, cross mapping bits from one byte to another

riden
Support Volunteer
Posts: 1754
Joined: Fri Jun 03, 2005 10:41 pm
Location: Illinois

Post by riden » Sun Jan 10, 2010 3:47 pm

Code: Select all

if((reg & 0x40)==0) m_pin10=0;     else m_pin10=1; 
could also be written as

Code: Select all

m_pin10 = (reg & 0x40) > 0;
Ralph

stevech
Support Volunteer
Posts: 2907
Joined: Sat Jun 05, 2004 8:51 pm
Location: USA, California

Post by stevech » Sun Jan 10, 2010 9:21 pm

or more esoteric, in C

m_pin10 = (reg & 0x40) ? 1 : 0;

m_pin10 = ( (reg & 0x40) != 0); // this one adds clarity, but probably same asm code result

Post Reply