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 with reading port of ATMega16

Status
Not open for further replies.

yas_iiee

Newbie level 5
Joined
Apr 28, 2010
Messages
8
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
karachi
Activity points
1,342
problem reading port

I m facing prob in reading port of avr ATMega16. I wanna check status of PINC bit 0 and I m using following instruction for the purpose:

if( ( PINC & 0x01 ) == 1)
keyg=1;
else
keyg=0;

obviously data direction is set and external pull-ups are used.

the problem is that unless I force all bits of the port high the statement doesnt work and if any bit gets low it works for 'else'.
Complete code is attatched
 

Re: problem reading port

Try reading the port first into a variable then just use the bitwise and.

Code:
unsigned char port = PINC;

if(port & 0x01){ 
  keyg = 1;
  } 
else{ 
  keyg = 0;
  }
 

Re: problem reading port

Can you try the bit_is_set(PORT,PIN) of WinAVR, if you are using WinAVR. In this case it will be:
Code:
if (bit_is_set(PINC, PINC0)){
keyg=1;
}else{
keyg=0;
}

If you are not using WinAVR, you can do:
Code:
if (PINC & (1<<PINC0)){
keyg=1;
}else{
keyg=0;
}

Yes, this last one is the same code you are using, but who knows, maybe it works.
Do you have the "{" and "}" in your code? Maybe your compiler is not smart enought to realize what you want to do. Try adding the "{" and "}" first.

Regards
 

Re: problem reading port

btbass said:
Try reading the port first into a variable then just use the bitwise and.

Code:
unsigned char port = PINC;

if(port & 0x01){ 
  keyg = 1;
  } 
else{ 
  keyg = 0;
  }

I ve tried this as well on the seggustion of my teacher but of no use. The problem still remains

Added after 3 minutes:

diegobb said:
Can you try the bit_is_set(PORT,PIN) of WinAVR, if you are using WinAVR. In this case it will be:
Code:
if (bit_is_set(PINC, PINC0)){
keyg=1;
}else{
keyg=0;
}

If you are not using WinAVR, you can do:
Code:
if (PINC & (1<<PINC0)){
keyg=1;
}else{
keyg=0;
}

Yes, this last one is the same code you are using, but who knows, maybe it works.
Do you have the "{" and "}" in your code? Maybe your compiler is not smart enought to realize what you want to do. Try adding the "{" and "}" first.

Regards

I m using Winavr but the stuff u r reffering needs some header file i guess
Regards
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top