CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

PIC 16F877A LCD with ADC

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
steppermotor



Joined: 24 Oct 2010
Posts: 10

View user's profile Send private message

PIC 16F877A LCD with ADC
PostPosted: Thu Oct 28, 2010 7:33 pm     Reply with quote

I'm new to the world of PIC. I've got my LCD working, but I'm trying to display the voltage I that is read in by the PICs ADC port. I'm using the PIC 16F877A, here is sections of my code. However it keeps displaying 8, even when I have it connected to 5 Volts or ground. Any idea what's wrong with my code, that is does not measure the correct voltage.
Code:

char print_lcd(char s[])
{
   delay_ms(1000);
   lcd_putc('\f');
   delay_ms(250);
   output_high(pin_b0);
   delay_ms(250);
   printf(lcd_putc,"LCD Works");
   delay_ms(5000);
   lcd_gotoxy(0x1,2);
   lcd_putc(s);
   
   return s;
}

void measure_voltage()
{
     char txt[20];
     int16 voltage, adc_value;

      while(true){
      set_adc_channel(0);//set the pic to read from AN0
      delay_us(20)
      adc_value = read_adc();
      voltage = adc_value*(5/1023);
      sprintf(txt, "%i\0", (int)voltage);
      print_lcd(txt);
   }

}

void main()
{

   char txt[20] = "yes";
   setup_adc(ADC_CLOCK_DIV_8);
   setup_adc_ports(ALL_ANALOG);
   setup_psp(PSP_DISABLED);
   setup_spi(SPI_SS_DISABLED);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
   
   output_low(pin_b0);
   lcd_init();
   
   print_lcd(txt);
   measure_voltage();
}
vinniewryan



Joined: 29 Jul 2009
Posts: 154
Location: at work

View user's profile Send private message MSN Messenger

PostPosted: Thu Oct 28, 2010 8:19 pm     Reply with quote

Well in the line "voltage = adc_value*(5/1023);", you're multiplying "voltage" by .004, and if your ADC value is anywhere between 1875 and 2124, then your outcome would always be 8 because the PIC would round the number to the nearest integer. Try removing the "(5/1023)" and report the result.

I see no appearent mistakes in your code, so you may also want to post the rest of the code as well.

You can also try writing a simple code that only tests the ADC port, something like:

Code:

#include '16f677.h'
#use_delay(clock=xxxxxxx)
#use adc=10
#define//

int16 voltage=0;

void main()
{
   while(1)
   {
      setup_adc(ALL_ANALOG)
      set_adc_channel(0);
      delay_us(20);
      voltage=read_adc;
      printf('%i', voltage);
   }
}



This is just an example, not a working code.
_________________
Vinnie Ryan
steppermotor



Joined: 24 Oct 2010
Posts: 10

View user's profile Send private message

Re:LCD W ADC
PostPosted: Thu Oct 28, 2010 8:33 pm     Reply with quote

Thanks, Vinnie for your reply, I made the following modifications. The output is just 000000, even when I have plugged the RA0 to 5 V.
Code:

void measure_voltage()
{
//char txt[20];
int adc_value;
int16 voltage;
//float voltage;
  output_high(pin_b0);
   delay_ms(250);
   printf(lcd_putc,"LCD Works");

   while(1)
   {
      set_adc_channel(0);
      delay_us(20);
      voltage=read_adc();
      printf(lcd_putc,"%lu",voltage);
   }
}
void main()
{

   setup_adc(ADC_CLOCK_INTERNAL);
   setup_adc_ports(ALL_ANALOG);
   setup_psp(PSP_DISABLED);
   setup_spi(SPI_SS_DISABLED);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(TRUE);
   
   output_low(pin_b0);
   lcd_init();
   
   //print_lcd(txt);
   measure_voltage();
}
steppermotor



Joined: 24 Oct 2010
Posts: 10

View user's profile Send private message

Got it working
PostPosted: Thu Oct 28, 2010 8:43 pm     Reply with quote

Thanks, but I got it working.

I modified to the following:
Code:

 while(1)
   {
      set_adc_channel(0);
      delay_us(20);
      voltage=read_adc();
      printf(lcd_putc,"%u",voltage);
   }

and changed setup_adc(ADC_CLOCK_DIV_32);

from setup_adc(ADC_CLOCK_INTERNAL);

Thanks
steppermotor



Joined: 24 Oct 2010
Posts: 10

View user's profile Send private message

One more problem
PostPosted: Thu Oct 28, 2010 9:12 pm     Reply with quote

When I have a 5V input on my PIC 16F877A ADC AN0 Port is outputs:

1023 on the LCD which I know is the correct value, but when I attempt to multiply by 5/1023 and this does not gives me a result of zero. Perhaps I'm using the wrong int/float/lu. Any suggestions?
Code:

void measure_voltage()
{
int16 voltage;
int16 con_voltage;
float con_voltage2;
//float voltage;
  output_high(pin_b0);
   delay_ms(250);
   printf(lcd_putc,"LCD Works");

   //while(1)
  // {
      set_adc_channel(0);
      delay_us(20);
      voltage=read_adc();
      printf(lcd_putc,"%lu\n",voltage); // This displays 1023 which is correct for 5 VDC

      con_voltage = (int)voltage * (5/1023);
      printf(lcd_putc,"%lu",con_voltage); //outputs 0
      delay_ms(20);
      con_voltage2 = (int)voltage * (5/1023);
      delay_ms(20);
      printf(lcd_putc,"      %lf",con_voltage2); //outputs 0
   
  // }
}
steppermotor



Joined: 24 Oct 2010
Posts: 10

View user's profile Send private message

Got it again
PostPosted: Thu Oct 28, 2010 9:41 pm     Reply with quote

I changed:
Code:

while(1)
     {
      set_adc_channel(0);
      delay_us(20);
      voltage=read_adc();
      con_voltage2 = voltage * (5.0/1023.0);
      delay_ms(20);
      printf(lcd_putc,"\n%lf",con_voltage2);
      }
}
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group