Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

setting up ADC port

Status
Not open for further replies.
It could be an INTEGER, but unless you are expecting negative values from the ADC, a WORD would be better.

swordfish variables.jpg
 
here it is close but not liner when it gets to one end of resistor ladder
Code:
Device = 18F43k22
Clock = 20

// some LCD options...
#option LCD_DATA = PORTD.4
#option LCD_RS = PORTD.2
#option LCD_EN = PORTD.3

// uses LCD and AD libraries...
Include "LCD.bas"
Include "ADC.bas"
Include "convert.bas"
          
// read the AD port and scale for 0 - 5 volts...
Function ADInAsVolt() As Word
   result = (ADC.Read(0) + 1)*500/1024  //set ADC input  as porta.0
End Function

// sampled AD value...
Dim ADVal As Word
Dim led As PORTA.0
Dim led1 As PORTC.0

// initialise and clear LCD...
ADCON1 = $07       // PORTE as digital (LCD)
TRISA.1 = 1        // configure AN0 as an input ///
ADCON1.6 = 1       // set analogue input on PORTA.0 ///
DelayMS (500)
LCD.Cls
 Input(PORTA.0)                  // ADC IN on portA.1
    
   // ADCON1 = %10000000              //set up ADC regesters
    //ADCON0.7 = 1
    //ADCON0.6 = 1
 
    ADCON1 = $07               
    TRISA = %11111111   
    TRISB = %11111111
    TRISC = %00000000           
    TRISD = %00000000   
    TRISE = %000
    
    /////////////////////////////
     'ADC setup
    ADCON2=%10101111
    ADCON1=%00000000
    TRISA=%00000001
    ANSELA=%00000001
    ADCON0=%00000001
    ADCON0=%00000011
    ///////////////////////////////////
 
    While ADCON1.bits(1) <> 0  'ADC not done yet
        DelayUS(10)
    Wend

// main program loop...
While true

DelayMS(200)
   ADVal = ADInAsVolt
   LCD.MoveCursor (1,1)
   LCD.Write("DC Volts = ", DecToStr(ADVal))//, ".", DecToStr(ADVal, 2), " ")
   DelayMS(250)
Wend
the test points (where switches connect) go 454, 409, 322, 279, 238, 197, 156, 115, 745
as predicted it follows the voltage but my math is wrong. the 745 is supposed to be less than 115. should be .745??
 
Guess what the statement:

ADVal = ADInAsVolt

does?

It takes the 10 bit ADC value and turns it into volts. You lose resolution and then get lost in dealing with non-integer numbers. [echo, echo, echo....]

Replace that line with:

ADVal = ADC.read(0)

Change this:

LCD.Write("DC Volts = ", DecToStr(ADVal))//, ".", DecToStr(ADVal, 2), " ")

to this:

LCD.Write("ADC = ", DecToStr(ADVal)), " ")
 
after some editing suggestions it now works as desired
Code:
Device = 18F43k22
Clock = 20

// some LCD options...
#option LCD_DATA = PORTD.4
#option LCD_RS = PORTD.2
#option LCD_EN = PORTD.3

// uses LCD and AD libraries...
Include "LCD.bas"
Include "ADC.bas"
Include "convert.bas"
          
// read the AD port and scale for 0 - 5 volts...
Function ADInAsVolt() As Word
   result = (ADC.Read(0) + 1)*500/1024  //set ADC input  as porta.0
End Function

// sampled AD value...
Dim ADVal As Word
Dim led As PORTA.0
Dim led1 As PORTC.0
Dim led2 As PORTC.1
Dim led3 As PORTC.3
Dim ADCOut As Word
// initialise and clear LCD...
ADCON1 = $07       // PORTE as digital (LCD)
TRISA.1 = 1        // configure AN0 as an input ///
ADCON1.6 = 1       // set analogue input on PORTA.0 ///
DelayMS (500)
LCD.Cls
 Input(PORTA.0)                  // ADC IN on portA.1
 ADCOut = 0   
   // ADCON1 = %10000000              //set up ADC regesters
    //ADCON0.7 = 1
    //ADCON0.6 = 1
 
    ADCON1 = $07               
    TRISA = %11111111   
    TRISB = %11111111
    TRISC = %00000000           
    TRISD = %00000000   
    TRISE = %000
    
    /////////////////////////////
     'ADC setup
    ADCON2=%10101111
    ADCON1=%00000000
    TRISA=%00000001
    ANSELA=%00000001
    ADCON0=%00000001
    ADCON0=%00000011
    ///////////////////////////////////
 
    While ADCON1.bits(1) <> 0  'ADC not done yet
        DelayUS(10)
    Wend

// main program loop...
While true
  // Dim ADCOut as Word



DelayMS(200)
   ADVal = ADC.Read(0)   // changed as per "visitor"
   LCD.MoveCursor (1,1)
   LCD.Write("ADC = ", DecToStr(ADVal), " ")  // changed as per "visitor"
  
  
   //LCD.Write("DC Volts = ", DecToStr(ADVal))//, ".", DecToStr(ADVal, 2), " ")
   DelayMS(250)
 
  
Wend

output = 152, 236, 320, 405, 488, 572, 659, 723, 836, 929now omn to building the CASE SELECT  portion
 
Are you aware that there is a recent update (last few months)? I believe that ADC.BAS and most of the other include files have been updated to support new chip variants.
Yes, but ADC.BAS isn't one of the libraries that have been updated... it's still the same version as long ago.

If it doesn't work then you should be able to set up the ADC registers manually and still use ADC.Read() for most devices (at least for chan 0-15)
 
I just stand away. Been there a few years ago and realize that it's futile. However, still have to watch the train wreck.

Mike.
 
I played with this a bit
Code:
Device = 18F25K22
Clock = 64            

// some LCD options...
#option LCD_DATA = PORTC.4
#option LCD_RS = PORTC.0
#option LCD_EN = PORTC.1

// uses LCD and AD libraries...  /
Include "IntOSC.bas"
Include "LCD.bas"
Include "ADC.bas"
Include "convert.bas"
         
// read the AD port
Function ADCread() As Word
   result = (ADC.Read(0))
End Function

// sampled AD value...
Dim ADVal As Word

// initialise and clear LCD...
TRISA.0 = 1        // configure AN0 as an input
DelayMS (500)
LCD.Cls

     //ADC setup
    ADCON2=%10101111
    ADCON1=%00000000
    TRISA=%00000001
    ANSELA=%00000001
    ADCON0=%00000001
    ADCON0=%00000011


    While ADCON1.bits(1) <> 0  //Is ADC done yet
        DelayUS(10)
    Wend
// main program loop...
While true
   ADVal = ADCread
   LCD.MoveCursor (1,1)
   LCD.Write("ADC = ", DecToStr(ADVal),  " ")
   DelayMS(250)
Wend
 
thanks Burt. Will compare w/ what I have and try it out.
Presently working on a long delay that shouldn't be there.
I wired 4 leds and coding so when certain "switch" is pressed, the desired led is enabled UNTIL the same "switch" is pressed.
Will post code AFTER I figure out why the long delay.
 
done did that.
I found my extra delay that was hidden among the led statements that I removed.
All works. Debating leaving the FUNCTION--ETC WITH THE adc.read(0)+1)*500/1023
iT DIDN'T SEEM TO AFFECT ANYTHING WITH OR WITHOUT THE EXTRA MATH.
Code:
Device = 18F43k22
Clock = 20

// some LCD options...
#option LCD_DATA = PORTD.4
#option LCD_RS = PORTD.2
#option LCD_EN = PORTD.3

// uses LCD and AD libraries...
Include "LCD.bas"
Include "ADC.bas"
Include "convert.bas"
          
// read the AD port and scale for 0 - 5 volts...
Function ADInAsVolt() As Word
   result = (ADC.Read(0))// + 1)*500/1024  //set ADC input  as porta.0 HERE IS WHERE i TOOK OUT
End Function

// DIM STATEMENTS
Dim ADVal As Word
Dim led  As PORTC.4
Dim led1 As PORTC.0
Dim led2 As PORTC.1
Dim led3 As PORTC.3
Dim ADCOut As Word
// initialise and clear LCD...
ADCON1 = $07       // PORTE as digital (LCD)
TRISA.1 = 1        // configure AN0 as an input ///
ADCON1.6 = 1       // set analogue input on PORTA.0 ///
DelayMS (500)
LCD.Cls
 Input(PORTA.0)                  // ADC IN on portA.1
 ADCOut = 0
 Output(led)      //LED TESTING
 Output(led1)
 Output(led2)
 led=0
 led2=0
 led3=0   
   // ADCON1 = %10000000              //set up ADC regesters
    //ADCON0.7 = 1
    //ADCON0.6 = 1
 
    ADCON1 = $07               
    TRISA = %11111111   
    TRISB = %11111111
    TRISC = %00000000           
    TRISD = %00000000   
    TRISE = %000
    
    /////////////////////////////
     'ADC setup
    ADCON2=%10101111
    ADCON1=%00000000
    TRISA=%00000001
    ANSELA=%00000001
    ADCON0=%00000001
    ADCON0=%00000011
    ///////////////////////////////////
 
    While ADCON1.bits(1) <> 0  'ADC not done yet
        DelayUS(10)
    Wend

// main program loop...
While true
  ADVal = ADC.Read(0)   // changed as per "visitor"
 

DelayMS(200)
   ADVal = ADC.Read(0)   // changed as per "visitor"
   LCD.MoveCursor (1,1)
   LCD.Write("ADC = ", DecToStr(ADVal), "   ")  // changed as per "visitor"
  
  
   //LCD.Write("DC Volts = ", DecToStr(ADVal))//, ".", DecToStr(ADVal, 2), " ")
   //DelayMS(250)
Select ADVal

    Case > 1000 'value over 1000
        'no button pressed
    Case > 900 Toggle(led)'value between 901 – 1000
        'Button 10 pressed
    Case > 800 Toggle(led1) 'value between 801 – 900
        'Button 9 pressed
    Case > 700 Toggle(led2)'value between 701 – 800
        'Button 8 pressed

End Select
Wend
 
Debating leaving the FUNCTION--ETC WITH THE adc.read(0)+1)*500/1023
iT DIDN'T SEEM TO AFFECT ANYTHING WITH OR WITHOUT THE EXTRA MATH.

You do understand (apparently not) that the function ADCInAsVolt( ) is never called and therefore:

¤ Isn't needed

¤ Doesn't do anything (in this program)

ADCInAaVolt reads the ADC and converts the readings to voltage – an unnecessary conversion that gave you all kinds of problems. Remember?

You also know (right?) that the CASE/SELECT code I wrote nneeds to be adjusted for the values for your keypad, and CASE statements need to be added for each button? The values in the CASE statements should be adjusted so that each button value is in the middle of the range.
 
I know I shouldn't read these threads but it's just irresistible. Train wreck type of thing.

Mike.
 
I know I shouldn't read these threads but it's just irresistible. Train wreck type of thing.

Mike.

We seemed to be making progress with some near misses but not quite a train wreck up until now. There was some hope that maybe some paint would be scratched, but no lives lost along the way but I'm afraid that hope is gone.
 
I realize I need to alter the CASE/SELECT for my application If I am interpenetrating correctly I can eliminate the FUNCTION.
As I understand it I need the FUNCTION for the CASE/SELECT as per the help file.
Sorry Pommie, no train wreck yet.
 
Here you something to play with
Code:
Device = 18F43k22
Clock = 20

// some LCD options...
#option LCD_DATA = PORTD.4
#option LCD_RS = PORTD.2
#option LCD_EN = PORTD.3




// uses LCD and AD libraries...  /
Include "LCD.bas"
Include "ADC.bas"
Include "convert.bas"
     
// read the AD port
Function ADCread() As Word
   result = (ADC.Read(0))
End Function

// sampled AD value...
Dim ADVal As Word
Dim ADCOut As Word
// initialise and clear LCD...
TRISA.0 = 1        // configure AN0 as an input
LCD.Cls
DelayMS (500)

   //ADC setup
    ADCON2=%10101111
    ADCON1=%00000000
    TRISA=%00000001
    ANSELA=%00000001
    ADCON0=%00000001
    ADCON0=%00000011


    While ADCON1.bits(1) <> 0  //Is ADC done yet
        DelayUS(10)
    Wend
// main program loop...
While true
   ADVal = ADCread
   ADCOut = ADVal
   LCD.MoveCursor (1,1)
   Select ADCOut

    Case > 1000 'value over 1000
        ' button 1 pressed
        LCD.Write("button1 = ", DecToStr(ADVal),  " ")
   Case > 500  'value between 500 – 1000
        'Button 2 pressed
         LCD.Write("button2 = ", DecToStr(ADVal),  " ")
   Case > 330 'value between 330 – 500
        'Button 3 pressed
         LCD.Write("button3 = ", DecToStr(ADVal),  " ")
   Case > 230 'value between 230 – 330
       'Button 4 pressed
        LCD.Write("button4 = ", DecToStr(ADVal),  " ")
   Case > 190 'value between 190 – 230
       'Button 5 pressed
        LCD.Write("button5 = ", DecToStr(ADVal),  " ")

End Select
   DelayMS(250)
Wend
You have to change the case numbers to match your buttons adc readings
 
Last edited:
I realize I need to alter the CASE/SELECT for my application If I am interpenetrating correctly I can eliminate the FUNCTION.
As I understand it I need the FUNCTION for the CASE/SELECT as per the help file.
Sorry Pommie, no train wreck yet.


Huh? If by FUNCTION, you mean the ADCInAsVolt function, please re-read my last post
 
Last edited:
tHANKS bURT, NEVER OCCURED TO USE THE lcd to show for testing button pressed.
will try it out.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top