Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade
Voltage sensors are crucial components in various electronic systems, including those involving microchips. These sensors measure the voltage levels in a circuit and provide feedback to the microchip for monitoring and control purposes. This article will guide you through the process of implementing and utilizing a voltage sensor in a microchip environment, complete with practical examples and sample code.
Voltage sensors can be classified into two main types: analog and digital. Analog voltage sensors output a voltage proportional to the measured voltage, while digital voltage sensors provide a digital representation of the measured voltage, often through an ADC (Analog-to-Digital Converter) integrated into the sensor or the microchip.
For this example, we'll use the ZMPT101B AC voltage sensor with a PIC16F877A microcontroller. The ZMPT101B outputs an analog voltage proportional to the AC voltage it measures.
[ZMPT101B] ----> [Analog Input Pin (AN0) of PIC16F877A]
[Power Supply] ----> [ZMPT101B and PIC16F877A]
[Ground] ----> [ZMPT101B and PIC16F877A]
Below is a sample code to read the voltage from the ZMPT101B sensor using the PIC16F877A microcontroller. The code is written in C using the MPLAB X IDE and the XC8 compiler.
#include <xc.h>
// Configuration bits
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
#define _XTAL_FREQ 20000000 // Define the system clock frequency
void ADC_Init()
{
ADCON0 = 0x41; // ADCON0: ADON=1, Channel Select bits=00001 (AN0)
ADCON1 = 0x80; // ADCON1: ADFM=1 (Right justified), ADCS2=0, VCFG1=0, VCFG0=0
}
unsigned int ADC_Read(unsigned char channel)
{
ADCON0 &= 0xC5; // Clear the Channel Select bits
ADCON0 |= channel<<3; // Select the required channel
__delay_ms(2); // Acquisition time to charge hold capacitor
GO_nDONE = 1; // Start conversion
while(GO_nDONE); // Wait for the conversion to complete
return ((ADRESH<<8)+ADRESL); // Return the result
}
void main()
{
unsigned int adc_result;
float voltage;
TRISA = 0xFF; // Set PORTA as input
TRISB = 0x00; // Set PORTB as output
ADC_Init(); // Initialize the ADC module
while(1)
{
adc_result = ADC_Read(0); // Read the ADC value from channel 0 (AN0)
voltage = (adc_result * 5.0) / 1023.0; // Convert the ADC value to voltage
// Display or process the voltage value as needed
__delay_ms(1000); // Delay for 1 second
}
}
ADC_Init
function configures the ADC module of the PIC16F877A.ADC_Read
function reads the analog value from the specified channel and returns the digital result.main
function, the ADC value is read from channel 0 (AN0), converted to a voltage, and can be displayed or processed as needed.Implementing a voltage sensor in a microchip environment involves selecting the appropriate sensor, setting up the hardware, and writing the necessary code to read and process the voltage measurements. By following the steps outlined in this article, you can effectively monitor and control voltage levels in your electronic systems.