Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

display long values on the LCD with PIC16F877A using microc

Status
Not open for further replies.

jean12

Advanced Member level 2
Advanced Member level 2
Joined
Aug 27, 2013
Messages
529
Helped
5
Reputation
12
Reaction score
6
Trophy points
18
Activity points
5,497
Hello,I am implementing an AC voltmeter but printing the following is impossible,could you please help?I am using microc compiler:
PHP:
tlong0 =(long)max_point0*(5/255)*11; // find the peak amplitude of the line
              tlong1 =(long) max_point0*((5/255)*11*220/12)*0.707; // find the rms value of the line voltage
              
            message1[3] = (tlong0/100) + 48; // convert into ASCII
            message1[4] = (tlong0/10)%10 +48; // convert into ASCII
            message1[5] = (tlong0)%10 +48; // convert into ASCII
            Lcd_Out(1,1,message1); // Display message on LCD

The variable declaration was as follow:
PHP:
 unsigned int adc_rd0;
  unsigned int temp0=0,temp1=0;
  unsigned int max_point0 = 0;
   unsigned long tlong0,tlong1;  
  char message1[] = "PK:000V";
  char message2[] = "RMS:0000V";
 

Hi,

please be more descriptive:
but printing the following is impossible,

WHY is it impossible? What do you expect? What does happen?

Klaus
 

I was expecting to get 220V but I am not getting this but I am getting 000V,see how the message2[] was declared,I was supposed to get the peak and RMS voltage printed out on the LCD but I don`t get those values,it`s like my PIC is not reading at its AN3.


Please refer to my codes in attachment and the circuit from proteus,you can simulate and help me to get good result at the LCD.I am suing 4MHZ Quartz and PIC16F877A
The program was written to print out out also the frequency but this one is printed out as 50HZ the problem is the voltage.
 

Attachments

  • Discuss.rar
    65.8 KB · Views: 87

Code:
 max_point0 = 0;
            for(i=0;i<5000;i++)
            {
                ADCON0 = 0b00000101;
                if(temp0 = ADC_Read(0),temp0>max_point0)
                {
                   max_point0 = temp0;
                }
                Delay_us(5);
            }
This if statement in your code, stores 0 or 1 to temp0.
I know this was not your intension.
The logic expression in the if statement, forms a so called "comma separated expression" and evaluates to the last value in the comma list.

This is just one error and not necessary the only one.
 

Code:
  if(temp0 = ADC_Read(0),temp0>max_point0)
This if statement in your code, stores 0 or 1 to temp0.
No, it does not true in this case because the precedence (the priority) of the ',' (comma) operator is smaller than the precedence of the '=' (assign) operator. The whole 'if' works as the OP wanted, look at the .asm or the .lst file in the above rar.
But the program is very inefficient, and may have other (real) errors.
Sorry, but I do not have time now to review it all, its too late ...
 
Last edited:
  • Like
Reactions: xenos

    xenos

    Points: 2
    Helpful Answer Positive Rating
You always making tons of mistake again and again. Here I posting my code again as per your MCU requirement. Check it it working or not.

Code:
/*******************************************************************************
  Program for Power Factor measurement
  Written by_ Engr. Mithun K. Das
  E-mail: mithun060@gmail.com
  MCU: PIC16F877A  X-Tal: 8MHz
  mikroC_v.6.6.2
*******************************************************************************/
unsigned char ch;
unsigned int adc_rd0;
unsigned int temp0=0,temp1=0;
unsigned max_point0 = 0;
unsigned int i,tlong0,tlong1;


// LCD module connections
sbit LCD_RS at RC2_bit;
sbit LCD_EN at RC3_bit;
sbit LCD_D4 at RC4_bit;
sbit LCD_D5 at RC5_bit;
sbit LCD_D6 at RC6_bit;
sbit LCD_D7 at RC7_bit;

sbit LCD_RS_Direction at TRISC2_bit;
sbit LCD_EN_Direction at TRISC3_bit;
sbit LCD_D4_Direction at TRISC4_bit;
sbit LCD_D5_Direction at TRISC5_bit;
sbit LCD_D6_Direction at TRISC6_bit;
sbit LCD_D7_Direction at TRISC7_bit;
// End LCD module connections


char message1[] = "PK:000V";
char message2[] = "RMS:000V";

char *freq = "000";
void Display_Freq(unsigned int freq2write)
{
   freq[0] = (freq2write/100) + 48;    // Extract tens digit
   freq[1] = (freq2write/10)%10 + 48;    // Extract tens digit
   freq[2] =  (freq2write/1)%10 + 48;    // Extract ones digit
   // Display Frequency on LCD
   Lcd_Out(2, 11, freq);
   Lcd_Out(2,14,"Hz");
}
void main()
{
 TRISA = 0xFF;
 TRISC = 0x00;
 PORTC = 0x00;
 PORTA = 0x00;
 ADCON1=0x00;
 Delay_ms(1000);

 OPTION_REG = 0b00101000;

 Lcd_Init();
 Lcd_Cmd(_LCD_CLEAR);
 Lcd_Cmd(_LCD_CURSOR_OFF);
 Lcd_Out(1,1,"Measurement of");
 Lcd_Out(2,1,"AC parameter ");
 Delay_ms(2000);
 Lcd_Cmd(_LCD_CLEAR);

  while(1)
  {
      max_point0 = 0;
      for(i=0;i<1000;i++)
      {
          ADCON0 = 0b00000001;
          if(temp0 = ADC_Read(0),temp0>max_point0)  //find the peak point
          {
             max_point0 = temp0;
          }
      }
      max_point0 = abs(ceil((long)max_point0));

      tlong0 = (long)max_point0*3.9543; // Voltage = Transformer ratio * 5/255 * voltage divider ratio
      tlong1 = (long)max_point0*2.7961; // Voltage = 0.707 * Transformer ratio * 5/255 * voltage divider ratio

      message1[3] = tlong0/100 + 48;
      message1[4] = (tlong0/10)%10 +48;
      message1[5] = (tlong0)%10 +48;
      Lcd_Out(1,1,message1);

      message2[4] = tlong1/100 + 48;
      message2[5] = (tlong1/10)%10 +48;
      message2[6] = (tlong1)%10 +48;
      Lcd_Out(1,9,message2);

      TMR0=0;
      Delay_ms(985);  // Delay 1 Sec
      Lcd_Out(2,1,"FREQUENCY:");
      Display_Freq(TMR0);
      TMR0=0;

  }// while
}// void main
//


Screenshot 2016-06-01 14.26.42.png

- - - Updated - - -

Also compare with your own modified code. And compare the mistakes you made.
 

Attachments

  • AC Voltmeter.rar
    55.2 KB · Views: 79

The system is not showing zeroes but at given level it shows characters and numeric values,also I am afraid that what it is giving is not right measurement.

Best Regards,
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top