# 7-segment 4-digit display ghosting

Currently I'm tinkering with my ADC project with my ATmega328. I asked my microcontroller educator how I can fix this issue with the ghosting. I thought it had something to do with frequency or the 2N2222 transistors, but he said something about my code not being optimized and didn't give me any clue of where I could start.

Can I get some help?

#define F_CPU 8000000UL
#include <avr/io.h>
#include <util/delay.h>

// Definir los valores de los segmentos para mostrar dígitos en el display de 7 segmentos
int seg7_1[10] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x67};

{
ADMUX = (1 << REFS0); // AVCC con un condensador externo en el pin AREF
}

// Leer el valor del ADC
{
while (ADCSRA & (1 << ADSC)); // Esperar a que la conversión se complete
}

// Suavizar el valor del ADC
{
static uint16_t readings[4] = {0, 0, 0, 0};
uint32_t sum = 0;

// Desplazar las lecturas
for (int i = 0; i < 3; i++)
{
}

// Tomar una nueva lectura

// Calcular el promedio
return sum / 4;
}

int main(void)
{
// Configurar los pines de salida
DDRB = 0xFF; // 1111 1111
DDRD = 0xF0; // 1111 0000
DDRC &= ~(1 << PC0); // Configurar PC0 como entrada
PORTC |= (1 << PC0); // Habilitar pull-up en PC0

while (1)
{

// Separar el valor en dígitos individuales
int i, j, k, z;
j = (adc_value / 10) % 10;
k = (adc_value / 100) % 10;
z = (adc_value / 1000) % 10;

// Mostrar los dígitos en el display de 7 segmentos
PORTD |= 1 << PORTD4;
PORTD &= ~(1 << PORTD5) & ~(1 << PORTD6) & ~(1 << PORTD7); // 0100
PORTB = seg7_1[z];
_delay_ms(1);

PORTD |= 1 << PORTD5;
PORTD &= ~(1 << PORTD6) & ~(1 << PORTD7) & ~(1 << PORTD4); // 0010
PORTB = seg7_1[k];
_delay_ms(1);

PORTD |= 1 << PORTD6;
PORTD &= ~(1 << PORTD7) & ~(1 << PORTD4) & ~(1 << PORTD5); // 0001
PORTB = seg7_1[j];
_delay_ms(1);

PORTD |= 1 << PORTD7;
PORTD &= ~(1 << PORTD4) & ~(1 << PORTD5) & ~(1 << PORTD6); // 1000
PORTB = seg7_1[i];
_delay_ms(1);
}
}


• You should add a schematic or circuit diagram to your question. Commented Nov 22, 2023 at 1:26