Menu

pic10f322 readAD issues

Help
2017-06-12
2017-06-13
  • tony golding

    tony golding - 2017-06-12

    i am just trying to get a readAd function working on a pic10f322 using the built in gcb routines but it seems like the adc part is not working.

    for now i am just adjusting a potentiometer to change a preload value into timer0 to alter interrupt periods and toggle an led on a pin between on and off states.

    if i manually adjust the numeric value of timer0 preload by typing in different numbers in the code then it works fine and the pin toggles at the differinng rates but if i use readAD with the pot into a variable and use that to alter the timer0 preload then i get no change to the led output.

    i honestly cant see why its not working and just assume that i can use the gcb ADC commands to deal with setting up the adc for use but im guessing as its a lower end chip that i am probably going to have to manually set the adc registers unless someone can shed some light on the situation.

    ~~~

    #chip 10f322, 2
    #config MCLRE_off, OSC_INTOSC

    #define led PORTA.0
    #define ssr_out PORTA.1 'led on this pin for testing
    #define timer_pot AN2
    #define foot_switch PORTA.3

    TRISA = b'00001100'
    LATA = b'00000000'
    ' ANSELA = b'00000100'

    dim pot_val as Byte

    InitTimer0 Osc, PS0_256
    on Interrupt Timer0Overflow call timer_int
    TMR0 = 0

    do

    loop

    sub timer_int

    MOVLW b'00000010'
    XORWF LATA, F

    pot_val = ReadAD(timer_pot)

    TMR0IF = 0
    TMR0 = pot_val

    end sub

    ~~~

    thanks,
    tony

     

    Last edit: tony golding 2017-06-12
  • Anobium

    Anobium - 2017-06-12

    Let me post an updated adc library.   It will be in my morning. The library has been improved and we should get you ont the latest library before we try to resolve.

    Check back at 0800 London time.

    I may have the same chip so I can confirm also in my morning.

    Anobium

     
  • tony golding

    tony golding - 2017-06-12

    great to hear, thanks evan.

     
  • kent_twt4

    kent_twt4 - 2017-06-12

    While Evan works on the adc header for you, I have worked with the 10f322 a bit. Sometimes I set up a procedure just for a certain chip, just because.

    dir PortA.2 in    'a-d on PIC10F32X development BD.
    ....
    ....
    Call AD10F322
    linear = potvalue/5   '51 steps
    ....
    ....
    SUB AD10F322
    ANSELA = b'00000100'  'turn off digital input ANSA2
    CHS1 = 1:CHS0 = 0   'AN2
    ADON = 1        'ENABLE ADC AND START CONVERSION
    wait 10 us
    GO_NOT_DONE = 1
    ADCPOLL:
    BTFSC ADCON,GO_NOT_DONE
    goto ADCPOLL
    ADCON = 0
    ANSELA = 0        'Shut down adc module
    potvalue = ADRES    'read out the result
    End SUB
    
     
  • tony golding

    tony golding - 2017-06-13

    thanks kent,

    i ended up manually setting the adc registers and all is working good now so ill just wait for the updated adc file to add into gcb.

    tony

     
  • Anobium

    Anobium - 2017-06-13

    Works here with library from the upcoming release. I did optimise the library is the coming release to improve support.

    This was my test program. I have attached the full program to show the optimisation that you can add - in some compilers... you have to pay for optimisation. :-)

    See here for the latest ADC library. This is the library for the upcoming v.0.97.02 release.

      #chip 10f322, 2
    
     ; ----- Include library
      #include <SoftSerial.h>
    
      ; ----- Config Serial UART :
      #define SER1_BAUD 9600   ; baudrate must be defined
      ; Config I/O ports for transmitting:
      #define SER1_TXPORT PORTA  ; I/O port (without .bit) must be defined
      #define SER1_TXPIN 1       ; portbit  must be defined
    
      Ser1Send  13   'new line in Terminal
      Ser1Send  10
      Ser1Print "10F322 ADC test"
      Ser1Send  13   'new line in Terminal
      Ser1Send  10
    
      do
          Ser1Print ReadAD(AN2)
          Ser1Send  13   'new line in Terminal
          Ser1Send  10
          wait 100 ms
    
      loop
    

    I am impressed by the generated ASM. The code is very similar to @Kent's code.. which is should be. Remember this is a library so there will be some logic to support the huge number of potential configurations but this is pretty good code for a library. I have left the comments in the ASM below.

    ;Overloaded signature: BYTE:
    FN_READAD27
    ;***************************************
    ;Perform conversion
    ;LLReadAD 1
    ;Select Case ADReadPort ' #IFDEF Var(ANSELA). ANSELA exists @DebugADC_H
    ;Case 2: Set ANSELA.2 On
    SysSelect1Case1
        movlw   2
        subwf   ADREADPORT,W
        btfss   STATUS, Z
        goto    SysSelect1Case2
        bsf ANSELA,2
    ;End Select  'End Select #1
    SysSelect1Case2
    SysSelectEnd1
    ;SET ADCS1 OFF
        bcf ADCON,ADCS1
    ;SET ADCS0 ON
        bsf ADCON,ADCS0
    ;SET CHS0 OFF
        bcf ADCON,CHS0
    ;SET CHS1 OFF
        bcf ADCON,CHS1
    ;SET CHS2 OFF
        bcf ADCON,CHS2
    ;IF ADReadPort.0 On Then Set CHS0 On
        btfsc   ADREADPORT,0
        bsf ADCON,CHS0
    ;IF ADReadPort.1 On Then Set CHS1 On
        btfsc   ADREADPORT,1
        bsf ADCON,CHS1
    ;IF ADReadPort.2 On Then Set CHS2 On
        btfsc   ADREADPORT,2
        bsf ADCON,CHS2
    ;SET ADON ON
        bsf ADCON,ADON
    ;Wait AD_Delay
        movlw   2
        movwf   SysWaitTemp10US
        call    Delay_10US
    ;SET GO_NOT_DONE ON
        bsf ADCON,GO_NOT_DONE
    ;nop
        nop
    ;Wait While GO_NOT_DONE ON
    SysWaitLoop1
        btfsc   ADCON,GO_NOT_DONE
        goto    SysWaitLoop1
    ;ANSELA = 0
        clrf    ANSELA
    
    ;ReadAD = ADRES
        movf    ADRES,W
        movwf   READAD
        return
    
     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.