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.

How to check the input with ATMEGA32?

Status
Not open for further replies.

ahmed_mahmoud

Junior Member level 1
Joined
Apr 22, 2010
Messages
19
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,283
Activity points
1,416
I use gcc compiler WinAVR and I want to check the input

if(PINB0==1)
{
//do something
}

However, when I set the B0 nothing happens, but when I use

if((PINB&0x01)==0x01)
{
//do something
}

it works perfectly; nevertheless, its not practical to write like that each time i use if condition.

Regards,
Ahmed
 

if(PINB0==1)
{
//do something
}

PINB0 is not 1, the code will never execute.

However, when I set the B0 nothing happens, but when I use

if((PINB&0x01)==0x01)
{
//do something
}

This one works because 0x01 is equal to 0x01 AND probably one or more pins of the PINB are equal to 1.

Some of the pins have two or three different functions, you have to check the datasheet and figure out at what function is set the pin on start up. Then you need to enable the function that you want by setting
certain bits to zero or one. Again check the datasheet.
 

If you check the header file "iom.h", you'll see that PINB0 is defined as the constant 0. So, your condition is never true. So, you cannot use the 1st method. I've attached the header file but I changed the extension to .txt so that you can see for yourself.

Hope this helps.
Tahmid.
 

Attachments

  • iom32.txt
    14.6 KB · Views: 69
  • Like
Reactions: MRAMA

    MRAMA

    Points: 2
    Helpful Answer Positive Rating
Dear all,
your replies are highly appreciated.can I use "PINB.0" I saw some codes use this command; however, read that it compiler dependent.

regards,
Ahmed
 

Does it work?? Probably not if it's constantly 0. You could try a different input.
 

Read the link of post #2, this can only be used in codevision but you can do something similar with avrgcc

Alex
 

Dear all,
your replies are highly appreciated.can I use "PINB.0" I saw some codes use this command; however, read that it compiler dependent.

regards,
Ahmed

You can't use PINB.0 in AVR-GCC. That works in Codevision and mikroC. In AVR-GCC you cannot refer to a bit like that.
 

You can have bit access if you use the following


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// structure to allow bit field operations, name conversions: PORTA.0 -> PORT_A.b0  PORTB.7 -> PORT_B.b7
typedef struct{ uint8_t b0:1;
                uint8_t b1:1;
                uint8_t b2:1;
                uint8_t b3:1;
                uint8_t b4:1;
                uint8_t b5:1;
                uint8_t b6:1;
                uint8_t b7:1; } bits;
 
// define all the ports of your microcontroller
#define PORT_A (* (volatile bits *) &PORTA)
#define PIN_A (* (volatile bits *) &PINA)
#define DDR_A (* (volatile bits *) &DDRA)
 
#define PORT_B (* (volatile bits *) &PORTB)
#define PIN_B (* (volatile bits *) &PINB)
#define DDR_B (* (volatile bits *) &DDRB)
 
// ...define any other available ports
 
// then you can use like this to write or read specific bits
PORT_A.b0=1;   // write value to PORTA bit 0
PIN_B.b4=0;   // write value to PINB bit 4



The names used (PORT_A, PIN_A etc) can be changed but you have to use a name that is not defined.
You can also change b0, b1 etc to any other name

Alex
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top