Forum Menu




 


Log in Problems?
New User? Sign Up!
AVR Freaks Forum Index

Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Author Message
alvrAVR
PostPosted: Mar 15, 2012 - 02:17 PM
Rookie


Joined: Mar 15, 2012
Posts: 31


Hello,
I am beginer with AVR (I know only C51 and Keil IDE).
How can I use bit access to pin of port and define name of this pin? When I use access to whole port (as you see in first example) it is OK, but can I read and write only to pin (second example)?

example with port access is OK:

#define INPORT PORTA
#define OUTPORT PORTB

int main() {
DDRA = 0;//set to input
DDRB = 255;//set to output
OUTPORT = INPORT;//reading PORTA and writing to PORTB is OK
}

example with pin access:

#define INPIN PORTA4
#define OUTPIN PORTB4

int main() {
DDRA = 0;//set to input
DDRB = 255;//set to output
OUTPIN = INPIN;//reading PIN and writing to PIN it can not compile ??
PORTB5 = 1;//even it can not compile ??
}

Please, what is simply corect way for read and write pin and define his name?
Thank you very much and aI am sorry for my bad english..
 
 View user's profile Send private message  
Reply with quote Back to top
miltronik
PostPosted: Mar 15, 2012 - 02:30 PM
Wannabe


Joined: Feb 10, 2005
Posts: 54


Use this:

H-File:
Code:

typedef struct
{
    unsigned char   bit0 : 1;
    unsigned char   bit1 : 1;
    unsigned char   bit2 : 1;
    unsigned char   bit3 : 1;
    unsigned char   bit4 : 1;
    unsigned char   bit5 : 1;
    unsigned char   bit6 : 1;
    unsigned char   bit7 : 1;
} bit_field;

#define GET_BITFIELD( addr )    ( *(( volatile bit_field * ) (addr)) )

// make your define for the Port-Pin:
#define Your_Port_PIN GET_BITFIELD ( &PORTF ).bit4
// example: PORTF Bit 4 (output)






Than access your Bit with:


Code:


Your_Port_PIN = 1;

OR

Your_Port_PIN = 0;



The same with Inputs:

#define clock_input GET_BITFIELD ( &PINB ).bit6
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: Mar 15, 2012 - 03:48 PM
10k+ Postman


Joined: Jul 18, 2005
Posts: 62348
Location: (using avr-gcc in) Finchingfield, Essex, England

To add to what Mitronik says. See these two tutorials:

Bit manipulation 101
(this includes sbit.h on page 6 which expands on what Mitronik presented)

How to define a pin as a variable
(which focuses on the use of "bitfields" in a struct)

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
alvrAVR
PostPosted: Mar 15, 2012 - 04:43 PM
Rookie


Joined: Mar 15, 2012
Posts: 31


Thank you very much. Now it already run. And I will look on added tutorials.
May be I will ask you with other problem later....Smile
 
 View user's profile Send private message  
Reply with quote Back to top
Display posts from previous:     
Jump to:  
All times are GMT + 1 Hour
Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Powered by PNphpBB2 © 2003-2006 The PNphpBB Group
Credits