Sign In

Trouble In Measuring Voltage And Current Using Pic 18

shubham shubhampatna@gmail.com India

Hardware: On Breadboard Circuit Design Or Simulation In Proteus Software: C Project Using Mplab Ide

sendme the c code on c18 compiler.please dont use mikroc compiler

Images

Program

#include<p18f4550.h>
#define en PORTCbits.RC0
#define rw PORTCbits.RC1
#define rs PORTCbits.RC2
void ADC_init(void);
void LCD_init(void);
void LCD_data(unsigned char);
void LCD_cmd(unsigned char);
void HextoASCII(unsigned int);
void MSdelay(unsigned char);
void Digit_Display(unsigned char);
void main()
 {
    unsigned int val, voltage;
ADC_init();
  LCD_init();
  ADCON0bits.ADON=1; // Enable ADC module  
  MSdelay(10);  //time to sample
  ADCON0bits.GO=1; //soc
  while(ADCON0bits.DONE == 1);
  MSdelay(1);
  val= ADRES;
  voltage = ((val)*(68.96)/1023.0)*5*1000; //Voltage is in mV
  HextoASCII(voltage);
   }
void ADC_init(void)
{
TRISAbits.TRISA0=1;
ADCON0=0x00; // Channel 0, V_ref+=VDD and V_ref-=VSS
ADCON1=0b00001110;// Configured AN0 as analog
ADCON2=0b10001110;//Right justified, 2Tad, Fosc/64 
}
//////////////////// Converting Hex into ASCII///////////////////////////////
void HextoASCII(unsigned int myValue)
{
 unsigned int d1,d2,d3,d4;
//Taking digits one by one
d1=myValue % 0x0A;//Least Significant Digit (LSD).... myValue divided by 10, Remainder = d1
d1 = d1 | 0x30; //make it ASCII
d2=(myValue / 0x0A) % 0x0A;
d2 = d2 | 0x30; //make it ASCII
d3=((myValue/ 0x0A) / 0x0A) % 0x0A;
d3 = d3 | 0x30; //make it ASCII
d4=(((myValue/ 0x0A) / 0x0A) / 0x0A ) % 0x0A  ;//Most Sigificant Digit (MSD)
d4 = d4 | 0x30; //make it ASCII
Digit_Display(d4);// MSD
LCD_data('.'); // decimal point
Digit_Display(d3);
Digit_Display(d2);
Digit_Display(d1);// LSD
LCD_data('V');// Displaying unit V
MSdelay(1000);
}
void Digit_Display (unsigned char data)
{
LCD_cmd(0x06);//increment cursor (right shift)
LCD_data(data);
MSdelay(250);		
}
void LCD_init()
{
  TRISB=0;
  TRISC=0;
  en=0;
  LCD_cmd(0x38);
  MSdelay(250);
  LCD_cmd(0x0E);
  MSdelay(250);
  LCD_cmd(0x01);
  MSdelay(250);
  LCD_cmd(0x80);
  MSdelay(250);
  LCD_data('A');
  MSdelay(25);
  LCD_data('D');
  MSdelay(25);
  LCD_data('C');
  MSdelay(25);
  LCD_data(':');
  MSdelay(25);
}
void LCD_data(unsigned char val)
{
 PORTB=val;
 rs=1;
 rw=0;
 en=1;
 MSdelay(1);
 en=0;
}
void LCD_cmd(unsigned char val)
{
 PORTB=val;
 rs=0;
 rw=0;
 en=1;
 MSdelay(1);
 en=0;
}
void MSdelay(unsigned char ms)
{
unsigned char i,j;
for(i=0;i<=ms;i++)
for(j=0;j<=135;j++);
}

Post a reply to shubham

Think you can help shubham ? Then post your thoughts that might help shubham. You will earn a lot of reputation in the technical community.

If your program is in multiple files, then ZIP the entire folder and attach below

Images and Screenshots

These helps other better understand your suggestion.