• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

About changing status of individual pin of OUTPUT Port

Thread starter #1
Hey ,I am using Atmega controllers and i want to change status of individual PIN of PORT that is defined As OUTPUT PORT.

I am using following code for that but it is not working as we expected.....

DDRA=0xFF;//PORTA is defined as output port
//function call for assignning PORTA.4=1
change(&PORTA,4,1);


//function definition
void change(char *a, char b, char c)
{
a=c;
}
 

ericgibbs

Well-Known Member
Most Helpful Member
#2
hi,
On many PIC's PORTA.4 is Open Drain when configured as an output, could this be your problem.?
 
Thread starter #3
No,I'm using atmega controllers.To be specific atmega 16.N prob is not with manufacturing of ic(whether it is open coll or drain)...I am using another piece of code for same purpose .And that is working properly.It has logical shifting and "AND"ing and "OR"ing.I am not able to figure out what is wrong with the piece of code given above.
 
Last edited:

misterT

Well-Known Member
Most Helpful Member
#4
//function definition
void change(char *a, char b, char c)
{
a=c;
}


The line a=c; translates to:

*(a+b) = c;


That is not what you want to do. Try:

void change(char *a, char b, char c)
{
c ? ((*a) |= (1<<b)) : ((*a) &= ~(1<<b));
}


Anyway that approach is not very good. You should use macros like these:
Code:
#define bit_get(p,m) ((p) & (1<<m)) 
#define bit_set(p,m) ((p) |= (1<<m)) 
#define bit_clear(p,m) ((p) &= ~(1<<m)) 
#define bit_flip(p,m) ((p) ^= (1<<m)) 
#define bit_write(c,p,m) (c ? bit_set(p,m) : bit_clear(p,m)) 

// Examples:

// Set bit number 4 in PORTA:
bit_set(PORTA, 4);

//Write zero to PORTA.5
bit_write(0, PORTA, 5);
 
Last edited:

Latest threads

EE World Online Articles

Loading

 
Top