Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

// MikroC code for reading three-phase voltage system, displaying AC voltage level on 20x4 LCD, and

indicating voltage range

unsigned int adc_value;

float voltage;

char uart_text[10];

void main() {

ADC_Init();

UART1_Init(9600);

Lcd_Init(&PORTB, 4, 5, 6, 7, &PORTB, 0, 1, 2, 3); // Initialize 20x4 LCD

while (1) {

// Read phase A voltage

adc_value = ADC_Read(0); // Read ADC value from analog input channel 0

voltage = ((float)adc_value / 1023.0) * 5.0; // Convert ADC value to voltage (assuming 5V reference)

Lcd_Cmd(_LCD_CLEAR);

Lcd_Cmd(_LCD_CURSOR_OFF);

Lcd_Out(1, 1, "Phase A: ");

FloatToStr(voltage, uart_text);

Lcd_Out(2, 1, uart_text);

Lcd_Out(2, 5, " V");

if (voltage < 210.0) {

Lcd_Out(4, 1, "Low Voltage");


}

else if (voltage >= 211.0 && voltage <= 240.0) {

Lcd_Out(4, 1, "Normal Voltage");

else if (voltage > 240.0) {

Lcd_Out(4, 1, "Over Voltage");

Delay_ms(1000);

// Read phase B voltage

adc_value = ADC_Read(1); // Read ADC value from analog input channel 1

voltage = ((float)adc_value / 1023.0) * 5.0; // Convert ADC value to voltage (assuming 5V reference)

Lcd_Cmd(_LCD_CLEAR);

Lcd_Cmd(_LCD_CURSOR_OFF);

Lcd_Out(1, 1, "Phase B: ");

FloatToStr(voltage, uart_text);

Lcd_Out(2, 1, uart_text);

Lcd_Out(2, 5, " V");

if (voltage < 210.0) {

Lcd_Out(4, 1, "Low Voltage");

else if (voltage >= 211.0 && voltage <= 240.0) {


Lcd_Out(4, 1, "Normal Voltage");

else if (voltage > 240.0) {

Lcd_Out(4, 1, "Over Voltage");

Delay_ms(1000);

// Read phase C voltage

adc_value = ADC_Read(2); // Read ADC value from analog input channel 2

voltage = ((float)adc_value / 1023.0) * 5.0; // Convert ADC value to voltage (assuming 5V reference)

Lcd_Cmd(_LCD_CLEAR);

Lcd_Cmd(_LCD_CURSOR_OFF);

Lcd_Out(1, 1, "Phase C: ");

FloatToStr(voltage, uart_text);

Lcd_Out(2, 1, uart_text);

Lcd_Out(2, 5, " V");

if (voltage < 210.0) {

Lcd_Out(4, 1, "Low Voltage");

else if (voltage >= 211.0 && voltage <= 240.0) {

Lcd_Out(4, 1, "Normal Voltage");

}
else if (voltage > 240.0) {

Lcd_Out(4, 1, "Over Voltage");

Delay_ms(1000);

You might also like