Code for handling 7-segment display with potentiometer

Best regard
I have attached code for handling the 7 segment display with potentiometer but I need to change the instruction analog.Read (A0) in ATMEGA328P registers, how can you help me? It is a work of the University, the exercise requires me not to use this instruction, only with registers.


//Programa manejo del display de 7 segmentos Anodo comun con potenciometro.Version 2
#define m 0b11111111 // Puerto D como salidas
#define n 0b00000001 //Puerto B como entrada habilitar resistencias pull-up

void config_adc();
float voltaje=0.0;
float valor_pot();

void setup()
{
Serial.begin(9600);// inicializar el puerto serie para ver datos de voltaje
delay(50);
DDRD = m;//declaro salidas
DDRB = n;// pin 8 salida
PORTB = 0b11111111;//activo resistencias pull-up
}

void loop(){
int lectura=analogRead(A0);// Configuración puerto A0 como entrada.(ojo remplazar esta
condición por una función)
voltaje=(5.0*lectura)/1023;
Serial.println(voltaje);

if(lectura<254){
PORTB = 0b00000000; //linea
PORTD = 0b11111100;
}
if(lectura>=252&&lectura<504)

{

PORTB = 0b00000000;
PORTD = 0b00001100; //b

}
if(lectura>=504&&lectura<756)
{
PORTB=0b11111111;//número:i
PORTD=0b11100111;

}
if(lectura>=756&&lectura<1009)
{
PORTB = 0b10000000; //a
PORTD = 0b10000000;
}
}
void config_adc(){ //Configuracion del ADC

ADCSRA |= ((1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0)); //Prescaler para 128 segun tabla
125Khz fuente de reloj
ADMUX |= ~(1 << REFS0);
ADMUX &= ~(1 << REFS1); //Avcc(+5v) voltaje de Referencia
ADCSRB |= ((0 << ADTS2) | (0 << ADTS1) | (0 << ADTS0)); //ADC corre en modo free-running
ADCSRA |= (1 << ADATE); //Signal source, in this case is the free-running
ADCSRA |= (1 << ADEN); //Inicializa el ADC
ADCSRA |= (1 << ADSC); //Arranca el convertidor

}

Hi @alejandroroso,
help us to help you.
First correct your post by enclosing your code in </> tags.

Then tell us if this project is a school project.

And then why use direct access to ports if with "visually" simpler programming you can do the same?

RV mineirin