Search code examples
cavr

How to assign a specific [x] value to port pin?


How can I assign dynamic value to a pin a specific pin on my AVR chip. For instance I have:

void life_reset(void){
    PORTC |= (1<<PC0);
    PORTC |= (1<<PC1);
    PORTC |= (1<<PC2);
    life_number = 2;
}
void lost_life(void){
            life_number--;
            PORTC &= ~(1<<PC2);
}

Which decrements my life_number variable and bitwise AND's the left shift of PC2.

In this case, each PC[x] value is connected to an LED. How can I write my PORTC &= statement to be the PC[x] value of life_number.

Pseudocode -what I want to do

void lost_life(void){
            life_number--;
            PORTC &= ~(1<<PC[*life_number*]);
}

Solution

  • Just use the life number, because the PCn macros are in fact just the same numbers that show in their name.

    PORTC &= ~(1<<life_number);