# check value of the pins

Status
Not open for further replies.

#### foxbrain

##### Full Member level 2
How can I check the value of the pins of one of the atmega16a ports?
i know how to check a value of one pin only by : Address & (1<<pin) ....
i'm using avr studio
thanks

Last edited:

#### dariush_abbasi

##### Member level 3
Hi,
Would you please explain what you want to do?
I don't have your compiler and don't know its syntax but in PIC for example if you want to check whether the B PORT is equal to 8 ,you can simply use and if instruction.
Code:
if(PORTB == 8){}

#### Arch Zone

##### Full Member level 3
Try this.
This example explains if we want to test bit 0, 4 and 5 of Pin C

Ldi R16,0 ;port c as input
Out 0x14,R16

In R16,0x13 ;read from pin C
andi R16,0x31 ;discard another bits : xxxx xxxx & 0011 0001 ---> 00xx 000x
Cpi R16,0x31 ;compare immediate with 0x31
brne not_equal
.....

not_equal:
.....

#### foxbrain

##### Full Member level 2
Would you please explain what you want to do?
sorry for dis explain ....
first i'm using atmega16a and i'm using avr studio as a compiler.
second i'm programming in C language.
third i want (for example): if pins 0,1,2 are high and the others are low (3--7) then do something......
i tried to put IF(PINC==0b00000111){do something} but it didn't work

#### Arch Zone

##### Full Member level 3
Basicly, AVR studio is only for assembly language, but it's can be combined with c (avrgcc or others).
From my last post, you can learn it's algorithm and convert to c language.

unsigned char a; //for avrgcc uint8 a;
DDRC=0x00; //Port C as input
PORTC=0x00; //disable pull-up or you can change it with PORTC=0xFF (enable pull-up)
a=PINC; //store conditions of pins, so we only have to read it once.
if (a==0x03)
{
.....
.....
.....
}
else
{
if (a==...)
{
.....
.....
.....
}
else
{

};
};

and let me know, where it doesn't work.

Thanks.

#### alexan_e

PC4 to PC7 are JTAG pins by default, have you disabled the JTAG fuse (JTAGEN)?
You have to disable the fuse in order for the pins to operate as normal I/O.

You code is fine
IF(PINC==0b00000111){do something}
Alex

#### foxbrain

##### Full Member level 2
Arch Zone:
i meant avrgcc when i said avr studio because it is added to it........
unsigned char a; //for avrgcc uint8 a;
u meant uint8_t because uint8 didn't work by the compiler...
but even this code didn't work
alexan_e:
i'm exactly working on portD and on pins from 0 to 3 i'm connecting to them a dtmf receiver so i want to make every button on the telephone has a meaning .....
the code u wrote didn't work i tried it.

#### alexan_e

The code is fine , if for some reason the value is not the one you expect this is not a problem of the above code.
Maybe your device is giving a different signal of maybe you need to enable the internal pull up resistors.
Also make sure that the pins are set as inputs.
What do you mean when you say that it is not working, can you be more specific?

Concerning the comment of the previous post you can use unsigned char or signed char or uint8_t etc , as long as you use in the condition a binary or hex value the comparison result is exactly the same, there is only a difference if you want to use a decimal format number >127 .

#### foxbrain

##### Full Member level 2

i'm using this schematic but the addition is that i'm connecting it to atmega16a to portD0-3 and i'm connecting all portC to leds
and the code is:
#include <avr/io.h>
#include <util/delay.h>
int main(void)
{
uint8_t z;
DDRC=0xFF;
PORTC=0x00;
DDRD=0x00;

while(1)
{
z=PIND;
_delay_ms(50);
if(z==0x04)
{
SETBIT(PORTC,1);
}
else {}
_delay_ms(1000);
PORTC=0x00;
}
return 0;
}
so if i pressed 2 the led on portC1 should blink while nothing is happening...

Last edited:

#### alexan_e

Is port D connected to the led anodes (DS1-DS5)?
Are the leds in place?
Is the voltage at the led anode 5v then the led is on?

#### foxbrain

##### Full Member level 2
it looks i found the error
it's because i left the other pins of PIND not connected to the ground....

#### alexan_e

Note that (z==0x04) checks for an exact value of all 8 bits , are you sure that this is what you want?
Can be sure that the other 7 bits are 0?

#### foxbrain

##### Full Member level 2
but how can i specify only the first 4 pins in portD0-3 in my code?

#### alexan_e

if you want to mask the first 4 bits then you can use if((z & 0x0F) == (0x04)) so that you set them to 0 and then the code checks is the last four are 1000

Or in the step where you assign the variable

z= (PIND & 0x0F);

foxbrain

### foxbrain

points: 2

#### foxbrain

##### Full Member level 2
thanks, that's great....:razz:

Status
Not open for further replies.