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.

[SOLVED] Measure Voltage and Current Pic16f877a (ccs C compiler)

Status
Not open for further replies.

armghan11

Member level 1
Member level 1
Joined
Oct 9, 2012
Messages
41
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Visit site
Activity points
1,557
Hey i have a mickro c code to measure voltage and current ... i want to run this code on ccs compiler.... please help

Code:
// LCD module connections
sbit LCD_RS at RB5_bit;
sbit LCD_EN at RB7_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 TRISB5_bit;
sbit LCD_EN_Direction at TRISB7_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 look(int a)
{
   switch(a)
   {
       case 0:
              return '0';
       case 1:
              return '1';
       case 2:
              return '2';
       case 3:
              return '3';
       case 4:
              return '4';
       case 5:
              return '5';
       case 6:
              return '6';
       case 7:
              return '7';
       case 8:
              return '8';
       case 9:
              return '9';
       default:
              return '.';
   }
}



void main()
{

  unsigned int v,vp,ip,i;
  char *volt = "00.0";
  char *current = "0.00";
  TRISA  = 0xFF;
  Lcd_Init();
  Lcd_Cmd(_LCD_CLEAR);
  Lcd_Cmd(_LCD_CURSOR_OFF);

  do
  {

       ADCON1 = 0x00;
       v = ADC_Read(2);
       i = ADC_Read(3);
       i = (i*4.89)/0.47;
       v = ((v*4.89)/20)*120;
       if(v!=vp || i!=ip )
          Lcd_Cmd(_LCD_CLEAR);
       vp = v;
       ip = i;
       volt[0] = look(v/10000);
       volt[1] = look((v/1000)%10);
       volt[3] = look((v/100)%10);
       Lcd_Out(1,1,"Voltage = ");
       Lcd_Out(1,11,volt);
       Lcd_Out(1,16,"V");

       current[0] = look(i/1000);
       current[2] = look((i/100)%10);
       current[3] = look((i/10)%10);
       Lcd_Out(2,1,"Current = ");
       Lcd_Out(2,11,current);
       Lcd_Out(2,16,"A");
       Delay_ms(250);
  } while(1);
}
 

#define LCD_ENABLE_PIN PIN_B5
#define LCD_RS_PIN PIN_B7
#define LCD_DATA4 PIN_D4
#define LCD_DATA5 PIN_D5
#define LCD_DATA6 PIN_D6
#define LCD_DATA7 PIN_D7

#include <lcd.c>
char look(int a)
{
switch(a)
{
case 0:
return '0';
case 1:
return '1';
case 2:
return '2';
case 3:
return '3';
case 4:
return '4';
case 5:
return '5';
case 6:
return '6';
case 7:
return '7';
case 8:
return '8';
case 9:
return '9';
default:
return '.';
}
}


void main()
{
unsigned int16 v,vp,ip,i;
char *volt = "00.0";
char *current = "0.00";
set_tris_a (0xFF);
lcd_putc(\f); // \f Clear display
do
{
set_adc_channel(2);
delay_us(10);
v = read_ADC();
set_adc_channel(3);
delay_us(10);
i = read_ADC();
i = (i*4.89)/0.47;
v = ((v*4.89)/20)*120;
if(v!=vp || i!=ip )
lcd_putc(\f); // \f Clear display
vp = v;
ip = i;
volt[0] = look(v/10000);
volt[1] = look((v/1000)%10);
volt[3] = look((v/100)%10);

lcd_gotoxy(1,1);
printf(LCD_PUTC, "Voltage = %LuV",volt);
current[0] = look(i/1000);
current[2] = look((i/100)%10);
current[3] = look((i/10)%10);
lcd_gotoxy(2,1);
printf(LCD_PUTC, "Current = %LuA",current);

Delay_ms(250);
} while(1);
}
 

#define LCD_ENABLE_PIN PIN_B5
#define LCD_RS_PIN PIN_B7
#define LCD_DATA4 PIN_D4
#define LCD_DATA5 PIN_D5
#define LCD_DATA6 PIN_D6
#define LCD_DATA7 PIN_D7

#include <lcd.c>
char look(int a)
{
switch(a)
{
case 0:
return '0';
case 1:
return '1';
case 2:
return '2';
case 3:
return '3';
case 4:
return '4';
case 5:
return '5';
case 6:
return '6';
case 7:
return '7';
case 8:
return '8';
case 9:
return '9';
default:
return '.';
}
}


void main()
{
unsigned int16 v,vp,ip,i;
char *volt = "00.0";
char *current = "0.00";
set_tris_a (0xFF);
lcd_putc(\f); // \f Clear display
do
{
set_adc_channel(2);
delay_us(10);
v = read_ADC();
set_adc_channel(3);
delay_us(10);
i = read_ADC();
i = (i*4.89)/0.47;
v = ((v*4.89)/20)*120;
if(v!=vp || i!=ip )
lcd_putc(\f); // \f Clear display
vp = v;
ip = i;
volt[0] = look(v/10000);
volt[1] = look((v/1000)%10);
volt[3] = look((v/100)%10);

lcd_gotoxy(1,1);
printf(LCD_PUTC, "Voltage = %LuV",volt);
current[0] = look(i/1000);
current[2] = look((i/100)%10);
current[3] = look((i/10)%10);
lcd_gotoxy(2,1);
printf(LCD_PUTC, "Current = %LuA",current);

Delay_ms(250);
} while(1);
}

Thanksss so much

- - - Updated - - -

when i run this command... i got an error printf format type is invalid

Code:
printf(lcd_putc,"Voltage = %LuV",volt);
 

Sorry volt and current are string so use %s instead of %Lu

printf(lcd_putc,"Voltage = %sV",volt);

Similarly

printf(LCD_PUTC, "Current = %sA",current);
 

Please recommend me some books to understand CCS

- - - Updated - - -

Sorry volt and current are string so use %s instead of %Lu

printf(lcd_putc,"Voltage = %sV",volt);

Similarly

printf(LCD_PUTC, "Current = %sA",current);

Thanks it worked
 

The Compiler was giving no error but proteus simulation was not displaying anything... here are the changes that i made... now its working Alhamdulillah

Code:
#include <16F877a.H> 
#device adc=10 
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)

#include <lcd2.c>

char look(int a)
{
switch(a)
{
case 0:
return '0';
case 1:
return '1';
case 2:
return '2';
case 3:
return '3';
case 4:
return '4';
case 5:
return '5';
case 6:
return '6';
case 7:
return '7';
case 8:
return '8';
case 9:
return '9';
default:
return '.';
}
}


void main()
{

lcd_init();
unsigned int16 v,vp,ip,i;
Char *volt = "00.0";
Char *current = "0.00";
set_tris_a (0xFF);
do
{
setup_adc_ports(AN0); 
setup_adc(ADC_CLOCK_DIV_8); 
set_adc_channel(2); 
delay_us(20); 
v = read_ADC();
setup_adc_ports(AN0);
setup_adc(ADC_CLOCK_DIV_8); 
set_adc_channel(3); 
i = read_ADC();
i = (i*4.89)/0.47;
v = ((v*4.89)/20)*120;
if(v!=vp || i!=ip )
vp = v;
ip = i;
volt[0] = look(v/10000);
volt[1] = look((v/1000)%10);
volt[3] = look((v/100)%10);

lcd_gotoxy(1,1);
printf(lcd_putc,"Voltage = %sV",volt);
current[0] = look(i/1000);
current[2] = look((i/100)%10);
current[3] = look((i/10)%10);
lcd_gotoxy(1,2);
printf(LCD_PUTC, "Current = %sA",current);

Delay_ms(250);
} while(1);
}
 

The Compiler was giving no error but proteus simulation was not displaying anything... here are the changes that i made... now its working Alhamdulillah

Code:
#include <16F877a.H> 
#device adc=10 
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)

#include <lcd.c>

char look(int a)
{
switch(a)
{
case 0:
return '0';
case 1:
return '1';
case 2:
return '2';
case 3:
return '3';
case 4:
return '4';
case 5:
return '5';
case 6:
return '6';
case 7:
return '7';
case 8:
return '8';
case 9:
return '9';
default:
return '.';
}
}


void main()
{

lcd_init();
unsigned int16 v,vp,ip,i;      //A numeric expression must appear here "unsigned"
Char *volt = "00.0";           //A numeric expression must appear here "char"
Char *current = "0.00";      //A numeric expression must appear here "char"
set_tris_a (0xFF);
do
{
setup_adc_ports(AN0); 
setup_adc(ADC_CLOCK_DIV_8); 
set_adc_channel(2); 
delay_us(20); 
v = read_ADC();
setup_adc_ports(AN0);
setup_adc(ADC_CLOCK_DIV_8); 
set_adc_channel(3); 
i = read_ADC();
i = (i*4.89)/0.47;
v = ((v*4.89)/20)*120;
if(v!=vp || i!=ip )
vp = v;
ip = i;
volt[0] = look(v/10000);
volt[1] = look((v/1000)%10);
volt[3] = look((v/100)%10);

lcd_gotoxy(1,1);
printf(lcd_putc,"Voltage = %sV",volt);
current[0] = look(i/1000);
current[2] = look((i/100)%10);
current[3] = look((i/10)%10);
lcd_gotoxy(1,2);
printf(LCD_PUTC, "Current = %sA",current);

Delay_ms(250);
} while(1);
}

When i am compiled get 3 errors as "A numeric expression must here"
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top