0% found this document useful (0 votes)
38 views

Adc

This document is a code for a microcontroller-based multimeter that reads analog voltage using an ADC. It includes configuration settings, initialization of the ADC, reading the ADC value, and displaying the voltage on a 7-segment display. The code is structured with functions for initialization, reading, and displaying the voltage, and operates in an infinite loop to continuously update the display.

Uploaded by

k2cpc4dqfk
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Adc

This document is a code for a microcontroller-based multimeter that reads analog voltage using an ADC. It includes configuration settings, initialization of the ADC, reading the ADC value, and displaying the voltage on a 7-segment display. The code is structured with functions for initialization, reading, and displaying the voltage, and operates in an infinite loop to continuously update the display.

Uploaded by

k2cpc4dqfk
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

// MULTIMETRO

#pragma config PLLDIV = 1, CPUDIV = OSC1_PLL2, USBDIV = 1


//CONFIG1L
#pragma config FOSC = HS, FCMEN = OFF, IESO = OFF
//CONFIG1H
#pragma config PWRT = OFF, BOR = OFF, BORV = 3, VREGEN = OFF
//CONFIG2L
#pragma config WDT = OFF, WDTPS = 32768
//CONFIG2H
#pragma config MCLRE = ON, LPT1OSC = OFF, PBADEN = OFF, CCP2MX = ON
//CONFIG3H
#pragma config STVREN = OFF, LVP = OFF, ICPRT = OFF, XINST = OFF, DEBUG = OFF
//CONFIG4L
#pragma config CP0 = OFF, CP1 = OFF, CP2 = OFF, CP3 = OFF
//CONFIG5L
#pragma config CPB = OFF, CPD = OFF
//CONFIG5H
#pragma config WRT0 = OFF, WRT1 = OFF, WRT2 = OFF, WRT3 = OFF
//CONFIG6L
#pragma config WRTB = OFF, WRTC = OFF, WRTD = OFF
//CONFIG6H
#pragma config EBTR0 = OFF, EBTR1 = OFF, EBTR2 = OFF, EBTR3 = OFF
//CONFIG7L
#pragma config EBTRB = OFF
//CONFIG7H

#include <xc.h>

#define _XTAL_FREQ 20000000 //Frecuencia del oscilador


#define AN0_CHANNEL 0 //Canal AN0 para el ADC

//Prototipos
void init_ADC(void);
unsigned int read_ADC(void);
void display_voltage(unsigned int value);

//Tabla de segmentos para los dígitos


const unsigned char segment[] = {0b00000011, 0b10011111, 0b00100101, 0b00001101,
0b10011001, 0b01001001, 0b01000001, 0b00011111, 0b00000001, 0b00011001};
const unsigned char uniseg[] = {0b00000010, 0b10011110, 0b00100100, 0b00001100,
0b10011000, 0b01001000, 0b01000000, 0b00011110, 0b00000000, 0b00011000};

//Configuración inicial
void init_ADC(void) {
ADCON0 = 0b00000001; //Canal AN0, encender ADC
ADCON1 = 0b00001110; //AN0 como entrada analógica
ADCON2 = 0b10101001; //Justificación derecha, FOSC/8, 2TAD
}

//Leer el valor del ADC


unsigned int read_ADC(void) {
ADCON0bits.GO = 1; //Iniciar conversión
while (ADCON0bits.GO); //Esperar a que termine la conversión
return ((ADRESH << 8) + ADRESL); //Combinar registros
}

//Mostrar el valor en el display de 7 segmentos


void display_voltage(unsigned int value) {
float voltage = (value * 4.995) / 1023; //Calcular voltaje real
voltage = voltage * 1000;
unsigned int display_val = (unsigned int)(voltage); //Escalar para mostrar
decimal

// Dividir el valor en decenas y unidades


unsigned int uni = display_val / 1000; //Parte de "volts" (unidad
completa)
unsigned int dec = (display_val / 100) % 10; //Centésimas
unsigned int cen = (display_val / 10) % 10; //Décimas
unsigned int mil = display_val % 10; //Milésimas

PORTD = segment[mil];
PORTC = 0b10000000;
__delay_ms(10);
PORTD = 0xFF;

PORTD = segment[cen];
PORTC = 0b00000100;
__delay_ms(10);
PORTD = 0xFF;

PORTD = segment[dec];
PORTC = 0b00000010;
__delay_ms(10);
PORTD = 0xFF;

PORTD = uniseg[uni];
PORTC = 0b00000001;
__delay_ms(10);
PORTD = 0xFF;
}

void main(void) {
TRISA = 0x01; //AN0 como entrada
TRISC = 0x00; //Puerto C como salida (para segmentos)
TRISD = 0x00; //Puerto D como salida (para segmentos)

init_ADC(); //Inicializar el ADC

while (1) {
unsigned int adc_value = read_ADC(); // eer el ADC
display_voltage(adc_value); //Mostrar voltaje en displays
}
}

You might also like