Display of ac voltage on LCD using PIC18F Microcontroller

Thread Starter

Qual

Joined Aug 5, 2016
48
Hi all,

I trying to display ac voltage on LCD using micro-controller using PIC18F45K50. I am capturing the voltage and performing A/D Conversion.
Though i could see the variations with different Inputs in ADC register but I am unable to display the same on LCD.

I am using MPlab X IDE 3.2
 

Thread Starter

Qual

Joined Aug 5, 2016
48
I am using sprintf statement before LCD Statement so that it converts to string.
When i build the program it shows no errors but i am not able to see a single text on LCD when whole circuit is connected to AC mains.
 
Last edited:

Thread Starter

Qual

Joined Aug 5, 2016
48
ok...

Here is the code

C:
void ms_delay(unsigned int delay);
void ADC_Init();

unsigned int v;
long temp;
unsigned int delayinms;
char *volt = "00.0";
char s[20];

float t[40];
float  txt[5], txt1[5];

float max_Value,avg,sum=0.0;
int i;

float loc_max,inputarray[500];
int j;
unsigned int result;

void ADC_Init()
{
  ADCON0 = 0b00010101;               //Analog channel 5 and ADC is enabled
  ADCON1 = 0b10000000 ;              // selects the trigger from CTMU
  ADCON2 = 0b10110010;               // Right Justified , 16TAD , Fosc/32
}

void ms_delay(unsigned int delay)
{
    unsigned int delayinms;   
    for(delayinms=0;delayinms<delay;delayinms++)
    {
     __delay_ms(10); 
    } 
}

unsigned int  GetADCValue(unsigned int Channel)
{
    ADCON0bits.ADON =1;                              // ADC Enable
    ms_delay(5) ;                                            // Time for Acquisition capacitor
                                            // to charge up and show correct value
    GO_nDONE  = 1;                    // Enable Go/Done
    while(GO_nDONE);        // Wait for conversion completion
    return ((ADRESH<<8)+ADRESL);   // Return 10 bit ADC value
}

void main()
{
  ANSELA = 0X00;
  ANSELB = 0X00;
  ANSELC = 0X00;
  ANSELD = 0X00;
  ANSELE = 0x03;
  TRISA = 0x00;
  TRISB = 0x04;
  TRISC = 0X00;
  TRISD = 0X00;
  TRISE = 0xFF;
  
  Lcd_Init();
  ADC_Init();
while(1)
{ 
    result=GetADCValue(5);
    max_Value= 0;
    for (i=0;i<4;i++) 
    {
        loc_max= 0;
        do{ 
            result=GetADCValue(5);
          } while(result==loc_max);
        for (j=0; j<249; j++)   
        {
          result=GetADCValue(5); 
          if(result>loc_max) 
          loc_max = result;
        }   
      max_Value = loc_max;
      sum = sum + max_Value;
      avg = sum/5;    //get average of 5 signals
      //v= avg;
      avg= avg*0.707106781;
    
    
      sprintf(s,"voltage:%f",avg);
      Lcd_Write_String(s);
      //Lcd_Set_Cursor(1,1);
      //Lcd_Write_String("Voltage:");
      //Lcd_Set_Cursor(2,1);
     // Lcd_Write_String(v);
    }
  
}
}
 
Last edited by a moderator:

dannyf

Joined Sep 13, 2015
2,197
1. Show that you can blink a LED.
2. Show that you can display AA string.
3. Show that you can display a strig converted from a numeric value.
4. Show that you can display AA string converted from a numeric variable.

Ones step at a time, in that specific order.
 

Thread Starter

Qual

Joined Aug 5, 2016
48
dannyf thanks for your reply.

Well i tried making led OFF whenever there is zero value on ADC and led to ON whenever it is not equal to zero.

while(1)
{
result=GetADCValue(5);
if(result==0x00)
{

PORTBbits.RB7 = 0;
ms_delay(50);

}
else
{
PORTBbits.RB7 = 1;
}
}

but i am see led is ON irrespective of Changes in AC input Voltage.

And dannyf can you pls tell me what is AA string?!! Because i am newbie in micro-controller coding!! :(
But i could understand what you are trying to say.

Pls don't mind if am asking very stupid question.
 
Top