A to D conversion 16f690 help....

Discussion in 'Programmer's Corner' started by Dodgydave, Jul 8, 2012.

  1. Dodgydave

    Thread Starter Distinguished Member

    Jun 22, 2012
    4,999
    745
    :eek:
    I am trying to do a simple a/d conversion using the 16f690 pic using the microchip assy board, i want to alter the voltage on port AN0 with a pot and display the result on portc with leds..ok

    but how do you do it??
     
  2. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
  3. Dodgydave

    Thread Starter Distinguished Member

    Jun 22, 2012
    4,999
    745
    PORTc is 8 bits wide portb is 4 port a is 6

    yes i have got the cds with the programmer


    this is what i have so far>>

    Code ( (Unknown Language)):
    1.  
    2.   BSF STATUS,RP0 ;Bank 1
    3.  BCF STATUS,RP1 ;
    4.  MOVLW b'01110000' ;A/D R/C osc
    5.  MOVWF ADCON1 ;
    6.  BSF TRISA,0 ;Set RA0 to input
    7.  clrf TRISC
    8.  clrf TRISB ; set port c output
    9.  BCF STATUS,RP0 ;Bank 2
    10.  BSF STATUS,RP1 ;
    11.  BSF ANSEL,0 ;Set RA0 to analog
    12.  clrf ANSELH ; set port b/c to digital I/O
    13.  BCF STATUS,RP1 ;Bank 0
    14.  MOVLW b'10000001' ;Right just, Vdd Vref, AN0 source input
    15.  MOVWF ADCON0 ;
    16. More
    17.  
    18.  BSF ADCON0,GO ;Start conversion
    19.  BTFSC ADCON0,GO ;Is conversion done?
    20.  GOTO $-1 ;No, test again
    21.  MOVF ADRESH,W ;Read upper 2 bits
    22.  MOVWF RESULTHI ;
    23.  BSF STATUS,RP0 ;Bank 1
    24.  MOVF ADRESL,W ;Read lower 8 bits
    25.  BCF STATUS,RP0 ;Bank 0
    26.  MOVWF RESULTLO
    27.     clrf PORTB
    28.   movfw RESULTLO
    29.  movwf PORTC
    30.  
    31.  goto  More
    32.  
    33.         end
    34.  
     
    Last edited by a moderator: Jul 8, 2012
  4. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    This is the ADC example. I think your code is missing some parts
    Code ( (Unknown Language)):
    1.  
    2. ; PICkit 2 Lesson 4 - "A2D"
    3. ;
    4. ; This shows how to read the A2D converter and display the
    5. ; High order parts on the 4 bit LED display.
    6. ; The pot on the Low Pin Count Demo board varies the voltage
    7. ; coming in on in A0.
    8. ;
    9. ; The A2D is referenced to the same Vdd as the device, which
    10. ; is provided by the USB cable and nominally is 5V. The A2D
    11. ; returns the ratio of the voltage on Pin RA0 to 5V. The A2D
    12. ; has a resolution of 10 bits, with 1023 representing 5V and
    13. ; 0 representing 0V.
    14. ;
    15. ; *******************************************************************
    16. ; * See Low Pin Count Demo Board User's Guide for Lesson Information*
    17. ; *******************************************************************
    18. ; * NOTE: The PIC16F690 requires the AC162061 header for debugging *
    19. ; *******************************************************************
    20. #include <p16F690.inc>
    21. __config (_INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _BOR_OFF & _IESO_OFF & _FCMEN_OFF)
    22. cblock 0x20
    23. Delay1 ; Assign an address to label Delay1
    24. Delay2
    25. Display ; define a variable to hold the diplay
    26. endc
    27.  
    28. org 0
    29. Start:
    30. ; select Register Page 1
    31. movlw 0xFF
    32. banksel TRISA
    33. movwf TRISA ; Make PortA all input
    34. clrf TRISC ; Make PortC all output
    35. movlw 0x10 ; A2D Clock Fosc/8
    36. movwf ADCON1
    37. banksel 0
    38. ;bcf STATUS,RP0 ; back to Register Page 0
    39. bcf STATUS,RP0 ; address Register Page 2
    40. bsf STATUS,RP1
    41. movlw 0xFF ; we want all Port A pins Analoga
    42. movwf ANSEL
    43. bcf STATUS,RP0 ; address Register Page 0
    44. bcf STATUS,RP1
    45.  
    46. movlw 0x01
    47. movwf ADCON0 ; configure A2D for Channel 0 (RA0), Left justified, and turn on the A2D module
    48. MainLoop:
    49. nop ; wait 5uS for A2D amp to settle and capacitor to charge.
    50. nop ; wait 1uS
    51. nop ; wait 1uS
    52. nop ; wait 1uS
    53. nop ; wait 1uS
    54. bsf ADCON0,GO ; start conversion
    55. btfsc ADCON0,GO ; this bit will change to zero when the conversion is complete
    56. goto $-1
    57. swapf ADRESH,w ; Copy the display to the LEDs
    58. movwf PORTC
    59. goto MainLoop
    60. end
    61.  
     
  5. Dodgydave

    Thread Starter Distinguished Member

    Jun 22, 2012
    4,999
    745
    my programme works but uses right justified, so i swapped it to left justifiied and it works better.
     
Loading...