1. 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.
    Dismiss Notice

About changing status of individual pin of OUTPUT Port

Discussion in 'Code Repository' started by amol.gulhane07, Oct 23, 2012.

  1. amol.gulhane07

    amol.gulhane07 Member

    Joined:
    Jul 6, 2012
    Messages:
    32
    Likes:
    6
    Location:
    india
    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;
    }
     
  2. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,181
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    ONLINE
    hi,
    On many PIC's PORTA.4 is Open Drain when configured as an output, could this be your problem.?
     
  3. amol.gulhane07

    amol.gulhane07 Member

    Joined:
    Jul 6, 2012
    Messages:
    32
    Likes:
    6
    Location:
    india
    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: Oct 23, 2012
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland



    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 (text):

    #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: Oct 31, 2012

Share This Page