0% found this document useful (0 votes)
22 views5 pages

2nd Draft

This document describes how to interface an LCD module with a PIC16f877a microcontroller. It defines pin connections and configurations for the LCD, buzzer, motor relay and ADC. The code samples how to read voltage values from three phases, display them on the LCD and check for under/over voltage or single phasing conditions.

Uploaded by

Panje Darlington
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views5 pages

2nd Draft

This document describes how to interface an LCD module with a PIC16f877a microcontroller. It defines pin connections and configurations for the LCD, buzzer, motor relay and ADC. The code samples how to read voltage values from three phases, display them on the LCD and check for under/over voltage or single phasing conditions.

Uploaded by

Panje Darlington
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

//Title: LCD interfacing with PIC16f877a

//LCD module connections

sbit LCD_RS at RB0_bit; //LCD reset


sbit LCD_EN at RB1_bit; //LCD enable

sbit LCD_D4 at RB4_bit; //Data


sbit LCD_D5 at RB5_bit; //Data
sbit LCD_D6 at RB6_bit; //Data
sbit LCD_D7 at RB7_bit; //Data

//LCD Pin Direction

sbit LCD_RS_Direction at TRISB0_bit;


sbit LCD_EN_Direction at TRISB1_bit;
sbit LCD_D4_Direction at TRISB4_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D7_Direction at TRISB7_bit;
//BUZZER AND MOTOR RELAY CONNECTIONS
sbit Buzzer at RC0_bit; //buzzer output
sbit Motor_Relay at RC1_bit; //motor relay output
//port c pin directions
sbit Buzzer_Direction at TRISC0_bit;
sbit Motor_Relay_Direction at TRISC1_bit;

char i;
float ac_peak_voltage = 230.0;
float adc_resolution = 5.0/1023;
void main()
{
Lcd_Init();
// Configure Port A as input
TRISA = 0xFF; // set all pins of Port A as inputs

// Configure Port B as output


TRISB = 0x00; // set all pins of Port B as outputs

// Configure Port C as output


TRISC = 0x00; //set all pins of port C as outputs

// Initialize ADC module


ADC_Init(); // ADC_Initialisation

Lcd_cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);

Lcd_out(1,1,"MONITORING AND PROTECTION SYSTEM");


Delay_ms(500);

for(i=0; i<32; i++)


{
lcd_cmd(_lcd_shift_left);
delay_ms(250);
}
Lcd_cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
//SET INITIAL STATES TO BUZZER AND MOTOR RELAY
Buzzer =0;
Motor_Relay = 0;

while(1)
{
// READ ADC VALUES FOR THREE PHASES
float phase_a_voltage = ADC_Read(0); // Assuming ADC_Read() is a function
provided by mikroC PRO for PIC
float phase_b_voltage = ADC_Read(1); // Assuming ADC_Read() is a function
provided by mikroC PRO for PIC
float phase_c_voltage = ADC_Read(2); // Assuming ADC_Read() is a function
provided by mikroC PRO for PIC

// CONVERT ADC VALUES TO VOLTAGES


float phase_a_voltage_actual = phase_a_voltage * adc_resolution;
float phase_b_voltage_actual = phase_b_voltage * adc_resolution;
float phase_c_voltage_actual = phase_c_voltage * adc_resolution;

// CALCULATE AC VOLTAGES FOR EACH PHASE


float phase_a_ac_voltage = (phase_a_voltage_actual / 5.0) * ac_peak_voltage; //
Assuming 5V peak-to-peak AC source
float phase_b_ac_voltage = (phase_b_voltage_actual / 5.0) * ac_peak_voltage;
float phase_c_ac_voltage = (phase_c_voltage_actual / 5.0) * ac_peak_voltage;

// CONVERT AC VOLTAGES TO WHOLE NUMBERS


int phase_a_voltage_whole = (int)phase_a_ac_voltage;
int phase_b_voltage_whole = (int)phase_b_ac_voltage;
int phase_c_voltage_whole = (int)phase_c_ac_voltage;

// CONVERT AC VOLTAGES TO STRINGS


char phase_a_voltage_text[16]; // Assuming a maximum of 15 characters for voltage
display
char phase_b_voltage_text[16];
char phase_c_voltage_text[16];

intToStr(phase_a_voltage_whole,phase_a_voltage_text);
intToStr(phase_b_voltage_whole,phase_b_voltage_text);
intToStr(phase_c_voltage_whole,phase_c_voltage_text);

// DISPLAY VOLTAGES ON LCD


Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1, 1, "R PHASE:");
Lcd_Out(1, 9,(phase_a_voltage_text));
Lcd_Out(1, 15,"V" );
Lcd_Out(2, 1, "Y PHASE:");
Lcd_Out(2,9,(phase_b_voltage_text));
Lcd_Out(2,15,"V" );
Lcd_Out(3,1, "B PHASE:");
Lcd_Out(3,9,(phase_c_voltage_text));
Lcd_Out(3,15,"V" );

//CHECK PRESENTS OF ALL OF ALL PHASES


if (phase_a_voltage_whole == 0||phase_b_voltage_whole == 0||phase_c_voltage_whole
== 0)
{
Lcd_Out(4,1,"SINGLE PHASING!!");
Buzzer =1; //turn ON the buzzer
Motor_Relay =0; // turn OFF motor relay
}

//CHECK SYSTEM OPERATION STATUS


if (phase_a_voltage_whole<220||phase_b_voltage_whole<220||
phase_c_voltage_whole<220)
{
Lcd_Out(4,1,"UNDER-VOLTAGE!!");
Buzzer =1; //turn ON the buzzer
}

else if (phase_a_voltage_whole>240||phase_b_voltage_whole>240||
phase_c_voltage_whole>240)
{
Lcd_Out(4,1,"OVER-VOLTAGE!!");
Buzzer =1; //turn ON the buzzer
}
else
{
Lcd_Out(4,1,"NORMAL-VOLTAGE");
Buzzer =0; //turn OFF the buzzer
Motor_Relay =1; // turn ON motor relay
}

Delay_ms(1000); // Delay for readability 1s

} //while loop
} //main loop

You might also like