Announcement

Collapse
No announcement yet.

Using A/D on 12LF1822

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Using A/D on 12LF1822

    Pretty sure I'm not setting up registers properly to use the A/D on the 12lf1822.
    Getting strange characters. Any ideas appreciated.

    Looks like this:

    PAUSE 10
    DEFINE DEBUG_REG PORTA
    DEFINE DEBUG_BIT 2 'RA2 serial out to LCD
    DEFINE DEBUG_BAUD 9600
    DEFINE DEBUG_MODE 0
    DEFINE DEBUG_PACING 1000
    DEFINE ADC_BITS 8
    ANSELA=4 RA4=A/D in pin
    CM1CON0=0
    CM1CON1=0
    OSCCON=%01101010
    DEFINE OSC 4
    x var byte

    low porta.5
    debug $FE, $58


    Pulsout porta.5,1

    adcin porta.4,x

    debug dec x

    pause 100

    debug $FE, $58

    goto PI

  • #2
    Code:
    ANSELA=4 RA4=A/D in pin
    adcin porta.4,x
    ANSELA=16 ;ra4 is analog ,{an3 (channel 3)}
    adcin 3,x ;[ADCIN Channel, Var]

    what about
    DEFINE ADC_SAMPLEUS 50 ' Set sampling time in uS
    DEFINE ADC_CLOCK ?

    Right/Left Justify result ?


    Comment


    • #3
      Thanks Richard. Went to the data sheet to make sense of what you explain and what I misunderstood. Also, I thought the clock and sample timing had some kind of default because in the PBP manual it states: other DEFINEs 'could be used' such as sample time and clock.

      So the 12LF1822 has a 10 bit conversion. Is it that using the DEFINE ADC_BITS 8 is going to mask the result so I get the lower 8 bits returned into 'x' (var)? Or do I have to store the 10 bit as a word and then look at the lower byte?

      Comment


      • #4
        I thought the clock and sample timing had some kind of default
        quite possibly but the manual says

        The ADC clock source should also be set.
        my pref for enhanced core chips is to set each bit in ADCON1 manually the way i want it ,these adc defines cannot always be trusted in my experience



        Is it that using the DEFINE ADC_BITS 8 is going to mask the result so I get the lower 8 bits returned into 'x'
        not absolutely sure offhand. it would make sense to left justify result and just return adresh for a "8 bit" adc read. if you get wrong looking results then the left/right justify bit may need to be set appropriately. its not something i have tried, I generally use the max no. of bits and always need to make sure i have it set justification correctly for my application

        Comment


        • #5
          It would be a good idea to add the A/D related SFRs to your code:
          Code:
          ADCON0 = %00001110  'Connects AN3 to AD & turns A/D On
          ADCON1 = %00110000  'Left Justified for 8-bit result in ADRESH, FRC clock (built-in for ADC), Vref+ to Vdd, Vref- to Vss
          Using DEFINE only tells PBP what to expect and how to handle your PBP commands. The Special Function Registers are responsible for actually making it happen the way you want.
          We can crack this cotton PIC'n thang!

          Comment


          • #6
            Thanks Mike. I actually thought of that as a last minute addendum.

            Comment


            • #7
              Mike, I'm using the internal 4Mhz clock and have: DEFINE ADC_CLOCK 4. Am I correct to do this or is the ADCON0 suppose to take care of that and thus change the ADCON0 you listed?
              Also unsure of the sampleus rate DEFINE.

              Comment


              • #8
                You might want to add:
                Code:
                OSCCON = %01101011 '4X PLL OFF, 4 MHz, Internal OSC
                We can crack this cotton PIC'n thang!

                Comment


                • #9
                  I found the following program on my drive. The file date suggests it was tested recently, but I have no recollection of it. It looks to be written for the LAB-X4 experimenter board.

                  Code:
                  ' ADCIN for PIC12F1822
                  ' Reads analog on RA4/AN3, sends serial on RA1
                  
                  DEFINE OSC 32
                  OSCCON = %11110000  ' set internal osc to 32Mhz
                  
                  ' Define ADCIN parameters
                  Define ADC_BITS 8 ' Set number of bits in result
                  'Define ADC_CLOCK 3 ' Invalid define on 12F1822, set ADCON1 for clock selection
                  Define ADC_SAMPLEUS 50 ' Set sampling time in uS
                  
                  Include "modedefs.bas" ' Mode definitions for Serout
                  
                  LCD Var PORTA.1 ' LCD TX pin
                  
                  adval Var BYTE ' Create adval to store result
                  
                  TRISA = %11111111   ' to insure we don't get locked out of ICSP,
                  PAUSE 2000          ' set all pins to inputs and pause 2 seconds
                  
                  ANSELA = %00001000  ' set RA4/AN3 to analog
                  ADCON1 = %01110011  ' left-justified, FRC clock, Vref to FVR
                  
                  mainloop:
                      ADCIN 3, adval ' Read channel 3 to adval
                  
                      Serout LCD, T2400, [$fe, 1] ' Clear screen
                      Serout LCD, T2400, ["Value: ", #adval] ' Display the decimal value
                  
                      Pause 100 ' Wait .1 second
                  
                  Goto mainloop ' Do it forever
                  
                  
                    #CONFIG
                      __config _CONFIG1, _FOSC_INTOSC
                      __config _CONFIG2, _PLLEN_ON & _LVP_OFF
                    #ENDCONFIG
                  To clarify a few points for ADCIN:

                  DEFINE ADC_BITS is really a selector between an 8-bit, left-justified setting and a 16-bit, right-justified setting. With DEFINE ADC_BITS 8, PBP reads ADRESH for a byte result. With any value greater than 8, PBP reads the register pair ADRESH:ADRESL for a word result. The PIC hardware determines whether you get a 10, 12 or 16 bit result. This define exists to save a few instruction cycles when you only need 8-bits.

                  Over the course of many years and many PICs, Microchip moved the adc-clock-select bits so many times that it overwhelmed our method of coding those locations. I now have a method that uses individual files for variants of ADCIN, but I don't know if I'll ever have the time to locate and fix the chips that were broken in the distant past. The comment in the program above indicates that the ADC_CLOCK define doesn't function for the 12F1822. I assume I verified that when I tested. In any case, it's better to set the clock in the SFR.

                  Richard pointed out that ADCIN PORTA.4 and ADCIN 3 are different, but I want to document why. ADCIN needs a numeric channel number in order to transfer that value into the channel-select bits in the appropriate register. The channel numbers usually don't match pin numbers in a useful way. Since you can use a variable for the channel in the ADCIN command, PBP is forced to also accept SFR names. When you use the bit-entity PORTA.4, ADCIN reads the value of that entity just like it was a bit variable.

                  That means that ADCIN PORTA.4 will perform the conversion on AN0 if PORTA.4 reads low and on AN1 if PORTA.4 reads high. It will never perform a conversion on AN3.
                  Charles Leo
                  ME Labs, Inc.
                  http://melabs.com

                  Comment


                  • #10
                    Very cool, Charles. Thanks for that.

                    Comment


                    • #11
                      Now that's a post that should be copy/pasted straight into the manual IMHO. Thumbs up Charles!

                      /Henrik.

                      Comment


                      • #12
                        Something else to note, There seems to be a discrepancy in the data sheet. Noted in section 16.0 there is an AN3 input for the A/D. However in section 12-5 there is NO ANSA3 bit to be set as an analog input for the ANSELA register. Also in table 1-2 of the pin descriptions there is NO AN3 mentioned.
                        Dave Purola,
                        N8NTA
                        EN82fn

                        Comment

                        Working...
                        X