Measuring two resistors using a ADC channel

Thread Starter

kamarul amin

Joined Dec 2, 2014
62
Hi there...

I use 18F4550 pic...

how can I record two values of different resistors using one adc channel (AN2) ??
I use the programming while loop and if loop, neither of them working.. whenever i run the program and write it to the pic, it don't follow the sequence of the program, it will execute the condition while loop even though it is wrong... I don't know what to do now. I'm really want to apologise because of this noob question. I am beginner in microprocessor.

so this is my code
Code:
#include <18F4550.h>

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES HS      
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#device ADC=10

#use delay(clock=20000000)
#include <Flex_lcd.c>
#USE FAST_IO(B)

void main()
{
   set_tris_b(0x0f);
   unsigned int16 adcValue, adcValue1;
   float volt;
   float volt1;
   float load1, load;
   
   
   lcd_init();
   setup_adc_ports(ALL_ANALOG);          // set all into analog 
   setup_adc(ADC_CLOCK_INTERNAL);        // Use internal ADC clock.
   set_adc_channel(2);  
   
   while(1)
   {
      lcd_gotoxy(1,1);
      output_low(PIN_B6);
      delay_us(50);  
      set_adc_channel(2);          
      adcValue = read_adc();        // Get ADC reading
      delay_us(50);                 // Delay for sampling cap to charge
      volt=5.0*((float)adcValue/1023);      
      volt=(5.00/volt)-1;
      load1=(float)10000/volt;      
      printf(lcd_putc,"Resistor : %f",load1);    
      delay_ms(100);      
     
      while(input(pin_b1)!=1)
       {
               delay_us(50);
               output_high(PIN_B6);
               lcd_gotoxy(1,2);
               delay_us(50);              
               dcValue = read_adc();        // Get ADC reading
               delay_us(50);                 // Delay for sampling cap to charge                  
               volt=5.0*((float)adcValue/1023);                      
               volt=(5.00/volt)-1;
               load1=(float)10000/volt;      
               printf(lcd_putc,"Resistor : %f",load1);  
       }
   }

}
 

AlbertHall

Joined Jun 4, 2014
12,625
If you mean that the line
while(input(pin_b1)!=1)
acts as if pin_b1 is never equal to 1 then I think that is because of the line
setup_adc_ports(ALL_ANALOG); // set all into analog
This is setting all pins which have an analog function into analog mode - this would include pin_b1. The 'while' is doing a read of the pin as a digital input and pins set as analog will always read '0'.

Note: You don't say which compiler you are using, and I don't recognise it from the code. I have not used whichever compiler you used, so I have to imagine what the compiler functions actually do, but I think the above is a likely interpretation.
 

Thread Starter

kamarul amin

Joined Dec 2, 2014
62
If you mean that the line

acts as if pin_b1 is never equal to 1 then I think that is because of the line

This is setting all pins which have an analog function into analog mode - this would include pin_b1. The 'while' is doing a read of the pin as a digital input and pins set as analog will always read '0'.

Note: You don't say which compiler you are using, and I don't recognise it from the code. I have not used whichever compiler you used, so I have to imagine what the compiler functions actually do, but I think the above is a likely interpretation.

ahhh I don't realize that thing... i should only declare only AN2 to be analog input, ryte? Now it's working well. firstly, the code will read the first resistor. then it will read the second resistor if i push the b1 button.. Thank you!!!!! If i'm lost again, I'll seek for your advice :)

the compiler that I'm using right now is PIC C compiler..
 

AlbertHall

Joined Jun 4, 2014
12,625
Yes. The PICs power up with all possible analog pins set as analog. Any pins you want to be digital input/output must be declared as digital.

I use MPLAB-X with either MPASM for assembly or XC8 for C programs.
 

Thread Starter

kamarul amin

Joined Dec 2, 2014
62
Yes. The PICs power up with all possible analog pins set as analog. Any pins you want to be digital input/output must be declared as digital.

I use MPLAB-X with either MPASM for assembly or XC8 for C programs.
that is a good information. thank you!!!!
so, you are doing some project using 18F4550???

can I ask one more question ? I tried to write something on the lcd while having interrupt in the code, but nothing display on the lcd. I tried to make it as function, but nothing display on lcd.

here is my code
Code:
#include <18f4550.h>

#USE DELAY(CLOCK=20000000) 
#FUSES HS,NOPROTECT,NOWDT,NOLVP
#USE FAST_IO(B)
#include <Flex_lcd.c>
//The fast method of doing I/O will cause the compiler to perform I/O without programming of the direction register.
//This directive takes effect until another #use xxxx_IO directive is encountered. 
//The user must ensure the direction register is set correctly using set_tris_X()
//So, need to explicitly use set_trisx() command

int8 milisecs;
int1 outs = 1;

void lcd();

#int_TIMER0
void TIMER0_isr(void)
{ 
  int a=0;
  set_timer0(100);  //100ms overflow
  if(milisecs++ == 100)
  {
     milisecs = 0;  
     outs = outs ^ 1;
     output_bit(pin_b1, outs); 
     output_bit(pin_b0, outs);
  }
}

main()
{
   int a=0;         
   set_tris_b(0x00);
   
   output_low(PIN_B0);
   output_low(PIN_B1);
   output_low(PIN_B2);
   output_low(PIN_B3);
     
  setup_timer_0(RTCC_INTERNAL|RTCC_DIV_2); //timer 0 enebaled, pre-scaler of 2
  enable_interrupts(INT_TIMER0); //unmask timer0 interrupt to respond to timer0 match 
  enable_interrupts(global);  //enable all unmasked interrupts
  lcd_init();  
  delay_ms(50);
  lcd_putc("/f");
  while(1)
   {
     lcd();
   }
  }

void lcd()
{
      int a=0;
      a++;
      delay_ms(50);
      lcd_gotoxy(1,1);
      delay_ms(50);
      printf(lcd_putc,"Count %d", a);
}

do i need to use special function or some code to make use of lcd while having interrupt? actually I'm still having a hard time to understand interrupt :(
 

ErnieM

Joined Apr 24, 2011
8,415
What kind of LCD are you using? It is rather unusual to use printf() for this as that function tends to be rather large for the work it needs to do.

Also, which PIC C compiler are you using? There are many, and some may have LCD libraries as part of the package.
 

AlbertHall

Joined Jun 4, 2014
12,625
Usually you have to clear the interrupt flag in the interrupt routine: TMR1IF = 0
Once again I don't know your compiler, whether the compiler does this automatically for you.
If the flag is not cleared, the interrupt will be called continuously - no other code will be executed.

If you comment out the enable global interrupts line do you see the write to the LCD?
 

Thread Starter

kamarul amin

Joined Dec 2, 2014
62
What kind of LCD are you using? It is rather unusual to use printf() for this as that function tends to be rather large for the work it needs to do.

Also, which PIC C compiler are you using? There are many, and some may have LCD libraries as part of the package.
sorry for the not giving too much detail. okey, I use lcd 16x2 1602a with blue backlight.. and I use PIC C compiler and PicKit2... before this I use printf to display something on the LCD and it works fine.. but this time I don't know why it won't displaying.... the lcd libraries I got from my lecturer..
 

Thread Starter

kamarul amin

Joined Dec 2, 2014
62
Usually you have to clear the interrupt flag in the interrupt routine: TMR1IF = 0
Once again I don't know your compiler, whether the compiler does this automatically for you.
If the flag is not cleared, the interrupt will be called continuously - no other code will be executed.

If you comment out the enable global interrupts line do you see the write to the LCD?

I use PIC C compiler and PicKit2.. I comment out it already but still nothing display :( to clear the interrupt flag, I should define it? Still confusing about this chapter (interrupt) !!!!!!!! T_T
 

AlbertHall

Joined Jun 4, 2014
12,625
OK, so even with interrupts disabled you get nothing on the LCD. There are many, many possibilities for this.
Somewhere you must tell the code which PIC pins you are using to connect to the LCD. You must make sure that the LCD is actually connected where the code expects it to be.

Which company wrote the 'PIC C compiler' which you are using?
 

Thread Starter

kamarul amin

Joined Dec 2, 2014
62
OK, so even with interrupts disabled you get nothing on the LCD. There are many, many possibilities for this.
Somewhere you must tell the code which PIC pins you are using to connect to the LCD. You must make sure that the LCD is actually connected where the code expects it to be.

Which company wrote the 'PIC C compiler' which you are using?
Yes, I connected it based on the library i use. Still working on how it won't display. So, the compiler is from PCWHD. I got it from internet and also my laboratory use it. so for the pins that were connected to the LCD were...

1-GND
2-VDD
3-Con
4-RB4
5-GND
6-RB5
7 -> 14 (RD0-RD7)
15-VDD
16-Backlight
 

AlbertHall

Joined Jun 4, 2014
12,625
Do you have RB4, RB5, RD0-RD7 set as digital I/O?
When you adjust the contrast control can you make a line (or possibly two lines) of black (or possibly white) squares appear and disappear across the display?
 

Thread Starter

kamarul amin

Joined Dec 2, 2014
62
Do you have RB4, RB5, RD0-RD7 set as digital I/O?
When you adjust the contrast control can you make a line (or possibly two lines) of black (or possibly white) squares appear and disappear across the display?
set as digital I/O ? I never set it before this but it display words. it might be that whenever i use interrupt i need to set it ? Yeah right now I can see white squares boxes appears on the LCD..
 

AlbertHall

Joined Jun 4, 2014
12,625
It's only RB4 you need to worry about. Like with original question at the top of this thread, at power up RB4 will be an analog input. You need to set it as digital I/O.

If you've had words on the LCD before, what has changed since then?
 

Thread Starter

kamarul amin

Joined Dec 2, 2014
62
It's only RB4 you need to worry about. Like with original question at the top of this thread, at power up RB4 will be an analog input. You need to set it as digital I/O.

If you've had words on the LCD before, what has changed since then?

Hm how to declare it as digital I/O ? As i know it can set only to analog ryte?
 

ErnieM

Joined Apr 24, 2011
8,415
Hm how to declare it as digital I/O ? As i know it can set only to analog ryte?
No, many pins share both analog and digital functions. When they share each pin defaults to the analog function at power up, which keeps them safe no matter whatis connected to them. A digital only pin will similarly default to input again to keep things safe till the set up code is complete. As far as I know that is true across all microchip products.

Port d is digital only. Most pins of port b also have analog functions. AN 2 is on port a, and the only way to make it analog is to also make an 1 & 0 analog; see ADCON1 description.

You should be spending your time reading the spec sheet for your device. The documentation is very good and complete, so it is large. You can use the PDF table of contents to narrow down to the topics you need, such as I/O ports or A/D converter.
 

Thread Starter

kamarul amin

Joined Dec 2, 2014
62
Top