/* The code
Written in C with Microchip Studio: */
/* * 7-segment.c * from 7-segment-test-porta.c * ATtiny414 * This will drive both Common Andode and Cathode. * You must remark #define CommonCathode to use common Anode. * * Created: 10/7/2025 14:46:21 * Author : Sherman Stebbins * a * -- * f| |b * g * __ * e| |c * -- * d */ #include <avr/io.h>//#define CommonCathode //remark out for common Anode#ifdef CommonCathodeconst int8_t Hex[]={ //gfedcba 0b01111110, //0 0b11111100 0b01100000, //1 0b10110110, //2 0b10011110, //3 0b11001100, //4 0b11011010, //5 0b11111010, //6 0b00001110, //7 0b11111110, //8 0b11001110, //9 0b11101110, //A 0b11111000, //B 0b01110010, //C 0b10111100, //D 0b11110010, //E 0b11100010, //F 0b00000000 //blank };#else // Common Anode:const int8_t Hex[]={ // gfedcba 0b10000000, //0 - 0b11110010, //1 - 0b01001000, //2 - 0b01100000, //3 - 0b00110010, //4 - 0b00100100, //5 - 0b00000100, //6 - 0b11110000, //7 - 0b00000000, //8 - 0b00110000, //9 - 0b00010000, //A - 10 0b00000110, //B - 11 0b10001100, //C - 12 0b01000010, //D - 13 0b00001100, //E - 14 0b00011100, //F - 15 0b11111110 //Blank - 16 };#endif
int main(void){// PORTA_DIR = 0b11111110; PORTA_DIR = PIN1_bm|PIN2_bm|PIN3_bm|PIN4_bm|PIN5_bm|PIN6_bm|PIN7_bm; PORTB.DIRCLR = PIN0_bm|PIN1_bm|PIN2_bm|PIN3_bm;
while (1) { uint8_t OutBite = 16; //blank
if((PORTB.IN == 0b0000)){ OutBite = Hex[0]; }else if((PORTB.IN == 0b0001)){ OutBite = Hex[1]; }else if((PORTB.IN == 0b0010)){ OutBite = Hex[2]; }else if((PORTB.IN == 0b0011)){ OutBite = Hex[3]; }else if((PORTB.IN == 0b0100)){ OutBite = Hex[4]; }else if((PORTB.IN == 0b0101)){ OutBite = Hex[5]; }else if(PORTB.IN == 0b0110){ OutBite = Hex[6]; }else if(PORTB.IN == 0b0111){ OutBite = Hex[7]; }else if(PORTB.IN == 0b1000){ OutBite = Hex[8]; }else if(PORTB.IN == 0b1001){ OutBite = Hex[9]; }else if(PORTB.IN == 0b1010){ OutBite = Hex[0xa]; //10 }else if(PORTB.IN == 0b1011){ OutBite = Hex[0xb]; //11 }else if(PORTB.IN == 0b1100){ OutBite = Hex[0xc]; //12 }else if(PORTB.IN == 0b1101){ OutBite = Hex[0xd]; //13 }else if(PORTB.IN == 0b1110){ OutBite = Hex[0xe]; //14 }else if(PORTB.IN == 0b1111){ OutBite = Hex[0xf]; //15 }else{ OutBite = Hex[16]; } PORTA_OUT = OutBite; }}