Hi,everybody.I wrote a program with mikroC for PIC PRO/2009/V2.50 ,to measure
a DC voltage input using 16F877A.But it shows the input dc voltage wrongly and uncorrectly;[ it shows 0.014v instead of correct form; 14v ].
I attached my program and its proteus.would you please tell me what is wrong with my program to correct it?

Thanks,

Hi,everybody.Here is my code/program that I have problem with it as I said above;
[ ;[ it shows 0.014v instead of correct form; 14v ].

Thanks,

unsigned char ch0=0;
int t0=0,index0=0;
char *tc;

#define stepsizev  98 //(100/1024)x1000:(0v-100v)voltage input range in mv;
                                         //(97,6 mv ~ 98 mv),10-bit resolution


void Display_Adcz(int adcval0) {
  long longadc0=0;
  char ch0=0;
  adcval0=0 ;

  t0  = ADC_Read(0);

  index0   = (stepsizev);
  ADRESH   = (ADRESH)&(0b00000011);
  longadc0 = ADRESH + ADRESL;
  adcval0  = longadc0 * index0;//in mv

  ch0     = adcval0/1000 ; //prepare value for display in V . xxxxxxxxxxxx
  Lcd_Chr(1, 5, 48 + ch0);//write ASCII at 1st row, 1st column BBBB.
  Lcd_Chr_Cp('.');

  ch0    = (adcval0 /100) % 10;
  Lcd_Chr_Cp(48 + ch0);

  ch0    = (adcval0 /10) % 10;
  Lcd_Chr_Cp(48 + ch0);

  ch0    = adcval0 % 10;
  Lcd_Chr_Cp(48 + ch0);
  Lcd_Chr_Cp('V');


}//~



// LCD module connections
sbit LCD_RW at RD2_bit;
sbit LCD_RS at RD1_bit;
sbit LCD_EN at RD0_bit;
sbit LCD_D4 at RD4_bit;
sbit LCD_D5 at RD5_bit;
sbit LCD_D6 at RD6_bit;
sbit LCD_D7 at RD7_bit;

sbit LCD_RW_Direction at TRISD2_bit;
sbit LCD_RS_Direction at TRISD1_bit;
sbit LCD_EN_Direction at TRISD0_bit;
sbit LCD_D4_Direction at TRISD4_bit;
sbit LCD_D5_Direction at TRISD5_bit;
sbit LCD_D6_Direction at TRISD6_bit;
sbit LCD_D7_Direction at TRISD7_bit;
// End LCD module connections



void main() {

  PORTA=0;//Clear port A
  TRISA=0XFF;//Port A is input
  PORTD=0;//Clear port D
  TRISD=0;//Port D is output
  PORTC=0;//Clear port C
  TRISC=0;//port C is output
  PORTB=0;//Clear port B
  TRISB=0XFF;//port B is input
  PORTE=0;//Clear port E
  TRISE=0;//Port E is Output zzzzz
  TRISC = PORTC = 0;                       // Only for PIC16f877A //

  INTCON.GIE = 1; // Enables all unmasked interrupts,Global interrupt enable bit
  INTCON.RBIE = 1;//Enables the RB Port change interrupt
  INTCON.INTE=1;//Enables the INT0 external interrupt
  OPTION_REG.INTEDG = 0;//Interrupt on falling edge of RB0/INT pin.

  LCD_Init();// initialize(4-bit interface connection)
  LCD_Cmd(_LCD_CURSOR_OFF);// send command to LCD (cursor off)
  LCD_Cmd(_LCD_CLEAR);// send command  to LCD (clear LCD)

  tc = "Hi!";// assign a text to string
  LCD_Out(1,1,tc);// print a string  on LCD, 1st row, 1st column

  PORTC.F2=1;//Turn the buzzer on (Rc2/pin17)
  Delay_ms(1000);//One second pause
  PORTC.F2=0;//Turn the buzzer off (RC2/pin17)


  //Not required to determine ADCON0;it is asked by uC compiler on NEW Project
  // icon bar at the top at first automatically &done via:Adc_Read() Library

  ADCON1=0x80;//configure VDD=Vref+,Vss=Vref-,8-analog channels&Right justified
                                                           //A/D Result Format
  Delay_ms(400);

VLP1:LCD_Cmd(_LCD_CLEAR);
  tc  = "Vch:";//assign a text to string
  LCD_Out(1,1,tc);//print a string on LCD, 1st row, 1st column
  Display_Adcz(t0);
  Delay_ms(400);

  goto VLP1;

  }//~!
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.