Display of ac voltage on LCD using PIC18F Microcontroller

Discussion in 'Embedded Systems and Microcontrollers' started by Qual, Aug 27, 2016.

  1. Qual

    Thread Starter New Member

    Aug 5, 2016
    26
    0
    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
     
  2. Qual

    Thread Starter New Member

    Aug 5, 2016
    26
    0
    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: Aug 27, 2016
  3. be80be

    Senior Member

    Jul 5, 2008
    431
    57
    Maybe you forgot the code it helps to see it here and on the LCD plz post your code so we can help you.
     
  4. Qual

    Thread Starter New Member

    Aug 5, 2016
    26
    0
    ok...

    Here is the code

    Code (C):
    1.  
    2. void ms_delay(unsigned int delay);
    3. void ADC_Init();
    4.  
    5. unsigned int v;
    6. long temp;
    7. unsigned int delayinms;
    8. char *volt = "00.0";
    9. char s[20];
    10.  
    11. float t[40];
    12. float  txt[5], txt1[5];
    13.  
    14. float max_Value,avg,sum=0.0;
    15. int i;
    16.  
    17. float loc_max,inputarray[500];
    18. int j;
    19. unsigned int result;
    20.  
    21. void ADC_Init()
    22. {
    23.   ADCON0 = 0b00010101;               //Analog channel 5 and ADC is enabled
    24.   ADCON1 = 0b10000000 ;              // selects the trigger from CTMU
    25.   ADCON2 = 0b10110010;               // Right Justified , 16TAD , Fosc/32
    26. }
    27.  
    28. void ms_delay(unsigned int delay)
    29. {
    30.     unsigned int delayinms;  
    31.     for(delayinms=0;delayinms<delay;delayinms++)
    32.     {
    33.      __delay_ms(10);
    34.     }
    35. }
    36.  
    37. unsigned int  GetADCValue(unsigned int Channel)
    38. {
    39.     ADCON0bits.ADON =1;                              // ADC Enable
    40.     ms_delay(5) ;                                            // Time for Acquisition capacitor
    41.                                             // to charge up and show correct value
    42.     GO_nDONE  = 1;                    // Enable Go/Done
    43.     while(GO_nDONE);        // Wait for conversion completion
    44.     return ((ADRESH<<8)+ADRESL);   // Return 10 bit ADC value
    45. }
    46.  
    47. void main()
    48. {
    49.   ANSELA = 0X00;
    50.   ANSELB = 0X00;
    51.   ANSELC = 0X00;
    52.   ANSELD = 0X00;
    53.   ANSELE = 0x03;
    54.   TRISA = 0x00;
    55.   TRISB = 0x04;
    56.   TRISC = 0X00;
    57.   TRISD = 0X00;
    58.   TRISE = 0xFF;
    59.  
    60.   Lcd_Init();
    61.   ADC_Init();
    62. while(1)
    63. {
    64.     result=GetADCValue(5);
    65.     max_Value= 0;
    66.     for (i=0;i<4;i++)
    67.     {
    68.         loc_max= 0;
    69.         do{
    70.             result=GetADCValue(5);
    71.           } while(result==loc_max);
    72.         for (j=0; j<249; j++)  
    73.         {
    74.           result=GetADCValue(5);
    75.           if(result>loc_max)
    76.           loc_max = result;
    77.         }  
    78.       max_Value = loc_max;
    79.       sum = sum + max_Value;
    80.       avg = sum/5;    //get average of 5 signals
    81.       //v= avg;
    82.       avg= avg*0.707106781;
    83.    
    84.    
    85.       sprintf(s,"voltage:%f",avg);
    86.       Lcd_Write_String(s);
    87.       //Lcd_Set_Cursor(1,1);
    88.       //Lcd_Write_String("Voltage:");
    89.       //Lcd_Set_Cursor(2,1);
    90.      // Lcd_Write_String(v);
    91.     }
    92.  
    93. }
    94. }
    95.  
     
    Last edited by a moderator: Aug 27, 2016
  5. dannyf

    Well-Known Member

    Sep 13, 2015
    1,779
    360
    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.
     
    Qual likes this.
  6. Qual

    Thread Starter New Member

    Aug 5, 2016
    26
    0
    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.
     
Loading...